forked from vkarpov15/dookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (120 loc) · 3.63 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
'use strict';
const clone = require('clone');
const co = require('co');
const dot = require('dot-component');
const ejson = require('mongodb-extended-json');
const fs = require('fs');
const mongodb = require('mongodb');
const ns = require('mongodb-ns');
const path = require('path');
const thunkify = require('thunkify');
const vm = require('vm');
const yaml = require('js-yaml');
function push(uri, data, filename) {
return co(function*() {
const db = yield mongodb.MongoClient.connect(uri);
yield db.dropDatabase();
// $require
for (const key in data) {
if (key === '$require') {
if (!filename) {
throw new Error(`Can't $require without specifying a filename`);
}
const directory = path.dirname(filename);
const extension = path.extname(filename);
const fileToRead = path.join(directory, data[key]);
const fileContents = yield thunkify(fs.readFile)(fileToRead);
const parsedContents = {
'.yml': () => yaml.safeLoad(fileContents),
'.json': () => JSON.parse(fileContents)
}[extension]();
for (const _key in parsedContents) {
if (data[_key] && !_key.startsWith('$') &&
Array.isArray(data[_key]) && Array.isArray(parsedContents[_key])) {
data[_key] = parsedContents[_key].concat(data[_key]);
} else {
data[_key] = parsedContents[_key];
}
}
}
}
// extensions
let extensions = {};
for (const key in data) {
if (key[0] !== '$') {
continue;
}
extensions[key] = data[key];
delete data[key];
}
// insert
let promises = [];
for (const collection in data) {
let docs = data[collection];
if (docs.length === 0) {
continue;
}
for (let i = 0; i < docs.length; ++i) {
const doc = docs[i];
expand(extensions, doc);
const tmp = doc.$set;
delete doc.$set;
for (const key in tmp) {
dot.set(doc, key, tmp[key]);
}
docs[i] = ejson.deflate(doc);
}
promises.push(db.collection(collection).insert(docs));
}
const res = yield promises;
return res;
});
}
function expand(extensions, doc) {
if (doc.$extend) {
const tmp = doc.$extend;
delete doc.$extend;
for (const key in extensions[tmp]) {
if (typeof doc[key] === 'undefined') {
doc[key] = clone(extensions[tmp][key]);
}
}
}
Object.keys(doc).forEach(function(key) {
if (doc[key] && typeof doc[key] === 'object') {
if (doc[key].$eval) {
const context = vm.createContext(doc);
doc[key] = vm.runInContext(doc[key].$eval, context);
}
expand(extensions, doc[key]);
}
});
}
function pull(uri) {
return co(function*() {
const db = yield mongodb.MongoClient.connect(uri);
const collections = yield db.listCollections().toArray();
let promises = [];
let filteredCollections = [];
for (let i = 0; i < collections.length; ++i) {
let namespace = ns(`test.${collections[i].name}`);
if (namespace.system || namespace.oplog || namespace.special) {
continue;
}
filteredCollections.push(collections[i].name);
promises.push(db.collection(collections[i].name).find({}).toArray());
}
const contents = yield promises;
let res = {};
filteredCollections.forEach(function(collection, i) {
res[collection] = contents[i].map(function(doc) { return ejson.inflate(doc); });
});
return res;
});
}
exports.push = function(uri, data, path) {
return push(uri, data, path);
};
exports.pull = function(uri) {
return pull(uri);
};