fix(plugin-sequence): fix missed createdAt field in bulk hook (#1448)

This commit is contained in:
Junyi 2023-02-10 23:41:30 +08:00 committed by GitHub
parent 0cc007250b
commit e2e85808e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 9 deletions

View File

@ -764,6 +764,7 @@ describe('sequence field', () => {
type: 'sequence', type: 'sequence',
name: 'seq', name: 'seq',
patterns: [ patterns: [
{ type: 'date' },
{ type: 'integer', options: { key: 1 } } { type: 'integer', options: { key: 1 } }
] ]
} }
@ -800,6 +801,9 @@ describe('sequence field', () => {
await db.sync(); await db.sync();
const now = new Date();
const dateStr = moment(now).format('YYYYMMDD');
const tagsRepo = db.getRepository('tags'); const tagsRepo = db.getRepository('tags');
const tags = await tagsRepo.create({ const tags = await tagsRepo.create({
values: [ values: [
@ -828,9 +832,9 @@ describe('sequence field', () => {
order: [['seq', 'ASC']] order: [['seq', 'ASC']]
}); });
expect(postsTags[0].seq).toBe('0'); expect(postsTags[0].seq).toBe(`${dateStr}0`);
expect(postsTags[1].seq).toBe('1'); expect(postsTags[1].seq).toBe(`${dateStr}1`);
expect(postsTags[2].seq).toBe('2'); expect(postsTags[2].seq).toBe(`${dateStr}2`);
}); });
}); });
}); });

View File

@ -65,7 +65,7 @@ sequencePatterns.register('integer', {
// return null; // return null;
// }, // },
async generate(this: SequenceField, instance: Model, options, { transaction }) { async generate(this: SequenceField, instance: Model, options, { transaction }) {
const recordTime = <Date>instance.get('createdAt'); const recordTime = <Date>instance.get('createdAt') ?? new Date();
const { digits = 1, start = 0, base = 10, cycle, key } = options; const { digits = 1, start = 0, base = 10, cycle, key } = options;
const { repository: SeqRepo, model: SeqModel } = this.database.getCollection('sequences'); const { repository: SeqRepo, model: SeqModel } = this.database.getCollection('sequences');
const lastSeq = (await SeqRepo.findOne({ const lastSeq = (await SeqRepo.findOne({
@ -137,7 +137,7 @@ sequencePatterns.register('integer', {
}); });
instances.forEach((instance, i) => { instances.forEach((instance, i) => {
const recordTime = <Date>instance.get('createdAt'); const recordTime = <Date>instance.get('createdAt') ?? new Date();
const value = instance.get(name); const value = instance.get(name);
if (value != null && this.options.inputable) { if (value != null && this.options.inputable) {
const matcher = this.match(value); const matcher = this.match(value);
@ -192,7 +192,7 @@ sequencePatterns.register('integer', {
}, },
async update(instance, value, options, { transaction }) { async update(instance, value, options, { transaction }) {
const recordTime = <Date>instance.get('createdAt'); const recordTime = <Date>instance.get('createdAt') ?? new Date();
const { digits = 1, start = 0, base = 10, cycle, key } = options; const { digits = 1, start = 0, base = 10, cycle, key } = options;
const SeqRepo = this.database.getRepository('sequences'); const SeqRepo = this.database.getRepository('sequences');
const lastSeq = await SeqRepo.findOne({ const lastSeq = await SeqRepo.findOne({
@ -260,9 +260,9 @@ sequencePatterns.register('date', {
return moment(instance.get(options?.field ?? 'createdAt')).format(options?.format ?? 'YYYYMMDD'); return moment(instance.get(options?.field ?? 'createdAt')).format(options?.format ?? 'YYYYMMDD');
}, },
batchGenerate(instances, values, options) { batchGenerate(instances, values, options) {
const { name, inputable } = options; const { field, inputable } = options;
instances.forEach((instance, i) => { instances.forEach((instance, i) => {
if (!inputable || instance.get(name) == null) { if (!inputable || instance.get(field ?? 'createdAt') == null) {
values[i] = sequencePatterns.get('date').generate.call(this, instance, options); values[i] = sequencePatterns.get('date').generate.call(this, instance, options);
} }
}); });
@ -367,7 +367,7 @@ export class SequenceField extends Field {
const array = Array(patterns.length).fill(null).map(() => Array(instances.length)); const array = Array(patterns.length).fill(null).map(() => Array(instances.length));
await patterns.reduce((promise, p, i) => promise.then(() => await patterns.reduce((promise, p, i) => promise.then(() =>
sequencePatterns.get(p.type).batchGenerate.call(this, instances, array[i], p.options, options)), sequencePatterns.get(p.type).batchGenerate.call(this, instances, array[i], p.options ?? {}, options)),
Promise.resolve()); Promise.resolve());
instances.forEach((instance, i) => { instances.forEach((instance, i) => {