fix(plugin-sequence): fix missed createdAt field in bulk hook (#1448)
This commit is contained in:
parent
0cc007250b
commit
e2e85808e4
@ -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`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -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) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user