Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support timestamp and json #63

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 --forceExit",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage --testPathPattern=unit"
},
Expand Down
38 changes: 19 additions & 19 deletions src/connections/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 = {
Expand Down Expand Up @@ -377,7 +377,7 @@ export class MySQLConnection extends SqlConnection {
);
}

async connect(): Promise<any> {}
async connect(): Promise<any> { }
async disconnect(): Promise<any> {
this.conn.destroy();
}
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,
});
});
})
Loading