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