mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 05:29:26 +08:00
fix(database): fix datetimeNoTz field (#6588)
* fix(database): fix datetimeNoTz field * fix(database): fix test case
This commit is contained in:
parent
4c3255d455
commit
164772afb8
@ -29,26 +29,24 @@ export class DatetimeNoTzField extends Field {
|
|||||||
return DatetimeNoTzTypeMySQL;
|
return DatetimeNoTzTypeMySQL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return DataTypes.STRING;
|
return DataTypes.DATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
beforeSave = async (instance, options) => {
|
||||||
const { name, defaultToCurrentTime, onUpdateToCurrentTime } = this.options;
|
const { name, defaultToCurrentTime, onUpdateToCurrentTime } = this.options;
|
||||||
|
|
||||||
this.beforeSave = async (instance, options) => {
|
const value = instance.get(name);
|
||||||
const value = instance.get(name);
|
|
||||||
|
|
||||||
if (!value && instance.isNewRecord && defaultToCurrentTime) {
|
if (!value && instance.isNewRecord && defaultToCurrentTime) {
|
||||||
instance.set(name, new Date());
|
instance.set(name, new Date());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (onUpdateToCurrentTime) {
|
if (onUpdateToCurrentTime) {
|
||||||
instance.set(name, new Date());
|
instance.set(name, new Date());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
additionalSequelizeOptions(): {} {
|
additionalSequelizeOptions(): {} {
|
||||||
const { name } = this.options;
|
const { name } = this.options;
|
||||||
@ -57,17 +55,14 @@ export class DatetimeNoTzField extends Field {
|
|||||||
const timezone = this.database.options.rawTimezone || '+00:00';
|
const timezone = this.database.options.rawTimezone || '+00:00';
|
||||||
|
|
||||||
const isPg = this.database.inDialect('postgres');
|
const isPg = this.database.inDialect('postgres');
|
||||||
|
const isMySQLCompatibleDialect = this.database.isMySQLCompatibleDialect();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get() {
|
get() {
|
||||||
const val = this.getDataValue(name);
|
const val = this.getDataValue(name);
|
||||||
|
|
||||||
if (val instanceof Date) {
|
if (val instanceof Date) {
|
||||||
if (isPg) {
|
const momentVal = moment(val);
|
||||||
return moment(val).format('YYYY-MM-DD HH:mm:ss');
|
|
||||||
}
|
|
||||||
// format to YYYY-MM-DD HH:mm:ss
|
|
||||||
const momentVal = moment(val).utcOffset(timezone);
|
|
||||||
return momentVal.format('YYYY-MM-DD HH:mm:ss');
|
return momentVal.format('YYYY-MM-DD HH:mm:ss');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,18 +70,24 @@ export class DatetimeNoTzField extends Field {
|
|||||||
},
|
},
|
||||||
|
|
||||||
set(val) {
|
set(val) {
|
||||||
if (typeof val === 'string' && isIso8601(val)) {
|
if (val == null) {
|
||||||
const momentVal = moment(val).utcOffset(timezone);
|
return this.setDataValue(name, null);
|
||||||
val = momentVal.format('YYYY-MM-DD HH:mm:ss');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (val && val instanceof Date) {
|
const dateOffset = new Date().getTimezoneOffset();
|
||||||
// format to YYYY-MM-DD HH:mm:ss
|
const momentVal = moment(val);
|
||||||
const momentVal = moment(val).utcOffset(timezone);
|
if ((typeof val === 'string' && isIso8601(val)) || val instanceof Date) {
|
||||||
val = momentVal.format('YYYY-MM-DD HH:mm:ss');
|
momentVal.utcOffset(timezone);
|
||||||
|
momentVal.utcOffset(-dateOffset, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.setDataValue(name, val);
|
if (isMySQLCompatibleDialect) {
|
||||||
|
momentVal.millisecond(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const date = momentVal.toDate();
|
||||||
|
|
||||||
|
return this.setDataValue(name, date);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -359,6 +359,50 @@ describe('workflow > triggers > collection', () => {
|
|||||||
const executions = await workflow.getExecutions();
|
const executions = await workflow.getExecutions();
|
||||||
expect(executions.length).toBe(1);
|
expect(executions.length).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('datetime field not changed', async () => {
|
||||||
|
const workflow = await WorkflowModel.create({
|
||||||
|
enabled: true,
|
||||||
|
sync: true,
|
||||||
|
type: 'collection',
|
||||||
|
config: {
|
||||||
|
mode: 2,
|
||||||
|
collection: 'posts',
|
||||||
|
changed: ['createdAt'],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const post = await PostRepo.create({ values: { title: 't1' } });
|
||||||
|
await PostRepo.update({ filterByTk: post.id, values: { ...post.get(), title: 't2' } });
|
||||||
|
|
||||||
|
const executions = await workflow.getExecutions();
|
||||||
|
expect(executions.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('datetimeNoTz field not changed', async () => {
|
||||||
|
db.getCollection('posts').addField('dateOnly', {
|
||||||
|
type: 'datetimeNoTz',
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.sync();
|
||||||
|
|
||||||
|
const workflow = await WorkflowModel.create({
|
||||||
|
enabled: true,
|
||||||
|
sync: true,
|
||||||
|
type: 'collection',
|
||||||
|
config: {
|
||||||
|
mode: 2,
|
||||||
|
collection: 'posts',
|
||||||
|
changed: ['dateOnly'],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const post = await PostRepo.create({ values: { title: 't1', dateOnly: '2020-01-01 00:00:00' } });
|
||||||
|
await PostRepo.update({ filterByTk: post.id, values: { ...post.get(), title: 't2' } });
|
||||||
|
|
||||||
|
const executions = await workflow.getExecutions();
|
||||||
|
expect(executions.length).toBe(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('config.condition', () => {
|
describe('config.condition', () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user