From 0c9ff757d4722d12702634fa0ebd3ecba9238356 Mon Sep 17 00:00:00 2001 From: Jakub Knejzlik Date: Thu, 1 Feb 2024 09:50:44 +0100 Subject: [PATCH] Add operation types metadata --- src/Expression.ts | 2 +- src/Mutation-metadata.test.ts | 5 +++++ src/Mutation.ts | 26 ++++++++++++++++++++++---- src/Query-metadata.test.ts | 6 ++++++ src/Query.ts | 19 +++++++++++-------- src/interfaces.ts | 14 ++++++++++++++ 6 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 src/interfaces.ts diff --git a/src/Expression.ts b/src/Expression.ts index b4820de..3389e2d 100644 --- a/src/Expression.ts +++ b/src/Expression.ts @@ -1,5 +1,5 @@ import { ISQLFlavor } from "./Flavor"; -import { ISequelizable, ISerializable } from "./Query"; +import { ISequelizable, ISerializable } from "./interfaces"; export type ExpressionRawValue = string | number; export type ExpressionValue = Expression | ExpressionRawValue; diff --git a/src/Mutation-metadata.test.ts b/src/Mutation-metadata.test.ts index 0b1a34d..2ba1e57 100644 --- a/src/Mutation-metadata.test.ts +++ b/src/Mutation-metadata.test.ts @@ -16,4 +16,9 @@ describe("Query builder metadata", () => { const tables = query.getTableNames(); expect(tables).toEqual(["table"]); }); + it("should get operation type", () => { + expect(Q.insert("table").getOperationType()).toEqual("insert"); + expect(Q.update("table").getOperationType()).toEqual("update"); + expect(Q.delete("table").getOperationType()).toEqual("delete"); + }); }); diff --git a/src/Mutation.ts b/src/Mutation.ts index 8ce5245..70482b5 100644 --- a/src/Mutation.ts +++ b/src/Mutation.ts @@ -1,7 +1,13 @@ import { Condition, ConditionValue } from "./Condition"; import { ISQLFlavor } from "./Flavor"; -import { ISequelizable, ISerializable, Table } from "./Query"; +import { Table } from "./Query"; import { MySQLFlavor } from "./flavors/mysql"; +import { + IMetadata, + ISequelizable, + ISerializable, + MetadataOperationType, +} from "./interfaces"; export class MutationBase { protected _table: Table; @@ -37,10 +43,14 @@ export class MutationBase { export class DeleteMutation extends MutationBase - implements ISerializable, ISequelizable + implements ISerializable, ISequelizable, IMetadata { protected _where: Condition[] = []; + public getOperationType(): MetadataOperationType { + return "delete"; + } + public clone(): this { const clone = super.clone(); clone._where = [...this._where]; @@ -86,10 +96,14 @@ export class DeleteMutation export class InsertMutation extends MutationBase - implements ISerializable, ISequelizable + implements ISerializable, ISequelizable, IMetadata { protected _values: Record = {}; + public getOperationType(): MetadataOperationType { + return "insert"; + } + public clone(): this { const clone = super.clone(); clone._values = { ...this._values }; @@ -135,11 +149,15 @@ export class InsertMutation export class UpdateMutation extends MutationBase - implements ISerializable, ISequelizable + implements ISerializable, ISequelizable, IMetadata { protected _values: Record = {}; protected _where: Condition[] = []; + public getOperationType(): MetadataOperationType { + return "update"; + } + public clone(): this { const clone = super.clone(); clone._values = { ...this._values }; diff --git a/src/Query-metadata.test.ts b/src/Query-metadata.test.ts index b33a754..5d1957b 100644 --- a/src/Query-metadata.test.ts +++ b/src/Query-metadata.test.ts @@ -55,4 +55,10 @@ describe("Query builder metadata", () => { const tables = query.getTableNames(); expect(tables).toEqual([]); }); + + it("should get operation type for query", () => { + const query = Q.select(); + const operation = query.getOperationType(); + expect(operation).toEqual("select"); + }); }); diff --git a/src/Query.ts b/src/Query.ts index cdcde97..ed62c4e 100644 --- a/src/Query.ts +++ b/src/Query.ts @@ -4,6 +4,12 @@ import { ISQLFlavor } from "./Flavor"; import { AWSTimestreamFlavor } from "./flavors/aws-timestream"; import { MySQLFlavor } from "./flavors/mysql"; import { Fn } from "./Function"; +import { + IMetadata, + ISequelizable, + ISerializable, + MetadataOperationType, +} from "./interfaces"; import { DeleteMutation, InsertMutation, UpdateMutation } from "./Mutation"; const flavors = { @@ -13,13 +19,6 @@ const flavors = { type TableSource = string | SelectQuery; -export interface ISequelizable { - toSQL(flavor: ISQLFlavor): string; -} -export interface ISerializable { - serialize(): string; -} - export class Table implements ISequelizable, ISerializable { constructor(public source: TableSource, public alias?: string) {} public clone(): this { @@ -71,10 +70,14 @@ export const escapeTable = (table: TableSource, flavor: ISQLFlavor): string => { return flavor.escapeTable(table); }; -export class QueryBase implements ISequelizable { +export class QueryBase implements ISequelizable, IMetadata { protected _tables: Table[] = []; protected _joins: Join[] = []; + public getOperationType(): MetadataOperationType { + return "select"; + } + // @ts-ignore public get table(): Table | undefined { if (this._tables.length === 0) return undefined; diff --git a/src/interfaces.ts b/src/interfaces.ts new file mode 100644 index 0000000..2170e66 --- /dev/null +++ b/src/interfaces.ts @@ -0,0 +1,14 @@ +import { ISQLFlavor } from "./Flavor"; + +export interface ISequelizable { + toSQL(flavor: ISQLFlavor): string; +} +export interface ISerializable { + serialize(): string; +} + +export type MetadataOperationType = "select" | "insert" | "update" | "delete"; +export interface IMetadata { + getTableNames(): string[]; + getOperationType(): MetadataOperationType; +}