mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
chore: return bigInt as string type (#3887)
* chore: test * chore: return bigInt as string * chore: test * chore: test * chore: test * chore: test * chore: test * chore: test * chore: postgres BIGINT parser * chore: test
This commit is contained in:
parent
aeb87a8e28
commit
ba76c77212
@ -1,9 +1,7 @@
|
||||
import { Database } from '../database';
|
||||
import { mockDatabase } from './index';
|
||||
|
||||
const excludeSqlite = () => (process.env.DB_DIALECT != 'sqlite' ? describe : describe.skip);
|
||||
|
||||
excludeSqlite()('collection', () => {
|
||||
describe.runIf(process.env.DB_DIALECT == 'postgres')('collection', () => {
|
||||
let db: Database;
|
||||
|
||||
beforeEach(async () => {
|
||||
@ -53,4 +51,76 @@ excludeSqlite()('collection', () => {
|
||||
expect(profileTableInfo[profile.model.rawAttributes['userId'].field].type).toBe('BIGINT');
|
||||
}
|
||||
});
|
||||
|
||||
it('should handle with number bigger than javascript MAX_SAFE_INTEGER ', async () => {
|
||||
const Test = db.collection({
|
||||
name: 'test',
|
||||
autoGenId: false,
|
||||
fields: [
|
||||
{
|
||||
type: 'bigInt',
|
||||
name: 'id',
|
||||
primaryKey: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await db.sync();
|
||||
|
||||
await Test.repository.create({
|
||||
values: {
|
||||
id: '35809622393264128',
|
||||
},
|
||||
});
|
||||
|
||||
const item = await Test.repository.findOne();
|
||||
|
||||
console.log(item.toJSON());
|
||||
expect(item.toJSON()['id']).toBe('35809622393264128');
|
||||
});
|
||||
|
||||
it('should return number type when bigint is less than MAX_SAFE_INTEGER', async () => {
|
||||
const Test = db.collection({
|
||||
name: 'test',
|
||||
autoGenId: false,
|
||||
fields: [
|
||||
{
|
||||
type: 'bigInt',
|
||||
name: 'id',
|
||||
primaryKey: true,
|
||||
},
|
||||
{
|
||||
type: 'bigInt',
|
||||
name: 'id2',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await db.sync();
|
||||
|
||||
await Test.repository.create({
|
||||
values: [
|
||||
{
|
||||
id: '123456',
|
||||
id2: '35809622393264128',
|
||||
},
|
||||
{
|
||||
id: '35809622393264128',
|
||||
id2: '123456',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const item = await Test.repository.findOne();
|
||||
|
||||
expect(item.toJSON()['id']).toBe(123456);
|
||||
expect(item.id).toBe(123456);
|
||||
expect(item['id']).toBe(123456);
|
||||
|
||||
const items = await Test.repository.find({
|
||||
raw: true,
|
||||
});
|
||||
|
||||
expect(items[0]['id']).toBe(123456);
|
||||
});
|
||||
});
|
||||
|
@ -248,8 +248,15 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
||||
}
|
||||
|
||||
if (options.dialect === 'postgres') {
|
||||
// https://github.com/sequelize/sequelize/issues/1774
|
||||
require('pg').defaults.parseInt8 = true;
|
||||
const types = require('pg').types;
|
||||
|
||||
types.setTypeParser(types.builtins.INT8, function (val) {
|
||||
if (val <= Number.MAX_SAFE_INTEGER) {
|
||||
return Number(val);
|
||||
}
|
||||
|
||||
return val;
|
||||
});
|
||||
}
|
||||
|
||||
this.options = opts;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import lodash from 'lodash';
|
||||
import lodash, { isPlainObject } from 'lodash';
|
||||
import { Model as SequelizeModel, ModelStatic } from 'sequelize';
|
||||
import { Collection } from './collection';
|
||||
import { Database } from './database';
|
||||
|
@ -976,6 +976,7 @@ describe('formula field', () => {
|
||||
const test = await Test.model.create<any>({
|
||||
a: BigInt(Number.MAX_SAFE_INTEGER),
|
||||
});
|
||||
|
||||
expect(test.get('result')).toBe(`${Number.MAX_SAFE_INTEGER}`);
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user