-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy.model.js
617 lines (538 loc) · 20.2 KB
/
lazy.model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
'use strict'
/** @const {Object} LazyDB requiring Database library */
const LazyDB = require('./lazy.db');
const {LazyTypes} = require('./lazy.utils');
const MYSQL_ON_DELETE_CASCADE_CLAUSES = [
"CASCADE", "SET NULL", "NO ACTION", "RESTRICT"
]
const MYSQL_DATA_TYPES = [
"CHAR", "VARCHAR", "BINARY", "VARBINARY",
"TINYBLOB", "TINYTEXT", "TEXT", "BLOB",
"MEDIUMTEXT", "MEDIUMBLOB", "LONGTEXT",
"LONGBLOB", "ENUM", "SET", "BIT", "TINYINT",
"BOOL", "BOOLEAN", "SMALLINT", "MEDIUMINT",
"INT", "INTEGER", "BIGINT", "FLOAT", "DOUBLE",
"DOUBLE PRECISION", "DECIMAL", "DEC", "DATE",
"DATETIME", "TIMESTAMP", "TIME", "YEAR"
]
const MYSQL_SIZED_TYPES = [
"CHAR", "VARCHAR", "BINARY", "VARBINARY",
"TEXT", "BLOB", "BIT", "TINYINT","SMALLINT",
"MEDIUMINT","INT", "INTEGER", "BIGINT", "FLOAT",
"DOUBLE", "DOUBLE PRECISION", "DECIMAL", "DEC",
"TIMESTAMP"
]
const MYSQL_DEFAULT_REQUIRES_BRACKETS = [
"CHAR", "VARCHAR", "TINYTEXT", "TEXT",
"MEDIUMTEXT", "LONGTEXT"
]
/**
* @todo Check selector and data for wrong kay/values
*/
class LazyForeignKey{
constructor(column, onDelete, onUpdate, join, on, alias, occurence){
this.occurence = occurence;
this.join = join;
this.on = on;
this.alias = alias;
this.column = column;
this.onDelete = onDelete;
this.onUpdate = onUpdate
}
set occurence(occurence){
if(!LazyTypes.isNumber(occurence)){
throw new Error(`FK "occurence" value should be a number. got ${LazyTypes.getFullType(occurence)}.`);
}
this._occurence = occurence;
}
set column(column){
if(!LazyTypes.isString(column)){
throw new Error(`FK "column" should be a string. got ${LazyTypes.getFullType(column)}.`);
}
this._column = column;
}
set onDelete(onDelete){
if(!LazyTypes.isString(onDelete)){
throw new Error(`FK "onDelete" should be a string. got ${LazyTypes.getFullType(onDelete)}.`);
}else if(!MYSQL_ON_DELETE_CASCADE_CLAUSES.includes(onDelete)){
throw new Error(`Invalid onDelete type "${onDelete}". Valid options: ${MYSQL_ON_DELETE_CASCADE_CLAUSES.join(',')}.`);
}
this._onDelete = onDelete;
}
set onUpdate(onUpdate){
if(!LazyTypes.isString(onUpdate)){
throw new Error(`FK "onUpdate" should be a string. got ${LazyTypes.getFullType(onUpdate)}.`);
}else if(!MYSQL_ON_DELETE_CASCADE_CLAUSES.includes(onUpdate)){
throw new Error(`Invalid onDelete type "${onUpdate}". Valid options: ${MYSQL_ON_DELETE_CASCADE_CLAUSES.join(',')}.`);
}
this._onUpdate = onUpdate;
}
set join(join){
if(!LazyTypes.isObject(join)){
throw new Error(`FK "join" should be an object. got ${LazyTypes.getFullType(join)}.`);
}
this._join = join;
}
set on(on){
if(!LazyTypes.isString(on)){
throw new Error(`FK "foreignColumn" should be a string. got ${LazyTypes.getFullType(on)}.`);
}
this._on = on;
}
set alias(alias){
if(!LazyTypes.isString(alias) && alias){
throw new Error(`FK "alias" should be a string. got ${LazyTypes.getFullType(alias)}.`);
}
this._alias = alias;
}
/**
* Column name
* @type {String}
*/
get occurence(){
return this._occurence;
}
/**
* Column name
* @type {String}
*/
get name(){
return this._name;
}
/**
* FK parent column
* @type {String}
*/
get column(){
return this._column;
}
/**
* FK on delete clause
* @type {String}
*/
get onDelete(){
return this._onDelete;
}
/**
* FK on delete clause
* @type {String}
*/
get onUpdate(){
return this._onUpdate;
}
/**
* FK parent
* @type {Object}
*/
get join(){
return this._join
}
/**
* FK parent column
* @type {String}
*/
get on(){
return this._on;
}
/**
* FK alias
* @type {String}
*/
get alias(){
return this._alias;
}
get joinTableAlias(){
return `${this._join.table}${this._occurence}`
}
}
class LazyColumn{
constructor(name, type, size, deflt, pk, fk){
this.name = name;
this.type = type;
this.size = size;
this.default = deflt;
this.pk = pk;
this.fk = fk;
}
set name(name){
if(!LazyTypes.isString(name)){
throw new Error(`Column "name" should be a string. got ${LazyTypes.getFullType(name)}.`);
}
this._name = name;
}
set type(type){
if(!LazyTypes.isString(type)){
throw new Error(`Column "type" should be a string. got ${LazyTypes.getFullType(type)}.`);
}else if(!MYSQL_DATA_TYPES.includes(type)){
throw new Error(`Invalid column type ${type}. Valid options: ${MYSQL_DATA_TYPES.join(',')}.`);
}
this._type = type;
}
set size(size){
if(MYSQL_SIZED_TYPES.includes(this.type) && !size){
throw new Error(`Column type ${this.type} requires a size parameter.`);
}else if(MYSQL_SIZED_TYPES.includes(this.type) && !LazyTypes.isNumber(size)){
throw new Error(`Column "size" should be a number. got ${LazyTypes.getFullType(size)}.`);
}
this._size = size;
}
set default(deflt){
if(!LazyTypes.isString(deflt) && deflt){
throw new Error(`Column "default" should be a string. got ${LazyTypes.getFullType(deflt)}.`);
}
switch(this.type){
case "TIMESTAMP":
this._default = `${deflt}(${this.size})`;
break;
default:
this._default = deflt;
}
}
set pk(pk = false){
if(!LazyTypes.isBoolean(pk)){
throw new Error(`Column "pk" should be a boolean. got ${LazyTypes.getFullType(pk)}.`);
}
this._pk = pk;
}
set fk(fk){
if(fk && !LazyTypes.isObject(fk)){
throw new Error(`Column "fk" should be an object. got ${LazyTypes.getFullType(fk)}.`);
}else if(fk && !(fk instanceof LazyForeignKey)){
throw new Error(`COlumn foreign key not an instance of LazyForeignKey.`);
}
this._fk = fk;
}
/**
* Column name
* @type {String}
*/
get name(){
return this._name;
}
/**
* Column type
* @type {String}
*/
get type(){
return this._type;
}
/**
* Column size
* @type {Number}
*/
get size(){
return this._size;
}
/**
* Column default value
* @type {String}
*/
get default(){
return this._default
}
/**
* Is column primary key ?
* @type {Boolean}
*/
get pk(){
return this._pk;
}
/**
* Column's foreign key
* @type {Object}
*/
get fk(){
return this._fk;
}
/**
* Get SQL-formated column datatype
* @type {String}
*/
get dataTypeString(){
return `${this.type}${MYSQL_SIZED_TYPES.includes(this.type) ? `(${this.size})` : ''}`
}
/**
* Get SQL-formated default column value query string slice
* @type {String}
*/
get defaultValueString(){
return this.default === "NULL" ? "NULL DEFAULT NULL" :
`NOT NULL ${this.default === undefined ? '' : `DEFAULT ${MYSQL_DEFAULT_REQUIRES_BRACKETS.includes(this.type) ? "'" + this.default + "'" : this.default}` }`
}
/**
* Column creation SQL query string slice
* @type {String}
*/
get createQuery(){
return `${this.name} ${this.dataTypeString} ${this.defaultValueString}`;
}
}
module.exports = class LazyModel{
constructor(model){
if(model){
this.table = model.table;
this.schema = model.schema;
this.virtual = false;
}else{
this.virtual = true;
}
}
set schema(schema){
if(!LazyTypes.isObject(schema)){
throw new Error(`Schema should be an object. got ${LazyTypes.getFullType(schema)}.`);
}
this._schema = schema;
this._columns = Object.keys(this.schema).map(field => {
this._fk = this._fk ? this._fk : [];
let data = this.schema[field];
let fk = data.fk ? new LazyForeignKey(field, data.fk.delete, data.fk.update, data.fk.join, data.fk.on, data.fk.as, this._fk.reduce((i, fk) => fk.join.table === data.fk.join.table ? ++i : i , 0)) : undefined;
if(fk) this._fk.push(fk);
if(data.pk) this.pk = field;
return new LazyColumn(field, data.type, data.size, data.default, data.pk, fk);
})
}
set pk(pk){
if(this._pk){
throw new Error(`Schema contains multiple Primary Keys.`);
}
this._pk = pk;
}
set table(table){
if(!LazyTypes.isString(table)){
throw new Error(`Model's "table" should be a string. got ${LazyTypes.getFullType(table)}.`);
}
this._table = table;
}
set virtual(isVirtual = false){
if(!LazyTypes.isBoolean(isVirtual)){
throw new Error(`Enpoint's "virtual" should be a boolean. got ${LazyTypes.getFullType(isVirtual)}.`);
}
this._virtual = isVirtual;
}
get columns(){
return this._columns;
}
get fk(){
return this._fk || [];
}
get pk(){
return this._pk || null;
}
get table(){
return this._table;
}
get virtual(){
return this._virtual;
}
get schema(){
return this._schema;
}
async create(data){
if(!this.virtual){
let values = Object.keys(data).map(field => data[field]);
let query = `INSERT INTO ${this.table} (`;
query += Object.keys(data).map(field => field).join(",");
query += ") VALUES (";
query += Object.keys(data).map(() => "?").join(",");
query += ");";
await LazyDB.execute(query, values);
return await this.findOne(data);
}
}
async updateAll(selector, data){
return await this._update(selector, data, false)
}
async updateOne(selector, data){
return await this._update(selector, data, true)
}
async findAll(selector, order, desc = false){
return await this._find(selector, false, order, desc);
}
async findOne(selector, order, desc = false){
return await this._find(selector, true, order, desc)
}
async checkExists(selector){
return await this.findOne(selector) != undefined;
}
async deleteAll(selector){
return await this._delete(selector, false);
}
async deleteOne(selector){
return await this._delete(selector, true);
}
async _find(selector, one = false, order, desc = false){
if(!this.virtual){
let orderColumn = this.columns.find(column => column.name === order);
if(!LazyTypes.isObject(selector)){
throw new Error(`"selector" argument should be an object. got ${LazyTypes.getFullType(selector)}.`);
}else if(order && !LazyTypes.isString(order)){
throw new Error(`"order" argument should be a string. got ${LazyTypes.getFullType(selector)}.`);
}else if(order && !orderColumn){
throw new Error(`Cannot order by "${order}". column "${order}" does not exists.`)
}
let query = `SELECT `;
let columnsQueries = this.columns.map(column => `${this.table}.${column.name} AS '${this.table}*${column.name}'`);
let fkQueries = this.fk.flatMap(fk => fk.alias ? fk.join.columns.map(column => `${fk.joinTableAlias}.${column.name} AS '${fk.joinTableAlias}*${column.name}*${fk.alias}'`) : []);
let joinQueries = this.columns.flatMap(column => column.fk ? ` LEFT JOIN ${column.fk.join.table} ${column.fk.joinTableAlias} ON ${this.table}.${column.name}=${column.fk.joinTableAlias}.${column.fk.on}` : []);
query += [columnsQueries,...fkQueries].join(",");
query += ` FROM ${this.table} `
query += joinQueries.join("");
query += this._prepareWhereClause(selector);
query += orderColumn ? ` ORDER BY ${orderColumn.fk ? `${orderColumn.fk.join.table}.${orderColumn.name} ${desc ? " DESC" : " ASC"}` : `${this.table}.${orderColumn.name}`} ${desc ? " DESC" : " ASC"}` : '';
query += one ? " LIMIT 1;" : ";";
let raw = await LazyDB.execute(query, this._fetchSelectorValues(selector));
let objectified = raw.map(row => {
let data = {};
for(const encodedField in row){
let [, field, fkAlias] = encodedField.split("*");
if(fkAlias){
data[fkAlias] = data[fkAlias] || {};
data[fkAlias][field] = row[encodedField];
}else{
data[field] = row[encodedField]
}
}
return data
});
return one ? objectified[0] : objectified;
}
}
async _update(selector, data, one = false){
if(!this.virtual){
if(!LazyTypes.isObject(selector)){
throw new Error(`"selector" argument should be an object. got ${LazyTypes.getFullType(selector)}.`);
}else if(!LazyTypes.isObject(data)){
throw new Error(`"data" argument should be an object. got ${LazyTypes.getFullType(data)}.`);
}else if(!await this.checkExists(selector)){
throw {
status: 400,
hint: `no matches found for {${Object.keys(selector).map(field => `${field}: '${selector[field]}'`).join(",")}}`
}
}else{
let query = `UPDATE ${this.table} SET`;
query += Object.keys(data).map(field => {
let fk = this.fk.find(fk => field == fk.alias);
field = fk ? fk.on : field;
return ` ${field}=?`
}).join(",");
query += this._prepareWhereClause(selector);
query += one ? " LIMIT 1;" : ";"
let values = this._fetchSelectorValues(data);
values = values.concat(this._fetchSelectorValues(selector));
await LazyDB.execute(query, values);
return await this._find(Object.assign(selector, data), one);
}
}
}
async _delete(selector, one = false){
if(!this.virtual){
if(!await this.checkExists(selector)){
throw {
status: 400,
hint: `no matches found for {${Object.keys(selector).map(field => `${field}: '${selector[field]}'`).join(",")}}`
}
}else{
let query = `DELETE FROM ${this.table}`;
query += this._prepareWhereClause(selector);
query += one ? " LIMIT 1;" : ";"
return await LazyDB.execute(query, this._fetchSelectorValues(selector));
}
}
}
_fetchSelectorValues(selector){
if(!this.virtual && Object.keys(selector).length > 0){
let values = Object.keys(selector).flatMap(field => {
let fk = this.fk.find(fk => field == fk.alias);
field = fk ? fk.on : field;
return selector[field];
});
return values
}else{
return []
}
}
_prepareWhereClause(selector){
if(!this.virtual && Object.keys(selector).length > 0){
let query = " WHERE "
return query += Object.keys(selector).map(field => {
let fk = this.fk.find(fk => field == fk.alias);
let column = fk ? fk.on : field;
return `${this.table}.${column}<=>?`
}).join(" AND ");
}else{
return "";
}
}
async makeTable(){
if(!this.virtual){
let tables = await LazyDB.execute(`SHOW TABLES LIKE '${this.table}'`);
if(tables.length === 0){
await this._createTable();
}else if(tables.length === 1){
await this._updateTable();
}else{
throw new Error(`Error while migrating table ${this.table}`)
}
}
}
async makeRelations(){
if(!this.virtual){
let query = `ALTER TABLE ${this.table}`;
let existingForeignKeys = await LazyDB.execute(`SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = '${this.table}' AND TABLE_SCHEMA= '${LazyDB.db}' AND CONSTRAINT_NAME != 'PRIMARY' `)
await Promise.all(existingForeignKeys.map( async fk => {
return await LazyDB.execute(`ALTER TABLE ${this.table} DROP CONSTRAINT ${fk.CONSTRAINT_NAME}`)
}))
query += this.fk.map((fk, index) => ` ADD CONSTRAINT ${this.table}_fk_${index} FOREIGN KEY (${fk.column}) REFERENCES ${fk.join.table} (${fk.on}) ON DELETE ${fk.onDelete} ON UPDATE ${fk.onUpdate}`).join(',');
return await LazyDB.execute(query);
}
}
async _createTable(){
if(!this.virtual){
let query = `CREATE TABLE ${this.table}(`;
query += this.columns.map(column => column.createQuery).join(", ");
query += this.pk ? `, PRIMARY KEY (${this.pk})` : '';
query += `) ENGINE=${LazyDB.engine} DEFAULT CHARSET=${LazyDB.charset} COLLATE=${LazyDB.collation}`;
return await LazyDB.execute(query);
}
}
/**
* @todo TEST THIS /!\/!\
*/
async _updateTable(){
if(!this.virtual){
let existingColumns = await LazyDB.execute(`SHOW COLUMNS FROM ${this.table}`);
let query = `ALTER TABLE ${this.table}`;
let previousField = null;
query += this.columns.map(column => {
let query = existingColumns.some(props => props.Field === column.name) ?
` CHANGE ${column.name} ${column.createQuery}` :
` ADD ${column.createQuery} ${previousField ? `AFTER ${previousField}` : 'FIRST'}`
previousField = column.name;
return query
}).join(",");
return await LazyDB.execute(query);
}
}
// let existingColumns = await TinyDB.execute(`SHOW COLUMNS FROM ${this.table}`);
// // let existingForeignKeys = await TinyDB.execute(`SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = '${this.table}' AND CONSTRAINT_NAME != 'PRIMARY' `)
// let query = `ALTER TABLE ${this.table}`;
// let previousField = null;
// let columnQueries = Object.keys(this.schema).map((field, index) => {
// let column = this.schema[field]
// let query = existingColumns.some(props => props.Field === field) ?
// ` CHANGE ${field} ${field} ${column.type} ${column.default === "NULL" ? "NULL" : "NOT NULL"} ${column.default ? `DEFAULT '${column.default}'` : ""}` :
// ` ADD ${field} ${column.type} ${column.default === "NULL" ? "NULL" : "NOT NULL"} ${column.default ? `DEFAULT '${column.default}'` : ""} ${previousField ? `AFTER ${previousField}` : 'FIRST'}`;
// previousField = field;
// return query;
// });
// // await Promise.all(existingForeignKeys.map( async fk => {
// // return await TinyDB.execute(`ALTER TABLE ${this.table} DROP FOREIGN KEY ${fk.CONSTRAINT_NAME}, DROP KEY ${fk.CONSTRAINT_NAME}`)
// // }))
// // let constraintQueries = Object.keys(this.schema).flatMap((field) => {
// // let column = this.schema[field]
// // return column.fk === undefined ? [] : ` ADD CONSTRAINT ${column.fk.name} FOREIGN KEY (${field}) REFERENCES ${column.fk.model.table} (${column.fk.column}) ON DELETE ${column.fk.delete} ON UPDATE ${column.fk.update}`
// // })
// // query += [...columnQueries,...constraintQueries].join(",");
// query += [...columnQueries].join(",");
// return await TinyDB.execute(query);
}