From 3baea35684c9356c3d75212bfe768385c3e357f8 Mon Sep 17 00:00:00 2001 From: Visal In Date: Wed, 18 Dec 2024 19:27:10 +0800 Subject: [PATCH 1/3] support timestamp and json --- package-lock.json | 4 +- package.json | 2 +- src/connections/postgre/postgresql.ts | 9 ++- tests/connections/connection.test.ts | 11 ++-- tests/connections/postgres.test.ts | 87 +++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 tests/connections/postgres.test.ts diff --git a/package-lock.json b/package-lock.json index 3bab59f..59f5873 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@outerbase/sdk", - "version": "2.0.0-rc.1", + "version": "2.0.0-rc.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@outerbase/sdk", - "version": "2.0.0-rc.1", + "version": "2.0.0-rc.3", "license": "MIT", "dependencies": { "handlebars": "^4.7.8" diff --git a/package.json b/package.json index 61d60dd..5f85f20 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/connections/postgre/postgresql.ts b/src/connections/postgre/postgresql.ts index 4c36088..8436c97 100644 --- a/src/connections/postgre/postgresql.ts +++ b/src/connections/postgre/postgresql.ts @@ -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'; @@ -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() { diff --git a/tests/connections/connection.test.ts b/tests/connections/connection.test.ts index 8580605..4b14a97 100644 --- a/tests/connections/connection.test.ts +++ b/tests/connections/connection.test.ts @@ -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 () => { diff --git a/tests/connections/postgres.test.ts b/tests/connections/postgres.test.ts new file mode 100644 index 0000000..6c66e01 --- /dev/null +++ b/tests/connections/postgres.test.ts @@ -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, + }); + }); +}) \ No newline at end of file From 1484f6b5a66573f2087bd438f7c427cca0415028 Mon Sep 17 00:00:00 2001 From: Visal In Date: Wed, 18 Dec 2024 19:40:28 +0800 Subject: [PATCH 2/3] check if it will pass mysql weird error --- tests/connections/postgres.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/connections/postgres.test.ts b/tests/connections/postgres.test.ts index 6c66e01..1a86d78 100644 --- a/tests/connections/postgres.test.ts +++ b/tests/connections/postgres.test.ts @@ -2,12 +2,10 @@ 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(); }); From 3c31643e8308cc63b721702b6a51a27a97aa9add Mon Sep 17 00:00:00 2001 From: Visal In Date: Wed, 18 Dec 2024 19:58:46 +0800 Subject: [PATCH 3/3] add force exist jest --- package.json | 2 +- src/connections/mysql.ts | 38 ++++++++++++++-------------- tests/connections/connection.test.ts | 2 +- tests/connections/postgres.test.ts | 2 ++ 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 5f85f20..dcdcc92 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "prepack": "npm run build", "prepare": "husky install", "test": "jest --verbose --testPathPattern=unit", - "test:connection": "jest --verbose --testPathPattern=connection --runInBand", + "test:connection": "jest --verbose --testPathPattern=connection --runInBand --forceExit", "test:watch": "jest --watch", "test:coverage": "jest --coverage --testPathPattern=unit" }, diff --git a/src/connections/mysql.ts b/src/connections/mysql.ts index 33321b4..0882de5 100644 --- a/src/connections/mysql.ts +++ b/src/connections/mysql.ts @@ -127,10 +127,10 @@ export function buildMySQLDatabaseSchmea({ columnLookup[ column.TABLE_SCHEMA + - '.' + - column.TABLE_NAME + - '.' + - column.COLUMN_NAME + '.' + + column.TABLE_NAME + + '.' + + column.COLUMN_NAME ] = columnObject; table.columns.push(columnObject); @@ -156,10 +156,10 @@ export function buildMySQLDatabaseSchmea({ constraintLookup[ constraint.TABLE_SCHEMA + - '.' + - constraint.TABLE_NAME + - '.' + - constraint.CONSTRAINT_NAME + '.' + + constraint.TABLE_NAME + + '.' + + constraint.CONSTRAINT_NAME ] = constraintObject; table.constraints.push(constraintObject); @@ -169,22 +169,22 @@ export function buildMySQLDatabaseSchmea({ for (const constraintColumn of constraintColumnsList) { const constraint = constraintLookup[ - constraintColumn.TABLE_SCHEMA + - '.' + - constraintColumn.TABLE_NAME + - '.' + - constraintColumn.CONSTRAINT_NAME + constraintColumn.TABLE_SCHEMA + + '.' + + constraintColumn.TABLE_NAME + + '.' + + constraintColumn.CONSTRAINT_NAME ]; if (!constraint) continue; const currentColumn = columnLookup[ - constraintColumn.TABLE_SCHEMA + - '.' + - constraintColumn.TABLE_NAME + - '.' + - constraintColumn.COLUMN_NAME + constraintColumn.TABLE_SCHEMA + + '.' + + constraintColumn.TABLE_NAME + + '.' + + constraintColumn.COLUMN_NAME ]; if (currentColumn && constraintColumn.REFERENCED_COLUMN_NAME) { currentColumn.definition.references = { @@ -377,7 +377,7 @@ export class MySQLConnection extends SqlConnection { ); } - async connect(): Promise {} + async connect(): Promise { } async disconnect(): Promise { this.conn.destroy(); } diff --git a/tests/connections/connection.test.ts b/tests/connections/connection.test.ts index 4b14a97..ba546b2 100644 --- a/tests/connections/connection.test.ts +++ b/tests/connections/connection.test.ts @@ -10,7 +10,7 @@ beforeAll(async () => { // Clean up all tables const schemaList = await db.fetchDatabaseSchema(); - const currentSchema = schemaList[DEFAULT_SCHEMA]; + const currentSchema = schemaList[DEFAULT_SCHEMA] ?? {}; for (const table of Object.values(currentSchema)) { await db.dropTable(DEFAULT_SCHEMA, table.name) diff --git a/tests/connections/postgres.test.ts b/tests/connections/postgres.test.ts index 1a86d78..6c66e01 100644 --- a/tests/connections/postgres.test.ts +++ b/tests/connections/postgres.test.ts @@ -2,10 +2,12 @@ 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(); });