leoric 2.2.0 → 2.2.3
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/History.md +27 -0
- package/index.js +1 -1
- package/package.json +2 -2
- package/src/bone.js +5 -4
- package/src/drivers/abstract/attribute.js +9 -1
- package/src/realm.js +1 -6
- package/src/setup_hooks.js +21 -2
package/History.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
2.2.3 / 2022-03-01
|
|
2
|
+
==================
|
|
3
|
+
|
|
4
|
+
## What's Changed
|
|
5
|
+
* fix: normalize attribute defaultValue by @cyjake in https://github.com/cyjake/leoric/pull/285
|
|
6
|
+
* fix: instance beforeUpdate hooks should not modify any Raw if there are no Raw assignment in them by @JimmyDaddy in https://github.com/cyjake/leoric/pull/283
|
|
7
|
+
|
|
8
|
+
**Full Changelog**: https://github.com/cyjake/leoric/compare/v2.2.2...v2.2.3
|
|
9
|
+
|
|
10
|
+
2.2.2 / 2022-02-28
|
|
11
|
+
==================
|
|
12
|
+
|
|
13
|
+
## What's Changed
|
|
14
|
+
* fix: tddl gives misleading information_schema.columns.table_name by @cyjake in https://github.com/cyjake/leoric/pull/284
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
**Full Changelog**: https://github.com/cyjake/leoric/compare/v2.2.1...v2.2.2
|
|
18
|
+
|
|
19
|
+
2.2.1 / 2022-02-24
|
|
20
|
+
==================
|
|
21
|
+
|
|
22
|
+
## What's Changed
|
|
23
|
+
* fix: realm.DataTypes should be invokable by @cyjake in https://github.com/cyjake/leoric/pull/282
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
**Full Changelog**: https://github.com/cyjake/leoric/compare/v2.2.0...v2.2.1
|
|
27
|
+
|
|
1
28
|
2.2.0 / 2022-02-24
|
|
2
29
|
==================
|
|
3
30
|
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "leoric",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"description": "JavaScript Object-relational mapping alchemy",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -72,6 +72,6 @@
|
|
|
72
72
|
"pg": "^8.5.1",
|
|
73
73
|
"sinon": "^10.0.0",
|
|
74
74
|
"sqlite3": "^5.0.2",
|
|
75
|
-
"typescript": "^4.
|
|
75
|
+
"typescript": "^4.6.2"
|
|
76
76
|
}
|
|
77
77
|
}
|
package/src/bone.js
CHANGED
|
@@ -199,14 +199,14 @@ class Bone {
|
|
|
199
199
|
}
|
|
200
200
|
|
|
201
201
|
/**
|
|
202
|
-
*
|
|
203
|
-
* clone instance
|
|
202
|
+
* @protected clone instance
|
|
204
203
|
* @param {Bone} target
|
|
205
204
|
* @memberof Bone
|
|
206
205
|
*/
|
|
207
206
|
_clone(target) {
|
|
208
|
-
Object.assign(
|
|
209
|
-
Object.assign(
|
|
207
|
+
this.#raw = Object.assign({}, target.getRaw());
|
|
208
|
+
this.#rawSaved = Object.assign({}, target.getRawSaved());
|
|
209
|
+
this.#rawPrevious = Object.assign({}, target.getRawPrevious());
|
|
210
210
|
}
|
|
211
211
|
|
|
212
212
|
/**
|
|
@@ -717,6 +717,7 @@ class Bone {
|
|
|
717
717
|
for (const name in attributes) {
|
|
718
718
|
const value = this.attribute(name);
|
|
719
719
|
const { defaultValue } = attributes[name];
|
|
720
|
+
// console.log(attributes[name], name, defaultValue);
|
|
720
721
|
if (value != null) {
|
|
721
722
|
data[name] = value;
|
|
722
723
|
} else if (value === undefined && defaultValue != null) {
|
|
@@ -99,15 +99,23 @@ class Attribute {
|
|
|
99
99
|
}
|
|
100
100
|
const type = createType(DataTypes, params);
|
|
101
101
|
const dataType = params.dataType || type.dataType;
|
|
102
|
+
let { defaultValue = null } = params;
|
|
103
|
+
try {
|
|
104
|
+
// normalize column defaults like `'0'` or `CURRENT_TIMESTAMP`
|
|
105
|
+
defaultValue = type.cast(type.uncast(defaultValue));
|
|
106
|
+
} catch {
|
|
107
|
+
defaultValue = null;
|
|
108
|
+
}
|
|
109
|
+
|
|
102
110
|
Object.assign(this, {
|
|
103
111
|
name,
|
|
104
112
|
columnName,
|
|
105
113
|
primaryKey: false,
|
|
106
|
-
defaultValue: null,
|
|
107
114
|
allowNull: !params.primaryKey,
|
|
108
115
|
columnType: type.toSqlString().toLowerCase(),
|
|
109
116
|
...params,
|
|
110
117
|
type,
|
|
118
|
+
defaultValue,
|
|
111
119
|
dataType,
|
|
112
120
|
jsType: findJsType(DataTypes, type, dataType),
|
|
113
121
|
});
|
package/src/realm.js
CHANGED
|
@@ -75,7 +75,7 @@ async function loadModels(Spine, models, opts) {
|
|
|
75
75
|
const schemaInfo = await Spine.driver.querySchemaInfo(database, tables);
|
|
76
76
|
|
|
77
77
|
for (const model of models) {
|
|
78
|
-
const columns = schemaInfo[model.physicTable];
|
|
78
|
+
const columns = schemaInfo[model.physicTable] || schemaInfo[model.table];
|
|
79
79
|
if (!model.attributes) initAttributes(model, columns);
|
|
80
80
|
model.load(columns);
|
|
81
81
|
Spine.models[model.name] = model;
|
|
@@ -129,11 +129,6 @@ class Realm {
|
|
|
129
129
|
this.options = Spine.options = options;
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
get DataTypes() {
|
|
133
|
-
if (!this.driver) throw new Error('database not connected yet');
|
|
134
|
-
return this.driver.DataTypes;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
132
|
define(name, attributes, opts = {}, descriptors = {}) {
|
|
138
133
|
const Model = class extends this.Bone {
|
|
139
134
|
static name = name;
|
package/src/setup_hooks.js
CHANGED
|
@@ -129,9 +129,28 @@ function addHook(target, hookName, func) {
|
|
|
129
129
|
if (useHooks && type === hookType.BEFORE) {
|
|
130
130
|
// this.change(key) or this.attributeChanged(key) should work at before update
|
|
131
131
|
if (method === 'update' && typeof arguments[0] === 'object' && !arguments[0] != null) {
|
|
132
|
-
|
|
132
|
+
const values = arguments[0];
|
|
133
|
+
const fields = arguments[1] && arguments[1].fields && arguments[1].fields.length? arguments[1].fields : [];
|
|
134
|
+
const originalRaw = {};
|
|
135
|
+
const changeRaw = {};
|
|
136
|
+
for (const name in values) {
|
|
137
|
+
if (!fields.length || fields.includes(name)) {
|
|
138
|
+
originalRaw[name] = this.attribute(name);
|
|
139
|
+
this[name] = values[name];
|
|
140
|
+
changeRaw[name] = this.attribute(name);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
await func.apply(this, args);
|
|
144
|
+
// revert instance after before hooks
|
|
145
|
+
Object.keys(originalRaw).forEach((key) => {
|
|
146
|
+
const current = this.attribute(key);
|
|
147
|
+
// raw[key] may changed in beforeUpdate hooks
|
|
148
|
+
if (current !== originalRaw[key] && current !== changeRaw[key]) return;
|
|
149
|
+
this.attribute(key, originalRaw[key]);
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
await func.apply(this, args);
|
|
133
153
|
}
|
|
134
|
-
await func.apply(this, args);
|
|
135
154
|
}
|
|
136
155
|
const res = await instanceOriginFunc.call(this, ...arguments);
|
|
137
156
|
if (useHooks && type === hookType.AFTER) {
|