feat: init sort field values (#236)
* feat: init sort field values * feat: handle sequelize afterSync hook
This commit is contained in:
		
							parent
							
								
									cf279409b4
								
							
						
					
					
						commit
						4c0af45105
					
				@ -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',
 | 
			
		||||
 | 
			
		||||
@ -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';
 | 
			
		||||
 | 
			
		||||
@ -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));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -31,8 +31,14 @@ export class ModelHook {
 | 
			
		||||
        return (<Model>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);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user