-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathABDefinitionCore.js
More file actions
90 lines (84 loc) · 2.11 KB
/
ABDefinitionCore.js
File metadata and controls
90 lines (84 loc) · 2.11 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// import ABApplication from "./ABApplication"
module.exports = class ABDefinitionCore {
constructor(attributes, AB) {
this.AB = AB;
this.fromValues(attributes);
}
///
/// Static Methods
///
/// Available to the Class level object. These methods are not dependent
/// on the instance values of the Application.
///
fromValues(attributes) {
/*
{
id: uuid(),
name: 'name',
type: 'xxxxx',
json: "{json}"
}
*/
if (attributes.id) {
this.id = attributes.id;
}
this.name =
attributes?.name ||
attributes?.json?.name ||
attributes?.json?.label ||
attributes?.json?.translations?.[0]?.label ||
"";
if (!this.name) {
this.AB.notify.builder(
new Error("Attributes for definition had no 'name'"),
{
context: "ABDefinitionCore.fromValues()",
attributes,
}
);
}
this.type = attributes.type || attributes?.json?.type || "";
this.json = attributes.json || null;
}
/**
* @method toObj()
*
* properly compile the current state of this ABApplication instance
* into the values needed for saving to the DB.
*
* Most of the instance data is stored in .json field, so be sure to
* update that from all the current values of our child fields.
*
* @return {json}
*/
toObj() {
return {
id: this.id,
name: this.name,
type: this.type,
json: this.json,
};
}
/**
* @method destroy()
* destroy the current instance of ABDefinition
* Also remove it from our parent application
* @return {Promise}
*/
destroy() {
return this.AB.definitionDestroy(this.id);
}
/**
* @method save()
* persist this instance of ABObject with it's parent ABApplication
* @return {Promise}
* .resolve( {this} )
*/
async save() {
if (this.id) {
return this.AB.definitionUpdate(this.id, this.toObj());
} else {
return this.AB.definitionCreate(this.toObj());
}
}
};