velocious 1.0.101 → 1.0.102
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/package.json +1 -1
- package/src/configuration-types.js +7 -1
- package/src/configuration.js +2 -2
- package/src/database/drivers/base.js +27 -4
- package/src/database/query/index.js +31 -18
- package/src/database/record/index.js +444 -172
- package/src/database/record/instance-relationships/base.js +41 -44
- package/src/database/record/instance-relationships/belongs-to.js +15 -3
- package/src/database/record/instance-relationships/has-many.js +49 -28
- package/src/database/record/instance-relationships/has-one.js +22 -7
- package/src/database/record/relationships/base.js +33 -43
- package/src/database/record/relationships/belongs-to.js +13 -3
- package/src/database/record/relationships/has-many.js +8 -2
- package/src/database/record/relationships/has-one.js +8 -2
- package/src/database/record/validators/base.js +14 -2
- package/src/database/record/validators/presence.js +7 -0
- package/src/environment-handlers/node.js +1 -2
package/package.json
CHANGED
|
@@ -21,9 +21,15 @@
|
|
|
21
21
|
* @property {string} [host]
|
|
22
22
|
* @property {boolean} [migrations]
|
|
23
23
|
* @property {string} [password]
|
|
24
|
+
* @property {object} [record]
|
|
25
|
+
* @property {boolean} [record.transactions]
|
|
24
26
|
* @property {string} [username]
|
|
25
27
|
*/
|
|
26
28
|
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {Record<string, string[]>} LocaleFallbacksType
|
|
31
|
+
*/
|
|
32
|
+
|
|
27
33
|
/**
|
|
28
34
|
* @typedef {object} ConfigurationArgsType
|
|
29
35
|
* @property {object} args
|
|
@@ -37,7 +43,7 @@
|
|
|
37
43
|
* @property {InitializersType} initializers
|
|
38
44
|
* @property {string | function() : string} locale
|
|
39
45
|
* @property {string[]} locales
|
|
40
|
-
* @property {
|
|
46
|
+
* @property {LocaleFallbacksType} localeFallbacks
|
|
41
47
|
* @property {string} testing
|
|
42
48
|
*/
|
|
43
49
|
|
package/src/configuration.js
CHANGED
|
@@ -162,12 +162,12 @@ export default class VelociousConfiguration {
|
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
/**
|
|
165
|
-
* @returns {
|
|
165
|
+
* @returns {import("./configuration-types.js").LocaleFallbacksType | undefined}
|
|
166
166
|
*/
|
|
167
167
|
getLocaleFallbacks() { return this.localeFallbacks }
|
|
168
168
|
|
|
169
169
|
/**
|
|
170
|
-
* @param {
|
|
170
|
+
* @param {import("./configuration-types.js").LocaleFallbacksType} newLocaleFallbacks
|
|
171
171
|
* @returns {void}
|
|
172
172
|
*/
|
|
173
173
|
setLocaleFallbacks(newLocaleFallbacks) { this.localeFallbacks = newLocaleFallbacks }
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
* @property {string[]} [columns]
|
|
24
24
|
* @property {{[key: string]: any}} [data]
|
|
25
25
|
* @property {boolean} [multiple]
|
|
26
|
-
* @property {boolean} [returnLastInsertedColumnNames]
|
|
26
|
+
* @property {boolean | string[]} [returnLastInsertedColumnNames]
|
|
27
27
|
* @property {Array<Array<any>>} [rows]
|
|
28
28
|
* @property {string} tableName
|
|
29
29
|
*/
|
|
@@ -189,7 +189,7 @@ export default class VelociousDatabaseDriversBase {
|
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
/**
|
|
192
|
-
* @returns {
|
|
192
|
+
* @returns {import("../../configuration-types.js").DatabaseConfigurationType}
|
|
193
193
|
*/
|
|
194
194
|
getArgs() {
|
|
195
195
|
return this._args
|
|
@@ -227,9 +227,21 @@ export default class VelociousDatabaseDriversBase {
|
|
|
227
227
|
*/
|
|
228
228
|
async getTableByName(name, args) {
|
|
229
229
|
const tables = await this.getTables()
|
|
230
|
-
const
|
|
230
|
+
const tableNames = []
|
|
231
|
+
let table
|
|
231
232
|
|
|
232
|
-
|
|
233
|
+
for (const candidate of tables) {
|
|
234
|
+
const candidateName = candidate.getName()
|
|
235
|
+
|
|
236
|
+
if (candidateName == name) {
|
|
237
|
+
table = candidate
|
|
238
|
+
break
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
tableNames.push(candidateName)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (!table && args?.throwError !== false) throw new Error(`Couldn't find a table by that name "${name}" in: ${tableNames.join(", ")}`)
|
|
233
245
|
|
|
234
246
|
return table
|
|
235
247
|
}
|
|
@@ -252,6 +264,17 @@ export default class VelociousDatabaseDriversBase {
|
|
|
252
264
|
await this.query(sql)
|
|
253
265
|
}
|
|
254
266
|
|
|
267
|
+
/**
|
|
268
|
+
* @abstract
|
|
269
|
+
* @param {string} tableName
|
|
270
|
+
* @param {Array<string>} columns
|
|
271
|
+
* @param {Array<Array<string>>} rows
|
|
272
|
+
* @returns {Promise<void>}
|
|
273
|
+
*/
|
|
274
|
+
async insertMultiple(tableName, columns, rows) { // eslint-disable-line no-unused-vars
|
|
275
|
+
throw new Error("'insertMultiple' not implemented")
|
|
276
|
+
}
|
|
277
|
+
|
|
255
278
|
/**
|
|
256
279
|
* @abstract
|
|
257
280
|
* @param {InsertSqlArgsType} args
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {{[key: string]: boolean | NestedPreloadRecord }} NestedPreloadRecord
|
|
5
|
+
* @typedef {string|SelectPlain} SelectArgumentType
|
|
6
|
+
* @typedef {object|string} WhereArgumentType
|
|
7
|
+
*/
|
|
8
|
+
|
|
3
9
|
import FromPlain from "./from-plain.js"
|
|
4
10
|
import {incorporate} from "incorporator"
|
|
5
11
|
import * as inflection from "inflection"
|
|
@@ -16,10 +22,6 @@ import WhereHash from "./where-hash.js"
|
|
|
16
22
|
import WherePlain from "./where-plain.js"
|
|
17
23
|
import restArgsError from "../../utils/rest-args-error.js"
|
|
18
24
|
|
|
19
|
-
/**
|
|
20
|
-
* @typedef {{[key: string]: boolean | NestedPreloadRecord }} NestedPreloadRecord
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
25
|
/**
|
|
24
26
|
* A generic query over some model type.
|
|
25
27
|
*/
|
|
@@ -112,7 +114,7 @@ export default class VelociousDatabaseQuery {
|
|
|
112
114
|
*/
|
|
113
115
|
async count() {
|
|
114
116
|
// Generate count SQL
|
|
115
|
-
let sql = `COUNT(${this.driver.quoteTable(this.
|
|
117
|
+
let sql = `COUNT(${this.driver.quoteTable(this.getModelClass().tableName())}.${this.driver.quoteColumn(this.getModelClass().primaryKey())})`
|
|
116
118
|
|
|
117
119
|
if (this.driver.getType() == "pgsql") sql += "::int"
|
|
118
120
|
|
|
@@ -162,6 +164,15 @@ export default class VelociousDatabaseQuery {
|
|
|
162
164
|
return this._groups
|
|
163
165
|
}
|
|
164
166
|
|
|
167
|
+
/**
|
|
168
|
+
* @returns {typeof import("../record/index.js").default}
|
|
169
|
+
*/
|
|
170
|
+
getModelClass() {
|
|
171
|
+
if (!this.modelClass) throw new Error("modelClass not set")
|
|
172
|
+
|
|
173
|
+
return this.modelClass
|
|
174
|
+
}
|
|
175
|
+
|
|
165
176
|
/**
|
|
166
177
|
* @returns {import("../query-parser/options.js").default}
|
|
167
178
|
*/
|
|
@@ -191,13 +202,13 @@ export default class VelociousDatabaseQuery {
|
|
|
191
202
|
/** @type {{[key: string]: number | string}} */
|
|
192
203
|
const conditions = {}
|
|
193
204
|
|
|
194
|
-
conditions[this.
|
|
205
|
+
conditions[this.getModelClass().primaryKey()] = recordId
|
|
195
206
|
|
|
196
207
|
const query = this.clone().where(conditions)
|
|
197
208
|
const record = await query.first()
|
|
198
209
|
|
|
199
210
|
if (!record) {
|
|
200
|
-
throw new RecordNotFoundError(`Couldn't find ${this.
|
|
211
|
+
throw new RecordNotFoundError(`Couldn't find ${this.getModelClass().name} with '${this.getModelClass().primaryKey()}'=${recordId}`)
|
|
201
212
|
}
|
|
202
213
|
|
|
203
214
|
return record
|
|
@@ -268,7 +279,8 @@ export default class VelociousDatabaseQuery {
|
|
|
268
279
|
|
|
269
280
|
if (record) return record
|
|
270
281
|
|
|
271
|
-
const
|
|
282
|
+
const ModelClass = this.getModelClass()
|
|
283
|
+
const newRecord = new ModelClass(conditions)
|
|
272
284
|
|
|
273
285
|
if (callback) {
|
|
274
286
|
callback(newRecord)
|
|
@@ -281,14 +293,14 @@ export default class VelociousDatabaseQuery {
|
|
|
281
293
|
* @returns {Promise<import("../record/index.js").default>}
|
|
282
294
|
*/
|
|
283
295
|
async first() {
|
|
284
|
-
const newQuery = this.clone().limit(1).reorder(`${this.driver.quoteTable(this.
|
|
296
|
+
const newQuery = this.clone().limit(1).reorder(`${this.driver.quoteTable(this.getModelClass().tableName())}.${this.driver.quoteColumn(this.getModelClass().orderableColumn())}`)
|
|
285
297
|
const results = await newQuery.toArray()
|
|
286
298
|
|
|
287
299
|
return results[0]
|
|
288
300
|
}
|
|
289
301
|
|
|
290
302
|
/**
|
|
291
|
-
* @param {string|
|
|
303
|
+
* @param {string|import("./from-base.js").default} from
|
|
292
304
|
* @returns {this}
|
|
293
305
|
*/
|
|
294
306
|
from(from) {
|
|
@@ -327,8 +339,8 @@ export default class VelociousDatabaseQuery {
|
|
|
327
339
|
* @returns {Promise<import("../record/index.js").default>}
|
|
328
340
|
*/
|
|
329
341
|
async last() {
|
|
330
|
-
const primaryKey = this.
|
|
331
|
-
const tableName = this.
|
|
342
|
+
const primaryKey = this.getModelClass().primaryKey()
|
|
343
|
+
const tableName = this.getModelClass().tableName()
|
|
332
344
|
const results = await this.clone().reorder(`${this.driver.quoteTable(tableName)}.${this.driver.quoteColumn(primaryKey)} DESC`).limit(1).toArray()
|
|
333
345
|
|
|
334
346
|
return results[0]
|
|
@@ -353,7 +365,7 @@ export default class VelociousDatabaseQuery {
|
|
|
353
365
|
}
|
|
354
366
|
|
|
355
367
|
/**
|
|
356
|
-
* @param {
|
|
368
|
+
* @param {string | number} order
|
|
357
369
|
* @returns {this}
|
|
358
370
|
*/
|
|
359
371
|
order(order) {
|
|
@@ -393,7 +405,7 @@ export default class VelociousDatabaseQuery {
|
|
|
393
405
|
}
|
|
394
406
|
|
|
395
407
|
/**
|
|
396
|
-
* @param {
|
|
408
|
+
* @param {NestedPreloadRecord} data
|
|
397
409
|
* @returns {this}
|
|
398
410
|
*/
|
|
399
411
|
preload(data) {
|
|
@@ -402,7 +414,7 @@ export default class VelociousDatabaseQuery {
|
|
|
402
414
|
}
|
|
403
415
|
|
|
404
416
|
/**
|
|
405
|
-
* @param {string|
|
|
417
|
+
* @param {string | number} order
|
|
406
418
|
* @returns {this}
|
|
407
419
|
*/
|
|
408
420
|
reorder(order) {
|
|
@@ -423,7 +435,7 @@ export default class VelociousDatabaseQuery {
|
|
|
423
435
|
}
|
|
424
436
|
|
|
425
437
|
/**
|
|
426
|
-
* @param {
|
|
438
|
+
* @param {SelectArgumentType} select
|
|
427
439
|
* @returns {this}
|
|
428
440
|
*/
|
|
429
441
|
select(select) {
|
|
@@ -474,7 +486,8 @@ export default class VelociousDatabaseQuery {
|
|
|
474
486
|
const results = await this.results()
|
|
475
487
|
|
|
476
488
|
for (const result of results) {
|
|
477
|
-
const
|
|
489
|
+
const ModelClass = this.getModelClass()
|
|
490
|
+
const model = new ModelClass()
|
|
478
491
|
|
|
479
492
|
model.loadExistingRecord(result)
|
|
480
493
|
models.push(model)
|
|
@@ -500,7 +513,7 @@ export default class VelociousDatabaseQuery {
|
|
|
500
513
|
toSql() { return this.driver.queryToSql(this) }
|
|
501
514
|
|
|
502
515
|
/**
|
|
503
|
-
* @param {
|
|
516
|
+
* @param {WhereArgumentType} where
|
|
504
517
|
* @returns {VelociousDatabaseQuery} This query instance
|
|
505
518
|
*/
|
|
506
519
|
where(where) {
|