Skip to content

Commit

Permalink
Rename DmlHandlerI to IDmlHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
darunrs committed Jul 18, 2024
1 parent fd23a43 commit 0cf33da
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
22 changes: 11 additions & 11 deletions runner/src/dml-handler/dml-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import { type QueryResult } from 'pg';

export type PostgresRowValue = string | number | any;
export type PostgresRow = Record<string, PostgresRowValue>;
export type WhereClauseMulti = Record<string, (PostgresRowValue | Array<PostgresRowValue>)>;
export type WhereClauseMulti = Record<string, (PostgresRowValue | PostgresRowValue[])>;
export type WhereClauseSingle = Record<string, PostgresRowValue>;

export interface DmlHandlerI {
export interface IDmlHandler {
insert: (tableDefinitionNames: TableDefinitionNames, rowsToInsert: PostgresRow[]) => Promise<PostgresRow[]>
select: (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti, limit: number | null) => Promise<PostgresRow[]>
update: (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseSingle, updateObject: any) => Promise<PostgresRow[]>
upsert: (tableDefinitionNames: TableDefinitionNames, rowsToUpsert: PostgresRow[], conflictColumns: string[], updateColumns: string[]) => Promise<PostgresRow[]>
delete: (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti) => Promise<PostgresRow[]>
}
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,
Expand All @@ -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<string | number>, tableName: string, operation: string): Promise<QueryResult<any>> {
private async query (query: string, queryVars: Array<string | number>, tableName: string, operation: string): Promise<QueryResult<any>> {
return await this.tracer.startActiveSpan(`context db ${operation}`, async (operationSpan: Span) => {
operationSpan.setAttribute('sql query', query);
try {
Expand All @@ -43,7 +43,7 @@ export default class DmlHandler implements DmlHandlerI {
});
}

private getWhereClause(whereObject: WhereClauseMulti, columnLookup: Map<string, string>): { queryVars: Array<string | number>, whereClause: string } {
private getWhereClause (whereObject: WhereClauseMulti, columnLookup: Map<string, string>): { queryVars: Array<string | number>, whereClause: string } {
const columns = Object.keys(whereObject);
const queryVars: Array<string | number> = [];
const whereClause = columns.map((colName) => {
Expand All @@ -62,7 +62,7 @@ export default class DmlHandler implements DmlHandlerI {
return { queryVars, whereClause };
}

async insert(tableDefinitionNames: TableDefinitionNames, rowsToInsert: PostgresRow[]): Promise<PostgresRow[]> {
async insert (tableDefinitionNames: TableDefinitionNames, rowsToInsert: PostgresRow[]): Promise<PostgresRow[]> {
if (!rowsToInsert?.length) {
return [];
}
Expand All @@ -76,7 +76,7 @@ export default class DmlHandler implements DmlHandlerI {
return result.rows;
}

async select(tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti, limit: number | null = null): Promise<PostgresRow[]> {
async select (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti, limit: number | null = null): Promise<PostgresRow[]> {
const { queryVars, whereClause } = this.getWhereClause(whereObject, tableDefinitionNames.originalColumnNames);
let query = `SELECT * FROM ${this.indexerConfig.schemaName()}.${tableDefinitionNames.originalTableName} WHERE ${whereClause}`;
if (limit !== null) {
Expand All @@ -87,7 +87,7 @@ export default class DmlHandler implements DmlHandlerI {
return result.rows;
}

async update(tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseSingle, updateObject: any): Promise<PostgresRow[]> {
async update (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseSingle, updateObject: any): Promise<PostgresRow[]> {
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);
Expand All @@ -100,7 +100,7 @@ export default class DmlHandler implements DmlHandlerI {
return result.rows;
}

async upsert(tableDefinitionNames: TableDefinitionNames, rowsToUpsert: PostgresRow[], conflictColumns: string[], updateColumns: string[]): Promise<PostgresRow[]> {
async upsert (tableDefinitionNames: TableDefinitionNames, rowsToUpsert: PostgresRow[], conflictColumns: string[], updateColumns: string[]): Promise<PostgresRow[]> {
if (!rowsToUpsert?.length) {
return [];
}
Expand All @@ -117,7 +117,7 @@ export default class DmlHandler implements DmlHandlerI {
return result.rows;
}

async delete(tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti): Promise<PostgresRow[]> {
async delete (tableDefinitionNames: TableDefinitionNames, whereObject: WhereClauseMulti): Promise<PostgresRow[]> {
const { queryVars, whereClause } = this.getWhereClause(whereObject, tableDefinitionNames.originalColumnNames);
const query = `DELETE FROM ${this.indexerConfig.schemaName()}.${tableDefinitionNames.originalTableName} WHERE ${whereClause} RETURNING *`;

Expand Down
28 changes: 14 additions & 14 deletions runner/src/dml-handler/in-memory-dml-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
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: [],
};

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: [],
};

const DEFAULT_ITEM_2_WITH_ID = {
id: 2,
account_id: 'TEST_NEAR',
block_height: 2,
content: "CONTENT",
content: 'CONTENT',
accounts_liked: [],
};

Expand All @@ -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<string, string>([])
Expand Down Expand Up @@ -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: [],
}];

Expand Down Expand Up @@ -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: [],
}];

Expand All @@ -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: [],
}];

Expand All @@ -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: [],
}];

Expand All @@ -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);
})
});
});
4 changes: 2 additions & 2 deletions runner/src/dml-handler/in-memory-dml-handler.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0cf33da

Please sign in to comment.