Skip to content

Commit

Permalink
Add operation types metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Feb 1, 2024
1 parent d641bb5 commit 0c9ff75
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Expression.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Mutation-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
26 changes: 22 additions & 4 deletions src/Mutation.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -86,10 +96,14 @@ export class DeleteMutation

export class InsertMutation
extends MutationBase
implements ISerializable, ISequelizable
implements ISerializable, ISequelizable, IMetadata
{
protected _values: Record<string, ConditionValue> = {};

public getOperationType(): MetadataOperationType {
return "insert";
}

public clone(): this {
const clone = super.clone();
clone._values = { ...this._values };
Expand Down Expand Up @@ -135,11 +149,15 @@ export class InsertMutation

export class UpdateMutation
extends MutationBase
implements ISerializable, ISequelizable
implements ISerializable, ISequelizable, IMetadata
{
protected _values: Record<string, ConditionValue> = {};
protected _where: Condition[] = [];

public getOperationType(): MetadataOperationType {
return "update";
}

public clone(): this {
const clone = super.clone();
clone._values = { ...this._values };
Expand Down
6 changes: 6 additions & 0 deletions src/Query-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
19 changes: 11 additions & 8 deletions src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 0c9ff75

Please sign in to comment.