-
Notifications
You must be signed in to change notification settings - Fork 192
/
db.js
229 lines (208 loc) · 5.72 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var uuid = require('./lib/uuid')
var verifyNodes = require('./util/verify-nodes')
var treesToMap = require('./util/trees-to-map')
module.exports = Db
function Db(pl, plugins) {
this.nodes = {}
this.pl = pl
if (pl.setupTypes) {
pl.setupTypes(['node'])
}
this.plugins = plugins || []
this.addNewNodeAttrs = []
plugins.forEach((plugin) => {
if (plugin.addNewNodeAttrs) {
this.addNewNodeAttrs.push(plugin.addNewNodeAttrs)
}
})
if (!this.addNewNodeAttrs.length) {
this.addNewNodeAttrs = false
}
}
Db.prototype = {
init: function (defaultData, done) {
if (this.pl.init) {
this.pl.init().then(
() => this._getInitialData(defaultData, done),
err => done(err)
).catch(err => console.error('Failed to init?', err))
} else {
this._getInitialData(defaultData, done)
}
},
_getInitialData(defaultData, done) {
this.pl.findAll('root', (err, roots) => {
if (err) return done(err)
if (!roots.length) return this.makeRoot(defaultData, done)
this.root = roots[0].id // TODO why ignore all other potential roots?
this.pl.findAll('node', (err, nodes) => {
if (err) return done(err)
var map = {}
nodes.forEach((node) => map[node.id] = node)
var err = verifyNodes(this.root, map)
if (err) {
return done(err)
}
this.nodes = map
done()
})
})
},
create: function (pid, ix, content, type) {
var id = uuid()
var now = Date.now()
var node = {
id: id,
created: now,
modified: now,
collapsed: true,
content: content || '',
type: type || 'base',
children: [],
parent: pid,
}
if (this.addNewNodeAttrs) {
this.addNewNodeAttrs.map(fn => fn(node))
}
this.save(id, node)
this.insertChild(pid, id, ix)
return id
},
// dump children INTO the pid at index ix
dump: function (pid, children, ix, done) {
var mapped = treesToMap(children, pid, true)
this.batchSave(mapped.nodes, (err) => {
if (err) return done(err)
var oldChildren = this.nodes[pid].children
if (!ix && ix !== 0) {
children = oldChildren.concat(mapped.roots)
} else {
children = oldChildren.slice()
;[].splice.apply(children, [ix, 0].concat(mapped.roots))
}
this.set(pid, 'children', children, (err) => {
done(err, {ids: mapped.roots, oldChildren: oldChildren})
})
})
},
exportTree: function (pid, keepIds) {
pid = pid || this.root
var node = this.nodes[pid]
, out = {id: pid}
for (var name in node) {
out[name] = node[name]
}
out.children = node.children.map(id => this.exportTree(id, keepIds))
if (!keepIds) delete out.id
delete out.parent
return out
},
exportMany: function (ids, keepIds) {
return ids.map(id => this.exportTree(id, keepIds))
},
makeRoot: function (defaultData, done) {
this.root = defaultData && defaultData.id || uuid()
this.pl.save('root', this.root, {id: this.root}, (err) => {
if (err) return done(err)
this.nodes = {}
var now = Date.now()
this.nodes[this.root] = {
id: this.root,
created: now,
modified: now,
content: defaultData.content || 'Home',
parent: null,
children: []
}
this.pl.save('node', this.root, this.nodes[this.root], (err) => {
if (err) return done(err)
this.dump(this.root, defaultData.children, null, done)
})
})
},
batchSave: function (nodes, done) {
for (var id in nodes) {
this.nodes[id] = nodes[id]
this.nodes[id].modified = Date.now()
}
this.pl.batchSave('node', nodes, done)
},
save: function (id, value, modified) {
this.nodes[id] = value
this.nodes[id].modified = modified || Date.now()
this.pl.save('node', id, value)
},
set: function (id, attr, value, done) {
this.nodes[id] = {
...this.nodes[id],
[attr]: value,
modified: Date.now()
}
this.pl.set('node', id, attr, value, done)
},
remove: function (id) {
delete this.nodes[id]
this.pl.remove('node', id)
},
removeMany: function (ids) {
ids.forEach(this.remove.bind(this))
},
saveMany: function (nodes) {
nodes.forEach((node) => this.save(node.id, node))
},
// returns the old index
removeChild: function (pid, id, count) {
count = count || 1
var ix = this.nodes[pid].children.indexOf(id)
if (ix === -1) return -1
var ch = this.nodes[pid].children.slice()
ch.splice(ix, count)
this.set(pid, 'children', ch)
return ix
},
insertChild: function (pid, id, ix) {
var ch = this.nodes[pid].children.slice()
ch.splice(ix, 0, id)
this.set(pid, 'children', ch)
return ix
},
insertChildren: function (pid, ids, ix) {
var ch = this.nodes[pid].children.slice()
ch.splice.apply(ch, [ix, 0].concat(ids))
this.set(pid, 'children', ch)
return ix
},
setMany: function (attr, ids, value, done) {
var now = Date.now()
const update = {}
if (Array.isArray(value)) {
ids.forEach((id, i) => {
this.nodes[id] = {
...this.nodes[id],
[attr]: value[i],
modified: now,
}
update[id] = this.nodes[id]
})
} else {
ids.forEach((id, i) => {
this.nodes[id] = {
...this.nodes[id],
[attr]: value,
modified: now,
}
update[id] = this.nodes[id]
})
}
this.pl.batchSave('node', update, done)
// this.pl.batchSet('node', attr, ids, value, done)
},
update: function (id, update, done) {
this.nodes[id] = {
...this.nodes[id],
...update,
modified: Date.now(),
}
this.pl.update('node', id, update, done)
},
}