masterrecord 0.2.7 → 0.2.9
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/Migrations/cli.js +78 -0
- package/Migrations/migrations.js +4 -1
- package/Migrations/schema.js +2 -2
- package/mySQLEngine.js +17 -2
- package/mySQLSyncConnect.js +22 -5
- package/package.json +1 -1
package/Migrations/cli.js
CHANGED
|
@@ -218,6 +218,84 @@ program.option('-V', 'output the version');
|
|
|
218
218
|
});
|
|
219
219
|
|
|
220
220
|
|
|
221
|
+
program
|
|
222
|
+
.command('update-database-down <contextFileName>')
|
|
223
|
+
.alias('udd')
|
|
224
|
+
.description('Run the latest migration down method for the given context')
|
|
225
|
+
.action(function(contextFileName){
|
|
226
|
+
var executedLocation = process.cwd();
|
|
227
|
+
contextFileName = contextFileName.toLowerCase();
|
|
228
|
+
var migration = new Migration();
|
|
229
|
+
try{
|
|
230
|
+
var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
|
|
231
|
+
var files = globSearch.sync(search, executedLocation);
|
|
232
|
+
var file = files && files[0];
|
|
233
|
+
if(!file){
|
|
234
|
+
console.log(`Error - Cannot read or find Context snapshot '${contextFileName}_contextSnapShot.json' in '${executedLocation}'.`);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
var contextSnapshot;
|
|
238
|
+
try{
|
|
239
|
+
contextSnapshot = require(file);
|
|
240
|
+
}catch(_){
|
|
241
|
+
console.log(`Error - Cannot read context snapshot at '${file}'.`);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
|
|
245
|
+
var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
|
|
246
|
+
if(!(migrationFiles && migrationFiles.length)){
|
|
247
|
+
console.log("Error - Cannot read or find migration file");
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
// Sort and select latest
|
|
251
|
+
var mFiles = migrationFiles.slice().sort(function(a, b){
|
|
252
|
+
return __getMigrationTimestamp(a) - __getMigrationTimestamp(b);
|
|
253
|
+
});
|
|
254
|
+
var latestFile = mFiles[mFiles.length - 1];
|
|
255
|
+
|
|
256
|
+
// Prepare context and table object
|
|
257
|
+
let ContextCtor;
|
|
258
|
+
try{
|
|
259
|
+
ContextCtor = require(contextSnapshot.contextLocation);
|
|
260
|
+
}catch(_){
|
|
261
|
+
console.log(`Error - Cannot load Context file at '${contextSnapshot.contextLocation}'.`);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
var contextInstance;
|
|
265
|
+
try{
|
|
266
|
+
contextInstance = new ContextCtor();
|
|
267
|
+
}catch(_){
|
|
268
|
+
console.log(`Error - Failed to construct Context from '${contextSnapshot.contextLocation}'.`);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
var cleanEntities = migration.cleanEntities(contextInstance.__entities);
|
|
272
|
+
var tableObj = migration.buildUpObject(contextSnapshot.schema, cleanEntities);
|
|
273
|
+
|
|
274
|
+
var MigCtor = require(latestFile);
|
|
275
|
+
var migInstance = new MigCtor(ContextCtor);
|
|
276
|
+
if(typeof migInstance.down === 'function'){
|
|
277
|
+
migInstance.down(tableObj);
|
|
278
|
+
}else{
|
|
279
|
+
console.log(`Warning - Migration '${path.basename(latestFile)}' has no down method; skipping.`);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Update snapshot
|
|
283
|
+
var snap = {
|
|
284
|
+
file : contextSnapshot.contextLocation,
|
|
285
|
+
executedLocation : executedLocation,
|
|
286
|
+
context : contextInstance,
|
|
287
|
+
contextEntities : cleanEntities,
|
|
288
|
+
contextFileName: contextFileName
|
|
289
|
+
}
|
|
290
|
+
migration.createSnapShot(snap);
|
|
291
|
+
console.log("database updated");
|
|
292
|
+
|
|
293
|
+
}catch (e){
|
|
294
|
+
console.log("Error - Cannot read or find file ", e);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
|
|
221
299
|
program
|
|
222
300
|
.command('update-database-restart <contextFileName>')
|
|
223
301
|
.alias('udr')
|
package/Migrations/migrations.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// version 0.0.
|
|
1
|
+
// version 0.0.9
|
|
2
2
|
// learn more about seeding info - https://www.pauric.blog/Database-Updates-and-Migrations-with-Entity-Framework/
|
|
3
3
|
|
|
4
4
|
var fs = require('fs');
|
|
@@ -238,6 +238,9 @@ class Migrations{
|
|
|
238
238
|
tables.forEach(function (item, index) {
|
|
239
239
|
// add new columns for table
|
|
240
240
|
var columnInfo = tables[index];
|
|
241
|
+
// Always expose each table under its name so templates like
|
|
242
|
+
// this.createTable(table.TableName) can safely access it.
|
|
243
|
+
tableObj[item.name] = columnInfo.new;
|
|
241
244
|
|
|
242
245
|
item.newTables.forEach(function (column, ind) {
|
|
243
246
|
tableObj[item.name] = columnInfo.new;
|
package/Migrations/schema.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// version 0.0.
|
|
1
|
+
// version 0.0.6
|
|
2
2
|
class schema{
|
|
3
3
|
|
|
4
4
|
constructor(context){
|
|
@@ -85,7 +85,7 @@ class schema{
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
}else{
|
|
88
|
-
console.log("Table that
|
|
88
|
+
console.log("Table that you're trying to create is undefined. Please check if there are any changes that need to be made");
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
|
package/mySQLEngine.js
CHANGED
|
@@ -570,8 +570,23 @@ class SQLLiteEngine {
|
|
|
570
570
|
|
|
571
571
|
_execute(query){
|
|
572
572
|
console.log("SQL:", query);
|
|
573
|
-
|
|
574
|
-
|
|
573
|
+
try{
|
|
574
|
+
this.db.connect(this.db);
|
|
575
|
+
const res = this.db.query(query);
|
|
576
|
+
if(res === null){
|
|
577
|
+
console.error('MySQL execute skipped: connection not defined');
|
|
578
|
+
return null;
|
|
579
|
+
}
|
|
580
|
+
return res;
|
|
581
|
+
}catch(err){
|
|
582
|
+
const code = err && err.code ? err.code : '';
|
|
583
|
+
if(code === 'ER_BAD_DB_ERROR' || code === 'ECONNREFUSED' || code === 'PROTOCOL_CONNECTION_LOST'){
|
|
584
|
+
console.error('MySQL execute skipped: connection not defined');
|
|
585
|
+
return null;
|
|
586
|
+
}
|
|
587
|
+
console.error(err);
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
575
590
|
}
|
|
576
591
|
|
|
577
592
|
_run(query){
|
package/mySQLSyncConnect.js
CHANGED
|
@@ -1,28 +1,45 @@
|
|
|
1
|
-
// version : 0.0.
|
|
1
|
+
// version : 0.0.3
|
|
2
2
|
|
|
3
3
|
var MySql = require('sync-mysql2');
|
|
4
4
|
|
|
5
5
|
class MySQLClient {
|
|
6
6
|
constructor(config) {
|
|
7
|
-
this.config = config;
|
|
7
|
+
this.config = config ? { ...config } : {};
|
|
8
|
+
if (this.config && Object.prototype.hasOwnProperty.call(this.config, 'type')) {
|
|
9
|
+
delete this.config.type;
|
|
10
|
+
}
|
|
8
11
|
this.connection = null;
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
connect() {
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
try {
|
|
16
|
+
if (!this.connection) {
|
|
17
|
+
const { type, ...safeConfig } = this.config || {};
|
|
18
|
+
this.connection = new MySql(safeConfig);
|
|
19
|
+
}
|
|
20
|
+
} catch (err) {
|
|
21
|
+
// Swallow connection errors and leave connection undefined so callers can react gracefully
|
|
22
|
+
console.error('MySQL connect error:', err && err.code ? err.code : err);
|
|
23
|
+
this.connection = null;
|
|
24
|
+
return null;
|
|
14
25
|
}
|
|
15
26
|
}
|
|
16
27
|
|
|
17
28
|
query(sql, params = []) {
|
|
18
29
|
try {
|
|
19
30
|
if (!this.connection) {
|
|
20
|
-
|
|
31
|
+
return null;
|
|
21
32
|
}
|
|
22
33
|
var jj = this.connection.query(sql);
|
|
23
34
|
this.connection.finishAll();
|
|
24
35
|
return jj;
|
|
25
36
|
} catch (err) {
|
|
37
|
+
// If the underlying driver surfaces bad DB or network errors, normalize to null
|
|
38
|
+
const code = err && err.code ? err.code : '';
|
|
39
|
+
if(code === 'ER_BAD_DB_ERROR' || code === 'ECONNREFUSED' || code === 'PROTOCOL_CONNECTION_LOST'){
|
|
40
|
+
console.error('MySQL query skipped due to connection not defined');
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
26
43
|
console.error(err);
|
|
27
44
|
return null;
|
|
28
45
|
}
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"app-root-path": "^3.1.0",
|
|
10
10
|
"better-sqlite3": "^12.4.1"
|
|
11
11
|
},
|
|
12
|
-
"version": "0.2.
|
|
12
|
+
"version": "0.2.9",
|
|
13
13
|
"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 ",
|
|
14
14
|
"homepage": "https://github.com/Tailor/MasterRecord#readme",
|
|
15
15
|
"repository": {
|