Skip to content

Commit

Permalink
support timestamp and json
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Dec 18, 2024
1 parent ca870df commit 3baea35
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"prepack": "npm run build",
"prepare": "husky install",
"test": "jest --verbose --testPathPattern=unit",
"test:connection": "jest --verbose --testPathPattern=connection",
"test:connection": "jest --verbose --testPathPattern=connection --runInBand",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage --testPathPattern=unit"
},
Expand Down
9 changes: 8 additions & 1 deletion src/connections/postgre/postgresql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client } from 'pg';
import { Client, types } from 'pg';
import { QueryResult } from '..';
import { Query } from '../../query';
import { AbstractDialect } from './../../query-builder';
Expand All @@ -17,6 +17,13 @@ export class PostgreSQLConnection extends PostgreBaseConnection {
constructor(pgClient: any) {
super();
this.client = pgClient;

this.client.setTypeParser(types.builtins.TIMESTAMP, str => str)
this.client.setTypeParser(types.builtins.DATE, str => str)
this.client.setTypeParser(types.builtins.TIMESTAMPTZ, str => str)
this.client.setTypeParser(types.builtins.TIME, str => str)
this.client.setTypeParser(types.builtins.TIMETZ, str => str)
this.client.setTypeParser(types.builtins.JSON, str => str);
}

async connect() {
Expand Down
11 changes: 7 additions & 4 deletions tests/connections/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ jest.setTimeout(10000);
beforeAll(async () => {
await db.connect();

// It is better to cleanup here in case any previous test failed
await db.dropTable(DEFAULT_SCHEMA, 'persons');
await db.dropTable(DEFAULT_SCHEMA, 'people');
await db.dropTable(DEFAULT_SCHEMA, 'teams');
// Clean up all tables
const schemaList = await db.fetchDatabaseSchema();
const currentSchema = schemaList[DEFAULT_SCHEMA];

for (const table of Object.values(currentSchema)) {
await db.dropTable(DEFAULT_SCHEMA, table.name)
}
});

afterAll(async () => {
Expand Down
87 changes: 87 additions & 0 deletions tests/connections/postgres.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import createTestClient from './create-test-connection';
const { client: db, defaultSchema: DEFAULT_SCHEMA } = createTestClient();

beforeAll(async () => {
if (process.env.CONNECTION_TYPE !== 'postgres') return;
await db.connect();
});

afterAll(async () => {
if (process.env.CONNECTION_TYPE !== 'postgres') return;
await db.disconnect();
});

describe("Postgres Specified Tests", () => {
test("Test timestamp data type", async () => {
if (process.env.CONNECTION_TYPE !== 'postgres') return;

await db.raw(`CREATE TABLE table_ts(
id SERIAL PRIMARY KEY,
ts TIMESTAMP,
date_column DATE
)`)

await db.insert(DEFAULT_SCHEMA, 'table_ts', {
id: 123,
ts: '2022-10-10 11:30:30',
date_column: '2022-10-10 00:00:00'
});

await db.insert(DEFAULT_SCHEMA, 'table_ts', {
id: 124,
ts: null,
date_column: null
});

const rows = await db.select(DEFAULT_SCHEMA, 'table_ts', {});

expect(rows.data.find(row => row.id === 123)).toEqual({
id: 123,
date_column: '2022-10-10',
ts:
'2022-10-10 11:30:30'
});

expect(rows.data.find(row => row.id === 124)).toEqual({
id: 124,
date_column: null,
ts: null
});
});

test("Test JSON data type", async () => {
if (process.env.CONNECTION_TYPE !== 'postgres') return;

await db.raw(`CREATE TABLE table_json(
id SERIAL PRIMARY KEY,
data_json JSON
)`)

const jsonData = JSON.stringify({
name: 'Outerbase',
age: 1000
})

await db.insert(DEFAULT_SCHEMA, 'table_json', {
id: 123,
data_json: jsonData
});

await db.insert(DEFAULT_SCHEMA, 'table_json', {
id: 124,
data_json: null
});

const rows = await db.select(DEFAULT_SCHEMA, 'table_json', {});

expect(rows.data.find(row => row.id === 123)).toEqual({
id: 123,
data_json: jsonData
});

expect(rows.data.find(row => row.id === 124)).toEqual({
id: 124,
data_json: null,
});
});
})

0 comments on commit 3baea35

Please sign in to comment.