-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatabase.js
42 lines (39 loc) · 1.38 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
const { MongoMemoryServer } = require('mongodb-memory-server');
(async () => {
const mongod = await MongoMemoryServer.create({
instance: {
port: 27017,
ip: '127.0.0.1',
dbName: 'properties'
}
});
const uri = mongod.getUri();
const port = mongod.instanceInfo.port;
const dbPath = mongod.instanceInfo.dbPath;
const dbName = mongod.instanceInfo.dbName;
console.log(`Database uri: ${uri}`);
console.log(`Database running on port: ${port}`);
console.log(`Database Name: ${dbName}`);
console.log(`Local DB Storage path ${dbPath}`);
const MongoClient = require('mongodb').MongoClient;
MongoClient.connect(uri, function(err, client) {
const db = client.db(dbName);
seedUsers(db, function(err, result) {
if(err) throw new Error("Error seeding");
console.log(`Seeded ${result.insertedCount} users into users collection.`)
client.close();
});
});
})();
const seedUsers = function(db, callback) {
const userCollection = db.collection('users');
userCollection.insertMany([{
"email": "[email protected]",
"token": "676cfd34-e706-4cce-87ca-97f947c43bd4",
}, {
"email": "[email protected]",
"token": "2f403433-ba0b-4ce9-be02-d1cf4ad6f453",
}], function(err, result) {
callback(err, result);
});
}