sedentary 0.0.43 → 0.0.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/db.js ADDED
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Transaction = exports.DB = exports.Table = exports.Attribute = exports.Type = exports.EntryBase = exports.loaded = exports.actions = void 0;
4
+ exports.actions = Symbol("actions");
5
+ exports.loaded = Symbol("loaded");
6
+ class EntryBase {
7
+ constructor(from) {
8
+ if (from === "load")
9
+ this.preLoad();
10
+ else {
11
+ if (from)
12
+ Object.assign(this, from);
13
+ this.construct();
14
+ }
15
+ }
16
+ construct() { }
17
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
18
+ postCommit(actions) { }
19
+ postLoad() { }
20
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
+ postRemove(deletedRecords) { }
22
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
+ postSave(savedRecords) { }
24
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
25
+ preCommit(actions) { }
26
+ preLoad() { }
27
+ preRemove() { }
28
+ preSave() { }
29
+ async remove() {
30
+ return false;
31
+ }
32
+ async save() {
33
+ return false;
34
+ }
35
+ }
36
+ exports.EntryBase = EntryBase;
37
+ class Type {
38
+ constructor(from) {
39
+ Object.assign(this, from);
40
+ }
41
+ }
42
+ exports.Type = Type;
43
+ class Attribute extends Type {
44
+ constructor(from) {
45
+ super(from);
46
+ }
47
+ }
48
+ exports.Attribute = Attribute;
49
+ function autoImplement() {
50
+ return class {
51
+ constructor(defaults) {
52
+ Object.assign(this, defaults);
53
+ }
54
+ };
55
+ }
56
+ class Table extends autoImplement() {
57
+ findField(name) {
58
+ return this.attributes.filter(_ => _.fieldName === name)[0];
59
+ }
60
+ }
61
+ exports.Table = Table;
62
+ class DB {
63
+ constructor(log) {
64
+ this.tables = [];
65
+ this.sync = true;
66
+ this.log = log;
67
+ }
68
+ findTable(name) {
69
+ return this.tables.filter(_ => _.tableName === name)[0];
70
+ }
71
+ indexesEq(a, b) {
72
+ if (a.fields.length !== b.fields.length)
73
+ return false;
74
+ for (const i in a.fields)
75
+ if (a.fields[i] !== b.fields[i])
76
+ return false;
77
+ if (a.type !== b.type)
78
+ return false;
79
+ if (a.unique !== b.unique)
80
+ return false;
81
+ return true;
82
+ }
83
+ async syncDataBase() {
84
+ for (const table of this.tables) {
85
+ this.sync = table.sync;
86
+ await this.syncTable(table);
87
+ const indexes = await this.dropConstraints(table);
88
+ await this.dropIndexes(table, indexes);
89
+ await this.dropFields(table);
90
+ await this.syncFields(table);
91
+ await this.syncSequence(table);
92
+ await this.syncConstraints(table);
93
+ await this.syncIndexes(table);
94
+ }
95
+ }
96
+ syncLog(message) {
97
+ this.log(this.sync ? message : "NOT SYNCING: " + message);
98
+ }
99
+ }
100
+ exports.DB = DB;
101
+ class Transaction {
102
+ constructor(log) {
103
+ this.entries = [];
104
+ this.log = log;
105
+ }
106
+ addEntry(entry) {
107
+ Object.defineProperty(entry, "tx", { configurable: true, value: this });
108
+ this.entries.push(entry);
109
+ }
110
+ clean() {
111
+ const { entries } = this;
112
+ for (const entry of entries) {
113
+ Object.defineProperty(entry, exports.actions, { configurable: true, value: undefined });
114
+ Object.defineProperty(entry, "tx", { configurable: true, value: undefined });
115
+ }
116
+ this.entries = [];
117
+ }
118
+ async commit() {
119
+ const { entries } = this;
120
+ for (const entry of entries)
121
+ if (entry[exports.actions])
122
+ entry.postCommit(entry[exports.actions]);
123
+ this.clean();
124
+ }
125
+ preCommit() {
126
+ const { entries } = this;
127
+ for (const entry of entries)
128
+ if (entry[exports.actions])
129
+ entry.preCommit(entry[exports.actions]);
130
+ }
131
+ async rollback() {
132
+ this.clean();
133
+ }
134
+ }
135
+ exports.Transaction = Transaction;