@nocobase/database 0.7.5-alpha.1 → 0.7.6-alpha.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/lib/database.js CHANGED
@@ -285,6 +285,28 @@ class Database extends _events().EventEmitter {
285
285
  opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`;
286
286
  }
287
287
  });
288
+ this.on('afterCreate', /*#__PURE__*/function () {
289
+ var _ref = _asyncToGenerator(function* (instance) {
290
+ var _instance$toChangedWi;
291
+
292
+ instance === null || instance === void 0 ? void 0 : (_instance$toChangedWi = instance.toChangedWithAssociations) === null || _instance$toChangedWi === void 0 ? void 0 : _instance$toChangedWi.call(instance);
293
+ });
294
+
295
+ return function (_x) {
296
+ return _ref.apply(this, arguments);
297
+ };
298
+ }());
299
+ this.on('afterUpdate', /*#__PURE__*/function () {
300
+ var _ref2 = _asyncToGenerator(function* (instance) {
301
+ var _instance$toChangedWi2;
302
+
303
+ instance === null || instance === void 0 ? void 0 : (_instance$toChangedWi2 = instance.toChangedWithAssociations) === null || _instance$toChangedWi2 === void 0 ? void 0 : _instance$toChangedWi2.call(instance);
304
+ });
305
+
306
+ return function (_x2) {
307
+ return _ref2.apply(this, arguments);
308
+ };
309
+ }());
288
310
  }
289
311
 
290
312
  addMigration(item) {
@@ -552,7 +574,7 @@ class Database extends _events().EventEmitter {
552
574
  let count = 1;
553
575
 
554
576
  const authenticate = /*#__PURE__*/function () {
555
- var _ref = _asyncToGenerator(function* () {
577
+ var _ref3 = _asyncToGenerator(function* () {
556
578
  try {
557
579
  yield _this5.sequelize.authenticate(others);
558
580
  console.log('Connection has been established successfully.');
@@ -570,7 +592,7 @@ class Database extends _events().EventEmitter {
570
592
  });
571
593
 
572
594
  return function authenticate() {
573
- return _ref.apply(this, arguments);
595
+ return _ref3.apply(this, arguments);
574
596
  };
575
597
  }();
576
598
 
package/lib/model.d.ts CHANGED
@@ -7,6 +7,11 @@ interface IModel {
7
7
  export declare class Model<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> extends SequelizeModel<TModelAttributes, TCreationAttributes> implements IModel {
8
8
  static database: Database;
9
9
  static collection: Collection;
10
+ protected _changedWithAssociations: Set<unknown>;
11
+ protected _previousDataValuesWithAssociations: {};
12
+ toChangedWithAssociations(): void;
13
+ changedWithAssociations(key?: string, value?: any): boolean | this | unknown[];
14
+ clearChangedWithAssociations(): void;
10
15
  toJSON<T extends TModelAttributes>(): T;
11
16
  private hiddenObjKey;
12
17
  private sortAssociations;
package/lib/model.js CHANGED
@@ -5,20 +5,20 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.Model = void 0;
7
7
 
8
- function _sequelize() {
9
- const data = require("sequelize");
8
+ function _lodash() {
9
+ const data = _interopRequireDefault(require("lodash"));
10
10
 
11
- _sequelize = function _sequelize() {
11
+ _lodash = function _lodash() {
12
12
  return data;
13
13
  };
14
14
 
15
15
  return data;
16
16
  }
17
17
 
18
- function _lodash() {
19
- const data = _interopRequireDefault(require("lodash"));
18
+ function _sequelize() {
19
+ const data = require("sequelize");
20
20
 
21
- _lodash = function _lodash() {
21
+ _sequelize = function _sequelize() {
22
22
  return data;
23
23
  };
24
24
 
@@ -28,6 +28,48 @@ function _lodash() {
28
28
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
29
 
30
30
  class Model extends _sequelize().Model {
31
+ constructor(...args) {
32
+ super(...args);
33
+ this._changedWithAssociations = new Set();
34
+ this._previousDataValuesWithAssociations = {};
35
+ }
36
+
37
+ // TODO
38
+ toChangedWithAssociations() {
39
+ // @ts-ignore
40
+ this._changedWithAssociations = new Set([...this._changedWithAssociations, ...this._changed]); // @ts-ignore
41
+
42
+ this._previousDataValuesWithAssociations = this._previousDataValues;
43
+ }
44
+
45
+ changedWithAssociations(key, value) {
46
+ if (key === undefined) {
47
+ if (this._changedWithAssociations.size > 0) {
48
+ return Array.from(this._changedWithAssociations);
49
+ }
50
+
51
+ return false;
52
+ }
53
+
54
+ if (value === true) {
55
+ this._changedWithAssociations.add(key);
56
+
57
+ return this;
58
+ }
59
+
60
+ if (value === false) {
61
+ this._changedWithAssociations.delete(key);
62
+
63
+ return this;
64
+ }
65
+
66
+ return this._changedWithAssociations.has(key);
67
+ }
68
+
69
+ clearChangedWithAssociations() {
70
+ this._changedWithAssociations = new Set();
71
+ }
72
+
31
73
  toJSON() {
32
74
  const handleObj = (obj, options) => {
33
75
  const handles = [data => {
@@ -30,6 +30,7 @@ export declare type Values = any;
30
30
  export interface CountOptions extends Omit<SequelizeCreateOptions, 'distinct' | 'where' | 'include'>, Transactionable {
31
31
  fields?: Fields;
32
32
  filter?: Filter;
33
+ context?: any;
33
34
  }
34
35
  export interface FilterByTk {
35
36
  filterByTk?: TargetKey;
package/lib/repository.js CHANGED
@@ -294,6 +294,7 @@ class Repository {
294
294
  yield _this5.database.emitAsync(`${_this5.collection.name}.afterSaveWithAssociations`, instance, _objectSpread(_objectSpread({}, options), {}, {
295
295
  transaction
296
296
  }));
297
+ instance.clearChangedWithAssociations();
297
298
  }
298
299
 
299
300
  return instance;
@@ -390,6 +391,7 @@ class Repository {
390
391
  yield _this7.database.emitAsync(`${_this7.collection.name}.afterSaveWithAssociations`, instance, _objectSpread(_objectSpread({}, options), {}, {
391
392
  transaction
392
393
  }));
394
+ instance.clearChangedWithAssociations();
393
395
  }
394
396
  } catch (err) {
395
397
  _iterator3.e(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "0.7.5-alpha.1",
3
+ "version": "0.7.6-alpha.2",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -12,7 +12,7 @@
12
12
  }
13
13
  ],
14
14
  "dependencies": {
15
- "@nocobase/utils": "0.7.5-alpha.1",
15
+ "@nocobase/utils": "0.7.6-alpha.2",
16
16
  "async-mutex": "^0.3.2",
17
17
  "cron-parser": "4.4.0",
18
18
  "deepmerge": "^4.2.2",
@@ -32,5 +32,5 @@
32
32
  "url": "git+https://github.com/nocobase/nocobase.git",
33
33
  "directory": "packages/database"
34
34
  },
35
- "gitHead": "f6eb27b68185bb0c0b4c2cfca1df84205a9b9173"
35
+ "gitHead": "2cfccff9d3ff14c521bf317fa7ee4efa18444c92"
36
36
  }
@@ -0,0 +1,46 @@
1
+ import Database from '../database';
2
+ import { mockDatabase } from '../mock-database';
3
+
4
+ describe('changedWithAssociations', () => {
5
+ let db: Database;
6
+
7
+ beforeEach(async () => {
8
+ db = mockDatabase();
9
+ await db.clean({ drop: true });
10
+ });
11
+
12
+ afterEach(async () => {
13
+ await db.close();
14
+ });
15
+
16
+ test('changedWithAssociations', async () => {
17
+ db.collection({
18
+ name: 'test',
19
+ fields: [
20
+ {
21
+ type: 'string',
22
+ name: 'n1',
23
+ },
24
+ {
25
+ type: 'string',
26
+ name: 'n2',
27
+ },
28
+ ],
29
+ });
30
+ let changed = [];
31
+ db.on('test.afterCreateWithAssociations', (model, options) => {
32
+ changed = model.changedWithAssociations();
33
+ });
34
+ db.on('test.afterUpdateWithAssociations', (model, options) => {
35
+ changed = model.changedWithAssociations();
36
+ });
37
+ await db.sync();
38
+ const r = db.getRepository('test');
39
+ const m = await r.create({ values: { n1: 'a' } });
40
+ expect(changed.includes('n1')).toBeTruthy();
41
+ expect(m.changedWithAssociations()).toBeFalsy();
42
+ await r.update({ filterByTk: m.id, values: { n1: 'b', n2: 'c' } });
43
+ expect(changed).toEqual(['n1', 'n2']);
44
+ expect(m.changedWithAssociations()).toBeFalsy();
45
+ });
46
+ });
package/src/database.ts CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  Sequelize,
15
15
  SyncOptions,
16
16
  Transactionable,
17
- Utils,
17
+ Utils
18
18
  } from 'sequelize';
19
19
  import { SequelizeStorage, Umzug } from 'umzug';
20
20
  import { Collection, CollectionOptions, RepositoryType } from './collection';
@@ -205,6 +205,14 @@ export class Database extends EventEmitter implements AsyncEmitter {
205
205
  opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`;
206
206
  }
