masterrecord 0.0.46 → 0.0.48
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/Entity/entityModel.js +18 -7
- package/Entity/entityTrackerModel.js +3 -3
- package/Migrations/cli.js +116 -11
- package/Migrations/migrationMySQLQuery.js +299 -0
- package/Migrations/migrationTemplate.js +7 -7
- package/Migrations/migrations.js +51 -8
- package/Migrations/schema.js +93 -38
- package/QueryLanguage/queryMethods.js +25 -0
- package/context.js +32 -24
- package/insertManager.js +22 -15
- package/mySQLEngine.js +124 -41
- package/mySQLSyncConnect.js +39 -0
- package/package.json +3 -3
- package/readme.md +1 -1
package/mySQLEngine.js
CHANGED
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
var tools = require('masterrecord/Tools');
|
|
2
|
+
var util = require('util');
|
|
2
3
|
|
|
3
4
|
class SQLLiteEngine {
|
|
4
5
|
|
|
5
6
|
unsupportedWords = ["order"]
|
|
6
7
|
|
|
7
8
|
update(query){
|
|
8
|
-
var sqlQuery = ` UPDATE
|
|
9
|
-
SET ${query.arg}
|
|
10
|
-
WHERE [${query.tableName}].[${query.primaryKey}] = ${query.primaryKeyValue}` // primary key for that table =
|
|
9
|
+
var sqlQuery = ` UPDATE ${query.tableName} SET ${query.arg} WHERE ${query.tableName}.${query.primaryKey} = ${query.primaryKeyValue}` // primary key for that table =
|
|
11
10
|
return this._run(sqlQuery);
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
delete(queryObject){
|
|
15
14
|
var sqlObject = this._buildDeleteObject(queryObject);
|
|
16
|
-
var sqlQuery = `DELETE FROM
|
|
17
|
-
return this.
|
|
15
|
+
var sqlQuery = `DELETE FROM ${sqlObject.tableName} WHERE ${sqlObject.tableName}.${sqlObject.primaryKey} = ${sqlObject.value}`;
|
|
16
|
+
return this._run(sqlQuery);
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
insert(queryObject){
|
|
21
20
|
var sqlObject = this._buildSQLInsertObject(queryObject, queryObject.__entity);
|
|
22
|
-
var query = `INSERT INTO
|
|
23
|
-
VALUES (${sqlObject.values})`;
|
|
21
|
+
var query = `INSERT INTO ${sqlObject.tableName} (${sqlObject.columns}) VALUES (${sqlObject.values})`;
|
|
24
22
|
var queryObj = this._run(query);
|
|
23
|
+
// return
|
|
25
24
|
var open = {
|
|
26
|
-
"id": queryObj.
|
|
25
|
+
"id": queryObj.insertId
|
|
27
26
|
};
|
|
28
27
|
return open;
|
|
29
28
|
}
|
|
@@ -39,8 +38,10 @@ class SQLLiteEngine {
|
|
|
39
38
|
}
|
|
40
39
|
if(queryString.query){
|
|
41
40
|
console.log("SQL:", queryString.query);
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
this.db.connect(this.db);
|
|
42
|
+
const result = this.db.query(queryString.query);
|
|
43
|
+
console.log("results:", result);
|
|
44
|
+
return result;
|
|
44
45
|
}
|
|
45
46
|
return null;
|
|
46
47
|
} catch (err) {
|
|
@@ -73,18 +74,20 @@ class SQLLiteEngine {
|
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
all(query, entity, context){
|
|
76
|
-
var
|
|
77
|
+
var queryString = {};
|
|
77
78
|
try {
|
|
78
79
|
if(query.raw){
|
|
79
|
-
|
|
80
|
+
queryString.query = query.raw;
|
|
80
81
|
}
|
|
81
82
|
else{
|
|
82
|
-
|
|
83
|
+
queryString = this.buildQuery(query, entity, context);
|
|
83
84
|
}
|
|
84
|
-
if(
|
|
85
|
-
console.log("SQL:",
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
if(queryString.query){
|
|
86
|
+
console.log("SQL:", queryString.query);
|
|
87
|
+
this.db.connect(this.db);
|
|
88
|
+
const result = this.db.query(queryString.query);
|
|
89
|
+
console.log("results:", result);
|
|
90
|
+
return result;
|
|
88
91
|
}
|
|
89
92
|
return null;
|
|
90
93
|
} catch (err) {
|
|
@@ -97,17 +100,23 @@ class SQLLiteEngine {
|
|
|
97
100
|
buildQuery(query, entity, context){
|
|
98
101
|
|
|
99
102
|
var queryObject = {};
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
103
|
+
if(entity){
|
|
104
|
+
queryObject.entity = this.getEntity(entity.__name, query.entityMap);
|
|
105
|
+
queryObject.select = this.buildSelect(query, entity);
|
|
106
|
+
queryObject.from = this.buildFrom(query, entity);
|
|
107
|
+
queryObject.include = this.buildInclude(query, entity, context, queryObject);
|
|
108
|
+
queryObject.where = this.buildWhere(query, entity);
|
|
109
|
+
|
|
110
|
+
var queryString = `${queryObject.select} ${queryObject.from} ${queryObject.include} ${queryObject.where}`;
|
|
111
|
+
return {
|
|
112
|
+
query : queryString,
|
|
113
|
+
entity : this.getEntity(entity.__name, query.entityMap)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else{
|
|
117
|
+
console.log("Error: Entity object is blank");
|
|
110
118
|
}
|
|
119
|
+
|
|
111
120
|
|
|
112
121
|
}
|
|
113
122
|
|
|
@@ -314,20 +323,38 @@ class SQLLiteEngine {
|
|
|
314
323
|
|
|
315
324
|
for (var column in dirtyFields) {
|
|
316
325
|
// TODO Boolean value is a string with a letter
|
|
317
|
-
|
|
326
|
+
var type = model.__entity[dirtyFields[column]].type;
|
|
327
|
+
switch(type){
|
|
318
328
|
case "integer" :
|
|
319
|
-
argument = argument === null ?
|
|
329
|
+
argument = argument === null ? `${dirtyFields[column]} = ${model[dirtyFields[column]]},` : `${argument} ${dirtyFields[column]} = ${model[dirtyFields[column]]},`;
|
|
320
330
|
break;
|
|
321
331
|
case "string" :
|
|
322
|
-
argument = argument === null ?
|
|
332
|
+
argument = argument === null ? `${dirtyFields[column]} = '${$that._santizeSingleQuotes(model[dirtyFields[column]])}',` : `${argument} ${dirtyFields[column]} = '${$that._santizeSingleQuotes(model[dirtyFields[column]])}',`;
|
|
333
|
+
break;
|
|
334
|
+
case "boolean" :
|
|
335
|
+
argument = argument === null ? `${dirtyFields[column]} = '${this.boolType(model[dirtyFields[column]])}',` : `${argument} ${dirtyFields[column]} = '${this.boolType(model[dirtyFields[column]])}',`;
|
|
323
336
|
break;
|
|
324
337
|
default:
|
|
325
|
-
argument = argument === null ?
|
|
338
|
+
argument = argument === null ? `${dirtyFields[column]} = '${model[dirtyFields[column]]}',` : `${argument} ${dirtyFields[column]} = '${model[dirtyFields[column]]}',`;
|
|
326
339
|
}
|
|
327
340
|
}
|
|
328
341
|
return argument.replace(/,\s*$/, "");
|
|
329
342
|
}
|
|
330
343
|
|
|
344
|
+
boolType(type){
|
|
345
|
+
var jj = String(type);
|
|
346
|
+
switch(jj) {
|
|
347
|
+
case "true":
|
|
348
|
+
return 1
|
|
349
|
+
break;
|
|
350
|
+
case "false":
|
|
351
|
+
return 0
|
|
352
|
+
break;
|
|
353
|
+
default:
|
|
354
|
+
return type;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
331
358
|
|
|
332
359
|
_buildDeleteObject(currentModel){
|
|
333
360
|
var primaryKey = currentModel.__Key === undefined ? tools.getPrimaryKeyObject(currentModel.__entity) : currentModel.__Key;
|
|
@@ -349,12 +376,11 @@ class SQLLiteEngine {
|
|
|
349
376
|
var fieldColumn = "";
|
|
350
377
|
// check if get function is avaliable if so use that
|
|
351
378
|
fieldColumn = fields[column];
|
|
379
|
+
|
|
380
|
+
|
|
352
381
|
|
|
353
382
|
if((fieldColumn !== undefined && fieldColumn !== null && fieldColumn !== "" ) && typeof(fieldColumn) !== "object"){
|
|
354
383
|
switch(modelEntity[column].type){
|
|
355
|
-
case "belongsTo" :
|
|
356
|
-
column = modelEntity[column].relationshipTable === undefined ? column : modelEntity[column].relationshipTable;
|
|
357
|
-
break;
|
|
358
384
|
case "string" :
|
|
359
385
|
fieldColumn = `'${$that._santizeSingleQuotes(fields[column])}'`;
|
|
360
386
|
break;
|
|
@@ -362,20 +388,32 @@ class SQLLiteEngine {
|
|
|
362
388
|
fieldColumn = fields[column];
|
|
363
389
|
break;
|
|
364
390
|
}
|
|
391
|
+
|
|
392
|
+
var relationship = modelEntity[column].relationshipType
|
|
393
|
+
if(relationship === "belongsTo"){
|
|
394
|
+
column = modelEntity[column].foreignKey
|
|
395
|
+
}
|
|
396
|
+
|
|
365
397
|
|
|
366
|
-
columns = columns === null ?
|
|
398
|
+
columns = columns === null ? `${column},` : `${columns} ${column},`;
|
|
367
399
|
values = values === null ? `${fieldColumn},` : `${values} ${fieldColumn},`;
|
|
368
400
|
|
|
369
401
|
}
|
|
370
402
|
}
|
|
371
403
|
}
|
|
404
|
+
|
|
372
405
|
return {tableName: modelEntity.__name, columns: columns.replace(/,\s*$/, ""), values: values.replace(/,\s*$/, "")};
|
|
373
406
|
|
|
374
407
|
}
|
|
375
408
|
|
|
376
409
|
// will add double single quotes to allow sting to be saved.
|
|
377
410
|
_santizeSingleQuotes(string){
|
|
378
|
-
|
|
411
|
+
|
|
412
|
+
if(typeof string === "string"){
|
|
413
|
+
return string.replace(/'/g, "''");
|
|
414
|
+
}else{
|
|
415
|
+
return `${string}`;
|
|
416
|
+
}
|
|
379
417
|
}
|
|
380
418
|
|
|
381
419
|
// converts any object into SQL parameter select string
|
|
@@ -392,12 +430,23 @@ class SQLLiteEngine {
|
|
|
392
430
|
|
|
393
431
|
_execute(query){
|
|
394
432
|
console.log("SQL:", query);
|
|
395
|
-
|
|
433
|
+
this.db.connect(this.db);
|
|
434
|
+
return this.db.query(query);
|
|
396
435
|
}
|
|
397
436
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
437
|
+
_run(query){
|
|
438
|
+
try{
|
|
439
|
+
|
|
440
|
+
console.log("SQL:", query);
|
|
441
|
+
this.db.connect(this.db);
|
|
442
|
+
const result = this.db.query(query);
|
|
443
|
+
|
|
444
|
+
return result;}
|
|
445
|
+
catch (error) {
|
|
446
|
+
console.error(error);
|
|
447
|
+
// Expected output: ReferenceError: nonExistentFunction is not defined
|
|
448
|
+
// (Note: the exact output may be browser-dependent)
|
|
449
|
+
}
|
|
401
450
|
}
|
|
402
451
|
|
|
403
452
|
setDB(db, type){
|
|
@@ -406,4 +455,38 @@ class SQLLiteEngine {
|
|
|
406
455
|
}
|
|
407
456
|
}
|
|
408
457
|
|
|
409
|
-
module.exports = SQLLiteEngine;
|
|
458
|
+
module.exports = SQLLiteEngine;
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
/***
|
|
464
|
+
*
|
|
465
|
+
*
|
|
466
|
+
*
|
|
467
|
+
* const mysql = require('mysql2/promise');
|
|
468
|
+
|
|
469
|
+
class MySQLClient {
|
|
470
|
+
constructor(config) {
|
|
471
|
+
this.config = config;
|
|
472
|
+
this.pool = mysql.createPool(config);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
async query(sql, params = []) {
|
|
476
|
+
const connection = await this.pool.getConnection();
|
|
477
|
+
try {
|
|
478
|
+
const [results] = await connection.execute(sql, params);
|
|
479
|
+
return results;
|
|
480
|
+
} finally {
|
|
481
|
+
connection.release();
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
async close() {
|
|
486
|
+
await this.pool.end();
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
module.exports = MySQLClient;
|
|
491
|
+
|
|
492
|
+
*/
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
var MySql = require('sync-mysql2');
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MySQLClient {
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.config = config;
|
|
7
|
+
this.connection = null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
connect() {
|
|
11
|
+
if (!this.connection) {
|
|
12
|
+
this.connection = new MySql(this.config);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
query(sql, params = []) {
|
|
17
|
+
try {
|
|
18
|
+
if (!this.connection) {
|
|
19
|
+
throw new Error('Database connection not established. Call connect() first.');
|
|
20
|
+
}
|
|
21
|
+
var jj = this.connection.query(sql);
|
|
22
|
+
this.connection.finishAll();
|
|
23
|
+
return jj;
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.error(err);
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
close() {
|
|
32
|
+
if (this.connection) {
|
|
33
|
+
this.connection.end();
|
|
34
|
+
this.connection = null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = MySQLClient;
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"better-sqlite3": "^9.0.0",
|
|
5
4
|
"commander": "^11.1.0",
|
|
6
5
|
"glob" : "^10.3.10",
|
|
7
6
|
"deep-object-diff" : "^1.1.9",
|
|
8
7
|
"pg" : "^8.13.3",
|
|
9
|
-
"
|
|
8
|
+
"sync-mysql2" : "^1.0.4",
|
|
9
|
+
"app-root-path": "^3.1.0"
|
|
10
10
|
},
|
|
11
|
-
"version": "0.0.
|
|
11
|
+
"version": "0.0.48",
|
|
12
12
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
13
13
|
"homepage": "https://github.com/Tailor/MasterRecord#readme",
|
|
14
14
|
"repository": {
|
package/readme.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
local install of CLI package run code in terminal - "npm install -g ./"
|
|
1
|
+
local install of CLI package run code in terminal - "npm install -g ./" master
|