fix(plugin-sequence): fix test case (#1268)
* fix(plugin-sequence): fix test case * fix(plugin-sequence): fix async call * fix(plugin-sequence): fix test case
This commit is contained in:
		
							parent
							
								
									a14a65596e
								
							
						
					
					
						commit
						217ecb27ae
					
				| @ -710,13 +710,13 @@ describe('sequence field', () => { | |||||||
|       await db.sync(); |       await db.sync(); | ||||||
| 
 | 
 | ||||||
|       const TestModel = db.getModel('tests'); |       const TestModel = db.getModel('tests'); | ||||||
|       const item1 = await TestModel.create(); |       const item1 = await TestModel.create({}); | ||||||
|       expect(item1.name).toBe('0'); |       expect(item1.name).toBe('0'); | ||||||
| 
 | 
 | ||||||
|       const item2 = await TestModel.create({ name: '2' }); |       const item2 = await TestModel.create({ name: '2' }); | ||||||
|       expect(item2.name).toBe('2'); |       expect(item2.name).toBe('2'); | ||||||
| 
 | 
 | ||||||
|       const item3 = await TestModel.create(); |       const item3 = await TestModel.create({}); | ||||||
|       expect(item3.name).toBe('3'); |       expect(item3.name).toBe('3'); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -9,7 +9,7 @@ import { Model, BaseColumnFieldOptions, Field, FieldContext } from '@nocobase/da | |||||||
| 
 | 
 | ||||||
| export interface Pattern { | export interface Pattern { | ||||||
|   validate?(options): string | null; |   validate?(options): string | null; | ||||||
|   generate(this: SequenceField, instance: Model, index: number, options: Transactionable): Promise<string> | string; |   generate(this: SequenceField, instance: Model, opts: { [key: string]: any }, options: Transactionable): Promise<string> | string; | ||||||
|   getLength(options): number; |   getLength(options): number; | ||||||
|   getMatcher(options): string; |   getMatcher(options): string; | ||||||
|   update?(this: SequenceField, instance: Model, value: string, options, transactionable: Transactionable): Promise<void>; |   update?(this: SequenceField, instance: Model, value: string, options, transactionable: Transactionable): Promise<void>; | ||||||
| @ -24,8 +24,7 @@ sequencePatterns.register('string', { | |||||||
|     } |     } | ||||||
|     return null; |     return null; | ||||||
|   }, |   }, | ||||||
|   generate(instance, index) { |   generate(instance, options) { | ||||||
|     const { options } = this.options.patterns[index]; |  | ||||||
|     return options.value; |     return options.value; | ||||||
|   }, |   }, | ||||||
|   getLength(options) { |   getLength(options) { | ||||||
| @ -43,9 +42,8 @@ sequencePatterns.register('integer', { | |||||||
|   //   }
 |   //   }
 | ||||||
|   //   return null;
 |   //   return null;
 | ||||||
|   // },
 |   // },
 | ||||||
|   async generate(this: SequenceField, instance: Model, index, { transaction }) { |   async generate(this: SequenceField, instance: Model, options, { transaction }) { | ||||||
|     const recordTime = <Date>instance.get('createdAt'); |     const recordTime = <Date>instance.get('createdAt'); | ||||||
|     const { options = {} } = this.options.patterns[index]; |  | ||||||
|     const { digits = 1, start = 0, base = 10, cycle, key } = options; |     const { digits = 1, start = 0, base = 10, cycle, key } = options; | ||||||
|     const max = Math.pow(base, digits) - 1; |     const max = Math.pow(base, digits) - 1; | ||||||
|     const SeqRepo = this.database.getRepository('sequences'); |     const SeqRepo = this.database.getRepository('sequences'); | ||||||
| @ -169,8 +167,7 @@ sequencePatterns.register('integer', { | |||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| sequencePatterns.register('date', { | sequencePatterns.register('date', { | ||||||
|   generate(this: SequenceField, instance, index) { |   generate(this: SequenceField, instance, options) { | ||||||
|     const { options } = this.options.patterns[index]; |  | ||||||
|     return moment(instance.get(options?.field ?? 'createdAt')).format(options?.format ?? 'YYYYMMDD'); |     return moment(instance.get(options?.field ?? 'createdAt')).format(options?.format ?? 'YYYYMMDD'); | ||||||
|   }, |   }, | ||||||
|   getLength(options) { |   getLength(options) { | ||||||
| @ -246,7 +243,7 @@ export class SequenceField extends Field { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const results = await patterns.reduce((promise, p, i) => promise.then(async (result) => { |     const results = await patterns.reduce((promise, p, i) => promise.then(async (result) => { | ||||||
|       const item = await (sequencePatterns.get(p.type)).generate.call(this, instance, i, options); |       const item = await (sequencePatterns.get(p.type)).generate.call(this, instance, p.options, options); | ||||||
|       return result.concat(item); |       return result.concat(item); | ||||||
|     }), Promise.resolve([])); |     }), Promise.resolve([])); | ||||||
|     instance.set(name, results.join('')); |     instance.set(name, results.join('')); | ||||||
| @ -260,14 +257,14 @@ export class SequenceField extends Field { | |||||||
|     const { name, patterns } = this.options; |     const { name, patterns } = this.options; | ||||||
|     const matched = this.match(instance.get(name)); |     const matched = this.match(instance.get(name)); | ||||||
|     if (matched) { |     if (matched) { | ||||||
|       await matched.slice(1) |       await (matched.slice(1) | ||||||
|         .map((_, i) => sequencePatterns.get(patterns[i].type).update) |         .map((_, i) => sequencePatterns.get(patterns[i].type).update) | ||||||
|         .reduce((promise, update, i) => promise.then(() => { |         .reduce((promise, update, i) => promise.then(() => { | ||||||
|           if (!update) { |           if (!update) { | ||||||
|             return; |             return; | ||||||
|           } |           } | ||||||
|           update.call(this, instance, matched[i + 1], patterns[i].options, options) |           return update.call(this, instance, matched[i + 1], patterns[i].options, options) | ||||||
|         }), Promise.resolve()); |         }), Promise.resolve())); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user