207
207
  });
208
+
209
+ this.on('afterCreate', async (instance) => {
210
+ instance?.toChangedWithAssociations?.();
211
+ });
212
+
213
+ this.on('afterUpdate', async (instance) => {
214
+ instance?.toChangedWithAssociations?.();
215
+ });
208
216
  }
209
217
 
210
218
  addMigration(item: MigrationItem) {
package/src/model.ts CHANGED
@@ -1,7 +1,7 @@
1
+ import lodash from 'lodash';
1
2
  import { Model as SequelizeModel, ModelCtor } from 'sequelize';
2
3
  import { Collection } from './collection';
3
4
  import { Database } from './database';
4
- import lodash from 'lodash';
5
5
  import { Field } from './fields';
6
6
 
7
7
  interface IModel {
@@ -23,6 +23,39 @@ export class Model<TModelAttributes extends {} = any, TCreationAttributes extend
23
23
  public static database: Database;
24
24
  public static collection: Collection;
25
25
 
26
+ protected _changedWithAssociations = new Set();
27
+ protected _previousDataValuesWithAssociations = {};
28
+
29
+ // TODO
30
+ public toChangedWithAssociations() {
31
+ // @ts-ignore
32
+ this._changedWithAssociations = new Set([...this._changedWithAssociations, ...this._changed]);
33
+ // @ts-ignore
34
+ this._previousDataValuesWithAssociations = this._previousDataValues;
35
+ }
36
+
37
+ public changedWithAssociations(key?: string, value?: any) {
38
+ if (key === undefined) {
39
+ if (this._changedWithAssociations.size > 0) {
40
+ return Array.from(this._changedWithAssociations);
41
+ }
42
+ return false;
43
+ }
44
+ if (value === true) {
45
+ this._changedWithAssociations.add(key);
46
+ return this;
47
+ }
48
+ if (value === false) {
49
+ this._changedWithAssociations.delete(key);
50
+ return this;
51
+ }
52
+ return this._changedWithAssociations.has(key);
53
+ }
54
+
55
+ public clearChangedWithAssociations() {
56
+ this._changedWithAssociations = new Set();
57
+ }
58
+
26
59
  public toJSON<T extends TModelAttributes>(): T {
27
60
  const handleObj = (obj, options: JSONTransformerOptions) => {
28
61
  const handles = [
package/src/repository.ts CHANGED
@@ -60,6 +60,7 @@ export type Values = any;
60
60
  export interface CountOptions extends Omit<SequelizeCreateOptions, 'distinct' | 'where' | 'include'>, Transactionable {
61
61
  fields?: Fields;
62
62
  filter?: Filter;
63
+ context?: any;
63
64
  }
64
65
 
65
66
  export interface FilterByTk {
@@ -340,6 +341,7 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
340
341
  ...options,
341
342
  transaction,
342
343
  });
344
+ instance.clearChangedWithAssociations();
343
345
  }
344
346
 
345
347
  return instance;
@@ -403,6 +405,7 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
403
405
  ...options,
404
406
  transaction,
405
407
  });
408
+ instance.clearChangedWithAssociations();
406
409
  }
407
410
  }
408
411