leoric 2.2.2 → 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 CHANGED
@@ -1,3 +1,12 @@
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
+
1
10
  2.2.2 / 2022-02-28
2
11
  ==================
3
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leoric",
3
- "version": "2.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.4.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(this.#raw, target.getRaw());
209
- Object.assign(this.#rawSaved, target.getRawSaved());
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
  });
@@ -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
- for (const name in arguments[0]) this[name] = arguments[0][name];
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) {