forked from vkarpov15/dookie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
189 lines (165 loc) · 4.95 KB
/
index.js
File metadata and controls
189 lines (165 loc) · 4.95 KB
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
'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 standardizePushOptions(options) {
if (!options) {
return {};
}
if (typeof options === 'string') {
return { filename: options };
}
return clone(options);
}
function push(uri, data, options) {
return co(function*() {
const db = yield mongodb.MongoClient.connect(uri);
options = standardizePushOptions(options);
if (options.dropDatabase !== false) {
yield db.dropDatabase();
}
// $require
for (const key in data) {
if (key === '$require') {
const filename = options.filename;
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.deserialize(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 _doc = clone(doc);
_doc.require = v => require(v);
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(doc => ejson.serialize(doc));
});
return res;
});
}
function pullToStream(uri, stream) {
return co(function*() {
const db = yield mongodb.MongoClient.connect(uri);
const collections = db.listCollections();
stream.write(`{\n`);
let first = true;
for (let collection = yield collections.next();
collection != null;
collection = yield collections.next()) {
let namespace = ns(`test.${collection.name}`);
if (namespace.system || namespace.oplog || namespace.special) {
continue;
}
if (!first) {
stream.write(',\n');
first = false;
}
stream.write(`"${collection.name}": [\n`);
const cursor = db.collection(collection.name).find();
for (let doc = yield cursor.next();
doc != null;
doc = yield cursor.next()) {
stream.write(JSON.stringify(ejson.serialize(doc), null, ' '));
}
stream.write(`\n]`);
}
stream.write(`\n}`);
});
}
exports.push = function(uri, data, path) {
return push(uri, data, path);
};
exports.pull = function(uri) {
return pull(uri);
};
exports.pullToStream = pullToStream;