diff --git a/packages/database/src/__tests__/fields/sort-field.test.ts b/packages/database/src/__tests__/fields/sort-field.test.ts index 77cbc7a1b..1a23e4649 100644 --- a/packages/database/src/__tests__/fields/sort-field.test.ts +++ b/packages/database/src/__tests__/fields/sort-field.test.ts @@ -7,6 +7,7 @@ describe('string field', () => { beforeEach(async () => { db = mockDatabase(); + await db.clean({ drop: true }); db.registerFieldTypes({ sort: SortField, }); @@ -31,6 +32,45 @@ describe('string field', () => { expect(test3.sort).toBe(3); }); + it('should init sort value on data already exits', async () => { + const Test = db.collection({ + name: 'tests', + fields: [ + { + type: 'string', + name: 'name', + }, + ], + }); + + await db.sync(); + + await db.getRepository('tests').create({ + values: { + name: 't1', + }, + }); + await db.getRepository('tests').create({ + values: { + name: 't2', + }, + }); + await db.getRepository('tests').create({ + values: { + name: 't3', + }, + }); + + const field = Test.addField('sort', { type: 'sort' }); + + await field.sync({}); + + const items = await db.getRepository('tests').find({ + order: ['id'], + }); + expect(items.map((item) => item.get('sort'))).toEqual([1, 2, 3]); + }); + test.skip('simultaneously create ', async () => { const Test = db.collection({ name: 'tests', diff --git a/packages/database/src/collection.ts b/packages/database/src/collection.ts index 922591d5b..76f6b570e 100644 --- a/packages/database/src/collection.ts +++ b/packages/database/src/collection.ts @@ -1,7 +1,7 @@ import merge from 'deepmerge'; import { EventEmitter } from 'events'; import { default as lodash, default as _ } from 'lodash'; -import { ModelCtor, ModelOptions, SyncOptions } from 'sequelize'; +import { col, ModelCtor, ModelOptions, SyncOptions } from 'sequelize'; import { Database } from './database'; import { Field, FieldOptions } from './fields'; import { Model } from './model'; diff --git a/packages/database/src/fields/sort-field.ts b/packages/database/src/fields/sort-field.ts index f151aca0e..4798951d7 100644 --- a/packages/database/src/fields/sort-field.ts +++ b/packages/database/src/fields/sort-field.ts @@ -41,8 +41,38 @@ export class SortField extends Field { } } + async initRecordsSortValue({ syncOptions }) { + const totalCount = await this.collection.repository.count(); + const emptyCount = await this.collection.repository.count({ + filter: { + [this.name]: null, + }, + }); + + if (emptyCount === totalCount && emptyCount > 0) { + const records = await this.collection.repository.find({ + order: [this.collection.model.primaryKeyAttribute], + }); + + let start = 1; + for (const record of records) { + await record.update( + { + sort: start, + }, + { + silent: true, + }, + ); + + start += 1; + } + } + } + bind() { super.bind(); + this.on('afterSync', this.initRecordsSortValue.bind(this)); this.on('beforeUpdate', this.onScopeChange.bind(this)); this.on('beforeCreate', this.setSortValue.bind(this)); } @@ -51,6 +81,7 @@ export class SortField extends Field { super.unbind(); this.off('beforeUpdate', this.onScopeChange.bind(this)); this.off('beforeCreate', this.setSortValue.bind(this)); + this.off('afterSync', this.initRecordsSortValue.bind(this)); } } diff --git a/packages/database/src/model-hook.ts b/packages/database/src/model-hook.ts index a2a38180a..d9176641e 100644 --- a/packages/database/src/model-hook.ts +++ b/packages/database/src/model-hook.ts @@ -31,8 +31,14 @@ export class ModelHook { return (arg).constructor.name; } - if (lodash.isPlainObject(arg) && arg['model']) { - return arg['model'].name; + if (lodash.isPlainObject(arg)) { + if (arg['model']) { + return arg['model'].name; + } + + if (lodash.get(arg, 'name.plural')) { + return lodash.get(arg, 'name.plural'); + } } } @@ -50,6 +56,7 @@ export class ModelHook { sequelizeHookBuilder(eventName) { return async (...args: any[]) => { const modelName = this.findModelName(args); + if (modelName) { // emit model event await this.database.emitAsync(`${modelName}.${eventName}`, ...args);