forked from evanplaice/ossu-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.js
59 lines (51 loc) · 1.4 KB
/
collection.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
'use strict';
require('dotenv').load();
// the command to execute
let command = process.argv[2] || '--create';
// collection name (defaults to 'profiles')
let name = process.argv[3] || 'profiles';
// database URI
let url = process.env.MONGO_URI;
// Create a connection context
let MongoClient = require('mongodb').MongoClient;
let assert = require('assert');
// Connect to the db
MongoClient.connect(url, function (err, db) {
assert.equal(null, err);
// console.log("Connected");
if (command === '--create') {
create(db, function () {
db.close();
});
} else if (command === '--destroy') {
destroy(db, function () {
db.close();
});
} else {
console.log(`error: unknown command ${command}`);
}
});
// creates a collection
let create = function (db, callback) {
db.createCollection(name, function (err, collection) {
if (!err) {
console.log(`Collection: ${name} created successfully`);
process.exit();
} else {
console.log("Collection: '" + name + "' creation failed");
process.exit(1);
}
});
};
// destroys a collection
let destroy = function (db, callback) {
db.collection(name).drop(function (err, collection) {
if (!err) {
console.log("Collection: '" + name + "' deleted successfully");
process.exit();
} else {
console.log("Collection: '" + name + "' deletion failed");
process.exit(1);
}
});
};