fix: sync event in inherited collection (#5477)

* fix: sync event in inherited collection

* fix: hooks options
This commit is contained in:
ChengLei Shao 2024-10-22 13:43:24 +08:00 committed by GitHub
parent 063ebd9d05
commit f3ef42968e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 10 deletions

View File

@ -84,6 +84,35 @@ describe.runIf(isPg())('collection inherits', () => {
expect(err).toBeUndefined(); expect(err).toBeUndefined();
}); });
it('should emit afterSync event', async () => {
const Root = db.collection({
name: 'root',
fields: [
{ name: 'name', type: 'string' },
{
name: 'bs',
type: 'hasMany',
target: 'b',
foreignKey: 'root_id',
},
],
});
const Child = db.collection({
name: 'child',
inherits: ['root'],
});
const fn = vi.fn();
db.on('child.afterSync', (options) => {
fn();
});
await db.sync();
expect(fn).toBeCalled();
});
it('should append __collection with eager load', async () => { it('should append __collection with eager load', async () => {
const Root = db.collection({ const Root = db.collection({
name: 'root', name: 'root',

View File

@ -13,6 +13,7 @@ import lodash from 'lodash';
export class InheritedSyncRunner { export class InheritedSyncRunner {
static async syncInheritModel(model: any, options: any) { static async syncInheritModel(model: any, options: any) {
const { transaction } = options; const { transaction } = options;
options.hooks = options.hooks === undefined ? true : !!options.hooks;
const inheritedCollection = model.collection as InheritedCollection; const inheritedCollection = model.collection as InheritedCollection;
const db = inheritedCollection.context.database; const db = inheritedCollection.context.database;
@ -62,10 +63,10 @@ export class InheritedSyncRunner {
for (const parent of parents) { for (const parent of parents) {
const sequenceNameResult = await queryInterface.sequelize.query( const sequenceNameResult = await queryInterface.sequelize.query(
`SELECT column_default `SELECT column_default
FROM information_schema.columns FROM information_schema.columns
WHERE table_name = '${parent.model.tableName}' WHERE table_name = '${parent.model.tableName}'
and table_schema = '${parent.collectionSchema()}' and table_schema = '${parent.collectionSchema()}'
and "column_name" = 'id';`, and "column_name" = 'id';`,
{ {
transaction, transaction,
}, },
@ -87,7 +88,7 @@ export class InheritedSyncRunner {
const sequenceName = match[1]; const sequenceName = match[1];
const sequenceCurrentValResult = await queryInterface.sequelize.query( const sequenceCurrentValResult = await queryInterface.sequelize.query(
`select last_value `select last_value
from ${sequenceName}`, from ${sequenceName}`,
{ {
transaction, transaction,
}, },
@ -117,10 +118,10 @@ export class InheritedSyncRunner {
const schemaName = sequenceTable.schema; const schemaName = sequenceTable.schema;
const idColumnSql = `SELECT column_name const idColumnSql = `SELECT column_name
FROM information_schema.columns FROM information_schema.columns
WHERE table_name = '${tableName}' WHERE table_name = '${tableName}'
and column_name = 'id' and column_name = 'id'
and table_schema = '${schemaName}'; and table_schema = '${schemaName}';
`; `;
const idColumnQuery = await queryInterface.sequelize.query(idColumnSql, { const idColumnQuery = await queryInterface.sequelize.query(idColumnSql, {
@ -133,7 +134,7 @@ export class InheritedSyncRunner {
await queryInterface.sequelize.query( await queryInterface.sequelize.query(
`alter table ${db.utils.quoteTable(sequenceTable)} `alter table ${db.utils.quoteTable(sequenceTable)}
alter column id set default nextval('${maxSequenceName}')`, alter column id set default nextval('${maxSequenceName}')`,
{ {
transaction, transaction,
}, },
@ -153,6 +154,14 @@ export class InheritedSyncRunner {
} }
} }
} }
if (options.hooks) {
await model.runHooks('afterSync', {
...options,
modelName: model.name,
transaction,
});
}
} }
static async createTable(tableName, attributes, options, model, parents) { static async createTable(tableName, attributes, options, model, parents) {