@seip/bluebird-js 0.1.2 → 0.1.4
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/.vscode/extensions.json +7 -0
- package/.vscode/settings.json +10 -0
- package/AGENTS.md +92 -60
- package/README.md +224 -152
- package/app/routes/web.js +47 -55
- package/app/views/about.jsx +98 -0
- package/app/views/components/Header.jsx +42 -0
- package/app/views/components/LiveCounter.jsx +67 -0
- package/app/views/dashboard.jsx +300 -0
- package/app/views/index.jsx +123 -0
- package/app/views/layouts/main/view.jsx +201 -0
- package/app/views/legal/terms.html +29 -19
- package/app/views/login.jsx +141 -0
- package/core/actions.d.ts +4 -0
- package/core/actions.js +329 -0
- package/core/app.js +38 -6
- package/core/cli/doctor.js +3 -4
- package/core/cli/init.js +20 -6
- package/core/cli/migrate.js +418 -145
- package/core/cli/view.js +76 -11
- package/core/database.js +6 -10
- package/core/debug.js +1 -1
- package/core/html.d.ts +54 -42
- package/core/html.js +473 -217
- package/core/index.d.ts +172 -84
- package/core/jsx.d.ts +38 -0
- package/core/jsx.js +1180 -0
- package/core/model.js +271 -61
- package/core/render-cache.js +212 -212
- package/core/render.d.ts +20 -2
- package/core/render.js +320 -43
- package/core/validate.js +1 -1
- package/core/view-loader.js +313 -22
- package/package.json +9 -3
- package/public/js/bluebird-client.js +2313 -1049
- package/public/js/tailwind.js +8 -0
- package/app/views/about.js +0 -63
- package/app/views/dashboard.js +0 -147
- package/app/views/index.js +0 -80
- package/app/views/layouts/main/view.js +0 -81
- package/app/views/login.js +0 -80
- package/public/css/bluebird.min.css +0 -2
- package/public/js/bluebird.min.js +0 -1073
package/core/model.js
CHANGED
|
@@ -11,13 +11,57 @@ import Hash from "./hash.js";
|
|
|
11
11
|
function buildWhere(where) {
|
|
12
12
|
const whereClauses = [];
|
|
13
13
|
const params = [];
|
|
14
|
+
const opMap = {
|
|
15
|
+
$gt: ">",
|
|
16
|
+
$gte: ">=",
|
|
17
|
+
$lt: "<",
|
|
18
|
+
$lte: "<=",
|
|
19
|
+
$ne: "!=",
|
|
20
|
+
$like: "LIKE",
|
|
21
|
+
};
|
|
14
22
|
|
|
15
23
|
for (const [col, val] of Object.entries(where)) {
|
|
16
24
|
if (!/^[a-zA-Z0-9_]+$/.test(col)) {
|
|
17
25
|
throw new Error(`[MODEL ERROR] Invalid column identifier '${col}'`);
|
|
18
26
|
}
|
|
19
|
-
|
|
27
|
+
|
|
28
|
+
if (Array.isArray(val)) {
|
|
29
|
+
if (val.length === 0) {
|
|
30
|
+
whereClauses.push("1 = 0");
|
|
31
|
+
} else {
|
|
32
|
+
whereClauses.push(`${col} IN (${val.map(() => "?").join(", ")})`);
|
|
33
|
+
params.push(...val);
|
|
34
|
+
}
|
|
35
|
+
} else if (val === null) {
|
|
20
36
|
whereClauses.push(`${col} IS NULL`);
|
|
37
|
+
} else if (typeof val === "object" && val !== null) {
|
|
38
|
+
for (const [op, opVal] of Object.entries(val)) {
|
|
39
|
+
if (op === "$in") {
|
|
40
|
+
if (Array.isArray(opVal) && opVal.length > 0) {
|
|
41
|
+
whereClauses.push(`${col} IN (${opVal.map(() => "?").join(", ")})`);
|
|
42
|
+
params.push(...opVal);
|
|
43
|
+
} else {
|
|
44
|
+
whereClauses.push("1 = 0");
|
|
45
|
+
}
|
|
46
|
+
} else if (op === "$nin") {
|
|
47
|
+
if (Array.isArray(opVal) && opVal.length > 0) {
|
|
48
|
+
whereClauses.push(`${col} NOT IN (${opVal.map(() => "?").join(", ")})`);
|
|
49
|
+
params.push(...opVal);
|
|
50
|
+
} else {
|
|
51
|
+
whereClauses.push("1 = 1");
|
|
52
|
+
}
|
|
53
|
+
} else if (op === "$notNull" && opVal) {
|
|
54
|
+
whereClauses.push(`${col} IS NOT NULL`);
|
|
55
|
+
} else if (op === "$null" && opVal) {
|
|
56
|
+
whereClauses.push(`${col} IS NULL`);
|
|
57
|
+
} else if (opMap[op]) {
|
|
58
|
+
whereClauses.push(`${col} ${opMap[op]} ?`);
|
|
59
|
+
params.push(opVal);
|
|
60
|
+
} else {
|
|
61
|
+
whereClauses.push(`${col} = ?`);
|
|
62
|
+
params.push(opVal);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
21
65
|
} else {
|
|
22
66
|
whereClauses.push(`${col} = ?`);
|
|
23
67
|
params.push(val);
|
|
@@ -73,6 +117,19 @@ class Model {
|
|
|
73
117
|
/** @type {Object<string, Object>} */
|
|
74
118
|
static schema = {};
|
|
75
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Optional list of columns allowed in create() and update() mass assignment.
|
|
122
|
+
* If empty, all non-guarded schema columns are permitted.
|
|
123
|
+
* @type {string[]}
|
|
124
|
+
*/
|
|
125
|
+
static fillable = [];
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Columns protected against mass assignment when `guard: true` is passed or `guarded` is configured.
|
|
129
|
+
* @type {string[]}
|
|
130
|
+
*/
|
|
131
|
+
static guarded = [];
|
|
132
|
+
|
|
76
133
|
/** @type {Database|null} */
|
|
77
134
|
static _dbInstance = null;
|
|
78
135
|
|
|
@@ -295,31 +352,57 @@ class Model {
|
|
|
295
352
|
|
|
296
353
|
const results = rows.map((r) => this.filterHidden(r, options.withHidden));
|
|
297
354
|
|
|
298
|
-
if (
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
355
|
+
if (results.length > 0) {
|
|
356
|
+
if (options.oneMany) {
|
|
357
|
+
const targets = Array.isArray(options.oneMany) ? options.oneMany : [options.oneMany];
|
|
358
|
+
const primaryKeys = results.map((r) => r[this.primaryKey]).filter((id) => id !== undefined && id !== null);
|
|
359
|
+
|
|
360
|
+
for (const target of targets) {
|
|
361
|
+
const fk = this.resolveForeignKey(target);
|
|
362
|
+
const relatedList = primaryKeys.length > 0
|
|
363
|
+
? await target.all({
|
|
364
|
+
where: { [fk]: primaryKeys },
|
|
365
|
+
withDeleted: options.withDeleted,
|
|
366
|
+
withHidden: options.withHidden,
|
|
367
|
+
})
|
|
368
|
+
: [];
|
|
369
|
+
|
|
370
|
+
const grouped = new Map();
|
|
371
|
+
for (const rel of relatedList) {
|
|
372
|
+
const parentId = rel[fk];
|
|
373
|
+
if (!grouped.has(parentId)) grouped.set(parentId, []);
|
|
374
|
+
grouped.get(parentId).push(rel);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
for (const item of results) {
|
|
378
|
+
item[target.table] = grouped.get(item[this.primaryKey]) || [];
|
|
309
379
|
}
|
|
310
380
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (options.belongsTo) {
|
|
384
|
+
const parents = Array.isArray(options.belongsTo) ? options.belongsTo : [options.belongsTo];
|
|
385
|
+
|
|
386
|
+
for (const parent of parents) {
|
|
387
|
+
const fk = parent.resolveForeignKey(this);
|
|
388
|
+
const parentIds = [...new Set(results.map((r) => r[fk]).filter((id) => id !== undefined && id !== null))];
|
|
389
|
+
|
|
390
|
+
const parentList = parentIds.length > 0
|
|
391
|
+
? await parent.all({
|
|
392
|
+
where: { [parent.primaryKey]: parentIds },
|
|
319
393
|
withDeleted: options.withDeleted,
|
|
320
394
|
withHidden: options.withHidden,
|
|
321
|
-
})
|
|
322
|
-
|
|
395
|
+
})
|
|
396
|
+
: [];
|
|
397
|
+
|
|
398
|
+
const parentMap = new Map();
|
|
399
|
+
for (const p of parentList) {
|
|
400
|
+
parentMap.set(p[parent.primaryKey], p);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const singularParent = parent.table.endsWith("s") ? parent.table.slice(0, -1) : parent.table;
|
|
404
|
+
for (const item of results) {
|
|
405
|
+
item[singularParent] = parentMap.get(item[fk]) || null;
|
|
323
406
|
}
|
|
324
407
|
}
|
|
325
408
|
}
|
|
@@ -360,21 +443,36 @@ class Model {
|
|
|
360
443
|
/**
|
|
361
444
|
* Inserts a new record, ignoring undeclared fields to prevent mass-assignment.
|
|
362
445
|
* @param {Object} data
|
|
446
|
+
* @param {Object} [options={}]
|
|
447
|
+
* @param {string[]} [options.only] - Explicit whitelist of columns allowed for this operation.
|
|
448
|
+
* @param {string[]} [options.except] - Explicit blacklist of columns excluded from this operation.
|
|
363
449
|
* @returns {Promise<number|string>} Inserted record identifier.
|
|
364
450
|
*/
|
|
365
|
-
static async create(data = {}) {
|
|
451
|
+
static async create(data = {}, options = {}) {
|
|
366
452
|
const payload = {};
|
|
367
453
|
const cols = [];
|
|
368
454
|
const placeholders = [];
|
|
369
455
|
const params = [];
|
|
456
|
+
const safeData = data && typeof data === "object" ? data : {};
|
|
457
|
+
const onlySet = Array.isArray(options.only) ? new Set(options.only) : null;
|
|
458
|
+
const exceptSet = Array.isArray(options.except) ? new Set(options.except) : null;
|
|
459
|
+
const fillableSet = Array.isArray(this.fillable) && this.fillable.length > 0 ? new Set(this.fillable) : null;
|
|
460
|
+
const guardedSet = Array.isArray(this.guarded) && this.guarded.length > 0 ? new Set(this.guarded) : null;
|
|
370
461
|
|
|
371
462
|
const now = new Date().toISOString();
|
|
372
463
|
|
|
373
464
|
for (const [col, def] of Object.entries(this.schema)) {
|
|
374
465
|
if (col === this.primaryKey && def.auto) continue;
|
|
375
466
|
|
|
376
|
-
|
|
377
|
-
|
|
467
|
+
const isAllowedByFilter =
|
|
468
|
+
(!onlySet || onlySet.has(col)) &&
|
|
469
|
+
(!exceptSet || !exceptSet.has(col)) &&
|
|
470
|
+
(!fillableSet || fillableSet.has(col)) &&
|
|
471
|
+
(!guardedSet || !guardedSet.has(col)) &&
|
|
472
|
+
!def.guarded;
|
|
473
|
+
|
|
474
|
+
if (isAllowedByFilter && Object.prototype.hasOwnProperty.call(safeData, col)) {
|
|
475
|
+
let val = safeData[col];
|
|
378
476
|
if (def.hash && typeof val === "string" && val.length > 0) {
|
|
379
477
|
const isAlreadyHashed = val.startsWith("$scrypt$") || /^\$2[aby]\$\d{2}\$/.test(val);
|
|
380
478
|
if (!isAlreadyHashed) {
|
|
@@ -415,20 +513,36 @@ class Model {
|
|
|
415
513
|
* Updates an existing record by identifier or conditions.
|
|
416
514
|
* @param {number|string|Object} idOrConditions
|
|
417
515
|
* @param {Object} data
|
|
516
|
+
* @param {Object} [options={}]
|
|
517
|
+
* @param {string[]} [options.only] - Explicit whitelist of columns allowed for this update.
|
|
518
|
+
* @param {string[]} [options.except] - Explicit blacklist of columns excluded from this update.
|
|
418
519
|
* @returns {Promise<number>} Affected rows count.
|
|
419
520
|
*/
|
|
420
|
-
static async update(idOrConditions, data = {}) {
|
|
521
|
+
static async update(idOrConditions, data = {}, options = {}) {
|
|
421
522
|
const where = typeof idOrConditions === "object" && idOrConditions !== null
|
|
422
523
|
? { ...idOrConditions }
|
|
423
524
|
: { [this.primaryKey]: idOrConditions };
|
|
424
525
|
|
|
425
526
|
const sets = [];
|
|
426
527
|
const params = [];
|
|
528
|
+
const safeData = data && typeof data === "object" ? data : {};
|
|
529
|
+
const onlySet = Array.isArray(options.only) ? new Set(options.only) : null;
|
|
530
|
+
const exceptSet = Array.isArray(options.except) ? new Set(options.except) : null;
|
|
531
|
+
const fillableSet = Array.isArray(this.fillable) && this.fillable.length > 0 ? new Set(this.fillable) : null;
|
|
532
|
+
const guardedSet = Array.isArray(this.guarded) && this.guarded.length > 0 ? new Set(this.guarded) : null;
|
|
427
533
|
|
|
428
534
|
for (const [col, def] of Object.entries(this.schema)) {
|
|
429
535
|
if (col === this.primaryKey) continue;
|
|
430
|
-
|
|
431
|
-
|
|
536
|
+
|
|
537
|
+
const isAllowedByFilter =
|
|
538
|
+
(!onlySet || onlySet.has(col)) &&
|
|
539
|
+
(!exceptSet || !exceptSet.has(col)) &&
|
|
540
|
+
(!fillableSet || fillableSet.has(col)) &&
|
|
541
|
+
(!guardedSet || !guardedSet.has(col)) &&
|
|
542
|
+
!def.guarded;
|
|
543
|
+
|
|
544
|
+
if (isAllowedByFilter && Object.prototype.hasOwnProperty.call(safeData, col)) {
|
|
545
|
+
let val = safeData[col];
|
|
432
546
|
if (def.hash && typeof val === "string" && val.length > 0) {
|
|
433
547
|
const isAlreadyHashed = val.startsWith("$scrypt$") || /^\$2[aby]\$\d{2}\$/.test(val);
|
|
434
548
|
if (!isAlreadyHashed) {
|
|
@@ -543,6 +657,117 @@ class Model {
|
|
|
543
657
|
return new Validator(rules, lang);
|
|
544
658
|
}
|
|
545
659
|
|
|
660
|
+
/**
|
|
661
|
+
* Retrieves the first record matching the given criteria.
|
|
662
|
+
* Semantic alias for Model.find(where, options).
|
|
663
|
+
* @param {Object|number|string} [where={}]
|
|
664
|
+
* @param {Object} [options={}]
|
|
665
|
+
* @returns {Promise<Object|null>}
|
|
666
|
+
*/
|
|
667
|
+
static async first(where = {}, options = {}) {
|
|
668
|
+
return this.find(where, options);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Returns the count of records matching the given criteria.
|
|
673
|
+
* @param {Object} [where={}]
|
|
674
|
+
* @returns {Promise<number>}
|
|
675
|
+
*/
|
|
676
|
+
static async count(where = {}) {
|
|
677
|
+
const conds = { ...where };
|
|
678
|
+
if (this.softDelete && !("deleted_at" in conds)) {
|
|
679
|
+
conds.deleted_at = null;
|
|
680
|
+
}
|
|
681
|
+
const { params, whereSql } = buildWhere(conds);
|
|
682
|
+
const sql = `SELECT COUNT(*) as total FROM ${this.table} ${whereSql}`.trim();
|
|
683
|
+
const row = await this.getDatabase().query(sql, params, { return_row: true });
|
|
684
|
+
return Number(row?.total || row?.count || 0);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Checks whether at least one record exists matching the given criteria.
|
|
689
|
+
* @param {Object} [where={}]
|
|
690
|
+
* @returns {Promise<boolean>}
|
|
691
|
+
*/
|
|
692
|
+
static async exists(where = {}) {
|
|
693
|
+
const total = await this.count(where);
|
|
694
|
+
return total > 0;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Finds the first record matching criteria or creates it if not found.
|
|
699
|
+
* @param {Object} where - Search conditions.
|
|
700
|
+
* @param {Object} [defaults={}] - Additional default values for creation.
|
|
701
|
+
* @returns {Promise<Object>}
|
|
702
|
+
*/
|
|
703
|
+
static async firstOrCreate(where, defaults = {}) {
|
|
704
|
+
const existing = await this.find(where);
|
|
705
|
+
if (existing) return existing;
|
|
706
|
+
const newId = await this.create({ ...where, ...defaults });
|
|
707
|
+
return this.find(newId);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Updates an existing record matching criteria or creates it if not found.
|
|
712
|
+
* @param {Object} where - Search conditions.
|
|
713
|
+
* @param {Object} [values={}] - Attributes to update or create.
|
|
714
|
+
* @returns {Promise<Object>}
|
|
715
|
+
*/
|
|
716
|
+
static async updateOrCreate(where, values = {}) {
|
|
717
|
+
const existing = await this.find(where);
|
|
718
|
+
if (existing) {
|
|
719
|
+
const pk = existing[this.primaryKey];
|
|
720
|
+
await this.update(pk, values);
|
|
721
|
+
return this.find(pk);
|
|
722
|
+
}
|
|
723
|
+
const newId = await this.create({ ...where, ...values });
|
|
724
|
+
return this.find(newId);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Converts a schema column definition to its SQL column specification string.
|
|
729
|
+
* @param {string} col - Column name.
|
|
730
|
+
* @param {Object} def - Column definition.
|
|
731
|
+
* @param {"sqlite"|"mysql"|"postgres"} [dialect="sqlite"]
|
|
732
|
+
* @returns {string}
|
|
733
|
+
*/
|
|
734
|
+
static columnToSQL(col, def, dialect = "sqlite") {
|
|
735
|
+
const type = (def.type || "string").toLowerCase();
|
|
736
|
+
|
|
737
|
+
if (def.primary) {
|
|
738
|
+
if (dialect === "postgres") {
|
|
739
|
+
return `${col} SERIAL PRIMARY KEY`;
|
|
740
|
+
}
|
|
741
|
+
if (dialect === "mysql") {
|
|
742
|
+
return `${col} INT AUTO_INCREMENT PRIMARY KEY`;
|
|
743
|
+
}
|
|
744
|
+
return `${col} INTEGER PRIMARY KEY AUTOINCREMENT`;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
let typeStr = "VARCHAR(255)";
|
|
748
|
+
if (type === "int" || type === "integer") typeStr = dialect === "postgres" ? "INTEGER" : "INT";
|
|
749
|
+
if (type === "bigint") typeStr = "BIGINT";
|
|
750
|
+
if (type === "float" || type === "double" || type === "decimal") typeStr = "DECIMAL(10,2)";
|
|
751
|
+
if (type === "text") typeStr = "TEXT";
|
|
752
|
+
if (type === "boolean" || type === "bool") typeStr = dialect === "sqlite" ? "INTEGER" : "BOOLEAN";
|
|
753
|
+
if (type === "date" || type === "datetime" || type === "timestamp") {
|
|
754
|
+
typeStr = dialect === "mysql" ? "DATETIME" : "TIMESTAMP";
|
|
755
|
+
}
|
|
756
|
+
if (type === "json") typeStr = dialect === "postgres" ? "JSONB" : (dialect === "sqlite" ? "TEXT" : "JSON");
|
|
757
|
+
if (type === "string") {
|
|
758
|
+
typeStr = `VARCHAR(${def.length || 255})`;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
let sqlCol = `${col} ${typeStr}`;
|
|
762
|
+
if (def.required && !def.nullable) sqlCol += " NOT NULL";
|
|
763
|
+
if (def.unique) sqlCol += " UNIQUE";
|
|
764
|
+
if (def.default !== undefined) {
|
|
765
|
+
const defVal = typeof def.default === "string" ? `'${def.default}'` : def.default;
|
|
766
|
+
sqlCol += ` DEFAULT ${defVal}`;
|
|
767
|
+
}
|
|
768
|
+
return sqlCol;
|
|
769
|
+
}
|
|
770
|
+
|
|
546
771
|
/**
|
|
547
772
|
* Generates a SQL CREATE TABLE DDL string tailored to the target dialect.
|
|
548
773
|
* @param {"sqlite"|"mysql"|"postgres"} [dialect="sqlite"]
|
|
@@ -550,44 +775,28 @@ class Model {
|
|
|
550
775
|
*/
|
|
551
776
|
static toDDL(dialect = "sqlite") {
|
|
552
777
|
const columns = [];
|
|
778
|
+
const foreignKeys = [];
|
|
553
779
|
|
|
554
780
|
for (const [col, def] of Object.entries(this.schema)) {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
let typeStr = "VARCHAR(255)";
|
|
568
|
-
if (type === "int" || type === "integer") typeStr = dialect === "postgres" ? "INTEGER" : "INT";
|
|
569
|
-
if (type === "bigint") typeStr = "BIGINT";
|
|
570
|
-
if (type === "float" || type === "double" || type === "decimal") typeStr = "DECIMAL(10,2)";
|
|
571
|
-
if (type === "text") typeStr = "TEXT";
|
|
572
|
-
if (type === "boolean" || type === "bool") typeStr = dialect === "sqlite" ? "INTEGER" : "BOOLEAN";
|
|
573
|
-
if (type === "date" || type === "datetime" || type === "timestamp") {
|
|
574
|
-
typeStr = dialect === "mysql" ? "DATETIME" : "TIMESTAMP";
|
|
575
|
-
}
|
|
576
|
-
if (type === "json") typeStr = dialect === "postgres" ? "JSONB" : (dialect === "sqlite" ? "TEXT" : "JSON");
|
|
577
|
-
if (type === "string") {
|
|
578
|
-
typeStr = `VARCHAR(${def.length || 255})`;
|
|
781
|
+
columns.push(` ${this.columnToSQL(col, def, dialect)}`);
|
|
782
|
+
|
|
783
|
+
if (def.references) {
|
|
784
|
+
let refTable = "";
|
|
785
|
+
let refCol = "id";
|
|
786
|
+
if (typeof def.references === "string") {
|
|
787
|
+
const parts = def.references.split(".");
|
|
788
|
+
refTable = parts[0];
|
|
789
|
+
if (parts[1]) refCol = parts[1];
|
|
790
|
+
} else if (def.references && def.references.table) {
|
|
791
|
+
refTable = def.references.table;
|
|
792
|
+
if (def.references.column) refCol = def.references.column;
|
|
579
793
|
}
|
|
580
794
|
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
if (def.default !== undefined) {
|
|
585
|
-
const defVal = typeof def.default === "string" ? `'${def.default}'` : def.default;
|
|
586
|
-
sqlCol += ` DEFAULT ${defVal}`;
|
|
795
|
+
if (refTable) {
|
|
796
|
+
const onAction = def.onDelete ? def.onDelete.toUpperCase() : (def.nullable ? "SET NULL" : "CASCADE");
|
|
797
|
+
foreignKeys.push(` FOREIGN KEY (${col}) REFERENCES ${refTable}(${refCol}) ON DELETE ${onAction}`);
|
|
587
798
|
}
|
|
588
799
|
}
|
|
589
|
-
|
|
590
|
-
columns.push(` ${sqlCol}`);
|
|
591
800
|
}
|
|
592
801
|
|
|
593
802
|
if (this.timestamps) {
|
|
@@ -603,7 +812,8 @@ class Model {
|
|
|
603
812
|
columns.push(dialect === "mysql" ? " deleted_at DATETIME NULL" : " deleted_at TIMESTAMP NULL");
|
|
604
813
|
}
|
|
605
814
|
|
|
606
|
-
|
|
815
|
+
const allLines = columns.concat(foreignKeys);
|
|
816
|
+
return `CREATE TABLE IF NOT EXISTS ${this.table} (\n${allLines.join(",\n")}\n);`;
|
|
607
817
|
}
|
|
608
818
|
}
|
|
609
819
|
|