mirror of
https://github.com/strapi/strapi.git
synced 2025-08-07 00:09:23 +00:00
parse date types
This commit is contained in:
parent
4c784318d8
commit
06b0cc22c8
@ -283,15 +283,22 @@ module.exports = async ({
|
|||||||
// Add created_at and updated_at field if timestamp option is true
|
// Add created_at and updated_at field if timestamp option is true
|
||||||
if (hasTimestamps) {
|
if (hasTimestamps) {
|
||||||
definition.attributes[createAtCol] = {
|
definition.attributes[createAtCol] = {
|
||||||
type: 'timestamp',
|
type: 'currentTimestamp',
|
||||||
};
|
};
|
||||||
definition.attributes[updatedAtCol] = {
|
definition.attributes[updatedAtCol] = {
|
||||||
type: 'timestampUpdate',
|
type: 'timestampUpdate',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save all attributes (with timestamps)
|
// Save all attributes (with timestamps) and right type
|
||||||
model.allAttributes = _.clone(definition.attributes);
|
model.allAttributes = _.assign(_.clone(definition.attributes), {
|
||||||
|
[createAtCol]: {
|
||||||
|
type: 'timestamp',
|
||||||
|
},
|
||||||
|
[updatedAtCol]: {
|
||||||
|
type: 'timestamp',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
// Equilize tables
|
// Equilize tables
|
||||||
if (connection.options && connection.options.autoMigration !== false) {
|
if (connection.options && connection.options.autoMigration !== false) {
|
||||||
@ -410,29 +417,34 @@ const getType = ({ definition, attribute, name, tableExists = false }) => {
|
|||||||
return client === 'pg' ? 'double precision' : 'double';
|
return client === 'pg' ? 'double precision' : 'double';
|
||||||
case 'decimal':
|
case 'decimal':
|
||||||
return 'decimal(10,2)';
|
return 'decimal(10,2)';
|
||||||
// TODO: split time types as they should be different
|
|
||||||
case 'date':
|
case 'date':
|
||||||
return 'date';
|
return 'date';
|
||||||
case 'time':
|
case 'time':
|
||||||
return 'time';
|
return 'time';
|
||||||
case 'datetime': {
|
case 'datetime': {
|
||||||
if (client === 'pg') {
|
if (client === 'pg') return 'timestampz';
|
||||||
return 'timestamp with time zone';
|
return 'datetime';
|
||||||
}
|
|
||||||
return 'timestamp';
|
|
||||||
}
|
}
|
||||||
case 'timestamp': {
|
case 'timestamp': {
|
||||||
if (client === 'pg') {
|
if (client === 'pg') {
|
||||||
return 'timestamp with time zone';
|
return 'timestampz';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'timestamp';
|
||||||
|
}
|
||||||
|
case 'currentTimestamp': {
|
||||||
|
if (client === 'pg') {
|
||||||
|
return 'timestamp with time zone DEFAULT CURRENT_TIMESTAMP';
|
||||||
} else if (client === 'sqlite3' && tableExists) {
|
} else if (client === 'sqlite3' && tableExists) {
|
||||||
return 'timestamp DEFAULT NULL';
|
return 'timestamp DEFAULT NULL';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'timestamp DEFAULT CURRENT_TIMESTAMP';
|
return 'timestamp DEFAULT CURRENT_TIMESTAMP';
|
||||||
}
|
}
|
||||||
case 'timestampUpdate':
|
case 'timestampUpdate':
|
||||||
switch (client) {
|
switch (client) {
|
||||||
case 'pg':
|
case 'pg':
|
||||||
return 'timestamp with time zone';
|
return 'timestamp with time zone DEFAULT CURRENT_TIMESTAMP';
|
||||||
case 'sqlite3':
|
case 'sqlite3':
|
||||||
if (tableExists) {
|
if (tableExists) {
|
||||||
return 'timestamp DEFAULT NULL';
|
return 'timestamp DEFAULT NULL';
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { parseType } = require('strapi-utils');
|
const { parseType } = require('strapi-utils');
|
||||||
const { parse, isValid, parseISO } = require('date-fns');
|
|
||||||
|
|
||||||
const createParser = () => (type, value) => {
|
const createParser = () => (type, value) => {
|
||||||
if (value === null) return null;
|
if (value === null) return null;
|
||||||
@ -9,48 +8,9 @@ const createParser = () => (type, value) => {
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case 'json':
|
case 'json':
|
||||||
return JSON.stringify(value);
|
return JSON.stringify(value);
|
||||||
case 'time':
|
|
||||||
return parseType({ type: 'time', value });
|
|
||||||
case 'date':
|
|
||||||
return parseDate(value);
|
|
||||||
case 'timestamp':
|
|
||||||
case 'datetime':
|
|
||||||
return parseDateTimeOrTimestamp(value);
|
|
||||||
default:
|
default:
|
||||||
return value;
|
return parseType({ type, value });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const parseDateOrThrow = format => value => {
|
|
||||||
try {
|
|
||||||
let date = parseISO(value);
|
|
||||||
if (isValid(date)) return date;
|
|
||||||
|
|
||||||
date = parse(value, format, new Date());
|
|
||||||
|
|
||||||
if (isValid(date)) {
|
|
||||||
return date;
|
|
||||||
} else {
|
|
||||||
throw new Error(`Invalid format, expected a ${format}`);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(`Invalid format, expected a ${format}`);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const parseDateTimeOrTimestamp = value => {
|
|
||||||
const date = parseISO(value);
|
|
||||||
if (isValid(date)) return date;
|
|
||||||
|
|
||||||
date.setTime(value);
|
|
||||||
|
|
||||||
if (!isValid(date)) {
|
|
||||||
throw new Error(`Invalid format, expected a timestamp or an ISO date`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return date;
|
|
||||||
};
|
|
||||||
|
|
||||||
const parseDate = parseDateOrThrow('yyyy-MM-dd');
|
|
||||||
|
|
||||||
module.exports = { createParser };
|
module.exports = { createParser };
|
||||||
|
@ -203,7 +203,7 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
|
|||||||
type: 'timestamp',
|
type: 'timestamp',
|
||||||
};
|
};
|
||||||
target[model].allAttributes[updatedAtCol] = {
|
target[model].allAttributes[updatedAtCol] = {
|
||||||
type: 'timestampUpdate',
|
type: 'timestamp',
|
||||||
};
|
};
|
||||||
} else if (timestampsOption === true) {
|
} else if (timestampsOption === true) {
|
||||||
schema.set('timestamps', true);
|
schema.set('timestamps', true);
|
||||||
@ -214,7 +214,7 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
|
|||||||
type: 'timestamp',
|
type: 'timestamp',
|
||||||
};
|
};
|
||||||
target[model].allAttributes.updatedAt = {
|
target[model].allAttributes.updatedAt = {
|
||||||
type: 'timestampUpdate',
|
type: 'timestamp',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
schema.set(
|
schema.set(
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const dates = require('date-fns');
|
||||||
|
|
||||||
const timeRegex = new RegExp(
|
const timeRegex = new RegExp(
|
||||||
'^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]{1,3})?$'
|
'^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]{1,3})?$'
|
||||||
@ -19,6 +20,30 @@ const parseTime = value => {
|
|||||||
return `${hours}:${minutes}:${seconds}.${fractionPart}`;
|
return `${hours}:${minutes}:${seconds}.${fractionPart}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const parseDate = value => {
|
||||||
|
try {
|
||||||
|
let date = dates.parseISO(value);
|
||||||
|
if (dates.isValid(date)) return dates.format(date, 'yyyy-MM-dd');
|
||||||
|
|
||||||
|
throw new Error(`Invalid format, expected an ISO compatble date`);
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Invalid format, expected an ISO compatble date`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const parseDateTimeOrTimestamp = value => {
|
||||||
|
const date = dates.parseISO(value);
|
||||||
|
if (dates.isValid(date)) return date;
|
||||||
|
|
||||||
|
dates.setTime(date, value);
|
||||||
|
|
||||||
|
if (!dates.isValid(date)) {
|
||||||
|
throw new Error(`Invalid format, expected a timestamp or an ISO date`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return date;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cast basic values based on attribute type
|
* Cast basic values based on attribute type
|
||||||
* @param {Object} options - Options
|
* @param {Object} options - Options
|
||||||
@ -47,6 +72,13 @@ const parseType = ({ type, value }) => {
|
|||||||
case 'time': {
|
case 'time': {
|
||||||
return parseTime(value);
|
return parseTime(value);
|
||||||
}
|
}
|
||||||
|
case 'date': {
|
||||||
|
return parseDate(value);
|
||||||
|
}
|
||||||
|
case 'timestamp':
|
||||||
|
case 'datetime': {
|
||||||
|
return parseDateTimeOrTimestamp(value);
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user