@nocobase/database 0.8.0-alpha.7 → 0.8.0-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,11 +19,12 @@ export interface SequenceFieldOptions extends BaseColumnFieldOptions {
19
19
  patterns: PatternConfig[];
20
20
  }
21
21
  export declare class SequenceField extends Field {
22
+ matcher: RegExp;
22
23
  get dataType(): DataTypes.StringDataTypeConstructor;
23
24
  constructor(options: SequenceFieldOptions, context: FieldContext);
24
25
  setValue: (instance: Model, options: any) => Promise<void>;
25
26
  setLast: (instance: Model, options: any) => void;
26
- match(value: any): any;
27
+ match(value: any): RegExpMatchArray;
27
28
  parse(value: string, patternIndex: number): string;
28
29
  bind(): void;
29
30
  unbind(): void;
@@ -104,7 +104,7 @@ sequencePatterns.register('integer', {
104
104
  lastRecord = _this$options$lastRec === void 0 ? null : _this$options$lastRec;
105
105
 
106
106
  if (typeof options.current === 'undefined') {
107
- if (lastRecord) {
107
+ if (lastRecord && lastRecord.get(this.options.name)) {
108
108
  // if match current pattern
109
109
  const matcher = this.match(lastRecord.get(this.options.name));
110
110
 
@@ -196,6 +196,7 @@ class SequenceField extends _field.Field {
196
196
 
197
197
  super(_options, context);
198
198
  _this = this;
199
+ this.matcher = void 0;
199
200
 
200
201
  this.setValue = /*#__PURE__*/function () {
201
202
  var _ref = _asyncToGenerator(function* (instance, options) {
@@ -258,7 +259,7 @@ class SequenceField extends _field.Field {
258
259
  }
259
260
 
260
261
  match(value) {
261
- return value.match(this.matcher);
262
+ return typeof value === 'string' ? value.match(this.matcher) : null;
262
263
  }
263
264
 
264
265
  parse(value, patternIndex) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "0.8.0-alpha.7",
3
+ "version": "0.8.0-alpha.9",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -12,7 +12,7 @@
12
12
  }
13
13
  ],
14
14
  "dependencies": {
15
- "@nocobase/utils": "0.8.0-alpha.7",
15
+ "@nocobase/utils": "0.8.0-alpha.9",
16
16
  "async-mutex": "^0.3.2",
17
17
  "cron-parser": "4.4.0",
18
18
  "deepmerge": "^4.2.2",
@@ -32,5 +32,5 @@
32
32
  "url": "git+https://github.com/nocobase/nocobase.git",
33
33
  "directory": "packages/database"
34
34
  },
35
- "gitHead": "8d6ee41e3ff401922cd8d2d93bc15e289cdf2977"
35
+ "gitHead": "6afe02d59bb22df0f96a7605c9001dcc17a30d6a"
36
36
  }
@@ -216,6 +216,31 @@ describe('string field', () => {
216
216
  const item4 = await TestModel.create();
217
217
  expect(item4.get('name')).toBe('1');
218
218
  });
219
+
220
+ it('last record has no value of this field', async () => {
221
+ const testCollection = db.collection({
222
+ name: 'tests',
223
+ fields: [],
224
+ });
225
+ await db.sync();
226
+
227
+ const TestModel = db.getModel('tests');
228
+ const item1 = await TestModel.create();
229
+ expect(item1.get('name')).toBeUndefined();
230
+
231
+ testCollection.addField('name', {
232
+ type: 'sequence',
233
+ patterns: [
234
+ {
235
+ type: 'integer'
236
+ }
237
+ ]
238
+ });
239
+ await db.sync();
240
+
241
+ const item2 = await TestModel.create();
242
+ expect(item2.get('name')).toBe('0');
243
+ });
219
244
  });
220
245
 
221
246
  describe('date pattern', () => {
@@ -44,7 +44,7 @@ sequencePatterns.register('integer', {
44
44
  const { lastRecord = null } = this.options;
45
45
 
46
46
  if (typeof options.current === 'undefined') {
47
- if (lastRecord) {
47
+ if (lastRecord && lastRecord.get(this.options.name)) {
48
48
  // if match current pattern
49
49
  const matcher = this.match(lastRecord.get(this.options.name));
50
50
  if (matcher) {
@@ -115,6 +115,8 @@ export interface SequenceFieldOptions extends BaseColumnFieldOptions {
115
115
  }
116
116
 
117
117
  export class SequenceField extends Field {
118
+ matcher: RegExp;
119
+
118
120
  get dataType() {
119
121
  return DataTypes.STRING;
120
122
  }
@@ -170,7 +172,7 @@ export class SequenceField extends Field {
170
172
  };
171
173
 
172
174
  match(value) {
173
- return value.match(this.matcher);
175
+ return typeof value === 'string' ? value.match(this.matcher) : null;
174
176
  }
175
177
 
176
178
  parse(value: string, patternIndex: number): string {