/
database.js
352 lines (254 loc) · 9.5 KB
/
database.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
const { expect } = require('chai');
const dropMongoDbCollections = require('drop-mongodb-collections');
const mongoist = require('../');
const mongojs = require('mongojs');
const connectionString = 'mongodb://localhost:27017/test';
describe('database', function() {
this.timeout(10000);
let db;
beforeEach(dropMongoDbCollections(connectionString));
beforeEach(async() => {
db = mongoist(connectionString);
await db.a.insert([{
name: 'Squirtle',type: 'water', level: 10, }, {
name: 'Starmie', type: 'water', level: 8, }, {
name: 'Charmander', type: 'fire', level: 8,}, {
name: 'Lapras', type: 'water', level: 12,}
]);
});
afterEach(() => db.close());
it('should return a collection if accessing a property that is a valid collection name', async() => {
expect(await db.xyz.count()).to.equal(0);
expect(await db.features.count()).to.equal(0);
expect(await db.options.count()).to.equal(0);
expect(await db.client.count()).to.equal(0);
expect(await db.connection.count()).to.equal(0);
expect(await db.domain.count()).to.equal(0);
});
it('should accept connection strings without mongodb:// protocol specified', async() => {
const dbShort = mongoist('localhost:27017/test');
const docs = await dbShort.a.find();
expect(docs).to.have.length(4);
await dbShort.close();
});
it('should accept connection strings with mongodb+srv:// protocol specified', async() => {
const dbFromSrvProtocol = mongoist('mongodb+srv://server.example.com/');
// Work around access db through a collection
expect(dbFromSrvProtocol.x.db.connectionString).to.equal('mongodb+srv://server.example.com/');
});
it('should accept connection strings without host and mongodb:// protocol specified', async() => {
const dbShort = mongoist('test');
const docs = await dbShort.a.find();
expect(docs).to.have.length(4);
await dbShort.close();
});
it('should accept connection with options', async () => {
// auth should fail with invalid 'authSource', to make sure options take effect
const cannotConnect = mongoist('localhost:27017/test', { 'authSource': 'dummy' });
let errorEvent = null;
cannotConnect.on('error', (error) => {
errorEvent = error;
});
try {
await cannotConnect.test.find();
await cannotConnect.close();
} catch (e) {
expect(errorEvent).to.exist;
return;
}
});
it('should create a collection', async() => {
const collection = await db.createCollection('test123');
expect(collection).to.exist;
try {
await db.createCollection('test123');
} catch (e) {
return;
}
throw new Error('Collection with duplicate collection name created a second time and an error was expected!');
});
it('should list collections', async() => {
await db.createCollection('test1');
await db.createCollection('test2');
const collections = await db.listCollections();
expect(collections).to.have.length(3);
});
it('should get collection names', async() => {
await db.createCollection('test1');
await db.createCollection('test2');
const collectionNames = await db.getCollectionNames();
expect(collectionNames).to.have.length(3);
expect(collectionNames).to.include('test1');
expect(collectionNames).to.include('test2');
expect(collectionNames).to.include('a');
});
it('should get db stats', async() => {
const stats = await db.stats();
expect(stats.ok).to.equal(1);
expect(stats.collections).to.exist;
expect(stats.indexes).to.exist;
});
it.skip('should emit an error event if a connection could not be established', async() => {
const cannotConnect = mongoist('mongodb://127.0.0.1:65535/testdb');
let errorEvent = null;
cannotConnect.on('error', (error) => {
errorEvent = error;
});
try {
await cannotConnect.xyz.find();
} catch (e) {
expect(errorEvent).to.exist;
cannotConnect.close();
return;
}
throw new Error('Expected error to be thrown');
});
it('should emit an event if database connection opened', async() => {
const db = mongoist(connectionString);
const events = {};
db.on('connect', () => events.connect = true);
await db.xyz.find();
expect(events).to.deep.equal({ connect: true });
await db.close();
});
describe('users', function() {
beforeEach(async () => await db.dropAllUsers());
it('should create a user', async() => {
const user = await db.createUser({
user: 'mongoist',
pwd: 'topsecret',
customData: { department: 'area51' },
roles: ['readWrite']
});
expect(user).to.exist;
});
it('should not create duplicate users', async() => {
const user = await db.createUser({
user: 'mongoist',
pwd: 'topsecret',
customData: { department: 'area51' },
roles: ['readWrite']
});
expect(user).to.exist;
try {
await db.createUser({
user: 'mongoist',
pwd: 'topsecret',
customData: { department: 'area51' },
roles: ['readWrite']
});
} catch(e) {
expect(e.code).to.equal(11000);
return;
}
throw new Error('Duplicate users should not be created');
});
it('should drop a user', async() => {
const user = await db.createUser({
user: 'mongoist',
pwd: 'topsecret',
customData: { department: 'area51' },
roles: ['readWrite']
});
expect(user).to.exist;
const result = await db.dropUser('mongoist');
expect(result.ok).to.exist;
});
});
it('should get a non existing last error', async () => {
const lastError = await db.getLastError();
expect(lastError).to.not.exist;
});
it('should get the last error obj with non existing error field', async () => {
const lastError = await db.getLastErrorObj();
expect(lastError).to.exist;
expect(lastError.err).to.not.exist;
});
it('should run a named command', async () => {
const stats = await db.runCommand('dbStats');
expect(stats.ok).to.equal(1);
});
it('should allow passing in a Promise<string>', async () => {
const db = mongoist(Promise.resolve(connectionString));
const docs = await db.a.find({});
expect(docs).to.have.length(4);
await db.close();
});
it('should normalize a Promise<string> connectionString', async () => {
const dbShort = mongoist(Promise.resolve('localhost:27017/test'));
const docs = await dbShort.a.find();
expect(docs).to.have.length(4);
await dbShort.close();
});
it('should allow passing in a mongojs connection', async() => {
const mongojsDb = mongojs(connectionString);
const db = mongoist(mongojsDb);
const docs = await db.a.find({});
expect(docs).to.have.length(4);
await mongojsDb.close();
await db.close();
});
it('should allow passing in a Promise that resolves to a mongojs connection', async() => {
const mongojsDb = mongojs(connectionString);
const db = mongoist(Promise.resolve(mongojsDb));
const docs = await db.a.find({});
expect(docs).to.have.length(4);
await mongojsDb.close();
await db.close();
});
it('should pass projections to mongojs connections using mongodb 2.x driver', async() => {
const mongojsDb = mongojs(connectionString);
const db = mongoist(mongojsDb);
const docs = await db.a.find({}, { name: true, _id: false });
expect(docs).to.have.length(4);
expect(docs).to.deep.contain({ name: 'Squirtle' });
expect(docs).to.deep.contain({ name: 'Starmie' });
expect(docs).to.deep.contain({ name: 'Charmander' });
expect(docs).to.deep.contain({ name: 'Lapras' });
const cursor = db.a.findAsCursor({}, { name: true, _id: false });
const doc = await cursor.next();
expect(doc).to.have.keys('name');
await mongojsDb.close();
await db.close();
});
it('should allow passing in a mongoist connection', async() => {
const mongoistDb = mongoist(connectionString);
const db = mongoist(mongoistDb);
const docs = await db.a.find({});
expect(docs).to.have.length(4);
await mongoistDb.close();
await db.close();
});
it('should pass projections to mongoist connections passing in a mongoist connection', async() => {
const mongoistDb = mongoist(connectionString);
const db = mongoist(mongoistDb);
const docs = await db.a.find({}, { name: true, _id: false });
expect(docs).to.have.length(4);
expect(docs).to.deep.contain({ name: 'Squirtle' });
expect(docs).to.deep.contain({ name: 'Starmie' });
expect(docs).to.deep.contain({ name: 'Charmander' });
expect(docs).to.deep.contain({ name: 'Lapras' });
const cursor = db.a.findAsCursor({}, { name: true, _id: false });
const doc = await cursor.next();
expect(doc).to.have.keys('name');
await mongoistDb.close();
await db.close();
});
it('should drop a database', async() => {
const dbConnectionString = 'mongodb://localhost:27017/test2';
const db = mongoist(dbConnectionString);
await db.b.insert({ name: 'Squirtle',type: 'water', level: 10, });
const docs = await db.b.find({});
expect(docs).to.have.length(1);
await db.dropDatabase();
const db2 = mongoist(dbConnectionString);
const docs2 = await db2.b.find({});
expect(docs2).to.have.length(0);
await db.close();
await db2.close();
});
it('should execute admin commands', async() => {
const dbStats = await db.adminCommand({ dbStats: 1 });
expect(dbStats.db).to.equal('admin');
})
});