forked from dmfay/massive-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
496 lines (419 loc) · 13.4 KB
/
index.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
var Runner = require("./lib/runner");
var _ = require("underscore")._;
var fs = require("fs");
var Executable = require("./lib/executable");
var Queryable = require("./lib/queryable");
var Table = require("./lib/table");
var util = require("util");
var assert = require("assert");
var ArgTypes = require("./lib/arg_types");
var path = require("path");
var DA = require('deasync');
var stripBom = require('strip-bom');
var self;
var Massive = function(args){
this.scriptsDir = args.scripts || process.cwd() + "/db";
var runner = new Runner(args.connectionString);
_.extend(this,runner);
this.tables = [];
this.views = [];
this.queryFiles = [];
this.schemas = [];
this.functions = [];
if(args.whitelist) {
this.whitelist = this.getTableFilter(args.whitelist);
} else {
this.allowedSchemas = this.getSchemaFilter(args.schema);
this.blacklist = this.getTableFilter(args.blacklist);
this.exceptions = this.getTableFilter(args.exceptions);
}
// any "truthy" value passed will cause functions to be excluded. No param
// will be a "falsy" value, and functions will be included...
this.excludeFunctions = args.excludeFunctions;
this.functionBlacklist = this.getTableFilter(args.functionBlacklist);
this.functionWhitelist = this.getTableFilter(args.functionWhitelist);
};
Massive.prototype.getSchemaFilter = function(allowedSchemas) {
// an empty string will cause all schema to be loaded by default:
var result = '';
if(allowedSchemas === 'all' || allowedSchemas === '*') {
// Do nothing else. Leave the default empty string:
allowedSchemas = null;
}
if(allowedSchemas) {
// there is a value of some sort other than our acceptable defaults:
if(_.isString(allowedSchemas)) {
// a string works. If comma-delimited, so much the better, we're done:
result = allowedSchemas;
} else {
if(!_.isArray(allowedSchemas)) {
throw("Specify allowed schemas using either a commma-delimited string or an array of strings");
}
// create a comma-delimited string:
result = allowedSchemas.join(", ");
}
}
return result;
};
Massive.prototype.getTableFilter = function(filter) {
// an empty string will cause all schema to be loaded by default:
var result = '';
if(filter) {
// there is a value of some sort other than our acceptable defaults:
if(_.isString(filter)) {
// a string works. If comma-delimited, so much the better, we're done:
result = filter;
} else {
if(!_.isArray(filter)) {
throw("Specify filter patterns using either a commma-delimited string or an array of strings");
}
// create a comma-delimited string:
result = filter.join(", ");
}
}
return result;
};
Massive.prototype.run = function(){
var args = ArgTypes.queryArgs(arguments);
this.query(args);
};
Massive.prototype.runSync = DA(Massive.prototype.run);
Massive.prototype.loadQueries = function() {
walkSqlFiles(this, this.scriptsDir);
};
Massive.prototype.loadTables = function(next) {
var tableSql = __dirname + "/lib/scripts/tables.sql";
var parameters = [this.allowedSchemas, this.blacklist, this.exceptions];
var self = this;
// ONLY allow whitelisted items:
if(this.whitelist) {
tableSql = __dirname + "/lib/scripts/whitelist.sql";
parameters = [this.whitelist];
}
this.executeSqlFile({file : tableSql, params: parameters}, function(err,tables) {
if (err) { return next(err, null); }
_.each(tables, function(table){
var _table = new Table({
schema : table.schema,
name : table.name,
pk : table.pk,
db : self
});
MapToNamespace(_table);
});
next(null,self);
});
};
Massive.prototype.loadViews = function(next) {
var viewSql = __dirname + "/lib/scripts/views.sql";
var parameters = [this.allowedSchemas, this.blacklist, this.exceptions];
var self = this;
this.executeSqlFile({file : viewSql, params: parameters}, function(err, views){
if (err) { return next(err, null); }
_.each(views, function(view) {
var _view = new Queryable({
schema : view.schema,
name : view.name,
db : self
});
MapToNamespace(_view, "views");
});
next(null, self);
});
};
Massive.prototype.saveDoc = function(collection, doc, next){
var self = this;
// default is public. Table constructor knows what to do if 'public' is used as the schema name:
var schemaName = "public";
var tableName = collection;
var potentialTable = null;
// is the collection namespace delimited?
var splits = collection.split(".");
if(splits.length > 1) {
// uh oh. Someone specified a schema name:
schemaName = splits[0];
tableName = splits[1];
potentialTable = self[schemaName][tableName];
} else {
potentialTable = self[tableName];
}
if(potentialTable) {
potentialTable.saveDoc(doc, next);
} else {
var _table = new Table({
schema : schemaName,
pk : "id",
name : tableName,
db : self
});
// Create the table in the back end:
var sql = this.documentTableSql(collection);
this.query(sql, function(err){
if(err){
next(err,null);
} else {
MapToNamespace(_table);
// recurse
self.saveDoc(collection,doc,next);
}
});
}
};
Massive.prototype.saveDocSync = DA(Massive.prototype.saveDoc);
var MapToNamespace = function(entity, collection) {
collection = collection || "tables";
var db = entity.db;
var executor;
var schemaName;
// executables are always invoked directly, so we need to handle them a bit differently
if (entity instanceof Executable) {
executor = function () {
entity.invoke.apply(entity, arguments);
};
}
if (entity.schema === "public") {
db[entity.name] = executor || entity;
} else {
schemaName = entity.schema;
// is this schema already attached?
if(!db[schemaName]) {
// if not, then bolt it on:
db[schemaName] = {};
}
// attach the entity to the schema:
db[schemaName][entity.name] = executor || entity;
}
db[collection].push(entity);
};
var RemoveFromNamespace = function(db, table) {
// right now only tables are supported
var collection = "tables";
var splits = table.split('.');
var tableName, schemaName;
if(splits.length > 1) {
schemaName = splits[0];
tableName = splits[1];
} else {
schemaName = "public";
tableName = table;
}
if(schemaName === "public" && db[table]) {
delete db[table];
}else if(db[schemaName] && db[schemaName][tableName]) {
delete db[schemaName][tableName];
}
if(db[collection]) {
db[collection] = _.reject(db[collection], function(element) {
return element.name
&& element.schema
&& element.schema === schemaName
&& element.name === tableName;
});
}
};
Massive.prototype.createDocumentTable = function(path, next) {
// Create the table in the back end:
var splits = path.split(".");
var tableName;
var schemaName;
if(splits.length > 1) {
// uh oh. Someone specified a schema name:
schemaName = splits[0];
tableName = splits[1];
} else {
schemaName = "public"; // default schema
tableName = path;
}
var _table = new Table({
schema : schemaName,
pk : "id",
name : tableName,
db : self
});
var sql = this.documentTableSql(path);
this.query(sql, function(err, res){
if(err){
next(err,null);
} else {
MapToNamespace(_table);
next(null, res);
}
});
};
Massive.prototype.documentTableSql = function(tableName){
var docSqlFile = __dirname + "/lib/scripts/create_document_table.sql";
var sql = fs.readFileSync(docSqlFile, {encoding: 'utf-8'});
var indexName = tableName.replace(".", "_");
sql = util.format(sql, tableName, indexName, tableName, indexName, tableName);
return sql;
};
Massive.prototype.dropTable = function(table, options, next) {
var sql = this.dropTableSql(table, options);
this.query(sql, function(err, res) {
if(err) {
next(err, null);
} else {
RemoveFromNamespace(self, table);
next(null, res);
}
});
};
Massive.prototype.dropTableSql = function(tableName, options){
var docSqlFile = __dirname + "/lib/scripts/drop_table.sql";
var sql = fs.readFileSync(docSqlFile, {encoding: 'utf-8'});
var cascadeOpt = options && options.cascade === true ? "CASCADE" : "";
sql = util.format(sql, tableName, cascadeOpt);
return sql;
};
Massive.prototype.createSchema = function(schemaName, next) {
var sql = this.createSchemaSql(schemaName);
this.query(sql, function(err, res) {
if(err) {
next(err);
} else {
self[schemaName] = {};
next(null, res);
}
});
};
Massive.prototype.createSchemaSql = function(schemaName) {
var docSqlFile = __dirname + "/lib/scripts/create_schema.sql";
var sql = fs.readFileSync(docSqlFile, {encoding: 'utf-8'});
sql = util.format(sql, schemaName);
return sql;
};
Massive.prototype.dropSchema = function(schemaName, options, next) {
var sql = this.dropSchemaSql(schemaName, options);
this.query(sql, function(err, res) {
if(err) {
next(err);
} else {
// Remove all the tables from the namespace
if(self[schemaName]) {
_.each(Object.keys(self[schemaName]), function(table) {
RemoveFromNamespace(self, schemaName + "." + table);
});
}
// Remove the schema from the namespace
delete self[schemaName];
next(null, res);
}
});
};
Massive.prototype.dropSchemaSql = function(schemaName, options) {
var docSqlFile = __dirname + "/lib/scripts/drop_schema.sql";
var sql = fs.readFileSync(docSqlFile, {encoding: 'utf-8'});
// Default to restrict, but optional cascade
var cascadeOpt = options && options.cascade === true ? "CASCADE" : "";
sql = util.format(sql, schemaName, cascadeOpt);
return sql;
};
//A recursive directory walker that would love to be refactored
var walkSqlFiles = function(rootObject, rootDir) {
var dirs;
try {
dirs = fs.readdirSync(rootDir);
} catch (ex) {
return;
}
//loop the directories found
_.each(dirs, function(item){
//parsing with path is a friendly way to get info about this dir or file
var ext = path.extname(item);
var name = path.basename(item, ext);
//is this a SQL file?
if (ext === ".sql") {
//why yes it is! Build the abspath so we can read the file
var filePath = path.join(rootDir,item);
//pull in the SQL - don't worry this only happens once, when
//massive is loaded using connect()
//remove the unicode Byte Order Mark
var sql = stripBom(fs.readFileSync(filePath, {encoding : "utf-8"}));
var _exec = new Executable({
sql: sql,
filePath: filePath,
name : name,
db : self
});
// unfortunately we can't use MapToNamespace here without making a ton of changes
// since we need to accommodate deeply-nested directories rather than 1-deep schemata
self.queryFiles.push(_exec);
rootObject[name] = function () {
return _exec.invoke.apply(_exec, arguments);
};
} else if (ext === '') {
//this is a directory so shift things and move on down
//set a property on our root object, then use *that*
//as the root in the next call
rootObject[name] = {};
//set the path to walk so we have a correct root directory
var pathToWalk = path.join(rootDir,item);
//recursive call - do it all again
walkSqlFiles(rootObject[name], pathToWalk);
}
});
};
Massive.prototype.loadFunctions = function(next) {
if (!this.excludeFunctions) {
var functionSql = __dirname + "/lib/scripts/functions.sql";
var parameters = [this.functionBlacklist, this.functionWhitelist];
this.executeSqlFile({file : functionSql, params : parameters}, function (err,functions) {
if (err) {
next(err, null);
} else {
_.each(functions, function(fn) {
var schema = fn.schema;
var sql;
var params = [];
for (var i = 1; i <= fn.param_count; i++) {
params.push("$" + i);
}
if (schema !== "public") {
sql = util.format("select * from \"%s\".\"%s\"", schema, fn.name);
self[schema] = self[schema] || {};
} else {
sql = util.format("select * from \"%s\"", fn.name);
}
sql += "(" + params.join(",") + ")";
var _exec = new Executable({
sql: sql,
schema: schema,
name : fn.name,
db : self
});
MapToNamespace(_exec, "functions");
});
next(null, self);
}
});
} else {
next(null, self);
}
};
//connects Massive to the DB
exports.connect = function(args, next){
assert((args.connectionString || args.db), "Need a connectionString or db (name of database on localhost) at the very least.");
//override if there's a db name passed in
if (args.db) {
args.connectionString = "postgres://localhost/"+args.db;
}
var massive = new Massive(args);
//load up the tables, queries, and commands
massive.loadTables(function(err, db) {
//handle error if loading the functions fails and bubble it up
if (err) {
return next(err, null);
}
self = db;
massive.loadViews(function() {
massive.loadFunctions(function(err, db) {
assert(!err, err);
//synchronous
db.loadQueries();
next(null,db);
});
});
});
};
exports.loadSync = DA(this.connect);
exports.connectSync = DA(this.connect);