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:
Junyi 2022-11-02 14:46:52 +08:00 committed by GitHub
parent 98259e88a1
commit 5521a02b04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View File

@ -216,6 +216,31 @@ describe('string field', () => {
const item4 = await TestModel.create(); const item4 = await TestModel.create();
expect(item4.get('name')).toBe('1'); 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', () => { describe('date pattern', () => {

View File

@ -44,7 +44,7 @@ sequencePatterns.register('integer', {
const { lastRecord = null } = this.options; const { lastRecord = null } = this.options;
if (typeof options.current === 'undefined') { if (typeof options.current === 'undefined') {
if (lastRecord) { if (lastRecord && lastRecord.get(this.options.name)) {
// if match current pattern // if match current pattern
const matcher = this.match(lastRecord.get(this.options.name)); const matcher = this.match(lastRecord.get(this.options.name));
if (matcher) { if (matcher) {
@ -115,6 +115,8 @@ export interface SequenceFieldOptions extends BaseColumnFieldOptions {
} }
export class SequenceField extends Field { export class SequenceField extends Field {
matcher: RegExp;
get dataType() { get dataType() {
return DataTypes.STRING; return DataTypes.STRING;
} }
@ -170,7 +172,7 @@ export class SequenceField extends Field {
}; };
match(value) { match(value) {
return value.match(this.matcher); return typeof value === 'string' ? value.match(this.matcher) : null;
} }
parse(value: string, patternIndex: number): string { parse(value: string, patternIndex: number): string {