From 1bdc29b706595843afc649136f102c301959a564 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 21 Oct 2022 11:53:43 +0800 Subject: [PATCH] feat: changed with associations (#943) * feat: changed with associations * fix: test error * fix: test error * fix: test # Conflicts: # packages/core/database/src/model.ts --- .../model.changedWithAssociations.test.ts | 46 +++++++++++++++++++ packages/core/database/src/database.ts | 10 +++- packages/core/database/src/repository.ts | 2 + 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 packages/core/database/src/__tests__/model.changedWithAssociations.test.ts diff --git a/packages/core/database/src/__tests__/model.changedWithAssociations.test.ts b/packages/core/database/src/__tests__/model.changedWithAssociations.test.ts new file mode 100644 index 0000000000..8a62e5a7e5 --- /dev/null +++ b/packages/core/database/src/__tests__/model.changedWithAssociations.test.ts @@ -0,0 +1,46 @@ +import Database from '../database'; +import { mockDatabase } from '../mock-database'; + +describe('changedWithAssociations', () => { + let db: Database; + + beforeEach(async () => { + db = mockDatabase(); + await db.clean({ drop: true }); + }); + + afterEach(async () => { + await db.close(); + }); + + test('changedWithAssociations', async () => { + db.collection({ + name: 'test', + fields: [ + { + type: 'string', + name: 'n1', + }, + { + type: 'string', + name: 'n2', + }, + ], + }); + let changed = []; + db.on('test.afterCreateWithAssociations', (model, options) => { + changed = model.changedWithAssociations(); + }); + db.on('test.afterUpdateWithAssociations', (model, options) => { + changed = model.changedWithAssociations(); + }); + await db.sync(); + const r = db.getRepository('test'); + const m = await r.create({ values: { n1: 'a' } }); + expect(changed.includes('n1')).toBeTruthy(); + expect(m.changedWithAssociations()).toBeFalsy(); + await r.update({ filterByTk: m.id, values: { n1: 'b', n2: 'c' } }); + expect(changed).toEqual(['n1', 'n2']); + expect(m.changedWithAssociations()).toBeFalsy(); + }); +}); diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index 5d9283062d..3698140947 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -14,7 +14,7 @@ import { Sequelize, SyncOptions, Transactionable, - Utils, + Utils } from 'sequelize'; import { SequelizeStorage, Umzug } from 'umzug'; import { Collection, CollectionOptions, RepositoryType } from './collection'; @@ -232,6 +232,14 @@ export class Database extends EventEmitter implements AsyncEmitter { opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`; } }); + + this.on('afterCreate', async (instance) => { + instance?.toChangedWithAssociations?.(); + }); + + this.on('afterUpdate', async (instance) => { + instance?.toChangedWithAssociations?.(); + }); } addMigration(item: MigrationItem) { diff --git a/packages/core/database/src/repository.ts b/packages/core/database/src/repository.ts index d68175ebc4..903e8c6ef5 100644 --- a/packages/core/database/src/repository.ts +++ b/packages/core/database/src/repository.ts @@ -361,6 +361,7 @@ export class Repository