Fix: sequence field (#1009)
* fix(database): fix sequence field match last * fix(database): fix sequence field match last * fix(database): add test case for sequence field
This commit is contained in:
		
							parent
							
								
									98259e88a1
								
							
						
					
					
						commit
						5521a02b04
					
				@ -216,6 +216,31 @@ describe('string field', () => {
 | 
			
		||||
      const item4 = await TestModel.create();
 | 
			
		||||
      expect(item4.get('name')).toBe('1');
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('last record has no value of this field', async () => {
 | 
			
		||||
      const testCollection = db.collection({
 | 
			
		||||
        name: 'tests',
 | 
			
		||||
        fields: [],
 | 
			
		||||
      });
 | 
			
		||||
      await db.sync();
 | 
			
		||||
 | 
			
		||||
      const TestModel = db.getModel('tests');
 | 
			
		||||
      const item1 = await TestModel.create();
 | 
			
		||||
      expect(item1.get('name')).toBeUndefined();
 | 
			
		||||
 | 
			
		||||
      testCollection.addField('name', {
 | 
			
		||||
        type: 'sequence',
 | 
			
		||||
        patterns: [
 | 
			
		||||
          {
 | 
			
		||||
            type: 'integer'
 | 
			
		||||
          }
 | 
			
		||||
        ]
 | 
			
		||||
      });
 | 
			
		||||
      await db.sync();
 | 
			
		||||
 | 
			
		||||
      const item2 = await TestModel.create();
 | 
			
		||||
      expect(item2.get('name')).toBe('0');
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  describe('date pattern', () => {
 | 
			
		||||
 | 
			
		||||
@ -44,7 +44,7 @@ sequencePatterns.register('integer', {
 | 
			
		||||
    const { lastRecord = null } = this.options;
 | 
			
		||||
 | 
			
		||||
    if (typeof options.current === 'undefined') {
 | 
			
		||||
      if (lastRecord) {
 | 
			
		||||
      if (lastRecord && lastRecord.get(this.options.name)) {
 | 
			
		||||
        // if match current pattern
 | 
			
		||||
        const matcher = this.match(lastRecord.get(this.options.name));
 | 
			
		||||
        if (matcher) {
 | 
			
		||||
@ -115,6 +115,8 @@ export interface SequenceFieldOptions extends BaseColumnFieldOptions {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class SequenceField extends Field {
 | 
			
		||||
  matcher: RegExp;
 | 
			
		||||
 | 
			
		||||
  get dataType() {
 | 
			
		||||
    return DataTypes.STRING;
 | 
			
		||||
  }
 | 
			
		||||
@ -170,7 +172,7 @@ export class SequenceField extends Field {
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  match(value) {
 | 
			
		||||
    return value.match(this.matcher);
 | 
			
		||||
    return typeof value === 'string' ? value.match(this.matcher) : null;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  parse(value: string, patternIndex: number): string {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user