sedentary 0.0.16 → 0.0.17
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/README.md +0 -4
- package/index.d.ts +8 -8
- package/index.js +54 -53
- package/lib/db.d.ts +1 -0
- package/lib/db.js +1 -1
- package/lib/minidb.js +64 -44
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -118,10 +118,6 @@ To work with the package under Windows, be sure to configure `bash.exe` as your
|
|
|
118
118
|
|
|
119
119
|
Do not hesitate to report any bug or inconsistency [@github](https://github.com/iccicci/sedentary/issues).
|
|
120
120
|
|
|
121
|
-
# ChangeLog
|
|
122
|
-
|
|
123
|
-
[ChangeLog](https://github.com/iccicci/sedentary/blob/master/CHANGELOG.md)
|
|
124
|
-
|
|
125
121
|
# Donating
|
|
126
122
|
|
|
127
123
|
If you find useful this package, please consider the opportunity to donate some satoshis to this bitcoin address:
|
package/index.d.ts
CHANGED
|
@@ -81,37 +81,37 @@ export declare class Sedentary {
|
|
|
81
81
|
end(): Promise<void>;
|
|
82
82
|
model<A extends AttributesDefinition, M extends Methods<T>, T extends Entry & {
|
|
83
83
|
id?: string;
|
|
84
|
-
} & ModelWithMetods<A, M>>(
|
|
84
|
+
} & ModelWithMetods<A, M>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
|
|
85
85
|
int8id: true;
|
|
86
86
|
methods: M;
|
|
87
87
|
}): Ancestor<A, string, T>;
|
|
88
|
-
model<A extends AttributesDefinition, K extends keyof A, M extends Methods<T>, N extends K extends keyof A ? Native<A[K]> : never, T extends Entry & ModelWithMetods<A, M>>(
|
|
88
|
+
model<A extends AttributesDefinition, K extends keyof A, M extends Methods<T>, N extends K extends keyof A ? Native<A[K]> : never, T extends Entry & ModelWithMetods<A, M>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
|
|
89
89
|
methods: M;
|
|
90
90
|
primaryKey: K;
|
|
91
91
|
}): Ancestor<A, N, T>;
|
|
92
|
-
model<A extends AttributesDefinition, M extends Methods<T>, P extends Meta<Natural, Entry>, N extends P extends Meta<infer N, Entry> ? N : never, T extends Parent<P> & ModelWithMetods<A, M>>(
|
|
92
|
+
model<A extends AttributesDefinition, M extends Methods<T>, P extends Meta<Natural, Entry>, N extends P extends Meta<infer N, Entry> ? N : never, T extends Parent<P> & ModelWithMetods<A, M>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
|
|
93
93
|
methods: M;
|
|
94
94
|
parent: P;
|
|
95
95
|
}): Ancestor<A, N, T>;
|
|
96
96
|
model<A extends AttributesDefinition, M extends Methods<T>, T extends Entry & {
|
|
97
97
|
id?: number;
|
|
98
|
-
} & ModelWithMetods<A, M>>(
|
|
98
|
+
} & ModelWithMetods<A, M>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
|
|
99
99
|
methods: M;
|
|
100
100
|
}): Ancestor<A, number, T>;
|
|
101
101
|
model<A extends AttributesDefinition, T extends Entry & {
|
|
102
102
|
id?: string;
|
|
103
|
-
} & Model<A>>(
|
|
103
|
+
} & Model<A>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
|
|
104
104
|
int8id: true;
|
|
105
105
|
}): Ancestor<A, string, T>;
|
|
106
|
-
model<A extends AttributesDefinition, K extends keyof A, N extends K extends keyof A ? Native<A[K]> : never, T extends Entry & Model<A>>(
|
|
106
|
+
model<A extends AttributesDefinition, K extends keyof A, N extends K extends keyof A ? Native<A[K]> : never, T extends Entry & Model<A>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
|
|
107
107
|
primaryKey: K;
|
|
108
108
|
}): Ancestor<A, N, T>;
|
|
109
|
-
model<A extends AttributesDefinition, P extends Meta<Natural, Entry>, N extends P extends Meta<infer N, Entry> ? N : never, T extends Parent<P> & Model<A>>(
|
|
109
|
+
model<A extends AttributesDefinition, P extends Meta<Natural, Entry>, N extends P extends Meta<infer N, Entry> ? N : never, T extends Parent<P> & Model<A>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
|
|
110
110
|
parent: P;
|
|
111
111
|
}): Ancestor<A, N, T>;
|
|
112
112
|
model<A extends AttributesDefinition, T extends Entry & {
|
|
113
113
|
id?: number;
|
|
114
|
-
} & Model<A>>(
|
|
114
|
+
} & Model<A>>(modelName: string, attributes: A, options?: BaseModelOptions<T>): Ancestor<A, number, T>;
|
|
115
115
|
checkSize(size: number, message: string): number;
|
|
116
116
|
}
|
|
117
117
|
export declare const Package: typeof Sedentary;
|
package/index.js
CHANGED
|
@@ -74,103 +74,103 @@ class Sedentary {
|
|
|
74
74
|
await this.db.end();
|
|
75
75
|
this.log("Connection closed");
|
|
76
76
|
}
|
|
77
|
-
model(
|
|
78
|
-
if (typeof
|
|
77
|
+
model(modelName, attributes, options) {
|
|
78
|
+
if (typeof modelName !== "string")
|
|
79
79
|
throw new Error("Sedentary.model: 'name' argument: Wrong type, expected 'string'");
|
|
80
|
-
if (this.models[
|
|
81
|
-
throw new Error(`Sedentary.model: '${
|
|
80
|
+
if (this.models[modelName])
|
|
81
|
+
throw new Error(`Sedentary.model: '${modelName}' model: Model already defined`);
|
|
82
82
|
if (!attributes)
|
|
83
83
|
attributes = {};
|
|
84
84
|
if (!(attributes instanceof Object))
|
|
85
|
-
throw new Error(`Sedentary.model: '${
|
|
85
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'attributes' argument: Wrong type, expected 'Object'`);
|
|
86
86
|
if (!options)
|
|
87
87
|
options = {};
|
|
88
88
|
if (!(options instanceof Object))
|
|
89
|
-
throw new Error(`Sedentary.model: '${
|
|
89
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'options' argument: Wrong type, expected 'Object'`);
|
|
90
90
|
for (const k in options)
|
|
91
91
|
if (!allowedOption.includes(k))
|
|
92
|
-
throw new Error(`Sedentary.model: '${
|
|
92
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'options' argument: Unknown '${k}' option`);
|
|
93
93
|
if (options.int8id && options.parent)
|
|
94
|
-
throw new Error(`Sedentary.model: '${
|
|
94
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'int8id' and 'parent' options conflict each other`);
|
|
95
95
|
if (options.int8id && options.primaryKey)
|
|
96
|
-
throw new Error(`Sedentary.model: '${
|
|
96
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'int8id' and 'primaryKey' options conflict each other`);
|
|
97
97
|
if (options.parent && options.primaryKey)
|
|
98
|
-
throw new Error(`Sedentary.model: '${
|
|
98
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'parent' and 'primaryKey' options conflict each other`);
|
|
99
99
|
let autoIncrement = true;
|
|
100
|
-
const { indexes, int8id, parent, primaryKey, sync, tableName } = Object.assign({ sync: this.sync, tableName:
|
|
100
|
+
const { indexes, int8id, parent, primaryKey, sync, tableName } = Object.assign({ sync: this.sync, tableName: modelName }, options);
|
|
101
101
|
let { methods } = options;
|
|
102
102
|
let aarray = int8id
|
|
103
|
-
? [new db_1.Attribute(Object.assign(Object.assign({}, this.INT8()), { attributeName: "id", fieldName: "id", notNull: true, tableName, unique: true }))]
|
|
104
|
-
: [new db_1.Attribute(Object.assign(Object.assign({}, this.INT(4)), { attributeName: "id", fieldName: "id", notNull: true, tableName, unique: true }))];
|
|
103
|
+
? [new db_1.Attribute(Object.assign(Object.assign({}, this.INT8()), { attributeName: "id", fieldName: "id", modelName, notNull: true, tableName, unique: true }))]
|
|
104
|
+
: [new db_1.Attribute(Object.assign(Object.assign({}, this.INT(4)), { attributeName: "id", fieldName: "id", modelName, notNull: true, tableName, unique: true }))];
|
|
105
105
|
let constraints = [{ attribute: aarray[0], constraintName: `${tableName}_id_unique`, type: "u" }];
|
|
106
106
|
const iarray = [];
|
|
107
107
|
const pk = aarray[0];
|
|
108
108
|
if (methods && !(methods instanceof Object))
|
|
109
|
-
throw new Error(`Sedentary.model: '${
|
|
109
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'methods' option: Wrong type, expected 'Object'`);
|
|
110
110
|
if (parent) {
|
|
111
|
-
methods = (methods ? Object.assign(Object.assign({}, (parent.methods || {})), methods) : parent.methods);
|
|
112
111
|
try {
|
|
113
112
|
if (!parent.isModel())
|
|
114
113
|
throw new Error();
|
|
115
114
|
}
|
|
116
115
|
catch (e) {
|
|
117
|
-
throw new Error(`Sedentary.model: '${
|
|
116
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'parent' option: Wrong type, expected 'Model'`);
|
|
118
117
|
}
|
|
118
|
+
methods = (methods ? Object.assign(Object.assign({}, (parent.methods || {})), methods) : parent.methods);
|
|
119
119
|
}
|
|
120
120
|
if (primaryKey && typeof primaryKey !== "string")
|
|
121
|
-
throw new Error(`Sedentary.model: '${
|
|
121
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Wrong type, expected 'string'`);
|
|
122
122
|
if (primaryKey && !Object.keys(attributes).includes(primaryKey))
|
|
123
|
-
throw new Error(`Sedentary.model: '${
|
|
123
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Attribute '${primaryKey}' does not exists`);
|
|
124
124
|
if (parent || primaryKey) {
|
|
125
125
|
autoIncrement = false;
|
|
126
126
|
aarray = [];
|
|
127
127
|
constraints = [];
|
|
128
128
|
}
|
|
129
|
-
for (const attributeName
|
|
129
|
+
for (const attributeName of Object.keys(attributes).sort()) {
|
|
130
130
|
if (reservedNames.includes(attributeName))
|
|
131
|
-
throw new Error(`Sedentary.model: '${
|
|
131
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: Reserved name`);
|
|
132
132
|
const call = (defaultValue, fieldName, notNull, unique, func, message1, message2) => {
|
|
133
133
|
if (func === this.FKEY)
|
|
134
134
|
throw new Error(`${message1} 'this.FKEY' can't be used directly`);
|
|
135
135
|
if (func !== this.DATETIME && func !== this.INT && func !== this.INT8 && func !== this.VARCHAR)
|
|
136
136
|
throw new Error(`${message1} ${message2}`);
|
|
137
|
-
return new db_1.Attribute(Object.assign({ attributeName, defaultValue, fieldName, notNull, tableName, unique }, func()));
|
|
137
|
+
return new db_1.Attribute(Object.assign({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique }, func()));
|
|
138
138
|
};
|
|
139
139
|
const attributeDefinition = attributes[attributeName];
|
|
140
140
|
let { base, defaultValue, fieldName, foreignKey, notNull, size, type, unique } = (() => {
|
|
141
141
|
const ret = (() => {
|
|
142
142
|
if (attributeDefinition instanceof db_1.Type)
|
|
143
|
-
return new db_1.Attribute(Object.assign({ attributeName, fieldName: attributeName, notNull: false, tableName }, attributeDefinition));
|
|
143
|
+
return new db_1.Attribute(Object.assign({ attributeName, fieldName: attributeName, modelName, notNull: false, tableName }, attributeDefinition));
|
|
144
144
|
if (attributeDefinition instanceof Function)
|
|
145
|
-
return call(undefined, attributeName, false, false, attributeDefinition, `Sedentary.model: '${
|
|
145
|
+
return call(undefined, attributeName, false, false, attributeDefinition, `Sedentary.model: '${modelName}' model: '${attributeName}' attribute:`, "Wrong type, expected 'Attribute'");
|
|
146
146
|
if (!(attributeDefinition instanceof Object))
|
|
147
|
-
throw new Error(`Sedentary.model: '${
|
|
147
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: Wrong attribute type, expected 'Attribute'`);
|
|
148
148
|
const attributeDefaults = Object.assign({ defaultValue: undefined, fieldName: attributeName, notNull: false, unique: false }, attributeDefinition);
|
|
149
149
|
const { defaultValue, fieldName, notNull, unique, type } = attributeDefaults;
|
|
150
150
|
if (defaultValue === null)
|
|
151
|
-
throw new Error(`Sedentary.model: '${
|
|
151
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Does 'null' default value really makes sense?`);
|
|
152
152
|
if (typeof fieldName !== "string")
|
|
153
|
-
throw new Error(`Sedentary.model: '${
|
|
153
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'fieldName' option: Wrong type, expected 'string'`);
|
|
154
154
|
if (typeof notNull !== "boolean")
|
|
155
|
-
throw new Error(`Sedentary.model: '${
|
|
155
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'notNull' option: Wrong type, expected 'boolean'`);
|
|
156
156
|
if (typeof unique !== "boolean")
|
|
157
|
-
throw new Error(`Sedentary.model: '${
|
|
157
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'unique' option: Wrong type, expected 'boolean'`);
|
|
158
158
|
if (type === undefined)
|
|
159
|
-
throw new Error(`Sedentary.model: '${
|
|
159
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: Missing 'type' option`);
|
|
160
160
|
if (type instanceof db_1.Type)
|
|
161
|
-
return new db_1.Attribute(Object.assign({ attributeName, defaultValue, fieldName, notNull, tableName, unique }, type));
|
|
161
|
+
return new db_1.Attribute(Object.assign({ attributeName, defaultValue, fieldName, modelName, notNull, tableName, unique }, type));
|
|
162
162
|
if (type instanceof Function)
|
|
163
|
-
return call(defaultValue, fieldName, notNull, unique, type, `Sedentary.model: '${
|
|
164
|
-
throw new Error(`Sedentary.model: '${
|
|
163
|
+
return call(defaultValue, fieldName, notNull, unique, type, `Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'type' option:`, "Wrong type, expected 'Type'");
|
|
164
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'type' option: Wrong type, expected 'Type'`);
|
|
165
165
|
})();
|
|
166
166
|
const { base, defaultValue } = ret;
|
|
167
167
|
if (defaultValue !== undefined) {
|
|
168
168
|
if (base === Date && !(defaultValue instanceof Date))
|
|
169
|
-
throw new Error(`Sedentary.model: '${
|
|
169
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'Date'`);
|
|
170
170
|
if (base === Number && typeof defaultValue !== "number")
|
|
171
|
-
throw new Error(`Sedentary.model: '${
|
|
171
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'number'`);
|
|
172
172
|
if (base === String && typeof defaultValue !== "string")
|
|
173
|
-
throw new Error(`Sedentary.model: '${
|
|
173
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${attributeName}' attribute: 'defaultValue' option: Wrong type, expected 'string'`);
|
|
174
174
|
}
|
|
175
175
|
return ret;
|
|
176
176
|
})();
|
|
@@ -180,7 +180,7 @@ class Sedentary {
|
|
|
180
180
|
}
|
|
181
181
|
if (defaultValue)
|
|
182
182
|
notNull = true;
|
|
183
|
-
const attribute = new db_1.Attribute({ attributeName, base, defaultValue, fieldName, foreignKey, notNull, size, tableName, type, unique });
|
|
183
|
+
const attribute = new db_1.Attribute({ attributeName, base, defaultValue, fieldName, foreignKey, modelName, notNull, size, tableName, type, unique });
|
|
184
184
|
aarray.push(attribute);
|
|
185
185
|
if (foreignKey)
|
|
186
186
|
constraints.push({ attribute, constraintName: `fkey_${fieldName}_${foreignKey.tableName}_${foreignKey.fieldName}`, type: "f" });
|
|
@@ -190,16 +190,16 @@ class Sedentary {
|
|
|
190
190
|
if (indexes) {
|
|
191
191
|
const flds = attributes;
|
|
192
192
|
if (!(indexes instanceof Object))
|
|
193
|
-
throw new Error(`Sedentary.model: '${
|
|
193
|
+
throw new Error(`Sedentary.model: '${modelName}' model: 'indexes' option: Wrong type, expected 'Object'`);
|
|
194
194
|
for (const indexName in indexes) {
|
|
195
195
|
if (aarray.filter(({ fieldName, unique }) => unique && `${tableName}_${fieldName}_unique` === indexName).length !== 0)
|
|
196
|
-
throw new Error(`Sedentary.model: '${
|
|
196
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: index name already inferred by the unique constraint on an attribute`);
|
|
197
197
|
const idx = indexes[indexName];
|
|
198
198
|
const checkAttribute = (attribute, l) => {
|
|
199
199
|
if (typeof attribute !== "string")
|
|
200
|
-
throw new Error(`Sedentary.model: '${
|
|
200
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: #${l + 1} attribute: Wrong type, expected 'string'`);
|
|
201
201
|
if (!(attribute in flds))
|
|
202
|
-
throw new Error(`Sedentary.model: '${
|
|
202
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: #${l + 1} attribute: Unknown attribute '${attribute}'`);
|
|
203
203
|
};
|
|
204
204
|
let attributes;
|
|
205
205
|
let type = "btree";
|
|
@@ -215,10 +215,10 @@ class Sedentary {
|
|
|
215
215
|
else if (idx instanceof Object) {
|
|
216
216
|
for (const k in idx)
|
|
217
217
|
if (!["attributes", "type", "unique"].includes(k))
|
|
218
|
-
throw new Error(`Sedentary.model: '${
|
|
218
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: Unknown index option '${k}'`);
|
|
219
219
|
({ attributes, type, unique } = Object.assign({ type: "btree", unique: false }, idx));
|
|
220
220
|
if (!attributes)
|
|
221
|
-
throw new Error(`Sedentary.model: '${
|
|
221
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: Missing 'attributes' option`);
|
|
222
222
|
if (attributes instanceof Array)
|
|
223
223
|
attributes.forEach(checkAttribute);
|
|
224
224
|
else if (typeof attributes === "string") {
|
|
@@ -226,21 +226,21 @@ class Sedentary {
|
|
|
226
226
|
attributes = [attributes];
|
|
227
227
|
}
|
|
228
228
|
else
|
|
229
|
-
throw new Error(`Sedentary.model: '${
|
|
229
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: 'attributes' option: Wrong type, expected 'FieldNames'`);
|
|
230
230
|
if (typeof type !== "string")
|
|
231
|
-
throw new Error(`Sedentary.model: '${
|
|
231
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: 'type' option: Wrong type, expected 'string'`);
|
|
232
232
|
if (!["btree", "hash"].includes(type))
|
|
233
|
-
throw new Error(`Sedentary.model: '${
|
|
233
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: 'type' option: Wrong value, expected 'btree' or 'hash'`);
|
|
234
234
|
if (typeof unique !== "boolean")
|
|
235
|
-
throw new Error(`Sedentary.model: '${
|
|
235
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: 'unique' option: Wrong type, expected 'boolean'`);
|
|
236
236
|
}
|
|
237
237
|
else
|
|
238
|
-
throw new Error(`Sedentary.model: '${
|
|
238
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${indexName}' index: Wrong type, expected 'Object'`);
|
|
239
239
|
iarray.push({ fields: attributes, indexName, type, unique });
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
this.db.tables.push(new db_1.Table({ autoIncrement, constraints, attributes: aarray, indexes: iarray, parent, sync, tableName }));
|
|
243
|
-
this.models[
|
|
243
|
+
this.models[modelName] = true;
|
|
244
244
|
const init = parent
|
|
245
245
|
? options.init
|
|
246
246
|
? function () {
|
|
@@ -260,7 +260,7 @@ class Sedentary {
|
|
|
260
260
|
save() {
|
|
261
261
|
return new Promise((resolve, reject) => {
|
|
262
262
|
const save = () => reject(new Error("eh no"));
|
|
263
|
-
Object.defineProperty(save, "name", { value:
|
|
263
|
+
Object.defineProperty(save, "name", { value: modelName + ".save" });
|
|
264
264
|
setTimeout(save, 10);
|
|
265
265
|
});
|
|
266
266
|
}
|
|
@@ -270,18 +270,18 @@ class Sedentary {
|
|
|
270
270
|
return resolve([new Class()]);
|
|
271
271
|
reject(new Error("boh"));
|
|
272
272
|
}, 10));
|
|
273
|
-
Object.defineProperty(load, "name", { value:
|
|
273
|
+
Object.defineProperty(load, "name", { value: modelName + ".load" });
|
|
274
274
|
const meta = { base: Number, type: "meta", tableName, primaryKey, init, methods };
|
|
275
275
|
Object.defineProperty(Class, "isModel", { value: () => true });
|
|
276
276
|
Object.defineProperty(Class, "load", { value: load });
|
|
277
277
|
Object.defineProperty(Class, "meta", { value: new db_1.Meta(meta) });
|
|
278
|
-
Object.defineProperty(Class, "name", { value:
|
|
279
|
-
Object.defineProperty(Class.prototype.save, "name", { value:
|
|
278
|
+
Object.defineProperty(Class, "name", { value: modelName });
|
|
279
|
+
Object.defineProperty(Class.prototype.save, "name", { value: modelName + ".save" });
|
|
280
280
|
Object.assign(Class, new db_1.Meta(meta));
|
|
281
281
|
Object.assign(Class.prototype, methods);
|
|
282
282
|
for (const attribute of aarray)
|
|
283
283
|
Object.defineProperty(Class, attribute.attributeName, { value: attribute });
|
|
284
|
-
for (const key of ["attributeName", "base", "fieldName", "size", "type", "unique"])
|
|
284
|
+
for (const key of ["attributeName", "base", "fieldName", "modelName", "size", "type", "unique"])
|
|
285
285
|
Object.defineProperty(Class, key, { value: pk[key] });
|
|
286
286
|
return Class;
|
|
287
287
|
}
|
|
@@ -323,6 +323,7 @@ class Super extends db.model("Super", {
|
|
|
323
323
|
this.num = 0;
|
|
324
324
|
const a = this.nLoad ? await this.nLoad() : { prova: () => null };
|
|
325
325
|
a.prova();
|
|
326
|
+
this.prova();
|
|
326
327
|
}
|
|
327
328
|
}) {
|
|
328
329
|
}
|
package/lib/db.d.ts
CHANGED
package/lib/db.js
CHANGED
package/lib/minidb.js
CHANGED
|
@@ -21,40 +21,44 @@ class MiniDB extends db_1.DB {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
async dropConstraints(table) {
|
|
24
|
-
const { constraints } = this.body.tables[table.tableName];
|
|
24
|
+
const { constraints } = this.body.tables[table.tableName] || { constraints: { f: {}, u: {} } };
|
|
25
25
|
for (const constraint of Object.keys(constraints.f).sort()) {
|
|
26
26
|
if (!table.constraints.filter(({ constraintName, type }) => constraintName === constraint && type === "f").length) {
|
|
27
|
-
this.
|
|
28
|
-
|
|
27
|
+
this.syncLog(`'${table.tableName}': Removing foreign key: '${constraint}'`);
|
|
28
|
+
if (this.sync)
|
|
29
|
+
delete constraints.f[constraint];
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
for (const constraint of Object.keys(constraints.u).sort()) {
|
|
32
33
|
if (!table.constraints.filter(({ constraintName, type }) => constraintName === constraint && type === "u").length) {
|
|
33
|
-
this.
|
|
34
|
-
|
|
34
|
+
this.syncLog(`'${table.tableName}': Removing unique constraint from field: '${constraints.u[constraint].fieldName}'`);
|
|
35
|
+
if (this.sync)
|
|
36
|
+
delete constraints.u[constraint];
|
|
35
37
|
}
|
|
36
38
|
}
|
|
37
39
|
await this.save();
|
|
38
40
|
return [];
|
|
39
41
|
}
|
|
40
42
|
async dropFields(table) {
|
|
41
|
-
const { fields } = this.body.tables[table.tableName];
|
|
42
|
-
for (const attribute
|
|
43
|
+
const { fields } = this.body.tables[table.tableName] || { fields: {} };
|
|
44
|
+
for (const attribute of Object.keys(fields).sort()) {
|
|
43
45
|
if (!table.findField(attribute)) {
|
|
44
|
-
this.
|
|
45
|
-
|
|
46
|
+
this.syncLog(`'${table.tableName}': Removing field: '${attribute}'`);
|
|
47
|
+
if (this.sync)
|
|
48
|
+
delete fields[attribute];
|
|
46
49
|
}
|
|
47
50
|
}
|
|
48
51
|
await this.save();
|
|
49
52
|
}
|
|
50
53
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
51
54
|
async dropIndexes(table, constraintIndexes) {
|
|
52
|
-
const { indexes } = this.body.tables[table.tableName];
|
|
53
|
-
for (const name
|
|
55
|
+
const { indexes } = this.body.tables[table.tableName] || { indexes: {} };
|
|
56
|
+
for (const name of Object.keys(indexes).sort()) {
|
|
54
57
|
const index = table.indexes.filter(_ => _.indexName === name);
|
|
55
58
|
if (index.length === 0 || !this.indexesEq(indexes[name], index[0])) {
|
|
56
|
-
this.
|
|
57
|
-
|
|
59
|
+
this.syncLog(`'${table.tableName}': Removing index: '${name}'`);
|
|
60
|
+
if (this.sync)
|
|
61
|
+
delete indexes[name];
|
|
58
62
|
}
|
|
59
63
|
}
|
|
60
64
|
await this.save();
|
|
@@ -64,19 +68,21 @@ class MiniDB extends db_1.DB {
|
|
|
64
68
|
await writeFile(this.file, JSON.stringify(this.body));
|
|
65
69
|
}
|
|
66
70
|
async syncConstraints(table) {
|
|
67
|
-
const { constraints } = this.body.tables[table.tableName];
|
|
71
|
+
const { constraints } = this.body.tables[table.tableName] || { constraints: { f: {}, u: {} } };
|
|
68
72
|
for (const constraint of table.constraints) {
|
|
69
73
|
const { constraintName, type } = constraint;
|
|
70
74
|
const { fieldName, foreignKey } = constraint.attribute;
|
|
71
75
|
if (!constraints[type][constraintName]) {
|
|
72
76
|
switch (type) {
|
|
73
77
|
case "f":
|
|
74
|
-
this.
|
|
75
|
-
|
|
78
|
+
this.syncLog(`'${table.tableName}': Adding foreign key '${constraint.constraintName}' on field: '${fieldName}' references '${foreignKey.tableName}(${foreignKey.fieldName})'`);
|
|
79
|
+
if (this.sync)
|
|
80
|
+
constraints[type][constraintName] = { fieldName, toField: foreignKey.fieldName, toTable: foreignKey.tableName };
|
|
76
81
|
break;
|
|
77
82
|
case "u":
|
|
78
|
-
this.
|
|
79
|
-
|
|
83
|
+
this.syncLog(`'${table.tableName}': Adding unique constraint on field: '${fieldName}'`);
|
|
84
|
+
if (this.sync)
|
|
85
|
+
constraints[type][constraintName] = { fieldName };
|
|
80
86
|
break;
|
|
81
87
|
}
|
|
82
88
|
}
|
|
@@ -84,52 +90,62 @@ class MiniDB extends db_1.DB {
|
|
|
84
90
|
await this.save();
|
|
85
91
|
}
|
|
86
92
|
async syncIndexes(table) {
|
|
87
|
-
const { indexes } = this.body.tables[table.tableName];
|
|
93
|
+
const { indexes } = this.body.tables[table.tableName] || { indexes: {} };
|
|
88
94
|
for (const index of table.indexes) {
|
|
89
95
|
const { indexName } = index;
|
|
90
96
|
if (!(indexName in indexes)) {
|
|
91
|
-
this.
|
|
92
|
-
|
|
97
|
+
this.syncLog(`'${table.tableName}': Adding index: '${indexName}' on (${index.fields.map(_ => `'${_}'`).join(", ")}) type '${index.type}'${index.unique ? " unique" : ""}`);
|
|
98
|
+
if (this.sync)
|
|
99
|
+
indexes[indexName] = index;
|
|
93
100
|
}
|
|
94
101
|
}
|
|
95
102
|
await this.save();
|
|
96
103
|
}
|
|
97
104
|
async syncFields(table) {
|
|
98
105
|
for (const attribute of table.attributes) {
|
|
99
|
-
const { fields } = this.body.tables[table.tableName];
|
|
106
|
+
const { fields } = this.body.tables[table.tableName] || { fields: {} };
|
|
100
107
|
const { defaultValue, fieldName, notNull, size, type } = attribute;
|
|
101
108
|
let field = fields[fieldName];
|
|
102
109
|
if (!field) {
|
|
103
|
-
this.
|
|
104
|
-
|
|
110
|
+
this.syncLog(`'${table.tableName}': Adding field: '${fieldName}' '${type}' '${size || ""}'`);
|
|
111
|
+
if (this.sync)
|
|
112
|
+
field = fields[fieldName] = { size, type };
|
|
113
|
+
else
|
|
114
|
+
field = {};
|
|
105
115
|
}
|
|
106
116
|
if (field.size !== size || field.type !== type) {
|
|
107
|
-
this.
|
|
108
|
-
|
|
117
|
+
this.syncLog(`'${table.tableName}': Changing field type: '${fieldName}' '${type}' '${size || ""}'`);
|
|
118
|
+
if (this.sync)
|
|
119
|
+
field = fields[fieldName] = Object.assign(Object.assign({}, field), { size, type });
|
|
109
120
|
}
|
|
110
121
|
if (field.default) {
|
|
111
122
|
if (!defaultValue) {
|
|
112
|
-
this.
|
|
113
|
-
|
|
123
|
+
this.syncLog(`'${table.tableName}': Dropping default value for field: '${fieldName}'`);
|
|
124
|
+
if (this.sync)
|
|
125
|
+
delete field.default;
|
|
114
126
|
}
|
|
115
127
|
else if (field.default !== defaultValue) {
|
|
116
|
-
this.
|
|
117
|
-
|
|
128
|
+
this.syncLog(`'${table.tableName}': Changing default value to '${defaultValue}' for field: '${fieldName}'`);
|
|
129
|
+
if (this.sync)
|
|
130
|
+
field.default = defaultValue;
|
|
118
131
|
}
|
|
119
132
|
}
|
|
120
133
|
else if (defaultValue) {
|
|
121
|
-
this.
|
|
122
|
-
|
|
134
|
+
this.syncLog(`'${table.tableName}': Setting default value '${defaultValue instanceof Date ? defaultValue.toISOString() : defaultValue}' for field: '${fieldName}'`);
|
|
135
|
+
if (this.sync)
|
|
136
|
+
field.default = defaultValue;
|
|
123
137
|
}
|
|
124
138
|
if (field.notNull) {
|
|
125
139
|
if (!notNull) {
|
|
126
|
-
this.
|
|
127
|
-
|
|
140
|
+
this.syncLog(`'${table.tableName}': Dropping not null for field: '${fieldName}'`);
|
|
141
|
+
if (this.sync)
|
|
142
|
+
delete field.notNull;
|
|
128
143
|
}
|
|
129
144
|
}
|
|
130
145
|
else if (notNull) {
|
|
131
|
-
this.
|
|
132
|
-
|
|
146
|
+
this.syncLog(`'${table.tableName}': Setting not null for field: '${fieldName}'`);
|
|
147
|
+
if (this.sync)
|
|
148
|
+
field.notNull = true;
|
|
133
149
|
}
|
|
134
150
|
}
|
|
135
151
|
await this.save();
|
|
@@ -145,20 +161,24 @@ class MiniDB extends db_1.DB {
|
|
|
145
161
|
}
|
|
146
162
|
else if (!this.body.tables[table.tableName].parent)
|
|
147
163
|
return;
|
|
148
|
-
this.
|
|
149
|
-
|
|
164
|
+
this.syncLog(`Removing table: '${table.tableName}'`);
|
|
165
|
+
if (this.sync)
|
|
166
|
+
delete this.body.tables[table.tableName];
|
|
150
167
|
})();
|
|
151
168
|
}
|
|
152
169
|
if (!this.body.tables[table.tableName]) {
|
|
153
|
-
this.
|
|
154
|
-
this.
|
|
170
|
+
this.syncLog(`Adding table: '${table.tableName}'`);
|
|
171
|
+
if (this.sync)
|
|
172
|
+
this.body.tables[table.tableName] = { constraints: { f: {}, u: {} }, fields: {}, indexes: {} };
|
|
155
173
|
if (table.parent) {
|
|
156
|
-
this.
|
|
157
|
-
this.
|
|
174
|
+
this.syncLog(`Setting parent: '${table.parent.tableName}' - to table: '${table.tableName}'`);
|
|
175
|
+
if (this.sync)
|
|
176
|
+
this.body.tables[table.tableName].parent = table.parent.tableName;
|
|
158
177
|
}
|
|
159
178
|
if (table.autoIncrement && !this.body.next[table.tableName]) {
|
|
160
|
-
this.
|
|
161
|
-
this.
|
|
179
|
+
this.syncLog(`Setting auto increment: '${table.tableName}'`);
|
|
180
|
+
if (this.sync)
|
|
181
|
+
this.body.next[table.tableName] = 1;
|
|
162
182
|
}
|
|
163
183
|
}
|
|
164
184
|
await this.save();
|
package/package.json
CHANGED