@devbro/sql-generator
Version:
generic sql generator
1 lines • 8.28 kB
Source Map (JSON)
{"version":3,"sources":["../src/Query.ts"],"sourcesContent":["import { Connection } from './Connection';\nimport { QueryGrammar } from './QueryGrammar';\nimport { CompiledSql, JoinCondition, Parameter, selectType, whereType, havingType, joinType } from './types';\n\nexport type QueryParts = {\n select: selectType[];\n table: string;\n join: joinType[];\n where: whereType[];\n groupBy: string[];\n having: havingType[];\n orderBy: string[];\n limit: number | null;\n offset: number | null;\n};\n\nexport class Query {\n allowedOperations: string[] = ['=', '>', '<', '!=', 'like', 'ilike'];\n parts: QueryParts = {\n select: ['*'],\n table: '',\n join: [],\n where: [],\n groupBy: [],\n having: [],\n orderBy: [],\n limit: null,\n offset: null,\n };\n\n constructor(\n private readonly connection: Connection | null,\n private readonly grammar: QueryGrammar\n ) {}\n\n table(tableName: string): this {\n this.parts.table = tableName;\n return this;\n }\n\n whereOp(\n column: string,\n operation: (typeof this.allowedOperations)[number],\n value: Parameter,\n joinCondition: JoinCondition = 'and',\n negateCondition: boolean = false\n ): this {\n this.parts.where.push({\n type: 'operation',\n column,\n operation,\n value,\n joinCondition,\n negateCondition,\n });\n return this;\n }\n\n whereColumn(column1: string, operation: (typeof this.allowedOperations)[number], column2: string, joinCondition: JoinCondition = 'and', negateCondition: boolean = false): this {\n this.parts.where.push({\n type: 'operationColumn',\n column1,\n operation,\n column2,\n joinCondition,\n negateCondition,\n });\n return this;\n }\n\n whereNull(\n column: string,\n joinCondition: JoinCondition = 'and',\n negateCondition: boolean = false\n ): this {\n this.parts.where.push({ type: 'null', column, joinCondition, negateCondition });\n return this;\n }\n\n clearWhere(): this {\n this.parts.where = [];\n return this;\n }\n\n select(selects: selectType[]): this {\n this.parts.select = [...selects];\n return this;\n }\n\n groupBy(columns: string[]): this {\n this.parts.groupBy = [...columns];\n return this;\n }\n\n havingOp(\n column: string,\n operation: (typeof this.allowedOperations)[number],\n value: Parameter,\n joinCondition: JoinCondition = 'and',\n negateCondition: boolean = false\n ): this {\n this.parts.having.push({\n type: 'operation',\n column,\n operation,\n value,\n joinCondition,\n negateCondition,\n });\n return this;\n }\n\n havingRaw(\n sql: string,\n bindings: Parameter[],\n joinCondition: JoinCondition = 'and',\n negateCondition: boolean = false\n ): this {\n this.parts.having.push({ type: 'raw', sql, bindings, joinCondition, negateCondition });\n return this;\n }\n\n orderBy(column: string, direction: 'asc' | 'desc' = 'asc'): this {\n this.parts.orderBy.push(`${column} ${direction}`);\n return this;\n }\n\n limit(limit: number): this {\n this.parts.limit = limit;\n return this;\n }\n\n offset(offset: number): this {\n this.parts.offset = offset;\n return this;\n }\n\n toSql(): CompiledSql {\n return this.grammar.toSql(this);\n }\n\n async get() {\n return await this.connection?.runQuery(this.toSql());\n }\n\n async getCursor() {\n return await this.connection?.runCursor(this.toSql());\n }\n\n getConnection(): Connection | null {\n return this.connection;\n }\n\n async insert(data: Record<string, Parameter>) {\n const csql: CompiledSql = this.grammar.compileInsert(this, data);\n return await this.connection?.runQuery(csql);\n }\n\n async insertGetId(\n data: Record<string, Parameter>,\n options: { primaryKey: string[] } = { primaryKey: ['id'] }\n ) {\n const csql: CompiledSql = this.grammar.compileInsertGetId(this, data, options);\n return await this.connection?.runQuery(csql);\n }\n\n async update(data: Record<string, Parameter>) {\n const csql: CompiledSql = this.grammar.compileUpdate(this, data);\n return await this.connection?.runQuery(csql);\n }\n\n async upsert(data: Record<string, Parameter>, uniqueColumns: string[], updateColumns: string[]) {\n const csql: CompiledSql = this.grammar.compileUpsert(this, data, uniqueColumns, updateColumns);\n return await this.connection?.runQuery(csql);\n }\n\n async delete() {\n const csql: CompiledSql = this.grammar.compileDelete(this);\n return await this.connection?.runQuery(csql);\n }\n\n innerJoin(table: string, condtions: whereType[]): this {\n this.parts.join.push({ type: 'inner', table, conditions: condtions });\n return this;\n }\n\n leftJoin(table: string, condtions: whereType[]): this {\n this.parts.join.push({ type: 'left', table, conditions: condtions });\n return this;\n }\n\n rightJoin(table: string, condtions: whereType[]): this {\n this.parts.join.push({ type: 'right', table, conditions: condtions });\n return this;\n }\n\n fullJoin(table: string, condtions: whereType[]): this {\n this.parts.join.push({ type: 'full', table, conditions: condtions });\n return this;\n }\n}\n"],"mappings":"AAgBO,MAAM,MAAM;AAAA,EAcjB,YACmB,YACA,SACjB;AAFiB;AACA;AAAA,EAChB;AAAA,EAhBH,oBAA8B,CAAC,KAAK,KAAK,KAAK,MAAM,QAAQ,OAAO;AAAA,EACnE,QAAoB;AAAA,IAClB,QAAQ,CAAC,GAAG;AAAA,IACZ,OAAO;AAAA,IACP,MAAM,CAAC;AAAA,IACP,OAAO,CAAC;AAAA,IACR,SAAS,CAAC;AAAA,IACV,QAAQ,CAAC;AAAA,IACT,SAAS,CAAC;AAAA,IACV,OAAO;AAAA,IACP,QAAQ;AAAA,EACV;AAAA,EAOA,MAAM,WAAyB;AAC7B,SAAK,MAAM,QAAQ;AACnB,WAAO;AAAA,EACT;AAAA,EAEA,QACE,QACA,WACA,OACA,gBAA+B,OAC/B,kBAA2B,OACrB;AACN,SAAK,MAAM,MAAM,KAAK;AAAA,MACpB,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,SAAiB,WAAoD,SAAiB,gBAA+B,OAAO,kBAA2B,OAAa;AAC9K,SAAK,MAAM,MAAM,KAAK;AAAA,MACpB,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AACD,WAAO;AAAA,EACT;AAAA,EAEA,UACE,QACA,gBAA+B,OAC/B,kBAA2B,OACrB;AACN,SAAK,MAAM,MAAM,KAAK,EAAE,MAAM,QAAQ,QAAQ,eAAe,gBAAgB,CAAC;AAC9E,WAAO;AAAA,EACT;AAAA,EAEA,aAAmB;AACjB,SAAK,MAAM,QAAQ,CAAC;AACpB,WAAO;AAAA,EACT;AAAA,EAEA,OAAO,SAA6B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