@starbemtech/star-db-query-builder 1.0.3 → 1.0.5
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.
- package/coverage/clover.xml +2 -2
- package/coverage/lcov-report/index.html +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/src/db/initDb.js +2 -2
- package/dist/src/db/initDb.js.map +1 -1
- package/dist/src/default/genericRepository.js +34 -12
- package/dist/src/default/genericRepository.js.map +1 -1
- package/dist/src/default/utils.d.ts +2 -2
- package/dist/src/default/utils.js +24 -10
- package/dist/src/default/utils.js.map +1 -1
- package/index.d.ts +6 -6
- package/index.ts +2 -1
- package/package.json +4 -2
- package/src/default/genericRepository.ts +85 -26
- package/src/default/types.ts +13 -13
- package/src/default/utils.ts +82 -39
package/coverage/clover.xml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<coverage generated="
|
|
3
|
-
<project timestamp="
|
|
2
|
+
<coverage generated="1712086913135" clover="3.2.0">
|
|
3
|
+
<project timestamp="1712086913135" name="All files">
|
|
4
4
|
<metrics statements="0" coveredstatements="0" conditionals="0" coveredconditionals="0" methods="0" coveredmethods="0" elements="0" coveredelements="0" complexity="0" loc="0" ncloc="0" packages="0" files="0" classes="0"/>
|
|
5
5
|
</project>
|
|
6
6
|
</coverage>
|
|
@@ -86,7 +86,7 @@
|
|
|
86
86
|
<div class='footer quiet pad2 space-top1 center small'>
|
|
87
87
|
Code coverage generated by
|
|
88
88
|
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
|
|
89
|
-
at 2024-
|
|
89
|
+
at 2024-04-02T19:41:53.131Z
|
|
90
90
|
</div>
|
|
91
91
|
<script src="prettify.js"></script>
|
|
92
92
|
<script>
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { PoolConfig } from 'pg';
|
|
2
|
-
import { PoolOptions } from 'mysql2';
|
|
1
|
+
import { PoolConfig as PgPoolConfig } from 'pg';
|
|
2
|
+
import { PoolOptions as MySqlPoolOptions } from 'mysql2';
|
|
3
3
|
export * from './src/db/initDb';
|
|
4
4
|
export * from './src/default/genericRepository';
|
|
5
|
-
export type PgPoolConfig
|
|
6
|
-
export type MySqlPoolOptions = PoolOptions;
|
|
5
|
+
export type { PgPoolConfig, MySqlPoolOptions };
|
package/dist/src/db/initDb.js
CHANGED
|
@@ -14,12 +14,12 @@ const initDb = (config) => {
|
|
|
14
14
|
if (config.type === 'pg') {
|
|
15
15
|
const pool = new pg_1.Pool(config.options);
|
|
16
16
|
dbClient = (0, pgClient_1.createPgClient)(pool);
|
|
17
|
-
console.log(
|
|
17
|
+
console.log(`@starbemtech/star-db-query-builder: Postgres db client created success`);
|
|
18
18
|
}
|
|
19
19
|
else if (config.type === 'mysql') {
|
|
20
20
|
const pool = (0, promise_1.createPool)(config.options);
|
|
21
21
|
dbClient = (0, mysqlClient_1.createMysqlClient)(pool);
|
|
22
|
-
console.info(
|
|
22
|
+
console.info(`@starbemtech/star-db-query-builder: MySQL db client created success.`);
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
25
|
throw new Error('Unsupported database type');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initDb.js","sourceRoot":"","sources":["../../../src/db/initDb.ts"],"names":[],"mappings":";;;AAAA,2BAAmC;AACnC,4CAA8D;AAC9D,yCAA2C;AAC3C,+CAAiD;AAIjD,IAAI,QAAQ,GAA2B,IAAI,CAAA;AAEpC,MAAM,MAAM,GAAG,CAAI,MAAuC,EAAQ,EAAE;IACzE,IAAI,CAAC,MAAM,CAAC,IAAI;QACd,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAA;IAEhE,IAAI,CAAC,MAAM,CAAC,OAAO;QAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;IAEtE,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,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I,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,21 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.deleteOne = exports.update = exports.insert = exports.findMany = exports.findFirst = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
4
5
|
const utils_1 = require("./utils");
|
|
5
6
|
const findFirst = async ({ tableName, dbClient, select, where, groupBy, orderBy, }) => {
|
|
6
7
|
if (!tableName)
|
|
7
8
|
throw new Error('Table name is required');
|
|
8
9
|
if (!dbClient)
|
|
9
10
|
throw new Error('DB client is required');
|
|
10
|
-
const fields = (0, utils_1.createSelectFields)(select);
|
|
11
|
-
const [whereClause, params] = (0, utils_1.createWhereClause)(where);
|
|
11
|
+
const fields = (0, utils_1.createSelectFields)(select, dbClient.clientType);
|
|
12
|
+
const [whereClause, params] = (0, utils_1.createWhereClause)(where, 1, dbClient.clientType);
|
|
12
13
|
const orderByClause = (0, utils_1.createOrderByClause)(orderBy);
|
|
13
14
|
const groupByClause = (0, utils_1.createGroupByClause)(groupBy);
|
|
14
15
|
const rows = await dbClient.query(`SELECT ${fields} FROM ${tableName}
|
|
15
16
|
${whereClause.length > 7 ? whereClause : ''}
|
|
16
17
|
${groupByClause}
|
|
17
18
|
${orderByClause}
|
|
18
|
-
|
|
19
|
+
`, params);
|
|
19
20
|
return rows[0] || null;
|
|
20
21
|
};
|
|
21
22
|
exports.findFirst = findFirst;
|
|
@@ -24,8 +25,8 @@ const findMany = async ({ tableName, dbClient, select, where, groupBy, orderBy,
|
|
|
24
25
|
throw new Error('Table name is required');
|
|
25
26
|
if (!dbClient)
|
|
26
27
|
throw new Error('DB client is required');
|
|
27
|
-
const fields = (0, utils_1.createSelectFields)(select);
|
|
28
|
-
const [whereClause, params] = (0, utils_1.createWhereClause)(where);
|
|
28
|
+
const fields = (0, utils_1.createSelectFields)(select, dbClient.clientType);
|
|
29
|
+
const [whereClause, params] = (0, utils_1.createWhereClause)(where, 1, dbClient.clientType);
|
|
29
30
|
const orderByClause = (0, utils_1.createOrderByClause)(orderBy);
|
|
30
31
|
const groupByClause = (0, utils_1.createGroupByClause)(groupBy);
|
|
31
32
|
const limitClause = (0, utils_1.createLimitClause)(limit);
|
|
@@ -34,7 +35,7 @@ const findMany = async ({ tableName, dbClient, select, where, groupBy, orderBy,
|
|
|
34
35
|
${groupByClause}
|
|
35
36
|
${orderByClause}
|
|
36
37
|
${limitClause}
|
|
37
|
-
`,
|
|
38
|
+
`, params);
|
|
38
39
|
return rows || [];
|
|
39
40
|
};
|
|
40
41
|
exports.findMany = findMany;
|
|
@@ -47,16 +48,29 @@ const insert = async ({ tableName, dbClient, data, returning, }) => {
|
|
|
47
48
|
throw new Error('Data object is required');
|
|
48
49
|
const keys = Object.keys(data);
|
|
49
50
|
const values = Object.values(data);
|
|
51
|
+
keys.unshift('id');
|
|
52
|
+
const generatedUUID = (0, uuid_1.v4)();
|
|
53
|
+
values.unshift(generatedUUID);
|
|
54
|
+
keys.push('updated_at');
|
|
55
|
+
values.push(new Date());
|
|
50
56
|
const placeholders = (0, utils_1.generatePlaceholders)(keys, dbClient.clientType);
|
|
51
57
|
let query = `INSERT INTO ${tableName} (${keys.join(', ')}) VALUES (${placeholders})`;
|
|
52
58
|
if (dbClient.clientType === 'pg') {
|
|
53
59
|
if (returning && returning.length > 0) {
|
|
54
|
-
query += ` RETURNING ${(0, utils_1.createSelectFields)(returning)}`;
|
|
60
|
+
query += ` RETURNING ${(0, utils_1.createSelectFields)(returning, dbClient.clientType)}`;
|
|
55
61
|
}
|
|
56
62
|
}
|
|
57
63
|
const inserted = await dbClient.query(query, values);
|
|
58
|
-
if (dbClient.clientType === '
|
|
59
|
-
|
|
64
|
+
if (dbClient.clientType === 'mysql') {
|
|
65
|
+
const rows = await dbClient.query(`SELECT ${returning && returning.length > 0
|
|
66
|
+
? (0, utils_1.createSelectFields)(returning, dbClient.clientType)
|
|
67
|
+
: '*'} FROM ${tableName}
|
|
68
|
+
WHERE
|
|
69
|
+
id = ?
|
|
70
|
+
`, [generatedUUID]);
|
|
71
|
+
return rows[0];
|
|
72
|
+
}
|
|
73
|
+
return inserted[0];
|
|
60
74
|
};
|
|
61
75
|
exports.insert = insert;
|
|
62
76
|
const update = async ({ tableName, dbClient, id, data, returning, }) => {
|
|
@@ -74,12 +88,20 @@ const update = async ({ tableName, dbClient, id, data, returning, }) => {
|
|
|
74
88
|
let query = `UPDATE ${tableName} SET ${setClause} WHERE ${id}`;
|
|
75
89
|
if (dbClient.clientType === 'pg') {
|
|
76
90
|
if (returning && returning.length > 0) {
|
|
77
|
-
query += ` RETURNIN ${(0, utils_1.createSelectFields)(returning)}`;
|
|
91
|
+
query += ` RETURNIN ${(0, utils_1.createSelectFields)(returning, dbClient.clientType)}`;
|
|
78
92
|
}
|
|
79
93
|
}
|
|
80
94
|
const updated = await dbClient.query(query, values);
|
|
81
|
-
if (dbClient.clientType === '
|
|
82
|
-
|
|
95
|
+
if (dbClient.clientType === 'mysql') {
|
|
96
|
+
const rows = await dbClient.query(`SELECT ${returning && returning.length > 0
|
|
97
|
+
? (0, utils_1.createSelectFields)(returning, dbClient.clientType)
|
|
98
|
+
: '*'} FROM ${tableName}
|
|
99
|
+
WHERE
|
|
100
|
+
id = ?
|
|
101
|
+
`, [id]);
|
|
102
|
+
return rows[0];
|
|
103
|
+
}
|
|
104
|
+
return updated[0];
|
|
83
105
|
};
|
|
84
106
|
exports.update = update;
|
|
85
107
|
const deleteOne = async ({ tableName, dbClient, id, permanently = false, }) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"genericRepository.js","sourceRoot":"","sources":["../../../src/default/genericRepository.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"genericRepository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SAAS,EAAE,QAAQ,CAAC,UAAU,CAAC;YACpD,CAAC,CAAC,GACN,SAAS,SAAS;;;OAGjB,EACD,CAAC,EAAE,CAAC,CACL,CAAA;QAED,OAAO,IAAI,CAAC,CAAC,CAAC,CAAA;IAChB,CAAC;IAED,OAAO,OAAO,CAAC,CAAC,CAAC,CAAA;AACnB,CAAC,CAAA;AA3CY,QAAA,MAAM,UA2ClB;AAEM,MAAM,SAAS,GAAG,KAAK,EAAE,EAC9B,SAAS,EACT,QAAQ,EACR,EAAE,EACF,WAAW,GAAG,KAAK,GACqB,EAAiB,EAAE;IAC3D,IAAI,CAAC,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAA;IACzD,IAAI,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,eACtB,QAAQ,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GACxC,EAAE;QACJ,CAAC,CAAC,UAAU,SAAS,sCACjB,QAAQ,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GACxC,EAAE,EACN,CAAC,EAAE,CAAC,CACL,CAAA;AACH,CAAC,CAAA;AApBY,QAAA,SAAS,aAoBrB"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Conditions, OrderBy, DBClients } from './types';
|
|
2
|
-
export declare const createSelectFields: (fields
|
|
2
|
+
export declare const createSelectFields: (fields: string[] | undefined, clientType: DBClients) => string;
|
|
3
3
|
export declare const generatePlaceholders: (keys: any[], clientType: DBClients) => string;
|
|
4
4
|
export declare const generateSetClause: (keys: any[], clientType: DBClients) => string;
|
|
5
|
-
export declare const createWhereClause: (conditions
|
|
5
|
+
export declare const createWhereClause: (conditions: Conditions | undefined, startIndex: number | undefined, clientType: DBClients) => [string, any[], number];
|
|
6
6
|
export declare const createOrderByClause: (orderBy?: OrderBy) => string;
|
|
7
7
|
export declare const createGroupByClause: (groupBy?: string[]) => string;
|
|
8
8
|
export declare const createLimitClause: (limit?: number) => string;
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
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}"`);
|
|
4
|
+
const arrayToStringWithQuotes = (items, clientType) => {
|
|
5
|
+
const itemsWithQuotes = items.map((item) => clientType === 'pg' ? `"${item}"` : `${item}`);
|
|
6
6
|
return itemsWithQuotes.join(', ');
|
|
7
7
|
};
|
|
8
|
-
const
|
|
9
|
-
|
|
8
|
+
const pgPlaceholderGenerator = (index) => `$${index}`;
|
|
9
|
+
const mysqlPlaceholderGenerator = () => `?`;
|
|
10
|
+
const createSelectFields = (fields = [], clientType) => {
|
|
11
|
+
return fields && fields.length > 0
|
|
12
|
+
? arrayToStringWithQuotes(fields, clientType)
|
|
13
|
+
: '*';
|
|
10
14
|
};
|
|
11
15
|
exports.createSelectFields = createSelectFields;
|
|
12
16
|
const generatePlaceholders = (keys, clientType) => {
|
|
@@ -21,7 +25,7 @@ const generateSetClause = (keys, clientType) => {
|
|
|
21
25
|
.join(', ');
|
|
22
26
|
};
|
|
23
27
|
exports.generateSetClause = generateSetClause;
|
|
24
|
-
const createWhereClause = (conditions = {}, startIndex = 1) => {
|
|
28
|
+
const createWhereClause = (conditions = {}, startIndex = 1, clientType) => {
|
|
25
29
|
let index = startIndex;
|
|
26
30
|
const whereParts = [];
|
|
27
31
|
const values = [];
|
|
@@ -30,17 +34,27 @@ const createWhereClause = (conditions = {}, startIndex = 1) => {
|
|
|
30
34
|
if ('operator' in condition && 'value' in condition) {
|
|
31
35
|
const { operator, value } = condition;
|
|
32
36
|
if (Array.isArray(value)) {
|
|
33
|
-
const placeholders = value
|
|
37
|
+
const placeholders = value
|
|
38
|
+
.map(() => clientType === 'pg'
|
|
39
|
+
? pgPlaceholderGenerator(index++)
|
|
40
|
+
: mysqlPlaceholderGenerator())
|
|
41
|
+
.join(', ');
|
|
34
42
|
if (operator === 'BETWEEN') {
|
|
35
|
-
whereParts.push(
|
|
43
|
+
whereParts.push(clientType === 'pg'
|
|
44
|
+
? `"${key}" ${operator} ${placeholders.replace(', ', ' AND ')}`
|
|
45
|
+
: `${key} ${operator} ${placeholders.replace(', ', ' AND ')}`);
|
|
36
46
|
}
|
|
37
47
|
else {
|
|
38
|
-
whereParts.push(
|
|
48
|
+
whereParts.push(clientType === 'pg'
|
|
49
|
+
? `"${key}" ${operator} (${placeholders})`
|
|
50
|
+
: `${key} ${operator} ${placeholders}`);
|
|
39
51
|
}
|
|
40
52
|
values.push(...value);
|
|
41
53
|
}
|
|
42
54
|
else {
|
|
43
|
-
whereParts.push(
|
|
55
|
+
whereParts.push(clientType === 'pg'
|
|
56
|
+
? `"${key}" ${operator} ${pgPlaceholderGenerator(index++)}`
|
|
57
|
+
: `${key} ${operator} ${mysqlPlaceholderGenerator()}`);
|
|
44
58
|
values.push(value);
|
|
45
59
|
}
|
|
46
60
|
}
|
|
@@ -56,7 +70,7 @@ const createWhereClause = (conditions = {}, startIndex = 1) => {
|
|
|
56
70
|
}
|
|
57
71
|
else if (key) {
|
|
58
72
|
// Condição simples
|
|
59
|
-
whereParts.push(`"${key}" = $${index}`);
|
|
73
|
+
whereParts.push(clientType === 'pg' ? `"${key}" = $${index}` : `${key} = ?`);
|
|
60
74
|
values.push(condition);
|
|
61
75
|
index++;
|
|
62
76
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/default/utils.ts"],"names":[],"mappings":";;;AAQA,MAAM,uBAAuB,GAAG,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/default/utils.ts"],"names":[],"mappings":";;;AAQA,MAAM,uBAAuB,GAAG,CAC9B,KAAe,EACf,UAAqB,EACb,EAAE;IACV,MAAM,eAAe,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CACzC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,CAC9C,CAAA;IACD,OAAO,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACnC,CAAC,CAAA;AAED,MAAM,sBAAsB,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,IAAI,KAAK,EAAE,CAAA;AAC7D,MAAM,yBAAyB,GAAG,GAAG,EAAE,CAAC,GAAG,CAAA;AAEpC,MAAM,kBAAkB,GAAG,CAChC,SAAmB,EAAE,EACrB,UAAqB,EACb,EAAE;IACV,OAAO,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC;QAChC,CAAC,CAAC,uBAAuB,CAAC,MAAM,EAAE,UAAU,CAAC;QAC7C,CAAC,CAAC,GAAG,CAAA;AACT,CAAC,CAAA;AAPY,QAAA,kBAAkB,sBAO9B;AAEM,MAAM,oBAAoB,GAAG,CAClC,IAAW,EACX,UAAqB,EACb,EAAE;IACV,OAAO,IAAI;SACR,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;SAChE,IAAI,CAAC,IAAI,CAAC,CAAA;AACf,CAAC,CAAA;AAPY,QAAA,oBAAoB,wBAOhC;AAEM,MAAM,iBAAi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,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"}
|
package/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export declare function findFirst<T>({
|
|
|
11
11
|
where,
|
|
12
12
|
groupBy,
|
|
13
13
|
orderBy,
|
|
14
|
-
}: QueryParams): Promise<T>
|
|
14
|
+
}: QueryParams<T>): Promise<T>
|
|
15
15
|
|
|
16
16
|
export declare function findMany<T>({
|
|
17
17
|
tableName,
|
|
@@ -21,14 +21,14 @@ export declare function findMany<T>({
|
|
|
21
21
|
groupBy,
|
|
22
22
|
orderBy,
|
|
23
23
|
limit,
|
|
24
|
-
}: QueryParams): Promise<T[]>
|
|
24
|
+
}: QueryParams<T>): Promise<T[]>
|
|
25
25
|
|
|
26
26
|
export declare function insert<T>({
|
|
27
27
|
tableName,
|
|
28
28
|
dbClient,
|
|
29
29
|
data,
|
|
30
30
|
returning,
|
|
31
|
-
}: QueryParams & { data: T; returning?: string[] }): Promise<T | void>
|
|
31
|
+
}: QueryParams<T> & { data: T; returning?: string[] }): Promise<T | void>
|
|
32
32
|
|
|
33
33
|
export declare function update<T>({
|
|
34
34
|
tableName,
|
|
@@ -36,11 +36,11 @@ export declare function update<T>({
|
|
|
36
36
|
id,
|
|
37
37
|
data,
|
|
38
38
|
returning,
|
|
39
|
-
}: QueryParams & { data: T; returning?: string[] }): Promise<T | void>
|
|
39
|
+
}: QueryParams<T> & { data: T; returning?: string[] }): Promise<T | void>
|
|
40
40
|
|
|
41
|
-
export declare function deleteOne({
|
|
41
|
+
export declare function deleteOne<T>({
|
|
42
42
|
tableName,
|
|
43
43
|
dbClient,
|
|
44
44
|
id,
|
|
45
45
|
permanently,
|
|
46
|
-
}: QueryParams & { permanently?: boolean }): Promise<void>
|
|
46
|
+
}: QueryParams<T> & { permanently?: boolean }): Promise<void>
|
package/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PoolConfig as PgPoolConfig } from 'pg'
|
|
2
2
|
import { PoolOptions as MySqlPoolOptions } from 'mysql2'
|
|
3
|
+
import { Conditions } from './src/default/types'
|
|
3
4
|
|
|
4
5
|
// Configs
|
|
5
6
|
export * from './src/db/initDb'
|
|
@@ -8,4 +9,4 @@ export * from './src/db/initDb'
|
|
|
8
9
|
export * from './src/default/genericRepository'
|
|
9
10
|
|
|
10
11
|
// Types Definition
|
|
11
|
-
export type { PgPoolConfig, MySqlPoolOptions }
|
|
12
|
+
export type { PgPoolConfig, MySqlPoolOptions, Conditions }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@starbemtech/star-db-query-builder",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
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",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"@types/mysql": "^2.15.26",
|
|
36
36
|
"@types/node": "^20.11.30",
|
|
37
37
|
"@types/pg": "^8.11.4",
|
|
38
|
+
"@types/uuid": "^9.0.8",
|
|
38
39
|
"@typescript-eslint/eslint-plugin": "^7.4.0",
|
|
39
40
|
"@typescript-eslint/parser": "^7.4.0",
|
|
40
41
|
"eslint": "^8.57.0",
|
|
@@ -51,7 +52,8 @@
|
|
|
51
52
|
},
|
|
52
53
|
"dependencies": {
|
|
53
54
|
"mysql2": "^3.9.3",
|
|
54
|
-
"pg": "^8.11.3"
|
|
55
|
+
"pg": "^8.11.3",
|
|
56
|
+
"uuid": "^9.0.1"
|
|
55
57
|
},
|
|
56
58
|
"lint-staged": {
|
|
57
59
|
"*.{ts}": [
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { v4 as uuid } from 'uuid'
|
|
1
2
|
import { QueryParams } from './types'
|
|
2
3
|
import {
|
|
3
4
|
createGroupByClause,
|
|
@@ -16,25 +17,35 @@ export const findFirst = async <T>({
|
|
|
16
17
|
where,
|
|
17
18
|
groupBy,
|
|
18
19
|
orderBy,
|
|
19
|
-
}: QueryParams): Promise<T> => {
|
|
20
|
+
}: QueryParams<T>): Promise<T | null> => {
|
|
20
21
|
if (!tableName) throw new Error('Table name is required')
|
|
21
22
|
if (!dbClient) throw new Error('DB client is required')
|
|
22
23
|
|
|
23
|
-
const fields = createSelectFields(select)
|
|
24
|
-
const [whereClause, params] = createWhereClause(
|
|
24
|
+
const fields = createSelectFields(select, dbClient.clientType)
|
|
25
|
+
const [whereClause, params] = createWhereClause<T>(
|
|
26
|
+
where,
|
|
27
|
+
1,
|
|
28
|
+
dbClient.clientType
|
|
29
|
+
)
|
|
30
|
+
console.log(whereClause, 'WHERE')
|
|
31
|
+
console.log(params)
|
|
25
32
|
const orderByClause = createOrderByClause(orderBy)
|
|
26
33
|
const groupByClause = createGroupByClause(groupBy)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
34
|
+
try {
|
|
35
|
+
const rows = await dbClient.query<T>(
|
|
36
|
+
`SELECT ${fields} FROM ${tableName}
|
|
30
37
|
${whereClause.length > 7 ? whereClause : ''}
|
|
31
38
|
${groupByClause}
|
|
32
39
|
${orderByClause}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
`,
|
|
41
|
+
params
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
return rows[0] || null
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.log(error, 'ERROR QUERY')
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
38
49
|
}
|
|
39
50
|
|
|
40
51
|
export const findMany = async <T>({
|
|
@@ -45,12 +56,12 @@ export const findMany = async <T>({
|
|
|
45
56
|
groupBy,
|
|
46
57
|
orderBy,
|
|
47
58
|
limit,
|
|
48
|
-
}: QueryParams): Promise<T[]> => {
|
|
59
|
+
}: QueryParams<T>): Promise<T[]> => {
|
|
49
60
|
if (!tableName) throw new Error('Table name is required')
|
|
50
61
|
if (!dbClient) throw new Error('DB client is required')
|
|
51
62
|
|
|
52
|
-
const fields = createSelectFields(select)
|
|
53
|
-
const [whereClause, params] = createWhereClause(where)
|
|
63
|
+
const fields = createSelectFields(select, dbClient.clientType)
|
|
64
|
+
const [whereClause, params] = createWhereClause(where, 1, dbClient.clientType)
|
|
54
65
|
const orderByClause = createOrderByClause(orderBy)
|
|
55
66
|
const groupByClause = createGroupByClause(groupBy)
|
|
56
67
|
const limitClause = createLimitClause(limit)
|
|
@@ -62,7 +73,7 @@ export const findMany = async <T>({
|
|
|
62
73
|
${orderByClause}
|
|
63
74
|
${limitClause}
|
|
64
75
|
`,
|
|
65
|
-
|
|
76
|
+
params
|
|
66
77
|
)
|
|
67
78
|
|
|
68
79
|
return rows || []
|
|
@@ -73,7 +84,7 @@ export const insert = async <T>({
|
|
|
73
84
|
dbClient,
|
|
74
85
|
data,
|
|
75
86
|
returning,
|
|
76
|
-
}: QueryParams & { data:
|
|
87
|
+
}: QueryParams<T> & { data: any; returning?: string[] }): Promise<T> => {
|
|
77
88
|
if (!tableName) throw new Error('Table name is required')
|
|
78
89
|
if (!dbClient) throw new Error('DB client is required')
|
|
79
90
|
if (!data) throw new Error('Data object is required')
|
|
@@ -81,18 +92,46 @@ export const insert = async <T>({
|
|
|
81
92
|
const keys = Object.keys(data)
|
|
82
93
|
const values = Object.values(data)
|
|
83
94
|
|
|
95
|
+
keys.unshift('id')
|
|
96
|
+
const generatedUUID: string = uuid()
|
|
97
|
+
values.unshift(generatedUUID)
|
|
98
|
+
|
|
99
|
+
keys.push('updated_at')
|
|
100
|
+
values.push(new Date())
|
|
101
|
+
|
|
84
102
|
const placeholders = generatePlaceholders(keys, dbClient.clientType)
|
|
85
|
-
let query = `INSERT INTO ${tableName} (${keys.join(
|
|
103
|
+
let query = `INSERT INTO ${tableName} (${keys.join(
|
|
104
|
+
', '
|
|
105
|
+
)}) VALUES (${placeholders})`
|
|
86
106
|
|
|
87
107
|
if (dbClient.clientType === 'pg') {
|
|
88
108
|
if (returning && returning.length > 0) {
|
|
89
|
-
query += ` RETURNING ${createSelectFields(
|
|
109
|
+
query += ` RETURNING ${createSelectFields(
|
|
110
|
+
returning,
|
|
111
|
+
dbClient.clientType
|
|
112
|
+
)}`
|
|
90
113
|
}
|
|
91
114
|
}
|
|
92
115
|
|
|
93
116
|
const inserted = await dbClient.query<T[]>(query, values)
|
|
94
117
|
|
|
95
|
-
if (dbClient.clientType === '
|
|
118
|
+
if (dbClient.clientType === 'mysql') {
|
|
119
|
+
const rows = await dbClient.query<T>(
|
|
120
|
+
`SELECT ${
|
|
121
|
+
returning && returning.length > 0
|
|
122
|
+
? createSelectFields(returning, dbClient.clientType)
|
|
123
|
+
: '*'
|
|
124
|
+
} FROM ${tableName}
|
|
125
|
+
WHERE
|
|
126
|
+
id = ?
|
|
127
|
+
`,
|
|
128
|
+
[generatedUUID]
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
return rows[0]
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return inserted[0]
|
|
96
135
|
}
|
|
97
136
|
|
|
98
137
|
export const update = async <T>({
|
|
@@ -101,7 +140,7 @@ export const update = async <T>({
|
|
|
101
140
|
id,
|
|
102
141
|
data,
|
|
103
142
|
returning,
|
|
104
|
-
}: QueryParams & { data: T; returning?: string[] }): Promise<T | void> => {
|
|
143
|
+
}: QueryParams<T> & { data: T; returning?: string[] }): Promise<T | void> => {
|
|
105
144
|
if (!tableName) throw new Error('Table name is required')
|
|
106
145
|
if (!dbClient) throw new Error('DB client is required')
|
|
107
146
|
if (!id) throw new Error('ID is required')
|
|
@@ -115,29 +154,49 @@ export const update = async <T>({
|
|
|
115
154
|
|
|
116
155
|
if (dbClient.clientType === 'pg') {
|
|
117
156
|
if (returning && returning.length > 0) {
|
|
118
|
-
query += ` RETURNIN ${createSelectFields(returning)}`
|
|
157
|
+
query += ` RETURNIN ${createSelectFields(returning, dbClient.clientType)}`
|
|
119
158
|
}
|
|
120
159
|
}
|
|
121
160
|
|
|
122
161
|
const updated = await dbClient.query<T[]>(query, values)
|
|
123
162
|
|
|
124
|
-
if (dbClient.clientType === '
|
|
163
|
+
if (dbClient.clientType === 'mysql') {
|
|
164
|
+
const rows = await dbClient.query<T>(
|
|
165
|
+
`SELECT ${
|
|
166
|
+
returning && returning.length > 0
|
|
167
|
+
? createSelectFields(returning, dbClient.clientType)
|
|
168
|
+
: '*'
|
|
169
|
+
} FROM ${tableName}
|
|
170
|
+
WHERE
|
|
171
|
+
id = ?
|
|
172
|
+
`,
|
|
173
|
+
[id]
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
return rows[0]
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return updated[0]
|
|
125
180
|
}
|
|
126
181
|
|
|
127
|
-
export const deleteOne = async ({
|
|
182
|
+
export const deleteOne = async <T>({
|
|
128
183
|
tableName,
|
|
129
184
|
dbClient,
|
|
130
185
|
id,
|
|
131
186
|
permanently = false,
|
|
132
|
-
}: QueryParams & { permanently?: boolean }): Promise<void> => {
|
|
187
|
+
}: QueryParams<T> & { permanently?: boolean }): Promise<void> => {
|
|
133
188
|
if (!tableName) throw new Error('Table name is required')
|
|
134
189
|
if (!dbClient) throw new Error('DB client is required')
|
|
135
190
|
if (!id) throw new Error('ID is required')
|
|
136
191
|
|
|
137
192
|
await dbClient.query(
|
|
138
193
|
permanently
|
|
139
|
-
? `DELETE FROM ${tableName} WHERE id = ${
|
|
140
|
-
|
|
194
|
+
? `DELETE FROM ${tableName} WHERE id = ${
|
|
195
|
+
dbClient.clientType === 'pg' ? '$1' : '?'
|
|
196
|
+
}`
|
|
197
|
+
: `UPDATE ${tableName} SET status = 'deleted' WHERE id = ${
|
|
198
|
+
dbClient.clientType === 'pg' ? '$1' : '?'
|
|
199
|
+
}`,
|
|
141
200
|
[id]
|
|
142
201
|
)
|
|
143
202
|
}
|
package/src/default/types.ts
CHANGED
|
@@ -7,9 +7,7 @@ export interface QueryExec {
|
|
|
7
7
|
|
|
8
8
|
type SimpleValue = string | number | boolean | Date
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
interface OperatorCondition {
|
|
10
|
+
export interface OperatorCondition {
|
|
13
11
|
operator:
|
|
14
12
|
| 'LIKE'
|
|
15
13
|
| '='
|
|
@@ -26,27 +24,29 @@ interface OperatorCondition {
|
|
|
26
24
|
value: SimpleValue | SimpleValue[]
|
|
27
25
|
}
|
|
28
26
|
|
|
29
|
-
export type
|
|
27
|
+
export type LogicalOperator = 'OR' | 'AND'
|
|
28
|
+
|
|
29
|
+
export type Condition<T> = OperatorCondition | LogicalCondition<T>
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
interface LogicalCondition<T> {
|
|
32
|
+
OR?: Conditions<T>[]
|
|
33
|
+
AND?: Conditions<T>[]
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
export type
|
|
36
|
+
export type Conditions<T> = {
|
|
37
|
+
[P in keyof T]?: Condition<T[P]>
|
|
38
|
+
} & LogicalCondition<T>
|
|
37
39
|
|
|
38
|
-
export
|
|
39
|
-
[key: string]: Condition
|
|
40
|
-
}
|
|
40
|
+
export type DBClients = 'pg' | 'mysql'
|
|
41
41
|
|
|
42
42
|
export type OrderBy = { field: string; direction: 'ASC' | 'DESC' }[]
|
|
43
43
|
|
|
44
|
-
export interface QueryParams {
|
|
44
|
+
export interface QueryParams<T> {
|
|
45
45
|
tableName: string
|
|
46
46
|
dbClient: IDatabaseClient
|
|
47
47
|
id?: string
|
|
48
48
|
select?: string[]
|
|
49
|
-
where?: Conditions
|
|
49
|
+
where?: Conditions<T>
|
|
50
50
|
orderBy?: OrderBy
|
|
51
51
|
groupBy?: string[]
|
|
52
52
|
limit?: number
|
package/src/default/utils.ts
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const itemsWithQuotes = items.map((item) => `"${item}"`)
|
|
1
|
+
import { Conditions, Condition, OrderBy, DBClients } from './types'
|
|
2
|
+
|
|
3
|
+
const arrayToStringWithQuotes = (
|
|
4
|
+
items: string[],
|
|
5
|
+
clientType: DBClients
|
|
6
|
+
): string => {
|
|
7
|
+
const itemsWithQuotes = items.map((item) =>
|
|
8
|
+
clientType === 'pg' ? `"${item}"` : `${item}`
|
|
9
|
+
)
|
|
11
10
|
return itemsWithQuotes.join(', ')
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
const pgPlaceholderGenerator = (index: number) => `$${index}`
|
|
14
|
+
const mysqlPlaceholderGenerator = () => `?`
|
|
15
|
+
|
|
16
|
+
export const createSelectFields = (
|
|
17
|
+
fields: string[] = [],
|
|
18
|
+
clientType: DBClients
|
|
19
|
+
): string => {
|
|
20
|
+
return fields && fields.length > 0
|
|
21
|
+
? arrayToStringWithQuotes(fields, clientType)
|
|
22
|
+
: '*'
|
|
16
23
|
}
|
|
17
24
|
|
|
18
25
|
export const generatePlaceholders = (
|
|
@@ -35,58 +42,94 @@ export const generateSetClause = (
|
|
|
35
42
|
.join(', ')
|
|
36
43
|
}
|
|
37
44
|
|
|
38
|
-
export const createWhereClause = (
|
|
39
|
-
conditions: Conditions = {},
|
|
40
|
-
startIndex
|
|
45
|
+
export const createWhereClause = <T>(
|
|
46
|
+
conditions: Conditions<T> = {},
|
|
47
|
+
startIndex = 1,
|
|
48
|
+
clientType: DBClients
|
|
41
49
|
): [string, any[], number] => {
|
|
42
50
|
let index = startIndex
|
|
43
51
|
const whereParts: string[] = []
|
|
44
52
|
const values: any[] = []
|
|
45
53
|
|
|
46
|
-
const processCondition = (key: string, condition: Condition)
|
|
54
|
+
const processCondition = (key: string, condition: Condition<T>) => {
|
|
47
55
|
if (typeof condition === 'object' && condition !== null) {
|
|
48
56
|
if ('operator' in condition && 'value' in condition) {
|
|
49
57
|
const { operator, value } = condition
|
|
50
58
|
if (Array.isArray(value)) {
|
|
51
|
-
const placeholders = value
|
|
59
|
+
const placeholders = value
|
|
60
|
+
.map(() =>
|
|
61
|
+
clientType === 'pg'
|
|
62
|
+
? pgPlaceholderGenerator(index++)
|
|
63
|
+
: mysqlPlaceholderGenerator()
|
|
64
|
+
)
|
|
65
|
+
.join(', ')
|
|
52
66
|
if (operator === 'BETWEEN') {
|
|
53
67
|
whereParts.push(
|
|
54
|
-
|
|
68
|
+
clientType === 'pg'
|
|
69
|
+
? `"${key}" ${operator} ${placeholders.replace(', ', ' AND ')}`
|
|
70
|
+
: `${key} ${operator} ${placeholders.replace(', ', ' AND ')}`
|
|
55
71
|
)
|
|
56
72
|
} else {
|
|
57
|
-
whereParts.push(
|
|
73
|
+
whereParts.push(
|
|
74
|
+
clientType === 'pg'
|
|
75
|
+
? `"${key}" ${operator} (${placeholders})`
|
|
76
|
+
: `${key} ${operator} ${placeholders}`
|
|
77
|
+
)
|
|
58
78
|
}
|
|
59
79
|
values.push(...value)
|
|
60
80
|
} else {
|
|
61
|
-
whereParts.push(
|
|
81
|
+
whereParts.push(
|
|
82
|
+
clientType === 'pg'
|
|
83
|
+
? `"${key}" ${operator} ${pgPlaceholderGenerator(index++)}`
|
|
84
|
+
: `${key} ${operator} ${mysqlPlaceholderGenerator()}`
|
|
85
|
+
)
|
|
62
86
|
values.push(value)
|
|
63
87
|
}
|
|
64
|
-
} else
|
|
65
|
-
|
|
66
|
-
const subWhereParts: string[] = []
|
|
67
|
-
compositeCondition.conditions.forEach((subCondition) => {
|
|
68
|
-
// Recurso: criar uma chave fictícia, pois as condições compostas não necessitam de chave
|
|
69
|
-
index = processCondition('', subCondition)
|
|
70
|
-
})
|
|
71
|
-
whereParts.push(
|
|
72
|
-
`(${subWhereParts.join(` ${compositeCondition.type} `)})`
|
|
73
|
-
)
|
|
88
|
+
} else {
|
|
89
|
+
console.error('Unsupported condition format: ', condition)
|
|
74
90
|
}
|
|
75
|
-
} else if (key) {
|
|
76
|
-
// Condição simples
|
|
77
|
-
whereParts.push(`"${key}" = $${index}`)
|
|
78
|
-
values.push(condition)
|
|
79
|
-
index++
|
|
80
91
|
}
|
|
81
|
-
return index
|
|
82
92
|
}
|
|
83
93
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
94
|
+
if ('OR' in conditions || 'AND' in conditions) {
|
|
95
|
+
const logicalOperator = conditions.OR ? 'OR' : 'AND'
|
|
96
|
+
const compositeConditions = conditions.OR || conditions.AND
|
|
97
|
+
|
|
98
|
+
if (Array.isArray(compositeConditions)) {
|
|
99
|
+
const subWhereParts = compositeConditions
|
|
100
|
+
.map((subCondition) => {
|
|
101
|
+
if (
|
|
102
|
+
typeof subCondition === 'object' &&
|
|
103
|
+
!Array.isArray(subCondition) &&
|
|
104
|
+
subCondition !== null
|
|
105
|
+
) {
|
|
106
|
+
const key = Object.keys(subCondition)[0]
|
|
107
|
+
const condition = subCondition[key]
|
|
108
|
+
processCondition(key, condition)
|
|
109
|
+
return whereParts.pop()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return ''
|
|
113
|
+
})
|
|
114
|
+
.filter((part) => part)
|
|
115
|
+
|
|
116
|
+
whereParts.push(
|
|
117
|
+
clientType === 'pg'
|
|
118
|
+
? `(${subWhereParts.join(` ${logicalOperator} `)})`
|
|
119
|
+
: `${subWhereParts.join(` ${logicalOperator} `)}`
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
Object.entries(conditions).forEach(([key, value]) => {
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
125
|
+
// @ts-expect-error
|
|
126
|
+
processCondition(key, value)
|
|
127
|
+
})
|
|
128
|
+
}
|
|
87
129
|
|
|
88
130
|
const whereClause =
|
|
89
131
|
whereParts.length > 0 ? ` WHERE ${whereParts.join(' AND ')}` : ''
|
|
132
|
+
|
|
90
133
|
return [whereClause, values, index]
|
|
91
134
|
}
|
|
92
135
|
|