-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathdb.js
129 lines (119 loc) · 2.89 KB
/
db.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
/** @module db */
const PouchDB = require('pouchdb-browser').default
const uuidv4 = require('uuid/v4')
/** Initialize Schema */
const notesDB = new PouchDB('notes')
const noteBooksDB = new PouchDB('notebooks')
/**
* Fetches all notes from database
* @return {Promise} of the result
* */
const fetchNotes = function () {
return notesDB.allDocs({ include_docs: true })
}
/**
* Adds / Updates a note to database.
* @param {string} id for the note
* @param {string} title for the note
* @param {string} content for the note
* @param {string} modified date for the note
* @param {string} rev used in case of updating existing note
* @return {Promise} of the result
**/
const addNote = function (id = uuidv4(), title = '', content = '', modified = Date.now(), rev = '') {
const doc = {
_id: id,
title: title,
content: content,
modified: modified
}
if (rev) {
doc._rev = rev
}
return notesDB.put(doc)
}
/**
* Gets a note from database using it's ID.
* @param {string} id for the note
* @return {Promise} of the result
**/
const getNote = function (noteID) {
return notesDB.get(noteID)
}
/**
* Deletes a note from database using it's ID and rev.
* @param {string} id for the note
* @param {string} rev for the note
* @return {Promise} of the result
**/
const removeNote = function (noteID, noteRev) {
return notesDB.remove(noteID, noteRev)
}
/**
* Searches for a note using query string.
* @param {string} query text of query
* @return {Promise} of the result
**/
const searchNote = function (query) {
return notesDB.query({
map: function (doc, emit) {
if (doc.content.includes(query)) {
emit(doc._id)
}
}
})
}
/**
* Fetches all notebooks from database
* @return {Promise} of the result
* */
const fetchNoteBooks = function () {
return noteBooksDB.allDocs({ include_docs: true })
}
/**
* Adds / Updates a notebook to database.
* @param {string} id for the notebook
* @param {string} name for the notebook
* @param {object} notes for the notebook
* @param {string} modified date for the notebook
* @param {string} rev used in case of updating existing notebook
* @return {Promise} of the result
**/
const addNoteBook = function (
id = uuidv4(),
name = '',
noteIDs = [],
modified = Date.now(),
rev = ''
) {
const doc = {
_id: id,
name: name,
noteIDs: noteIDs,
modified: modified
}
if (rev) {
doc._rev = rev
}
return noteBooksDB.put(doc)
}
/**
* Deletes a notebook from database using it's ID and rev.
* @param {string} id for the notebook
* @param {string} rev for the notebook
* @return {Promise} of the result
**/
const removeNoteBook = function (noteBookID, noteBookRev) {
return noteBooksDB.remove(noteBookID, noteBookRev)
}
/** export the functions defined here */
module.exports = {
fetchNotes,
addNote,
getNote,
removeNote,
searchNote,
fetchNoteBooks,
addNoteBook,
removeNoteBook
}