-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmemo-dao.js
More file actions
39 lines (29 loc) · 1.08 KB
/
memo-dao.js
File metadata and controls
39 lines (29 loc) · 1.08 KB
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
/* The MemosDAO must be constructed with a connected database object */
function MemosDAO(db) {
"use strict";
/* If this constructor is called without the "new" operator, "this" points
* to the global object. Log a warning and call it correctly. */
if (false === (this instanceof MemosDAO)) {
console.log("Warning: MemosDAO constructor called without 'new' operator");
return new MemosDAO(db);
}
const memosCol = db.collection("memos");
this.insert = (memo, callback) => {
// Create allocations document
const memos = {
memo,
timestamp: new Date()
};
memosCol.insert(memos, (err, result) => !err ? callback(null, result) : callback(err, null));
};
this.getAllMemos = (callback) => {
memosCol.find({}).sort({
timestamp: -1
}).toArray((err, memos) => {
if (err) return callback(err, null);
if (!memos) return callback("ERROR: No memos found", null);
callback(null, memos);
});
};
}
module.exports = { MemosDAO };