-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OTE-753] add new persistent cache table (#2175)
- Loading branch information
1 parent
61fa593
commit abfec2a
Showing
11 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
indexer/packages/postgres/__tests__/stores/persistent-cache-table.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { PersistentCacheFromDatabase } from '../../src/types'; | ||
import { clearData, migrate, teardown } from '../../src/helpers/db-helpers'; | ||
import { defaultKV, defaultKV2 } from '../helpers/constants'; | ||
import * as PersistentCacheTable from '../../src/stores/persistent-cache-table'; | ||
|
||
describe('Persistent cache store', () => { | ||
beforeAll(async () => { | ||
await migrate(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await clearData(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await teardown(); | ||
}); | ||
|
||
it('Successfully creates a key value pair', async () => { | ||
await PersistentCacheTable.create(defaultKV); | ||
}); | ||
|
||
it('Successfully upserts a kv pair multiple times', async () => { | ||
const newKv = { | ||
...defaultKV, | ||
value: 'someOtherValue', | ||
}; | ||
await PersistentCacheTable.upsert(newKv); | ||
let kv: PersistentCacheFromDatabase | undefined = await PersistentCacheTable.findById( | ||
defaultKV.key, | ||
); | ||
expect(kv).toEqual(expect.objectContaining(newKv)); | ||
|
||
const newKv2 = { | ||
...defaultKV, | ||
value: 'someOtherValue2', | ||
}; | ||
await PersistentCacheTable.upsert(newKv2); | ||
kv = await PersistentCacheTable.findById(defaultKV.key); | ||
|
||
expect(kv).toEqual(expect.objectContaining(newKv2)); | ||
}); | ||
|
||
it('Successfully finds all kv pairs', async () => { | ||
await Promise.all([ | ||
PersistentCacheTable.create(defaultKV), | ||
PersistentCacheTable.create(defaultKV2), | ||
]); | ||
|
||
const kvs: PersistentCacheFromDatabase[] = await PersistentCacheTable.findAll( | ||
{}, | ||
[], | ||
{ readReplica: true }, | ||
); | ||
|
||
expect(kvs.length).toEqual(2); | ||
expect(kvs).toEqual(expect.arrayContaining([ | ||
expect.objectContaining(defaultKV), | ||
expect.objectContaining(defaultKV2), | ||
])); | ||
}); | ||
|
||
it('Successfully finds a kv pair', async () => { | ||
await PersistentCacheTable.create(defaultKV); | ||
|
||
const kv: PersistentCacheFromDatabase | undefined = await PersistentCacheTable.findById( | ||
defaultKV.key, | ||
); | ||
|
||
expect(kv).toEqual(expect.objectContaining(defaultKV)); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
...ostgres/src/db/migrations/migration_files/20240829171730_create_persistent_cache_table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as Knex from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema.createTable('persistent_cache', (table) => { | ||
table.string('key').primary().notNullable(); | ||
table.string('value').notNullable(); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('persistent_cache'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
indexer/packages/postgres/src/models/persistent-cache-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import UpsertQueryBuilder from '../query-builders/upsert'; | ||
import BaseModel from './base-model'; | ||
|
||
export default class PersistentCacheModel extends BaseModel { | ||
static get tableName() { | ||
return 'persistent_cache'; | ||
} | ||
|
||
static get idColumn() { | ||
return 'key'; | ||
} | ||
|
||
static relationMappings = {}; | ||
|
||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: ['key', 'value'], | ||
properties: { | ||
key: { type: 'string' }, | ||
value: { type: 'string' }, | ||
}, | ||
}; | ||
} | ||
|
||
QueryBuilderType!: UpsertQueryBuilder<this>; | ||
|
||
key!: string; | ||
|
||
value!: string; | ||
} |
94 changes: 94 additions & 0 deletions
94
indexer/packages/postgres/src/stores/persistent-cache-table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { QueryBuilder } from 'objection'; | ||
|
||
import { DEFAULT_POSTGRES_OPTIONS } from '../constants'; | ||
import { setupBaseQuery, verifyAllRequiredFields } from '../helpers/stores-helpers'; | ||
import Transaction from '../helpers/transaction'; | ||
import PersistentCacheModel from '../models/persistent-cache-model'; | ||
import { | ||
Options, | ||
Ordering, | ||
QueryableField, | ||
QueryConfig, | ||
PersistentCacheColumns, | ||
PersistentCacheCreateObject, | ||
PersistentCacheFromDatabase, | ||
PersistentCacheQueryConfig, | ||
} from '../types'; | ||
|
||
export async function findAll( | ||
{ | ||
key, | ||
limit, | ||
}: PersistentCacheQueryConfig, | ||
requiredFields: QueryableField[], | ||
options: Options = DEFAULT_POSTGRES_OPTIONS, | ||
): Promise<PersistentCacheFromDatabase[]> { | ||
verifyAllRequiredFields( | ||
{ | ||
key, | ||
limit, | ||
} as QueryConfig, | ||
requiredFields, | ||
); | ||
|
||
let baseQuery: QueryBuilder<PersistentCacheModel> = setupBaseQuery<PersistentCacheModel>( | ||
PersistentCacheModel, | ||
options, | ||
); | ||
|
||
if (key) { | ||
baseQuery = baseQuery.where(PersistentCacheColumns.key, key); | ||
} | ||
|
||
if (options.orderBy !== undefined) { | ||
for (const [column, order] of options.orderBy) { | ||
baseQuery = baseQuery.orderBy( | ||
column, | ||
order, | ||
); | ||
} | ||
} else { | ||
baseQuery = baseQuery.orderBy( | ||
PersistentCacheColumns.key, | ||
Ordering.ASC, | ||
); | ||
} | ||
|
||
if (limit) { | ||
baseQuery = baseQuery.limit(limit); | ||
} | ||
|
||
return baseQuery.returning('*'); | ||
} | ||
|
||
export async function create( | ||
kvToCreate: PersistentCacheCreateObject, | ||
options: Options = { txId: undefined }, | ||
): Promise<PersistentCacheFromDatabase> { | ||
return PersistentCacheModel.query( | ||
Transaction.get(options.txId), | ||
).insert(kvToCreate).returning('*'); | ||
} | ||
|
||
export async function upsert( | ||
kvToUpsert: PersistentCacheCreateObject, | ||
options: Options = { txId: undefined }, | ||
): Promise<PersistentCacheFromDatabase> { | ||
const kvs: PersistentCacheModel[] = await PersistentCacheModel.query( | ||
Transaction.get(options.txId), | ||
).upsert(kvToUpsert).returning('*'); | ||
// should only ever be one key value pair | ||
return kvs[0]; | ||
} | ||
export async function findById( | ||
kv: string, | ||
options: Options = DEFAULT_POSTGRES_OPTIONS, | ||
): Promise<PersistentCacheFromDatabase | undefined> { | ||
const baseQuery: QueryBuilder<PersistentCacheModel> = setupBaseQuery<PersistentCacheModel>( | ||
PersistentCacheModel, | ||
options, | ||
); | ||
return baseQuery | ||
.findById(kv) | ||
.returning('*'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
indexer/packages/postgres/src/types/persistent-cache-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface PersistentCacheCreateObject { | ||
key: string, | ||
value: string, | ||
} | ||
|
||
export enum PersistentCacheColumns { | ||
key = 'key', | ||
value = 'value', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters