@cuboapp/database 1.0.0

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/.prettierrc ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "trailingComma": "none",
3
+ "tabWidth": 2,
4
+ "semi": false,
5
+ "singleQuote": true,
6
+ "eslintIntegration": true,
7
+ "printWidth": 140
8
+ }
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@cuboapp/database",
3
+ "version": "1.0.0",
4
+ "description": "Database methods",
5
+ "main": "src/index.ts",
6
+ "repository": "git@cuboapp.gitlab.yandexcloud.net:cubo/database.git",
7
+ "author": "CuboSoft",
8
+ "license": "MIT",
9
+ "type": "module",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "dependencies": {
14
+ "pg": "^8.16.3",
15
+ "sequelize": "^6.37.7"
16
+ },
17
+ "devDependencies": {
18
+ "@types/node": "^24.0.0",
19
+ "typescript": "^5.9.3"
20
+ }
21
+ }
package/pnpm-lock.yaml ADDED
@@ -0,0 +1,314 @@
1
+ lockfileVersion: '9.0'
2
+
3
+ settings:
4
+ autoInstallPeers: true
5
+ excludeLinksFromLockfile: false
6
+
7
+ importers:
8
+
9
+ .:
10
+ dependencies:
11
+ pg:
12
+ specifier: ^8.16.3
13
+ version: 8.16.3
14
+ sequelize:
15
+ specifier: ^6.37.7
16
+ version: 6.37.7(pg@8.16.3)
17
+ devDependencies:
18
+ '@types/node':
19
+ specifier: ^24.0.0
20
+ version: 24.8.1
21
+ typescript:
22
+ specifier: ^5.9.3
23
+ version: 5.9.3
24
+
25
+ packages:
26
+
27
+ '@types/debug@4.1.12':
28
+ resolution: {integrity: sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==}
29
+
30
+ '@types/ms@2.1.0':
31
+ resolution: {integrity: sha512-GsCCIZDE/p3i96vtEqx+7dBUGXrc7zeSK3wwPHIaRThS+9OhWIXRqzs4d6k1SVU8g91DrNRWxWUGhp5KXQb2VA==}
32
+
33
+ '@types/node@24.8.1':
34
+ resolution: {integrity: sha512-alv65KGRadQVfVcG69MuB4IzdYVpRwMG/mq8KWOaoOdyY617P5ivaDiMCGOFDWD2sAn5Q0mR3mRtUOgm99hL9Q==}
35
+
36
+ '@types/validator@13.15.3':
37
+ resolution: {integrity: sha512-7bcUmDyS6PN3EuD9SlGGOxM77F8WLVsrwkxyWxKnxzmXoequ6c7741QBrANq6htVRGOITJ7z72mTP6Z4XyuG+Q==}
38
+
39
+ debug@4.4.3:
40
+ resolution: {integrity: sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==}
41
+ engines: {node: '>=6.0'}
42
+ peerDependencies:
43
+ supports-color: '*'
44
+ peerDependenciesMeta:
45
+ supports-color:
46
+ optional: true
47
+
48
+ dottie@2.0.6:
49
+ resolution: {integrity: sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA==}
50
+
51
+ inflection@1.13.4:
52
+ resolution: {integrity: sha512-6I/HUDeYFfuNCVS3td055BaXBwKYuzw7K3ExVMStBowKo9oOAMJIXIHvdyR3iboTCp1b+1i5DSkIZTcwIktuDw==}
53
+ engines: {'0': node >= 0.4.0}
54
+
55
+ lodash@4.17.21:
56
+ resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==}
57
+
58
+ moment-timezone@0.5.48:
59
+ resolution: {integrity: sha512-f22b8LV1gbTO2ms2j2z13MuPogNoh5UzxL3nzNAYKGraILnbGc9NEE6dyiiiLv46DGRb8A4kg8UKWLjPthxBHw==}
60
+
61
+ moment@2.30.1:
62
+ resolution: {integrity: sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==}
63
+
64
+ ms@2.1.3:
65
+ resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==}
66
+
67
+ pg-cloudflare@1.2.7:
68
+ resolution: {integrity: sha512-YgCtzMH0ptvZJslLM1ffsY4EuGaU0cx4XSdXLRFae8bPP4dS5xL1tNB3k2o/N64cHJpwU7dxKli/nZ2lUa5fLg==}
69
+
70
+ pg-connection-string@2.9.1:
71
+ resolution: {integrity: sha512-nkc6NpDcvPVpZXxrreI/FOtX3XemeLl8E0qFr6F2Lrm/I8WOnaWNhIPK2Z7OHpw7gh5XJThi6j6ppgNoaT1w4w==}
72
+
73
+ pg-int8@1.0.1:
74
+ resolution: {integrity: sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==}
75
+ engines: {node: '>=4.0.0'}
76
+
77
+ pg-pool@3.10.1:
78
+ resolution: {integrity: sha512-Tu8jMlcX+9d8+QVzKIvM/uJtp07PKr82IUOYEphaWcoBhIYkoHpLXN3qO59nAI11ripznDsEzEv8nUxBVWajGg==}
79
+ peerDependencies:
80
+ pg: '>=8.0'
81
+
82
+ pg-protocol@1.10.3:
83
+ resolution: {integrity: sha512-6DIBgBQaTKDJyxnXaLiLR8wBpQQcGWuAESkRBX/t6OwA8YsqP+iVSiond2EDy6Y/dsGk8rh/jtax3js5NeV7JQ==}
84
+
85
+ pg-types@2.2.0:
86
+ resolution: {integrity: sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==}
87
+ engines: {node: '>=4'}
88
+
89
+ pg@8.16.3:
90
+ resolution: {integrity: sha512-enxc1h0jA/aq5oSDMvqyW3q89ra6XIIDZgCX9vkMrnz5DFTw/Ny3Li2lFQ+pt3L6MCgm/5o2o8HW9hiJji+xvw==}
91
+ engines: {node: '>= 16.0.0'}
92
+ peerDependencies:
93
+ pg-native: '>=3.0.1'
94
+ peerDependenciesMeta:
95
+ pg-native:
96
+ optional: true
97
+
98
+ pgpass@1.0.5:
99
+ resolution: {integrity: sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==}
100
+
101
+ postgres-array@2.0.0:
102
+ resolution: {integrity: sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==}
103
+ engines: {node: '>=4'}
104
+
105
+ postgres-bytea@1.0.0:
106
+ resolution: {integrity: sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==}
107
+ engines: {node: '>=0.10.0'}
108
+
109
+ postgres-date@1.0.7:
110
+ resolution: {integrity: sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==}
111
+ engines: {node: '>=0.10.0'}
112
+
113
+ postgres-interval@1.2.0:
114
+ resolution: {integrity: sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==}
115
+ engines: {node: '>=0.10.0'}
116
+
117
+ retry-as-promised@7.1.1:
118
+ resolution: {integrity: sha512-hMD7odLOt3LkTjcif8aRZqi/hybjpLNgSk5oF5FCowfCjok6LukpN2bDX7R5wDmbgBQFn7YoBxSagmtXHaJYJw==}
119
+
120
+ semver@7.7.3:
121
+ resolution: {integrity: sha512-SdsKMrI9TdgjdweUSR9MweHA4EJ8YxHn8DFaDisvhVlUOe4BF1tLD7GAj0lIqWVl+dPb/rExr0Btby5loQm20Q==}
122
+ engines: {node: '>=10'}
123
+ hasBin: true
124
+
125
+ sequelize-pool@7.1.0:
126
+ resolution: {integrity: sha512-G9c0qlIWQSK29pR/5U2JF5dDQeqqHRragoyahj/Nx4KOOQ3CPPfzxnfqFPCSB7x5UgjOgnZ61nSxz+fjDpRlJg==}
127
+ engines: {node: '>= 10.0.0'}
128
+
129
+ sequelize@6.37.7:
130
+ resolution: {integrity: sha512-mCnh83zuz7kQxxJirtFD7q6Huy6liPanI67BSlbzSYgVNl5eXVdE2CN1FuAeZwG1SNpGsNRCV+bJAVVnykZAFA==}
131
+ engines: {node: '>=10.0.0'}
132
+ peerDependencies:
133
+ ibm_db: '*'
134
+ mariadb: '*'
135
+ mysql2: '*'
136
+ oracledb: '*'
137
+ pg: '*'
138
+ pg-hstore: '*'
139
+ snowflake-sdk: '*'
140
+ sqlite3: '*'
141
+ tedious: '*'
142
+ peerDependenciesMeta:
143
+ ibm_db:
144
+ optional: true
145
+ mariadb:
146
+ optional: true
147
+ mysql2:
148
+ optional: true
149
+ oracledb:
150
+ optional: true
151
+ pg:
152
+ optional: true
153
+ pg-hstore:
154
+ optional: true
155
+ snowflake-sdk:
156
+ optional: true
157
+ sqlite3:
158
+ optional: true
159
+ tedious:
160
+ optional: true
161
+
162
+ split2@4.2.0:
163
+ resolution: {integrity: sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==}
164
+ engines: {node: '>= 10.x'}
165
+
166
+ toposort-class@1.0.1:
167
+ resolution: {integrity: sha512-OsLcGGbYF3rMjPUf8oKktyvCiUxSbqMMS39m33MAjLTC1DVIH6x3WSt63/M77ihI09+Sdfk1AXvfhCEeUmC7mg==}
168
+
169
+ typescript@5.9.3:
170
+ resolution: {integrity: sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==}
171
+ engines: {node: '>=14.17'}
172
+ hasBin: true
173
+
174
+ undici-types@7.14.0:
175
+ resolution: {integrity: sha512-QQiYxHuyZ9gQUIrmPo3IA+hUl4KYk8uSA7cHrcKd/l3p1OTpZcM0Tbp9x7FAtXdAYhlasd60ncPpgu6ihG6TOA==}
176
+
177
+ uuid@8.3.2:
178
+ resolution: {integrity: sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==}
179
+ hasBin: true
180
+
181
+ validator@13.15.15:
182
+ resolution: {integrity: sha512-BgWVbCI72aIQy937xbawcs+hrVaN/CZ2UwutgaJ36hGqRrLNM+f5LUT/YPRbo8IV/ASeFzXszezV+y2+rq3l8A==}
183
+ engines: {node: '>= 0.10'}
184
+
185
+ wkx@0.5.0:
186
+ resolution: {integrity: sha512-Xng/d4Ichh8uN4l0FToV/258EjMGU9MGcA0HV2d9B/ZpZB3lqQm7nkOdZdm5GhKtLLhAE7PiVQwN4eN+2YJJUg==}
187
+
188
+ xtend@4.0.2:
189
+ resolution: {integrity: sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==}
190
+ engines: {node: '>=0.4'}
191
+
192
+ snapshots:
193
+
194
+ '@types/debug@4.1.12':
195
+ dependencies:
196
+ '@types/ms': 2.1.0
197
+
198
+ '@types/ms@2.1.0': {}
199
+
200
+ '@types/node@24.8.1':
201
+ dependencies:
202
+ undici-types: 7.14.0
203
+
204
+ '@types/validator@13.15.3': {}
205
+
206
+ debug@4.4.3:
207
+ dependencies:
208
+ ms: 2.1.3
209
+
210
+ dottie@2.0.6: {}
211
+
212
+ inflection@1.13.4: {}
213
+
214
+ lodash@4.17.21: {}
215
+
216
+ moment-timezone@0.5.48:
217
+ dependencies:
218
+ moment: 2.30.1
219
+
220
+ moment@2.30.1: {}
221
+
222
+ ms@2.1.3: {}
223
+
224
+ pg-cloudflare@1.2.7:
225
+ optional: true
226
+
227
+ pg-connection-string@2.9.1: {}
228
+
229
+ pg-int8@1.0.1: {}
230
+
231
+ pg-pool@3.10.1(pg@8.16.3):
232
+ dependencies:
233
+ pg: 8.16.3
234
+
235
+ pg-protocol@1.10.3: {}
236
+
237
+ pg-types@2.2.0:
238
+ dependencies:
239
+ pg-int8: 1.0.1
240
+ postgres-array: 2.0.0
241
+ postgres-bytea: 1.0.0
242
+ postgres-date: 1.0.7
243
+ postgres-interval: 1.2.0
244
+
245
+ pg@8.16.3:
246
+ dependencies:
247
+ pg-connection-string: 2.9.1
248
+ pg-pool: 3.10.1(pg@8.16.3)
249
+ pg-protocol: 1.10.3
250
+ pg-types: 2.2.0
251
+ pgpass: 1.0.5
252
+ optionalDependencies:
253
+ pg-cloudflare: 1.2.7
254
+
255
+ pgpass@1.0.5:
256
+ dependencies:
257
+ split2: 4.2.0
258
+
259
+ postgres-array@2.0.0: {}
260
+
261
+ postgres-bytea@1.0.0: {}
262
+
263
+ postgres-date@1.0.7: {}
264
+
265
+ postgres-interval@1.2.0:
266
+ dependencies:
267
+ xtend: 4.0.2
268
+
269
+ retry-as-promised@7.1.1: {}
270
+
271
+ semver@7.7.3: {}
272
+
273
+ sequelize-pool@7.1.0: {}
274
+
275
+ sequelize@6.37.7(pg@8.16.3):
276
+ dependencies:
277
+ '@types/debug': 4.1.12
278
+ '@types/validator': 13.15.3
279
+ debug: 4.4.3
280
+ dottie: 2.0.6
281
+ inflection: 1.13.4
282
+ lodash: 4.17.21
283
+ moment: 2.30.1
284
+ moment-timezone: 0.5.48
285
+ pg-connection-string: 2.9.1
286
+ retry-as-promised: 7.1.1
287
+ semver: 7.7.3
288
+ sequelize-pool: 7.1.0
289
+ toposort-class: 1.0.1
290
+ uuid: 8.3.2
291
+ validator: 13.15.15
292
+ wkx: 0.5.0
293
+ optionalDependencies:
294
+ pg: 8.16.3
295
+ transitivePeerDependencies:
296
+ - supports-color
297
+
298
+ split2@4.2.0: {}
299
+
300
+ toposort-class@1.0.1: {}
301
+
302
+ typescript@5.9.3: {}
303
+
304
+ undici-types@7.14.0: {}
305
+
306
+ uuid@8.3.2: {}
307
+
308
+ validator@13.15.15: {}
309
+
310
+ wkx@0.5.0:
311
+ dependencies:
312
+ '@types/node': 24.8.1
313
+
314
+ xtend@4.0.2: {}
@@ -0,0 +1,145 @@
1
+ import { Options, QueryTypes, Sequelize, Transaction } from 'sequelize'
2
+
3
+ import { DatabaseOptions } from './types'
4
+ import { dbPrepareInsertQueryString, dbPrepareUpdateQueryString } from './utils'
5
+
6
+ export class Database {
7
+ public connection: Sequelize
8
+
9
+ constructor(private connectOptions: Options, private otherOptions?: DatabaseOptions) {}
10
+
11
+ public async connect() {
12
+ const opts: Options = {
13
+ ...this.connectOptions,
14
+ logging: false,
15
+ timezone: '+03:00'
16
+ }
17
+
18
+ if (this.otherOptions?.iaas === 'yandex-cloud') {
19
+ opts.dialectOptions = {
20
+ ...(opts.dialectOptions || {}),
21
+ ...{
22
+ ssl: {
23
+ ca: await fetch('https://static.cuboapp.ru/cloud-certs/yandex.pem').then((res) => res.text()),
24
+ rejectUnauthorized: true,
25
+ target_session_attrs: 'read-write',
26
+ require: true
27
+ }
28
+ }
29
+ }
30
+ }
31
+
32
+ this.connection = new Sequelize(opts)
33
+
34
+ await this.connection.authenticate()
35
+
36
+ return this.connection
37
+ }
38
+
39
+ public async disconnect() {
40
+ return this.connection.close()
41
+ }
42
+
43
+ public async create<T = number | string>(
44
+ table: string,
45
+ arInsert: any,
46
+ opts?: { log?: boolean; transaction?: Transaction; pk_key?: string; pk_type?: 'string' | 'number' }
47
+ ): Promise<T[]> {
48
+ if (Object.keys(arInsert).length > 0) {
49
+ const { keys, values, replacements } = dbPrepareInsertQueryString(arInsert)
50
+
51
+ const pkKey = opts?.pk_key ?? 'id'
52
+ const pkType = opts?.pk_type ?? 'number'
53
+
54
+ let sql = ''
55
+
56
+ switch (this.connectOptions.dialect) {
57
+ case 'postgres':
58
+ sql = `insert into ${table} (${keys}) values (${values}) returning ${pkKey}`
59
+ break
60
+ case 'mysql':
61
+ sql = `insert into ${table} (${keys}) values (${values})`
62
+ break
63
+ default:
64
+ throw ''
65
+ }
66
+
67
+ if (opts?.log) {
68
+ console.warn(`CREATE QUERY: ${sql} [${JSON.stringify(replacements)}]`)
69
+ }
70
+
71
+ return this.connection
72
+ .query(sql, {
73
+ type: QueryTypes.INSERT,
74
+ replacements,
75
+ transaction: opts?.transaction
76
+ })
77
+ .then((res: any) => {
78
+ let pk = []
79
+
80
+ switch (this.connectOptions.dialect) {
81
+ case 'postgres':
82
+ pk = (res?.[0] || []).map((r: any) => r[pkKey]) as string[]
83
+ break
84
+ case 'mysql':
85
+ pk = res as string[]
86
+ break
87
+ }
88
+
89
+ return pkType === 'number' ? pk.map((r) => +r) : pk
90
+ })
91
+ }
92
+
93
+ throw 'QUERY: db create insert object is empty'
94
+ }
95
+
96
+ public async update<T = number | string>(
97
+ table: string,
98
+ where: string,
99
+ replacements: Record<string, any>,
100
+ arUpdate: any,
101
+ opts?: { log?: boolean; transaction?: Transaction; pk_key?: string; pk_type?: 'string' | 'number' }
102
+ ): Promise<T[]> {
103
+ if (Object.keys(arUpdate).length > 0) {
104
+ const prepared = dbPrepareUpdateQueryString(arUpdate)
105
+
106
+ const pkKey = opts?.pk_key ?? 'id'
107
+ const pkType = opts?.pk_type ?? 'number'
108
+
109
+ let sql = ''
110
+
111
+ switch (this.connectOptions.dialect) {
112
+ case 'postgres':
113
+ sql = `update ${table} set ${prepared.query} where ${where} returning ${pkKey}`
114
+ break
115
+ case 'mysql':
116
+ sql = `update ${table} set ${prepared.query} where ${where}`
117
+ break
118
+ default:
119
+ throw ''
120
+ }
121
+
122
+ if (opts?.log) {
123
+ console.warn(`UPDATE QUERY: ${sql} [${JSON.stringify(replacements)}]`)
124
+ }
125
+
126
+ const result = await this.connection.query(sql, {
127
+ type: QueryTypes.UPDATE,
128
+ replacements: {
129
+ ...(replacements || {}),
130
+ ...(prepared.replacements || {})
131
+ },
132
+ transaction: opts?.transaction
133
+ })
134
+
135
+ switch (this.connectOptions.dialect) {
136
+ case 'postgres':
137
+ const res = result as unknown as [{ [pkKey]: T }[], T]
138
+
139
+ return res[0].map((r) => (pkType === 'number' ? +r[pkKey] : r[pkKey])) as T[]
140
+ case 'mysql':
141
+ return res as T[]
142
+ }
143
+ }
144
+ }
145
+ }
@@ -0,0 +1,5 @@
1
+ export type DatabaseOptions = {
2
+ iaas?: 'yandex-cloud'
3
+ pk_key?: string
4
+ pk_type?: 'string' | 'number'
5
+ }
@@ -0,0 +1,128 @@
1
+ import { QueryTypes, Sequelize, Transaction } from 'sequelize'
2
+
3
+ export function dbPrepareUpdateQueryString(obj: Record<string, any>, prefix = '') {
4
+ const keys: string[] = []
5
+ const replacements: Record<string, any> = {}
6
+
7
+ for (const key in obj) {
8
+ const value = obj[key]
9
+
10
+ keys.push(`${key + (prefix ? '_' + prefix : '')} = :${key + (prefix ? '_' + prefix : '')}`)
11
+
12
+ let val = null
13
+
14
+ if (typeof value === 'string') {
15
+ val = !!value ? value : null
16
+ } else if (typeof value === 'number') {
17
+ val = !isNaN(value) ? value : null
18
+ } else if (typeof value === 'boolean') {
19
+ val = value
20
+ } else if (Array.isArray(value) || typeof value === 'object') {
21
+ val = value ? JSON.stringify(value) : null
22
+ }
23
+
24
+ replacements[key + (prefix ? '_' + prefix : '')] = val
25
+ }
26
+
27
+ return { query: keys.join(', '), replacements }
28
+ }
29
+
30
+ export function dbPrepareInsertQueryString(obj: Record<string, any>, prefix = '') {
31
+ const values: string[] = []
32
+ const replacements: Record<string, any> = {}
33
+
34
+ for (const key in obj) {
35
+ const value = obj[key]
36
+
37
+ values.push(`:${key + (prefix ? '_' + prefix : '')}`)
38
+
39
+ let val = null
40
+
41
+ if (typeof value === 'string') {
42
+ val = !!value ? value : null
43
+ } else if (typeof value === 'number') {
44
+ val = !isNaN(value) ? value : null
45
+ } else if (typeof value === 'boolean') {
46
+ val = value
47
+ } else if (Array.isArray(value) || typeof value === 'object') {
48
+ val = value ? JSON.stringify(value) : null
49
+ }
50
+
51
+ replacements[key + (prefix ? '_' + prefix : '')] = val
52
+ }
53
+
54
+ return { keys: Object.keys(obj).join(', '), values: values.join(','), replacements }
55
+ }
56
+
57
+ export async function dbCreate<T = number>(
58
+ db: Sequelize,
59
+ table: string,
60
+ arInsert: any,
61
+ opts?: { log?: boolean; transaction?: Transaction; uuid?: boolean }
62
+ ): Promise<{ id: T }> {
63
+ if (Object.keys(arInsert).length > 0) {
64
+ const dialect = db.getDialect()
65
+
66
+ const { keys, values, replacements } = dbPrepareInsertQueryString(arInsert)
67
+
68
+ let sql = ''
69
+
70
+ switch (dialect) {
71
+ case 'postgres':
72
+ sql = `insert into ${table} (${keys}) values (${values}) returning id`
73
+ break
74
+ case 'mysql':
75
+ sql = `insert into ${table} (${keys}) values (${values})`
76
+ break
77
+ default:
78
+ throw ''
79
+ }
80
+
81
+ if (opts?.log) {
82
+ console.warn(`QUERY: ${sql} [${JSON.stringify(replacements)}]`)
83
+ }
84
+
85
+ return db
86
+ .query(sql, {
87
+ type: QueryTypes.INSERT,
88
+ replacements,
89
+ transaction: opts?.transaction
90
+ })
91
+ .then((res: any) => {
92
+ switch (dialect) {
93
+ case 'postgres':
94
+ return { id: opts?.uuid ? res?.[0]?.[0].id : +res?.[0]?.[0].id }
95
+ case 'mysql':
96
+ return { id: opts?.uuid ? res[0] : +res[0] }
97
+ }
98
+ })
99
+ }
100
+
101
+ throw new Error('QUERY: db create array empty')
102
+ }
103
+
104
+ export async function dbUpdate(
105
+ db: Sequelize,
106
+ table: string,
107
+ where: string,
108
+ replacements: Record<string, any>,
109
+ arUpdate: any,
110
+ opts?: { log?: boolean; transaction?: Transaction }
111
+ ) {
112
+ if (Object.keys(arUpdate).length > 0) {
113
+ const prepared = dbPrepareUpdateQueryString(arUpdate)
114
+
115
+ if (opts?.log) {
116
+ console.warn(`QUERY: update ${table} set ${prepared.query} where ${where} [${JSON.stringify(replacements)}]`)
117
+ }
118
+
119
+ await db.query(`update ${table} set ${prepared.query} where ${where}`, {
120
+ type: QueryTypes.UPDATE,
121
+ replacements: {
122
+ ...(replacements || {}),
123
+ ...(prepared.replacements || {})
124
+ },
125
+ transaction: opts?.transaction
126
+ })
127
+ }
128
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './db'
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "declaration": true,
5
+ "removeComments": true,
6
+ "emitDecoratorMetadata": true,
7
+ "experimentalDecorators": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "target": "ES2021",
10
+ "sourceMap": true,
11
+ "outDir": "./dist",
12
+ "baseUrl": "./",
13
+ "incremental": true,
14
+ "skipLibCheck": true,
15
+ "paths": {
16
+ "@/*": ["src/*"]
17
+ }
18
+ },
19
+ "include": ["src/**/*"],
20
+ "exclude": ["node_modules", "dist"]
21
+ }