sedentary 0.1.0 → 0.1.1
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 +184 -0
- package/dist/cjs/index.js +599 -0
- package/dist/cjs/package.json +1 -0
- package/dist/es/db.js +177 -0
- package/dist/es/index.js +585 -0
- package/dist/types/db.d.ts +130 -0
- package/dist/types/index.d.ts +165 -0
- package/package.json +4 -18
package/dist/cjs/db.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Transaction = exports.DB = exports.Table = exports.Attribute = exports.Type = exports.EntryBase = exports.transaction = exports.size = exports.loaded = exports.base = exports.actions = void 0;
|
|
4
|
+
exports.deepCopy = deepCopy;
|
|
5
|
+
exports.deepDiff = deepDiff;
|
|
6
|
+
exports.actions = Symbol("actions");
|
|
7
|
+
exports.base = Symbol("base");
|
|
8
|
+
exports.loaded = Symbol("loaded");
|
|
9
|
+
exports.size = Symbol("size");
|
|
10
|
+
exports.transaction = Symbol("transaction");
|
|
11
|
+
class EntryBase {
|
|
12
|
+
constructor(from) {
|
|
13
|
+
if (from === "load")
|
|
14
|
+
this.preLoad();
|
|
15
|
+
else {
|
|
16
|
+
if (from)
|
|
17
|
+
Object.assign(this, from);
|
|
18
|
+
this.construct();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
construct() { }
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
+
postCommit(actions) { }
|
|
24
|
+
postLoad() { }
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
26
|
+
postRemove(deletedRecords) { }
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
28
|
+
postSave(savedRecords) { }
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
30
|
+
preCommit(actions) { }
|
|
31
|
+
preLoad() { }
|
|
32
|
+
preRemove() { }
|
|
33
|
+
preSave() { }
|
|
34
|
+
remove() {
|
|
35
|
+
return Promise.resolve(false);
|
|
36
|
+
}
|
|
37
|
+
save() {
|
|
38
|
+
return Promise.resolve(false);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.EntryBase = EntryBase;
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
43
|
+
class Type {
|
|
44
|
+
constructor(from) {
|
|
45
|
+
Object.assign(this, from);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.Type = Type;
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
50
|
+
class Attribute extends Type {
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
52
|
+
constructor(from) {
|
|
53
|
+
super(from);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.Attribute = Attribute;
|
|
57
|
+
function autoImplement() {
|
|
58
|
+
return class {
|
|
59
|
+
constructor(defaults) {
|
|
60
|
+
Object.assign(this, defaults);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
class Table extends autoImplement() {
|
|
65
|
+
findAttribute(name) {
|
|
66
|
+
return this.attributes.find(_ => _.attributeName === name);
|
|
67
|
+
}
|
|
68
|
+
findField(name) {
|
|
69
|
+
return this.attributes.find(_ => _.fieldName === name);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.Table = Table;
|
|
73
|
+
class DB {
|
|
74
|
+
constructor(log) {
|
|
75
|
+
this.tables = [];
|
|
76
|
+
this.sync = true;
|
|
77
|
+
this.log = log;
|
|
78
|
+
}
|
|
79
|
+
findTable(name) {
|
|
80
|
+
return this.tables.find(_ => _.tableName === name);
|
|
81
|
+
}
|
|
82
|
+
indexesEq(a, b) {
|
|
83
|
+
if (a.fields.length !== b.fields.length)
|
|
84
|
+
return false;
|
|
85
|
+
for (let i = 0; i < a.fields.length; ++i)
|
|
86
|
+
if (a.fields[i] !== b.fields[i])
|
|
87
|
+
return false;
|
|
88
|
+
if (a.type !== b.type)
|
|
89
|
+
return false;
|
|
90
|
+
if (a.unique !== b.unique)
|
|
91
|
+
return false;
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
async syncDataBase() {
|
|
95
|
+
for (const table of this.tables) {
|
|
96
|
+
this.sync = table.sync;
|
|
97
|
+
await this.syncTable(table);
|
|
98
|
+
const indexes = await this.dropConstraints(table);
|
|
99
|
+
await this.dropIndexes(table, indexes);
|
|
100
|
+
await this.dropFields(table);
|
|
101
|
+
await this.syncFields(table);
|
|
102
|
+
await this.syncSequence(table);
|
|
103
|
+
await this.syncConstraints(table);
|
|
104
|
+
await this.syncIndexes(table);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
syncLog(message) {
|
|
108
|
+
this.log(this.sync ? message : `NOT SYNCING: ${message}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.DB = DB;
|
|
112
|
+
class Transaction {
|
|
113
|
+
constructor(log) {
|
|
114
|
+
this.entries = [];
|
|
115
|
+
this.log = log;
|
|
116
|
+
}
|
|
117
|
+
addEntry(entry) {
|
|
118
|
+
Object.defineProperty(entry, exports.transaction, { configurable: true, value: this });
|
|
119
|
+
this.entries.push(entry);
|
|
120
|
+
}
|
|
121
|
+
clean() {
|
|
122
|
+
const { entries } = this;
|
|
123
|
+
for (const entry of entries) {
|
|
124
|
+
Object.defineProperty(entry, exports.actions, { configurable: true, value: undefined });
|
|
125
|
+
Object.defineProperty(entry, exports.transaction, { configurable: true, value: undefined });
|
|
126
|
+
}
|
|
127
|
+
this.entries = [];
|
|
128
|
+
}
|
|
129
|
+
commit() {
|
|
130
|
+
const { entries } = this;
|
|
131
|
+
for (const entry of entries)
|
|
132
|
+
if (entry[exports.actions])
|
|
133
|
+
entry.postCommit(entry[exports.actions]);
|
|
134
|
+
this.clean();
|
|
135
|
+
return Promise.resolve();
|
|
136
|
+
}
|
|
137
|
+
preCommit() {
|
|
138
|
+
const { entries } = this;
|
|
139
|
+
for (const entry of entries)
|
|
140
|
+
if (entry[exports.actions])
|
|
141
|
+
entry.preCommit(entry[exports.actions]);
|
|
142
|
+
}
|
|
143
|
+
rollback() {
|
|
144
|
+
this.clean();
|
|
145
|
+
return Promise.resolve();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.Transaction = Transaction;
|
|
149
|
+
const sortedEntries = (obj) => Object.entries(obj).sort((entryA, entryB) => (entryA[0] > entryB[0] ? -1 : 1));
|
|
150
|
+
function deepCopy(o) {
|
|
151
|
+
if (!o || typeof o !== "object")
|
|
152
|
+
return o;
|
|
153
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
154
|
+
const [ret, entries] = o instanceof Array ? [new Array(o.length), o.entries()] : [{}, Object.entries(o)];
|
|
155
|
+
for (const [k, v] of entries)
|
|
156
|
+
ret[k] = deepCopy(v);
|
|
157
|
+
return ret;
|
|
158
|
+
}
|
|
159
|
+
function deepDiff(a, b) {
|
|
160
|
+
if (typeof a !== "object")
|
|
161
|
+
return a !== b;
|
|
162
|
+
if (typeof b !== "object")
|
|
163
|
+
return true;
|
|
164
|
+
if (a === null)
|
|
165
|
+
return b !== null;
|
|
166
|
+
if (b === null)
|
|
167
|
+
return true;
|
|
168
|
+
if (a instanceof Array) {
|
|
169
|
+
if (!(b instanceof Array))
|
|
170
|
+
return true;
|
|
171
|
+
for (const [i, value] of a.entries())
|
|
172
|
+
if (deepDiff(value, b[i]))
|
|
173
|
+
return true;
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
const entriesA = sortedEntries(a);
|
|
177
|
+
const entriesB = sortedEntries(b);
|
|
178
|
+
if (entriesA.length !== entriesB.length)
|
|
179
|
+
return true;
|
|
180
|
+
for (const [i, [key, value]] of entriesA.entries())
|
|
181
|
+
if (key !== entriesB[i][0] || deepDiff(value, entriesB[i][1]))
|
|
182
|
+
return true;
|
|
183
|
+
return false;
|
|
184
|
+
}
|