/
doc.ts
438 lines (383 loc) · 11.5 KB
/
doc.ts
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import { v4 } from 'uuid'
import { cloneDeep, merge } from 'smoldash'
import { nestedKey } from './utils/key'
import { updateReactiveIndex } from './utils/reactive'
import type { DBCollection } from './collection'
import type {
DocumentCustomPopulateOpts,
DocumentTreeOpts,
} from './types/Document'
import type { MemsDBEvent } from './types/events'
import { populate } from './utils/populate'
import { debounce } from './utils/debounce'
import { updateDocIndex } from './utils/indexed'
/**
* Class for creating structured documents
* @category Core
*/
export class DBDoc {
/** Document id */
id: string
private isCloned: boolean = false
_createdAt: number = Date.now()
_updatedAt: number = Date.now()
/** Debugger variable */
readonly doc_: debug.Debugger
/** Reference to the parent collection */
readonly collection: DBCollection
/** Reference to indexed data for repeated deep data matching */
indexed: {
[key: string]: any | any[]
} = {}
/** Object for any plugin related data */
_pluginData: { [key: string]: any } = {}
/**
* Construct a new Document with the collections schema and any provided data
* @param data Data to be assigned to the document schema
* @param collection Reference to the parent collection
*/
constructor(
data: { [key: string]: any },
collection: DBCollection,
id = v4(),
isCloned = false
) {
this.collection = collection
// Ensure the document has a valid and unique ID
this.id = id
this.isCloned = isCloned
// Ensure this.data is a replica of the schema before assigning the new data
this.setData(merge(cloneDeep(this.collection.schema), cloneDeep(data)))
// Assign the data to the new document
this.doc_ = collection.col_.extend(`<doc>${this.id}`)
}
private updateIndexes = debounce((path: string) => {
/* DEBUG */ this.collection.col_(
'Document %s was modified at path %s',
this.id,
path
)
this._updatedAt = Date.now()
if (Object.keys(this.indexed).length > 0) {
for (const key in this.indexed) {
updateDocIndex(this, key)
this.collection.col_('Updated index "%s" for document %s', key, this.id)
}
}
for (const key of this.collection.reactiveIndexed.keys()) {
updateReactiveIndex(this.collection, key)
this.collection.col_('Updated collection reactive index for key %j', key)
}
/* DEBUG */ this.collection.col_(
'Emitting event "EventCollectionDocumentUpdated"'
)
this.collection.emitEvent({
event: 'EventCollectionDocumentUpdated',
doc: this,
collection: this.collection,
})
}, 300)
/**
* The data of the document as provided by the storage provider
*/
get data() {
let data
const details = {
_createdAt: this._createdAt,
_updatedAt: this._updatedAt,
id: this.id,
}
if (this.isCloned) {
data = this.pluginData.get('internal:cloned')
} else {
data = this.collection.db.storageEngine.load(this)
}
return { ...data, ...details }
}
/**
* Set the value of a key in the doc to a specified value.
*
* **This should only be done on shallow key values**, lest you want keys like
* 'key1.key2.key3' as object keys in your data
* @param key Key to set the value of
* @param data Data to set to the afformentioned key
* @returns Returns nothing
*/
set(key: string, data: any) {
const docData = this.data
if (data === '') {
return
} else {
docData[key] = data
}
if (this.isCloned) {
this.pluginData.set('internal:cloned', docData)
} else {
this.collection.db.storageEngine.save(this, docData)
this.updateIndexes(key)
}
}
/**
* Set the root of the data object.
*
* This will completely replace the data object
* @param data Data to set
*/
setData(data: any) {
if (this.isCloned) {
this.pluginData.set('internal:cloned', data)
} else {
this.collection.db.storageEngine.save(this, data)
this.updateIndexes('root')
}
}
/**
* Object with functions for handling plugin data
*/
pluginData = {
/**
* Get the data object from a specific plugin
* @param plugin Plugin name to get data of
* @returns Data from the plugin
*/
get: (plugin: string) => {
return this._pluginData[plugin]
},
/**
* Set/replace the data object for a plugin
* @param plugin Plugin name to set data to
* @param data Data to replace the plugin data with
*/
set: (plugin: string, data: any) => {
this._pluginData[plugin] = data
},
/**
* Delete the data object of a specific plugin
* @param plugin Plugin name to delete data of
*/
delete: (plugin: string) => {
delete this._pluginData[plugin]
},
}
/**
* Delete this document from the db
*/
delete() {
try {
/* DEBUG */ this.doc_('Emitting event "EventDocumentDelete"')
this.emitEvent({
event: 'EventDocumentDelete',
doc: this,
})
/* DEBUG */ this.doc_('Splicing document from collection')
this.collection.docs.splice(
this.collection.docs.findIndex((val) => val === this),
1
)
for (const key of this.collection.reactiveIndexed.keys()) {
/* DEBUG */ this.doc_('Updating reactive index')
updateReactiveIndex(this.collection, key)
}
/* DEBUG */ this.doc_('Emitting event "EventDocumentDeleteComplete"')
this.emitEvent({
event: 'EventDocumentDeleteComplete',
id: this.id,
success: true,
})
} catch (err) {
/* DEBUG */ this.doc_('Failed to delete this document, %J', err)
/* DEBUG */ this.doc_(
'Emitting event "EventDocumentDeleteComplete" with error'
)
this.emitEvent({
event: 'EventDocumentDeleteComplete',
id: this.id,
success: false,
error: err as Error,
})
}
}
/**
* Populate down a tree of documents based on the provided MemsPL populateQuery
* @param populateQuery MemsPL population query
* @param filter Filter unspecified keys from the populated documents
* @returns Cloned version of this document
*/
populate(populateQuery: string, filter = false) {
const [populated] = populate(this.collection, [this], populateQuery, filter)
return populated
}
/**
* Populate the document with another document that matches the query.
* This will return a copy of the document and not a reference to the
* original.
*
* It's recommended you use the provided
* populate (`doc.populate(...)`) function instead.
* @param opts Options for the populate. Things like the target field and query don't have to be set
*/
customPopulate(opts: DocumentCustomPopulateOpts) {
// Debugger variable
const populate_ = this.doc_.extend('customPopulate')
// Construct a new document based on the original so as to not perform a mutation
/* DEBUG */ populate_(
'Creating identical document so as to avoid mutations'
)
const resultDoc = this.clone()
/* DEBUG */ this.doc_('Emitting event "EventDocumentCustomPopulate"')
this.emitEvent({
event: 'EventDocumentCustomPopulate',
doc: resultDoc,
opts,
})
// Destructure out variables
const {
srcField,
targetField = 'id',
targetCol,
query = [
{
key: targetField,
operation: '===',
comparison: srcField === 'id' ? this.id : this.data[srcField],
inverse: false,
},
],
destinationField = 'children',
unwind = false,
} = opts
/* DEBUG */ populate_(
'Populating document `%s` field with results from `%s.%s`',
destinationField,
targetCol,
targetField
)
/* DEBUG */ populate_('Finding child documents')
const queriedDocuments = targetCol.find({ queries: query })
// Set a specific field to the results of the query, unwinding if necessary
/* DEBUG */ populate_(
'Setting field on document to contain children. Unwind: %s',
unwind ? 'true' : 'false'
)
resultDoc.set(
destinationField,
unwind && queriedDocuments.length < 2
? queriedDocuments[0]
: queriedDocuments
)
/* DEBUG */ this.doc_(
'Emitting event "EventDocumentCustomPopulateComplete"'
)
this.emitEvent({
event: 'EventDocumentCustomPopulateComplete',
doc: resultDoc,
opts,
})
// Return the copied document and not the original
/* DEBUG */ populate_('Finished populating field, returning ghost document')
return resultDoc
}
/**
* Populate a tree of documents. It's recommended you use the provided
* populate (`doc.populate(...)`) function instead.
* @param opts Options for making a tree from the provided document
* @returns A cloned version of this doc that has the data field formatted into a tree
*/
tree(opts: DocumentTreeOpts = {}) {
opts = {
populations: [],
maxDepth: 0,
currentDepth: 1,
...opts,
}
// Debugger variable
const tree_ = this.doc_.extend('tree')
const doc = this.clone()
/* DEBUG */ this.doc_('Emitting event "EventDocumentTree"')
this.emitEvent({
event: 'EventDocumentTree',
doc,
opts,
})
if (!opts) return doc
/* DEBUG */ tree_('Number of populations: %d', opts.populations?.length)
// Map over populations array to run individual populations
opts.populations?.map((q, i) => {
if (this.collection.name === q.collection.name) {
/* DEBUG */ tree_('Running population number %d', i)
const children = q.collection.find({
queries: [
{
key: q.targetField,
operation: '===',
comparison:
q.srcField === 'id'
? this.id
: nestedKey(this.data, q.srcField),
inverse: false,
},
],
})
if (opts.maxDepth && <number>opts.currentDepth <= opts.maxDepth)
doc.set(
q.destinationField,
children.map((child: DBDoc) =>
child.tree({
...opts,
currentDepth: <number>opts.currentDepth + 1,
})
)
)
}
})
/* DEBUG */ this.doc_('Emitting event "EventDocumentTreeComplete"')
this.emitEvent({
event: 'EventDocumentTreeComplete',
doc,
opts,
})
/* DEBUG */ tree_(
'Finished running %d populations, returning result',
opts.populations?.length
)
return doc
}
/**
* Duplicate this document, making mutations to it not affect the original
*/
clone() {
/* DEBUG */ this.doc_('Emitting event "EventDocumentClone"')
this.emitEvent({
event: 'EventDocumentClone',
doc: this,
})
const cloned = new DBDoc({}, this.collection, this.id, true)
cloned.setData(cloneDeep(this.data))
cloned._createdAt = this._createdAt
cloned._updatedAt = this._updatedAt
/* DEBUG */ this.doc_('Emitting event "EventDocumentClone"')
this.emitEvent({
event: 'EventDocumentCloneComplete',
doc: cloned,
})
return cloned
}
/**
* Emit an event to the attached database
* @param event Event to emit
*/
emitEvent(event: MemsDBEvent) {
this.collection.emitEvent(event)
}
/**
* Returns a simplified view
*/
toJSON() {
return {
...this.data,
id: this.id,
_type: `(DBCollection<${this.collection.name}<DBDoc>>)`,
_indexes: Object.keys(this.indexed),
}
}
}