-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
168 lines (147 loc) · 4.46 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
'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 get = require('lodash.get');
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 client = yield mongodb.MongoClient.connect(uri);
const db = client.db();
options = standardizePushOptions(options);
if (options.dropDatabase === true) {
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));
}
if (get(options, 'clearConnection', null)) {
yield db.close();
}
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, options) {
return co(function*() {
const client = yield mongodb.MongoClient.connect(uri);
const db = client.db();
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));
});
if (get(options, 'clearConnection', null)) {
yield db.close();
}
return res;
});
}
exports.push = function(uri, data, pathOrOptions) {
return push(uri, data, pathOrOptions);
};
exports.pull = function(uri, options) {
return pull(uri, options);
};
exports.pullToFile = require('./src/pullToFile');
exports.pushFromFile = require('./src/pushFromFile');
exports.pushFromRemote = require('./src/pushFromRemote');