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:
ChengLei Shao 2024-04-01 22:08:10 +08:00 committed by GitHub
parent aeb87a8e28
commit ba76c77212
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 84 additions and 6 deletions

View File

@ -1,9 +1,7 @@
import { Database } from '../database'; import { Database } from '../database';
import { mockDatabase } from './index'; import { mockDatabase } from './index';
const excludeSqlite = () => (process.env.DB_DIALECT != 'sqlite' ? describe : describe.skip); describe.runIf(process.env.DB_DIALECT == 'postgres')('collection', () => {
excludeSqlite()('collection', () => {
let db: Database; let db: Database;
beforeEach(async () => { beforeEach(async () => {
@ -53,4 +51,76 @@ excludeSqlite()('collection', () => {
expect(profileTableInfo[profile.model.rawAttributes['userId'].field].type).toBe('BIGINT'); 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);
});
}); });

View File

@ -248,8 +248,15 @@ export class Database extends EventEmitter implements AsyncEmitter {
} }
if (options.dialect === 'postgres') { if (options.dialect === 'postgres') {
// https://github.com/sequelize/sequelize/issues/1774 const types = require('pg').types;
require('pg').defaults.parseInt8 = true;
types.setTypeParser(types.builtins.INT8, function (val) {
if (val <= Number.MAX_SAFE_INTEGER) {
return Number(val);
}
return val;
});
} }
this.options = opts; this.options = opts;

View File

@ -1,4 +1,4 @@
import lodash from 'lodash'; import lodash, { isPlainObject } from 'lodash';
import { Model as SequelizeModel, ModelStatic } from 'sequelize'; import { Model as SequelizeModel, ModelStatic } from 'sequelize';
import { Collection } from './collection'; import { Collection } from './collection';
import { Database } from './database'; import { Database } from './database';

View File

@ -976,6 +976,7 @@ describe('formula field', () => {
const test = await Test.model.create<any>({ const test = await Test.model.create<any>({
a: BigInt(Number.MAX_SAFE_INTEGER), a: BigInt(Number.MAX_SAFE_INTEGER),
}); });
expect(test.get('result')).toBe(`${Number.MAX_SAFE_INTEGER}`); expect(test.get('result')).toBe(`${Number.MAX_SAFE_INTEGER}`);
}); });
}); });