-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecord.js
More file actions
73 lines (50 loc) · 1.38 KB
/
Record.js
File metadata and controls
73 lines (50 loc) · 1.38 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
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
'use strict';
const Bone = require('@botsocket/bone');
module.exports = class Record {
constructor(table, key, draft) {
this.table = table;
this.key = key;
this._draft = draft;
this._object = Bone.isObject(this._draft);
}
get(path) {
if (this._object) {
return Bone.get(this._draft, path);
}
return this._draft;
}
has(path) {
const value = this.get(path);
return typeof value !== 'undefined';
}
set() {
if (typeof arguments[0] === 'string' && this._object) {
const [path, value] = arguments;
this._draft = Bone.set(this._draft, path, value);
}
else {
const [value] = arguments;
this._draft = value;
this._object = Bone.isObject(this._draft);
}
return this;
}
toJSON() {
return this._draft;
}
clone(key) {
const draft = Bone.clone(this._draft, { shallow: true });
return new Record(this.table, key ?? this.key, draft);
}
async save() {
await this.table.set(this.key, this._draft);
return this;
}
delete(path) {
if (path && this._object) {
Bone.set(this._draft, path, undefined);
return this;
}
return this.table.delete(this.table.name, this.key);
}
};