diff --git a/runner/src/dml-handler/dml-handler.ts b/runner/src/dml-handler/dml-handler.ts index c145c4f9..7a481f3e 100644 --- a/runner/src/dml-handler/dml-handler.ts +++ b/runner/src/dml-handler/dml-handler.ts @@ -8,22 +8,22 @@ import { type QueryResult } from 'pg'; export type PostgresRowValue = string | number | any; export type PostgresRow = Record; -export type WhereClauseMulti = Record)>; +export type WhereClauseMulti = Record; export type WhereClauseSingle = Record; -export interface DmlHandlerI { +export interface IDmlHandler { insert: (tableDefinitionNames: TableDefinitionNames, rowsToInsert: PostgresRow[]) => Promise select: (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti, limit: number | null) => Promise update: (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseSingle, updateObject: any) => Promise upsert: (tableDefinitionNames: TableDefinitionNames, rowsToUpsert: PostgresRow[], conflictColumns: string[], updateColumns: string[]) => Promise delete: (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti) => Promise } -export default class DmlHandler implements DmlHandlerI { +export default class DmlHandler implements IDmlHandler { validTableNameRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/; pgClient: PgClient; tracer: Tracer; - constructor( + constructor ( databaseConnectionParameters: PostgresConnectionParams, private readonly indexerConfig: IndexerConfig, pgClientInstance: PgClient | undefined = undefined, @@ -32,7 +32,7 @@ export default class DmlHandler implements DmlHandlerI { this.tracer = trace.getTracer('queryapi-runner-dml-handler'); } - private async query(query: string, queryVars: Array, tableName: string, operation: string): Promise> { + private async query (query: string, queryVars: Array, tableName: string, operation: string): Promise> { return await this.tracer.startActiveSpan(`context db ${operation}`, async (operationSpan: Span) => { operationSpan.setAttribute('sql query', query); try { @@ -43,7 +43,7 @@ export default class DmlHandler implements DmlHandlerI { }); } - private getWhereClause(whereObject: WhereClauseMulti, columnLookup: Map): { queryVars: Array, whereClause: string } { + private getWhereClause (whereObject: WhereClauseMulti, columnLookup: Map): { queryVars: Array, whereClause: string } { const columns = Object.keys(whereObject); const queryVars: Array = []; const whereClause = columns.map((colName) => { @@ -62,7 +62,7 @@ export default class DmlHandler implements DmlHandlerI { return { queryVars, whereClause }; } - async insert(tableDefinitionNames: TableDefinitionNames, rowsToInsert: PostgresRow[]): Promise { + async insert (tableDefinitionNames: TableDefinitionNames, rowsToInsert: PostgresRow[]): Promise { if (!rowsToInsert?.length) { return []; } @@ -76,7 +76,7 @@ export default class DmlHandler implements DmlHandlerI { return result.rows; } - async select(tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti, limit: number | null = null): Promise { + async select (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti, limit: number | null = null): Promise { const { queryVars, whereClause } = this.getWhereClause(whereObject, tableDefinitionNames.originalColumnNames); let query = `SELECT * FROM ${this.indexerConfig.schemaName()}.${tableDefinitionNames.originalTableName} WHERE ${whereClause}`; if (limit !== null) { @@ -87,7 +87,7 @@ export default class DmlHandler implements DmlHandlerI { return result.rows; } - async update(tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseSingle, updateObject: any): Promise { + async update (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseSingle, updateObject: any): Promise { const updateKeys = Object.keys(updateObject).map((col) => tableDefinitionNames.originalColumnNames.get(col) ?? col); const updateParam = Array.from({ length: updateKeys.length }, (_, index) => `${updateKeys[index]}=$${index + 1}`).join(', '); const whereKeys = Object.keys(whereObject).map((col) => tableDefinitionNames.originalColumnNames.get(col) ?? col); @@ -100,7 +100,7 @@ export default class DmlHandler implements DmlHandlerI { return result.rows; } - async upsert(tableDefinitionNames: TableDefinitionNames, rowsToUpsert: PostgresRow[], conflictColumns: string[], updateColumns: string[]): Promise { + async upsert (tableDefinitionNames: TableDefinitionNames, rowsToUpsert: PostgresRow[], conflictColumns: string[], updateColumns: string[]): Promise { if (!rowsToUpsert?.length) { return []; } @@ -117,7 +117,7 @@ export default class DmlHandler implements DmlHandlerI { return result.rows; } - async delete(tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti): Promise { + async delete (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti): Promise { const { queryVars, whereClause } = this.getWhereClause(whereObject, tableDefinitionNames.originalColumnNames); const query = `DELETE FROM ${this.indexerConfig.schemaName()}.${tableDefinitionNames.originalTableName} WHERE ${whereClause} RETURNING *`; diff --git a/runner/src/dml-handler/in-memory-dml-handler.test.ts b/runner/src/dml-handler/in-memory-dml-handler.test.ts index 257f13be..6fbc20c7 100644 --- a/runner/src/dml-handler/in-memory-dml-handler.test.ts +++ b/runner/src/dml-handler/in-memory-dml-handler.test.ts @@ -1,10 +1,10 @@ -import { TableDefinitionNames } from "../indexer"; -import InMemoryDmlHandler from "./in-memory-dml-handler"; +import { type TableDefinitionNames } from '../indexer'; +import InMemoryDmlHandler from './in-memory-dml-handler'; const DEFAULT_ITEM_1_WITHOUT_ID = { account_id: 'TEST_NEAR', block_height: 1, - content: "CONTENT", + content: 'CONTENT', accounts_liked: [], }; @@ -12,14 +12,14 @@ const DEFAULT_ITEM_1_WITH_ID = { id: 1, account_id: 'TEST_NEAR', block_height: 1, - content: "CONTENT", + content: 'CONTENT', accounts_liked: [], }; const DEFAULT_ITEM_2_WITHOUT_ID = { account_id: 'TEST_NEAR', block_height: 2, - content: "CONTENT", + content: 'CONTENT', accounts_liked: [], }; @@ -27,7 +27,7 @@ const DEFAULT_ITEM_2_WITH_ID = { id: 2, account_id: 'TEST_NEAR', block_height: 2, - content: "CONTENT", + content: 'CONTENT', accounts_liked: [], }; @@ -41,7 +41,7 @@ describe('DML Handler Fixture Tests', () => { "accounts_liked" JSONB NOT NULL DEFAULT '[]', CONSTRAINT "posts_pkey" PRIMARY KEY ("id", "account_id") );`; - let TABLE_DEFINITION_NAMES: TableDefinitionNames = { + const TABLE_DEFINITION_NAMES: TableDefinitionNames = { tableName: 'posts', originalTableName: '"posts"', originalColumnNames: new Map([]) @@ -90,7 +90,7 @@ describe('DML Handler Fixture Tests', () => { test('reject insert after not specifying primary key value', async () => { const inputObj = [{ block_height: 1, - content: "CONTENT", + content: 'CONTENT', accounts_liked: [], }]; @@ -130,13 +130,13 @@ describe('DML Handler Fixture Tests', () => { const upsertObj = [{ account_id: 'TEST_NEAR', block_height: 1, - content: "UPSERT", + content: 'UPSERT', accounts_liked: [], }, { account_id: 'TEST_NEAR', block_height: 2, - content: "UPSERT", + content: 'UPSERT', accounts_liked: [], }]; @@ -154,13 +154,13 @@ describe('DML Handler Fixture Tests', () => { const upsertObj = [{ account_id: 'TEST_NEAR', block_height: 1, - content: "UPSERT", + content: 'UPSERT', accounts_liked: [], }, { account_id: 'TEST_NEAR', block_height: 2, - content: "UPSERT", + content: 'UPSERT', accounts_liked: [], }]; @@ -184,7 +184,7 @@ describe('DML Handler Fixture Tests', () => { test('reject insert after not specifying primary key value', async () => { const inputObj = [{ block_height: 1, - content: "CONTENT", + content: 'CONTENT', accounts_liked: [], }]; @@ -201,5 +201,5 @@ describe('DML Handler Fixture Tests', () => { const deletedRows = await dmlHandler.delete(TABLE_DEFINITION_NAMES, { account_id: 'TEST_NEAR' }); expect(deletedRows).toEqual(correctResponse); - }) + }); }); diff --git a/runner/src/dml-handler/in-memory-dml-handler.ts b/runner/src/dml-handler/in-memory-dml-handler.ts index 9913ac70..574885ce 100644 --- a/runner/src/dml-handler/in-memory-dml-handler.ts +++ b/runner/src/dml-handler/in-memory-dml-handler.ts @@ -1,6 +1,6 @@ import { type AST, Parser } from 'node-sql-parser'; import { type TableDefinitionNames } from '../indexer'; -import { type PostgresRow, type WhereClauseMulti, type WhereClauseSingle, type DmlHandlerI } from './dml-handler'; +import { type PostgresRow, type WhereClauseMulti, type WhereClauseSingle, type IDmlHandler } from './dml-handler'; // TODO: Define class to represent specification interface TableSpecification { @@ -322,7 +322,7 @@ class IndexerData { } } -export default class InMemoryDmlHandler implements DmlHandlerI { +export default class InMemoryDmlHandler implements IDmlHandler { private readonly indexerData: IndexerData; constructor (schema: string) {