@starbemtech/star-db-query-builder 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +1 -0
  2. package/coverage/clover.xml +6 -0
  3. package/coverage/coverage-final.json +1 -0
  4. package/coverage/lcov-report/base.css +224 -0
  5. package/coverage/lcov-report/block-navigation.js +87 -0
  6. package/coverage/lcov-report/favicon.png +0 -0
  7. package/coverage/lcov-report/index.html +101 -0
  8. package/coverage/lcov-report/prettify.css +1 -0
  9. package/coverage/lcov-report/prettify.js +2 -0
  10. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  11. package/coverage/lcov-report/sorter.js +196 -0
  12. package/coverage/lcov.info +0 -0
  13. package/dist/index.d.ts +4 -0
  14. package/dist/index.js.map +1 -1
  15. package/index.d.ts +5 -1
  16. package/index.ts +6 -0
  17. package/package.json +3 -2
  18. package/.eslintrc.json +0 -32
  19. package/.husky/pre-commit +0 -1
  20. package/dist/.eslintrc.json +0 -32
  21. package/dist/src/db/IDatabaseClient.d.ts +0 -5
  22. package/dist/src/db/IDatabaseClient.js +0 -3
  23. package/dist/src/db/IDatabaseClient.js.map +0 -1
  24. package/dist/src/db/initDb.d.ts +0 -6
  25. package/dist/src/db/initDb.js +0 -30
  26. package/dist/src/db/initDb.js.map +0 -1
  27. package/dist/src/db/mysqlClient.d.ts +0 -3
  28. package/dist/src/db/mysqlClient.js +0 -12
  29. package/dist/src/db/mysqlClient.js.map +0 -1
  30. package/dist/src/db/pgClient.d.ts +0 -3
  31. package/dist/src/db/pgClient.js +0 -12
  32. package/dist/src/db/pgClient.js.map +0 -1
  33. package/dist/src/default/genericRepository.d.ts +0 -14
  34. package/dist/src/default/genericRepository.js +0 -97
  35. package/dist/src/default/genericRepository.js.map +0 -1
  36. package/dist/src/default/types.d.ts +0 -35
  37. package/dist/src/default/types.js +0 -3
  38. package/dist/src/default/types.js.map +0 -1
  39. package/dist/src/default/utils.d.ts +0 -8
  40. package/dist/src/default/utils.js +0 -91
  41. package/dist/src/default/utils.js.map +0 -1
  42. package/src/db/IDatabaseClient.ts +0 -6
  43. package/src/db/initDb.ts +0 -27
  44. package/src/db/mysqlClient.ts +0 -10
  45. package/src/db/pgClient.ts +0 -10
  46. package/src/default/genericRepository.ts +0 -143
  47. package/src/default/types.ts +0 -53
  48. package/src/default/utils.ts +0 -107
package/dist/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
+ import { PoolConfig } from 'pg';
2
+ import { PoolOptions } from 'mysql2';
1
3
  export * from './src/db/initDb';
2
4
  export * from './src/default/genericRepository';
5
+ export type PgPoolConfig = PoolConfig;
6
+ export type MySqlPoolOptions = PoolOptions;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,UAAU;AACV,kDAA+B;AAE/B,qBAAqB;AACrB,kEAA+C"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAGA,UAAU;AACV,kDAA+B;AAE/B,qBAAqB;AACrB,kEAA+C"}
package/index.d.ts CHANGED
@@ -1,4 +1,8 @@
1
- import { QueryParams } from './src/default/types'
1
+ import { IDatabaseClient } from './src/db/IDatabaseClient'
2
+ import { DBClients, QueryParams } from './src/default/types'
3
+
4
+ export declare function initDb<T>(config: { type: DBClients; options: T }): void
5
+ export declare function getDbClient(): IDatabaseClient
2
6
 
