@@ -18,8 +18,10 @@ const SESSION = require("../unisys/common-session");
18
18
const LOGGER = require ( "../unisys/server-logger" ) ;
19
19
const PROMPTS = require ( "../system/util/prompts" ) ;
20
20
const PR = PROMPTS . Pad ( "ServerDB" ) ;
21
- const DB_FILE = "./runtime/netcreate.loki" ;
21
+ const RUNTIMEPATH = './runtime/' ;
22
+ const TEMPLATEPATH = './app/assets/templates/' ;
22
23
const DB_CLONEMASTER = "blank.loki" ;
24
+ const NC_CONFIG = require ( "../assets/netcreate-config" ) ;
23
25
24
26
/// MODULE-WIDE VARS //////////////////////////////////////////////////////////
25
27
/// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
@@ -32,28 +34,34 @@ let NODES; // loki "nodes" collection
32
34
let EDGES ; // loki "edges" collection
33
35
let m_locked_nodes ;
34
36
let m_locked_edges ;
37
+ let TEMPLATE ;
35
38
36
39
/// API METHODS ///////////////////////////////////////////////////////////////
37
40
/// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
38
41
let DB = { } ;
39
42
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40
43
/*/ API: Initialize the database
41
44
/*/
42
- DB . InitializeDatabase = function ( options = { } ) {
43
- FS . ensureDirSync ( PATH . dirname ( DB_FILE ) ) ;
44
- if ( ! FS . existsSync ( DB_FILE ) ) {
45
- console . log ( PR , `NO EXISTING DATABASE ${ DB_FILE } , so creating BLANK DATABASE...` ) ;
45
+ DB . InitializeDatabase = function ( options = { } ) {
46
+
47
+ let dataset = NC_CONFIG . dataset ;
48
+ let db_file = m_GetValidDBFilePath ( dataset ) ;
49
+ FS . ensureDirSync ( PATH . dirname ( db_file ) ) ;
50
+ if ( ! FS . existsSync ( db_file ) ) {
51
+ console . log ( PR , `NO EXISTING DATABASE ${ db_file } , so creating BLANK DATABASE...` ) ;
46
52
}
53
+ console . log ( PR , `LOADING DATABASE ${ db_file } ` ) ;
47
54
let ropt = {
48
55
autoload : true ,
49
56
autoloadCallback : f_DatabaseInitialize ,
50
57
autosave : true ,
51
58
autosaveCallback : f_AutosaveStatus ,
52
- autosaveInterval : 4000 // save every four seconds
59
+ autosaveInterval : 4000 , // save every four seconds
53
60
} ;
54
61
ropt = Object . assign ( ropt , options ) ;
55
- m_db = new Loki ( DB_FILE , ropt ) ;
62
+ m_db = new Loki ( db_file , ropt ) ;
56
63
m_options = ropt ;
64
+ m_options . db_file = db_file ; // store for use by DB.WriteJSON
57
65
58
66
// callback on load
59
67
function f_DatabaseInitialize ( ) {
@@ -118,7 +126,20 @@ DB.InitializeDatabase = function(options = {}) {
118
126
console . log ( PR , `DATABASE LOADED! m_max_nodeID '${ m_max_nodeID } ', m_max_edgeID '${ m_max_edgeID } '` ) ;
119
127
m_db . saveDatabase ( ) ;
120
128
121
- if ( typeof m_options . onLoadComplete === 'function' ) {
129
+ // LOAD TEMPLATE - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
130
+ let templatePath = RUNTIMEPATH + NC_CONFIG . dataset + ".template" ;
131
+ FS . ensureDirSync ( PATH . dirname ( templatePath ) ) ;
132
+ // Does the template exist?
133
+ if ( ! FS . existsSync ( templatePath ) ) {
134
+ console . log ( PR , `NO EXISTING TEMPLATE ${ templatePath } , so cloning default template...` ) ;
135
+ FS . copySync ( TEMPLATEPATH + '_default.template' , templatePath ) ;
136
+ }
137
+ console . log ( PR , `LOADING TEMPLATE ${ templatePath } ` ) ;
138
+ // Now load it
139
+ TEMPLATE = FS . readJsonSync ( templatePath ) ;
140
+
141
+ // Call complete callback
142
+ if ( typeof m_options . onLoadComplete === 'function' ) {
122
143
m_options . onLoadComplete ( ) ;
123
144
}
124
145
} // end f_DatabaseInitialize
@@ -140,7 +161,7 @@ DB.PKT_GetDatabase = function(pkt) {
140
161
let edges = EDGES . chain ( ) . data ( { removeMeta : true } ) ;
141
162
if ( DBG ) console . log ( PR , `PKT_GetDatabase ${ pkt . Info ( ) } (loaded ${ nodes . length } nodes, ${ edges . length } edges)` ) ;
142
163
LOGGER . Write ( pkt . Info ( ) , `getdatabase` ) ;
143
- return { nodes, edges } ;
164
+ return { d3data : { nodes, edges } , template : TEMPLATE } ;
144
165
} ;
145
166
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146
167
/*/ API: reset database from scratch
@@ -430,8 +451,13 @@ DB.FilterEdgeLog = function(edge) {
430
451
/*/ called by brunch to generate an up-to-date JSON file to path.
431
452
creates the path if it doesn't exist
432
453
/*/
433
- DB . WriteJSON = function ( filePath ) {
434
- let db = new Loki ( DB_FILE , {
454
+ DB . WriteDbJSON = function ( filePath ) {
455
+ let dataset = NC_CONFIG . dataset ;
456
+
457
+ // Ideally we should use m_otions value, but in standlone mode,
458
+ // m_options might not be defined.
459
+ let db_file = m_options ? m_options . db_file : m_GetValidDBFilePath ( dataset ) ;
460
+ let db = new Loki ( db_file , {
435
461
autoload : true ,
436
462
autoloadCallback : ( ) => {
437
463
if ( typeof filePath === 'string' ) {
@@ -446,14 +472,29 @@ DB.WriteJSON = function( filePath ) {
446
472
FS . ensureDirSync ( PATH . dirname ( filePath ) ) ;
447
473
if ( DBG ) console . log ( PR , `writing file ${ filePath } ` ) ;
448
474
FS . writeFileSync ( filePath , json ) ;
449
- console . log ( PR , `*** WROTE JSON DATABASE` ) ;
475
+ console . log ( PR , `*** WROTE JSON DATABASE ${ filePath } ` ) ;
450
476
} else {
451
477
console . log ( PR , `ERR path ${ filePath } must be a pathname` ) ;
452
478
}
453
479
}
454
480
}
455
481
) ;
456
482
} ;
483
+ /// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
484
+ /*/ called by brunch to generate an up-to-date Template file to path.
485
+ creates the path if it doesn't exist
486
+ /*/
487
+ DB . WriteTemplateJSON = function ( filePath ) {
488
+ let templatePath = RUNTIMEPATH + NC_CONFIG . dataset + ".template" ;
489
+ FS . ensureDirSync ( PATH . dirname ( templatePath ) ) ;
490
+ // Does the template exist?
491
+ if ( ! FS . existsSync ( templatePath ) ) {
492
+ console . error ( PR , `ERR could not find template ${ templatePath } ` ) ;
493
+ } else {
494
+ FS . copySync ( templatePath , filePath ) ;
495
+ console . log ( PR , `*** COPIED TEMPLATE ${ templatePath } to ${ filePath } ` ) ;
496
+ }
497
+ } ;
457
498
458
499
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
459
500
/// utility function for cleaning nodes with numeric id property
@@ -486,6 +527,17 @@ function m_CleanID(prompt, id) {
486
527
}
487
528
return id ;
488
529
}
530
+ /// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
531
+ /// utility function for getting a valid file path
532
+ function m_GetValidDBFilePath ( dataset ) {
533
+ // validate dataset name
534
+ let regex = / ^ ( [ A - z 0 - 9 - _ + ./ ] ) * $ / ; // Allow _ - + . /, so nested pathways are allowed
535
+ if ( ! regex . test ( dataset ) ) {
536
+ console . error ( PR , `Trying to initialize database with bad dataset name: ${ dataset } ` ) ;
537
+ }
538
+
539
+ return RUNTIMEPATH + dataset + ".loki" ;
540
+ }
489
541
490
542
/// EXPORT MODULE DEFINITION //////////////////////////////////////////////////
491
543
/// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
0 commit comments