Support biginteger < 2^53

This commit is contained in:
Alexandre Bodin 2019-08-08 14:29:09 +02:00
parent b35712acc5
commit 4ad7f5bd89
4 changed files with 21 additions and 14 deletions

View File

@ -378,9 +378,8 @@ const getType = ({ definition, attribute, name, tableExists = false }) => {
case 'integer':
return client === 'pg' ? 'integer' : 'int';
case 'biginteger':
if (client === 'pg') return 'bigint';
if (client === 'sqlite3') return 'varchar(255)';
return 'bigint(53)';
if (client === 'sqlite3') return 'bigint(53)'; // no choice until the sqlite3 package supports returning strings for big integers
return 'bigint';
case 'float':
return client === 'pg' ? 'double precision' : 'double';
case 'decimal':

View File

@ -762,9 +762,11 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
];
const formatter = attributes => {
Object.keys(attributes).map(key => {
Object.keys(attributes).forEach(key => {
const attr = definition.attributes[key] || {};
if (attributes[key] === null) return;
if (attr.type === 'json') {
attributes[key] = JSON.parse(attributes[key]);
}
@ -774,11 +776,7 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
return;
}
const strVal =
attributes[key] !== null
? attributes[key].toString()
: attributes[key];
const strVal = attributes[key].toString();
if (strVal === '1') {
attributes[key] = true;
} else if (strVal === '0') {
@ -788,9 +786,16 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
}
}
if (attr.type === 'date' && definition.client == 'sqlite3') {
if (attr.type === 'date' && definition.client === 'sqlite3') {
attributes[key] = dateFns.parse(attributes[key]);
}
if (
attr.type === 'biginteger' &&
definition.client === 'sqlite3'
) {
attributes[key] = attributes[key].toString();
}
});
};
@ -868,6 +873,9 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
};
const castValueFromType = (type, value, definition) => {
// do not cast null values
if (value === null) return null;
switch (type) {
case 'json': {
if (definition.client === 'mysql' || definition.client === 'sqlite3') {

View File

@ -46,7 +46,7 @@ module.exports = (mongoose = Mongoose) => {
case 'json':
return 'Mixed';
case 'biginteger':
return 'String';
return 'Long';
case 'integer':
return 'Number';
case 'uuid':

View File

@ -20,7 +20,7 @@ describe('Test type biginteger', () => {
}, 60000);
test('Create entry with value input JSON', async () => {
const inputValue = '1223372036854775807';
const inputValue = '1223372036854775';
const res = await rq.post('/content-manager/explorer/withbiginteger', {
body: {
field: inputValue,
@ -34,7 +34,7 @@ describe('Test type biginteger', () => {
});
test('Create entry with value input Formdata', async () => {
const inputValue = '1223372036854775807';
const inputValue = '1223372036854775';
const res = await rq.post('/content-manager/explorer/withbiginteger', {
formData: {
data: JSON.stringify({ field: inputValue }),
@ -72,7 +72,7 @@ describe('Test type biginteger', () => {
});
test('Updating entry sets the right value and format', async () => {
const inputValue = '1223372036854775807';
const inputValue = '1223372036854775';
const res = await rq.post('/content-manager/explorer/withbiginteger', {
body: {
field: inputValue,