-
Notifications
You must be signed in to change notification settings - Fork 1
/
createdb.js
62 lines (55 loc) · 1.85 KB
/
createdb.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
const MongoClient = require('mongodb').MongoClient;
require('dotenv').config();
module.exports =
{
/**
* Get a license by UID
* @returns {Promise<string>} license
* @param {string} uid
*/
getUID: async function getByUID(uid) {
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri, { useNewUrlParser: true });
return client.connect().then(async () => {
const db = client.db('licensesdb');
const collection = db.collection('licenses');
const result = await collection.findOne({ uid: uid });
if (result) {
console.log('License found');
return result;
} else {
return "License not found";
}
}).catch((err) => {
console.error(err);
}).finally(() => {
console.log('Connection closed');
client.close();
});
},
/**
* Insert a new element into the database
* @returns {Promise<string>} license
* @param {string} uid
* @param {string} key
* @param {string} license
*/
insert: async function insert(uid, key, license) {
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri, { useNewUrlParser: true });
return await client.connect().then(() => {
const db = client.db('licensesdb');
const collection = db.collection('licenses');
return collection.insertOne({ uid: uid, key: key, license: license }).then(() => {
console.log('Inserted');
}).catch((err) => {
console.error(err);
}).finally(() => {
console.log('Connection closed');
client.close();
});
}).catch((err) => {
console.error(err);
});
}
}