3
7
  export declare function findFirst<T>({
4
8
  tableName,
package/index.ts CHANGED
@@ -1,5 +1,11 @@
1
+ import { PoolConfig as PgPoolConfig } from 'pg'
2
+ import { PoolOptions as MySqlPoolOptions } from 'mysql2'
3
+
1
4
  // Configs
2
5
  export * from './src/db/initDb'
3
6
 
4
7
  // Generic Repository
5
8
  export * from './src/default/genericRepository'
9
+
10
+ // Types Definition
11
+ export type { PgPoolConfig, MySqlPoolOptions }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@starbemtech/star-db-query-builder",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "A query builder to be used with mysql or postgres",
5
5
  "author": "Julio Sousa <julio.sousa@starbem.app>",
6
6
  "main": "./dist/index.js",
@@ -13,7 +13,7 @@
13
13
  "scripts": {
14
14
  "build": "tsc",
15
15
  "prepare": "husky",
16
- "test": "jest",
16
+ "test": "jest --passWithNoTests",
17
17
  "coverage": "jest --watchAll --coverage"
18
18
  },
19
19
  "repository": {
@@ -46,6 +46,7 @@
46
46
  "jest": "^29.7.0",
47
47
  "lint-staged": "^15.2.2",
48
48
  "prettier": "^3.2.5",
49
+ "ts-node": "^10.9.2",
49
50
  "typescript": "^5.4.3"
50
51
  },
51
52
  "dependencies": {
package/.eslintrc.json DELETED
@@ -1,32 +0,0 @@
1
- {
2
- "env": {
3
- "es2021": true,
4
- "node": true
5
- },
6
- "extends": [
7
- "eslint:recommended",
8
- "prettier",
9
- "plugin:@typescript-eslint/recommended"
10
- ],
11
- "parser": "@typescript-eslint/parser",
12
- "parserOptions": {
13
- "ecmaVersion": "latest",
14
- "sourceType": "module"
15
- },
16
- "plugins": [
17
- "@typescript-eslint",
18
- "prettier"
19
- ],
20
- "rules": {
21
- "prettier/prettier": "error",
22
- "@typescript-eslint/explicit-function-return-type": "off",
23
- "@typescript-eslint/no-explicit-any": "off",
24
- "@typescript-eslint/explicit-module-boundary-types": "off",
25
- "@typescript-eslint/no-require-imports": "off",
26
- "import/prefer-default-export": "off",
27
- "no-param-reassign": "off",
28
- "no-console": "off",
29
- "no-return-assign": "off",
30
- "array-callback-return": "off"
31
- }
32
- }
package/.husky/pre-commit DELETED
@@ -1 +0,0 @@
1
- npm test
@@ -1,32 +0,0 @@
1
- {
2
- "env": {
3
- "es2021": true,
4
- "node": true
5
- },
6
- "extends": [
7
- "eslint:recommended",
8
- "prettier",
9
- "plugin:@typescript-eslint/recommended"
10
- ],
11
- "parser": "@typescript-eslint/parser",
12
- "parserOptions": {
13
- "ecmaVersion": "latest",
14
- "sourceType": "module"
15
- },
16
- "plugins": [
17
- "@typescript-eslint",
18
- "prettier"
19
- ],
20
- "rules": {
21
- "prettier/prettier": "error",
22
- "@typescript-eslint/explicit-function-return-type": "off",
23
- "@typescript-eslint/no-explicit-any": "off",
24
- "@typescript-eslint/explicit-module-boundary-types": "off",
25
- "@typescript-eslint/no-require-imports": "off",
26
- "import/prefer-default-export": "off",
27
- "no-param-reassign": "off",
28
- "no-console": "off",
29
- "no-return-assign": "off",
30
- "array-callback-return": "off"
31
- }
32
- }
@@ -1,5 +0,0 @@
1
- import { DBClients } from '../default/types';
2
- export type IDatabaseClient = {
3
- clientType: DBClients;
4
- query: <T>(sql: string, params?: any[]) => Promise<T>;
5
- };
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=IDatabaseClient.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"IDatabaseClient.js","sourceRoot":"","sources":["../../../src/db/IDatabaseClient.ts"],"names":[],"mappings":""}
@@ -1,6 +0,0 @@
1
- import { IDatabaseClient } from './IDatabaseClient';
2
- export declare const initDb: (config: {
3
- type: 'pg' | 'mysql';
4
- options: any;
5
- }) => void;
6
- export declare const getDbClient: () => IDatabaseClient;
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDbClient = exports.initDb = void 0;
4
- const pg_1 = require("pg");
5
- const promise_1 = require("mysql2/promise");
6
- const pgClient_1 = require("./pgClient");
7
- const mysqlClient_1 = require("./mysqlClient");
8
- let dbClient = null;
9
- const initDb = (config) => {
10
- if (config.type === 'pg') {
11
- const pool = new pg_1.Pool(config.options);
12
- dbClient = (0, pgClient_1.createPgClient)(pool);
13
- }
14
- else if (config.type === 'mysql') {
15
- const pool = (0, promise_1.createPool)(config.options);
16
- dbClient = (0, mysqlClient_1.createMysqlClient)(pool);
17
- }
18
- else {
19
- throw new Error('Unsupported database type');
20
- }
21
- };
22
- exports.initDb = initDb;
23
- const getDbClient = () => {
24
- if (!dbClient) {
25
- throw new Error('Database client is not initialized');
26
- }
27
- return dbClient;
28
- };
29
- exports.getDbClient = getDbClient;
30
- //# sourceMappingURL=initDb.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"initDb.js","sourceRoot":"","sources":["../../../src/db/initDb.ts"],"names":[],"mappings":";;;AAAA,2BAAmC;AACnC,4CAA8D;AAC9D,yCAA2C;AAC3C,+CAAiD;AAGjD,IAAI,QAAQ,GAA2B,IAAI,CAAA;AAEpC,MAAM,MAAM,GAAG,CAAC,MAA8C,EAAE,EAAE;IACvE,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;QACzB,MAAM,IAAI,GAAG,IAAI,SAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QACvC,QAAQ,GAAG,IAAA,yBAAc,EAAC,IAAI,CAAC,CAAA;IACjC,CAAC;SAAM,IAAI,MAAM,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QACnC,MAAM,IAAI,GAAG,IAAA,oBAAe,EAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QAC5C,QAAQ,GAAG,IAAA,+BAAiB,EAAC,IAAI,CAAC,CAAA;IACpC,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;IAC9C,CAAC;AACH,CAAC,CAAA;AAVY,QAAA,MAAM,UAUlB;AAEM,MAAM,WAAW,GAAG,GAAoB,EAAE;IAC/C,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAA;IACvD,CAAC;IAED,OAAO,QAAQ,CAAA;AACjB,CAAC,CAAA;AANY,QAAA,WAAW,eAMvB"}
@@ -1,3 +0,0 @@
1
- import { Pool } from 'mysql2/promise';
2
- import { IDatabaseClient } from './IDatabaseClient';
3
- export declare const createMysqlClient: (pool: Pool) => IDatabaseClient;
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createMysqlClient = void 0;
4
- const createMysqlClient = (pool) => ({
5
- clientType: 'mysql',
6
- query: async (sql, params) => {
7
- const [rows] = await pool.execute(sql, params);
8
- return rows;
9
- },
10
- });
11
- exports.createMysqlClient = createMysqlClient;
12
- //# sourceMappingURL=mysqlClient.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mysqlClient.js","sourceRoot":"","sources":["../../../src/db/mysqlClient.ts"],"names":[],"mappings":";;;AAGO,MAAM,iBAAiB,GAAG,CAAC,IAAU,EAAmB,EAAE,CAAC,CAAC;IACjE,UAAU,EAAE,OAAO;IACnB,KAAK,EAAE,KAAK,EAAK,GAAW,EAAE,MAAc,EAAc,EAAE;QAC1D,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QAC9C,OAAO,IAAoB,CAAA;IAC7B,CAAC;CACF,CAAC,CAAA;AANW,QAAA,iBAAiB,qBAM5B"}
@@ -1,3 +0,0 @@
1
- import { Pool } from 'pg';
2
- import { IDatabaseClient } from './IDatabaseClient';
3
- export declare const createPgClient: (pool: Pool) => IDatabaseClient;
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createPgClient = void 0;
4
- const createPgClient = (pool) => ({
5
- clientType: 'pg',
6
- query: async (sql, params) => {
7
- const { rows } = await pool.query(sql, params);
8
- return rows;
9
- },
10
- });
11
- exports.createPgClient = createPgClient;
12
- //# sourceMappingURL=pgClient.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pgClient.js","sourceRoot":"","sources":["../../../src/db/pgClient.ts"],"names":[],"mappings":";;;AAGO,MAAM,cAAc,GAAG,CAAC,IAAU,EAAmB,EAAE,CAAC,CAAC;IAC9D,UAAU,EAAE,IAAI;IAChB,KAAK,EAAE,KAAK,EAAK,GAAW,EAAE,MAAc,EAAc,EAAE;QAC1D,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QAC9C,OAAO,IAAoB,CAAA;IAC7B,CAAC;CACF,CAAC,CAAA;AANW,QAAA,cAAc,kBAMzB"}
@@ -1,14 +0,0 @@
1
- import { QueryParams } from './types';
2
- export declare const findFirst: <T>({ tableName, dbClient, select, where, groupBy, orderBy, }: QueryParams) => Promise<T>;
3
- export declare const findMany: <T>({ tableName, dbClient, select, where, groupBy, orderBy, limit, }: QueryParams) => Promise<T[]>;
4
- export declare const insert: <T>({ tableName, dbClient, data, returning, }: QueryParams & {
5
- data: T;
6
- returning?: string[] | undefined;
7
- }) => Promise<T | void>;
8
- export declare const update: <T>({ tableName, dbClient, id, data, returning, }: QueryParams & {
9
- data: T;
10
- returning?: string[] | undefined;
11
- }) => Promise<T | void>;
12
- export declare const deleteOne: ({ tableName, dbClient, id, permanently, }: QueryParams & {
13
- permanently?: boolean | undefined;
14
- }) => Promise<void>;
@@ -1,97 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deleteOne = exports.update = exports.insert = exports.findMany = exports.findFirst = void 0;
4
- const utils_1 = require("./utils");
5
- const findFirst = async ({ tableName, dbClient, select, where, groupBy, orderBy, }) => {
6
- if (!tableName)
7
- throw new Error('Table name is required');
8
- if (!dbClient)
9
- throw new Error('DB client is required');
10
- const fields = (0, utils_1.createSelectFields)(select);
11
- const [whereClause, params] = (0, utils_1.createWhereClause)(where);
12
- const orderByClause = (0, utils_1.createOrderByClause)(orderBy);
13
- const groupByClause = (0, utils_1.createGroupByClause)(groupBy);
14
- const rows = await dbClient.query(`SELECT ${fields} FROM ${tableName}
15
- ${whereClause.length > 7 ? whereClause : ''}
16
- ${groupByClause}
17
- ${orderByClause}
18
- `, [params]);
19
- return rows[0] || null;
20
- };
21
- exports.findFirst = findFirst;
22
- const findMany = async ({ tableName, dbClient, select, where, groupBy, orderBy, limit, }) => {
23
- if (!tableName)
24
- throw new Error('Table name is required');
25
- if (!dbClient)
26
- throw new Error('DB client is required');
27
- const fields = (0, utils_1.createSelectFields)(select);
28
- const [whereClause, params] = (0, utils_1.createWhereClause)(where);
29
- const orderByClause = (0, utils_1.createOrderByClause)(orderBy);
30
- const groupByClause = (0, utils_1.createGroupByClause)(groupBy);
31
- const limitClause = (0, utils_1.createLimitClause)(limit);
32
- const rows = await dbClient.query(`SELECT ${fields} FROM ${tableName}
33
- ${whereClause.length > 7 ? whereClause : ''}
34
- ${groupByClause}
35
- ${orderByClause}
36
- ${limitClause}
37
- `, [params]);
38
- return rows || [];
39
- };
40
- exports.findMany = findMany;
41
- const insert = async ({ tableName, dbClient, data, returning, }) => {
42
- if (!tableName)
43
- throw new Error('Table name is required');
44
- if (!dbClient)
45
- throw new Error('DB client is required');
46
- if (!data)
47
- throw new Error('Data object is required');
48
- const keys = Object.keys(data);
49
- const values = Object.values(data);
50
- const placeholders = (0, utils_1.generatePlaceholders)(keys, dbClient.clientType);
51
- let query = `INSERT INTO ${tableName} (${keys.join(', ')}) VALUES (${placeholders})`;
52
- if (dbClient.clientType === 'pg') {
53
- if (returning && returning.length > 0) {
54
- query += ` RETURNING ${(0, utils_1.createSelectFields)(returning)}`;
55
- }
56
- }
57
- const inserted = await dbClient.query(query, values);
58
- if (dbClient.clientType === 'pg')
59
- return inserted[0];
60
- };
61
- exports.insert = insert;
62
- const update = async ({ tableName, dbClient, id, data, returning, }) => {
63
- if (!tableName)
64
- throw new Error('Table name is required');
65
- if (!dbClient)
66
- throw new Error('DB client is required');
67
- if (!id)
68
- throw new Error('ID is required');
69
- if (!data)
70
- throw new Error('Data object is required');
71
- const keys = Object.keys(data);
72
- const values = Object.values(data);
73
- const setClause = (0, utils_1.generateSetClause)(keys, dbClient.clientType);
74
- let query = `UPDATE ${tableName} SET ${setClause} WHERE ${id}`;
75
- if (dbClient.clientType === 'pg') {
76
- if (returning && returning.length > 0) {
77
- query += ` RETURNIN ${(0, utils_1.createSelectFields)(returning)}`;
78
- }
79
- }
80
- const updated = await dbClient.query(query, values);
81
- if (dbClient.clientType === 'pg')
82
- return updated[0];
83
- };
84
- exports.update = update;
85
- const deleteOne = async ({ tableName, dbClient, id, permanently = false, }) => {
86
- if (!tableName)
87
- throw new Error('Table name is required');
88
- if (!dbClient)
89
- throw new Error('DB client is required');
90
- if (!id)
91
- throw new Error('ID is required');
92
- await dbClient.query(permanently
93
- ? `DELETE FROM ${tableName} WHERE id = ${dbClient.clientType === 'pg' ? '$1' : '?'}`
94
- : `UPDATE ${tableName} SET status = 'deleted' WHERE id = ${dbClient.clientType === 'pg' ? '$1' : '?'}`, [id]);
95
- };
96
- exports.deleteOne = deleteOne;
97
- //# sourceMappingURL=genericRepository.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"genericRepository.js","sourceRoot":"","sources":["../../../src/default/genericRepository.ts"],"names":[],"mappings":";;;AACA,mCAQgB;AAET,MAAM,SAAS,GAAG,KAAK,EAAK,EACjC,SAAS,EACT,QAAQ,EACR,MAAM,EACN,KAAK,EACL,OAAO,EACP,OAAO,GACK,EAAc,EAAE;IAC5B,IAAI,CAAC,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAA;IACzD,IAAI,CAAC,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAA;IAEvD,MAAM,MAAM,GAAG,IAAA,0BAAkB,EAAC,MAAM,CAAC,CAAA;IACzC,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,GAAG,IAAA,yBAAiB,EAAC,KAAK,CAAC,CAAA;IACtD,MAAM,aAAa,GAAG,IAAA,2BAAmB,EAAC,OAAO,CAAC,CAAA;IAClD,MAAM,aAAa,GAAG,IAAA,2BAAmB,EAAC,OAAO,CAAC,CAAA;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AAI,CAAC,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAA;IACvD,IAAI,CAAC,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAA;IAE1C,MAAM,QAAQ,CAAC,KAAK,CAClB,WAAW;QACT,CAAC,CAAC,eAAe,SAAS,eAAe,QAAQ,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE;QACpF,CAAC,CAAC,UAAU,SAAS,sCAAsC,QAAQ,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,EACxG,CAAC,EAAE,CAAC,CACL,CAAA;AACH,CAAC,CAAA;AAhBY,QAAA,SAAS,aAgBrB"}
@@ -1,35 +0,0 @@
1
- import { IDatabaseClient } from '../db/IDatabaseClient';
2
- export interface QueryExec {
3
- text: string;
4
- values?: any[];
5
- }
6
- type SimpleValue = string | number | boolean | Date;
7
- type ConditionOperator = 'AND' | 'OR';
8
- interface OperatorCondition {
9
- operator: 'LIKE' | '=' | '>' | '<' | 'IN' | 'BETWEEN' | '!=' | '<=' | '>=' | 'NOT IN' | 'LIKE' | 'NOT LIKE';
10
- value: SimpleValue | SimpleValue[];
11
- }
12
- export type DBClients = 'pg' | 'mysql';
13
- export interface CompositeCondition {
14
- type: ConditionOperator;
15
- conditions: Condition[];
16
- }
17
- export type Condition = SimpleValue | OperatorCondition | CompositeCondition;
18
- export interface Conditions {
19
- [key: string]: Condition;
20
- }
21
- export type OrderBy = {
22
- field: string;
23
- direction: 'ASC' | 'DESC';
24
- }[];
25
- export interface QueryParams {
26
- tableName: string;
27
- dbClient: IDatabaseClient;
28
- id?: string;
29
- select?: string[];
30
- where?: Conditions;
31
- orderBy?: OrderBy;
32
- groupBy?: string[];
33
- limit?: number;
34
- }
35
- export {};
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/default/types.ts"],"names":[],"mappings":""}
@@ -1,8 +0,0 @@
1
- import { Conditions, OrderBy, DBClients } from './types';
2
- export declare const createSelectFields: (fields?: string[]) => string;
3
- export declare const generatePlaceholders: (keys: any[], clientType: DBClients) => string;
4
- export declare const generateSetClause: (keys: any[], clientType: DBClients) => string;
5
- export declare const createWhereClause: (conditions?: Conditions, startIndex?: number) => [string, any[], number];
6
- export declare const createOrderByClause: (orderBy?: OrderBy) => string;
7
- export declare const createGroupByClause: (groupBy?: string[]) => string;
8
- export declare const createLimitClause: (limit?: number) => string;
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createLimitClause = exports.createGroupByClause = exports.createOrderByClause = exports.createWhereClause = exports.generateSetClause = exports.generatePlaceholders = exports.createSelectFields = void 0;
4
- const arrayToStringWithQuotes = (items) => {
5
- const itemsWithQuotes = items.map((item) => `"${item}"`);
6
- return itemsWithQuotes.join(', ');
7
- };
8
- const createSelectFields = (fields) => {
9
- return fields && fields.length > 0 ? arrayToStringWithQuotes(fields) : '*';
10
- };
11
- exports.createSelectFields = createSelectFields;
12
- const generatePlaceholders = (keys, clientType) => {
13
- return keys
14
- .map((_, index) => (clientType === 'pg' ? `$${index + 1}` : '?'))
15
- .join(', ');
16
- };
17
- exports.generatePlaceholders = generatePlaceholders;
18
- const generateSetClause = (keys, clientType) => {
19
- return keys
20
- .map((key, index) => clientType === 'pg' ? `${key} = $${index + 1}` : `${key} = ?`)
21
- .join(', ');
22
- };
23
- exports.generateSetClause = generateSetClause;
24
- const createWhereClause = (conditions = {}, startIndex = 1) => {
25
- let index = startIndex;
26
- const whereParts = [];
27
- const values = [];
28
- const processCondition = (key, condition) => {
29
- if (typeof condition === 'object' && condition !== null) {
30
- if ('operator' in condition && 'value' in condition) {
31
- const { operator, value } = condition;
32
- if (Array.isArray(value)) {
33
- const placeholders = value.map(() => `$${index++}`).join(', ');
34
- if (operator === 'BETWEEN') {
35
- whereParts.push(`"${key}" ${operator} ${placeholders.replace(', ', ' AND ')}`);
36
- }
37
- else {
38
- whereParts.push(`"${key}" ${operator} (${placeholders})`);
39
- }
40
- values.push(...value);
41
- }
42
- else {
43
- whereParts.push(`"${key}" ${operator} $${index++}`);
44
- values.push(value);
45
- }
46
- }
47
- else if ('type' in condition && 'conditions' in condition) {
48
- const compositeCondition = condition;
49
- const subWhereParts = [];
50
- compositeCondition.conditions.forEach((subCondition) => {
51
- // Recurso: criar uma chave fictícia, pois as condições compostas não necessitam de chave
52
- index = processCondition('', subCondition);
53
- });
54
- whereParts.push(`(${subWhereParts.join(` ${compositeCondition.type} `)})`);
55
- }
56
- }
57
- else if (key) {
58
- // Condição simples
59
- whereParts.push(`"${key}" = $${index}`);
60
- values.push(condition);
61
- index++;
62
- }
63
- return index;
64
- };
65
- Object.entries(conditions).forEach(([key, value]) => {
66
- index = processCondition(key, value);
67
- });
68
- const whereClause = whereParts.length > 0 ? ` WHERE ${whereParts.join(' AND ')}` : '';
69
- return [whereClause, values, index];
70
- };
71
- exports.createWhereClause = createWhereClause;
72
- const createOrderByClause = (orderBy) => {
73
- if (!orderBy || orderBy.length === 0)
74
- return '';
75
- const clause = orderBy.map((o) => `${o.field} ${o.direction}`).join(', ');
76
- return ` ORDER BY ${clause}`;
77
- };
78
- exports.createOrderByClause = createOrderByClause;
79
- const createGroupByClause = (groupBy) => {
80
- if (!groupBy || groupBy.length === 0)
81
- return '';
82
- return ` GROUP BY ${groupBy.join(', ')}`;
83
- };
84
- exports.createGroupByClause = createGroupByClause;
85
- const createLimitClause = (limit) => {
86
- if (!limit)
87
- return '';
88
- return ` LIMIT ${limit}`;
89
- };
90
- exports.createLimitClause = createLimitClause;
91
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../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,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACzE,OAAO,aAAa,MAAM,EAAE,CAAA;AAC9B,CAAC,CAAA;AAJY,QAAA,mBAAmB,uBAI/B;AAEM,MAAM,mBAAmB,GAAG,CAAC,OAAkB,EAAE,EAAE;IACxD,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAA;IAC/C,OAAO,aAAa,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAA;AAC1C,CAAC,CAAA;AAHY,QAAA,mBAAmB,uBAG/B;AAEM,MAAM,iBAAiB,GAAG,CAAC,KAAc,EAAE,EAAE;IAClD,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,CAAA;IACrB,OAAO,UAAU,KAAK,EAAE,CAAA;AAC1B,CAAC,CAAA;AAHY,QAAA,iBAAiB,qBAG7B"}
@@ -1,6 +0,0 @@
1
- import { DBClients } from '../default/types'
2
-
3
- export type IDatabaseClient = {
4
- clientType: DBClients
5
- query: <T>(sql: string, params?: any[]) => Promise<T>
6
- }
package/src/db/initDb.ts DELETED
@@ -1,27 +0,0 @@
1
- import { Pool as PgPool } from 'pg'
2
- import { createPool as createMySqlPool } from 'mysql2/promise'
3
- import { createPgClient } from './pgClient'
4
- import { createMysqlClient } from './mysqlClient'
5
- import { IDatabaseClient } from './IDatabaseClient'
6
-
7
- let dbClient: IDatabaseClient | null = null
8
-
9
- export const initDb = (config: { type: 'pg' | 'mysql'; options: any }) => {
10
- if (config.type === 'pg') {
11
- const pool = new PgPool(config.options)
12
- dbClient = createPgClient(pool)
13
- } else if (config.type === 'mysql') {
14
- const pool = createMySqlPool(config.options)
15
- dbClient = createMysqlClient(pool)
16
- } else {
17
- throw new Error('Unsupported database type')
18
- }
19
- }
20
-
21
- export const getDbClient = (): IDatabaseClient => {
22
- if (!dbClient) {
23
- throw new Error('Database client is not initialized')
24
- }
25
-
26
- return dbClient
27
- }
@@ -1,10 +0,0 @@
1
- import { Pool } from 'mysql2/promise'
2
- import { IDatabaseClient } from './IDatabaseClient'
3
-
4
- export const createMysqlClient = (pool: Pool): IDatabaseClient => ({
5
- clientType: 'mysql',
6
- query: async <T>(sql: string, params?: any[]): Promise<T> => {
7
- const [rows] = await pool.execute(sql, params)
8
- return rows as unknown as T
9
- },
10
- })
@@ -1,10 +0,0 @@
1
- import { Pool } from 'pg'
2
- import { IDatabaseClient } from './IDatabaseClient'
3
-
4
- export const createPgClient = (pool: Pool): IDatabaseClient => ({
5
- clientType: 'pg',
6
- query: async <T>(sql: string, params?: any[]): Promise<T> => {
7
- const { rows } = await pool.query(sql, params)
8
- return rows as unknown as T
9
- },
10
- })