Skip to content

Commit e3937e4

Browse files
authored
Merge pull request #63 from outerbase/invisal/support-postgres-json-and-timestamp
support timestamp and json
2 parents ca870df + 3c31643 commit e3937e4

File tree

6 files changed

+124
-27
lines changed

6 files changed

+124
-27
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"prepack": "npm run build",
1818
"prepare": "husky install",
1919
"test": "jest --verbose --testPathPattern=unit",
20-
"test:connection": "jest --verbose --testPathPattern=connection",
20+
"test:connection": "jest --verbose --testPathPattern=connection --runInBand --forceExit",
2121
"test:watch": "jest --watch",
2222
"test:coverage": "jest --coverage --testPathPattern=unit"
2323
},

src/connections/mysql.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ export function buildMySQLDatabaseSchmea({
127127

128128
columnLookup[
129129
column.TABLE_SCHEMA +
130-
'.' +
131-
column.TABLE_NAME +
132-
'.' +
133-
column.COLUMN_NAME
130+
'.' +
131+
column.TABLE_NAME +
132+
'.' +
133+
column.COLUMN_NAME
134134
] = columnObject;
135135

136136
table.columns.push(columnObject);
@@ -156,10 +156,10 @@ export function buildMySQLDatabaseSchmea({
156156

157157
constraintLookup[
158158
constraint.TABLE_SCHEMA +
159-
'.' +
160-
constraint.TABLE_NAME +
161-
'.' +
162-
constraint.CONSTRAINT_NAME
159+
'.' +
160+
constraint.TABLE_NAME +
161+
'.' +
162+
constraint.CONSTRAINT_NAME
163163
] = constraintObject;
164164

165165
table.constraints.push(constraintObject);
@@ -169,22 +169,22 @@ export function buildMySQLDatabaseSchmea({
169169
for (const constraintColumn of constraintColumnsList) {
170170
const constraint =
171171
constraintLookup[
172-
constraintColumn.TABLE_SCHEMA +
173-
'.' +
174-
constraintColumn.TABLE_NAME +
175-
'.' +
176-
constraintColumn.CONSTRAINT_NAME
172+
constraintColumn.TABLE_SCHEMA +
173+
'.' +
174+
constraintColumn.TABLE_NAME +
175+
'.' +
176+
constraintColumn.CONSTRAINT_NAME
177177
];
178178

179179
if (!constraint) continue;
180180

181181
const currentColumn =
182182
columnLookup[
183-
constraintColumn.TABLE_SCHEMA +
184-
'.' +
185-
constraintColumn.TABLE_NAME +
186-
'.' +
187-
constraintColumn.COLUMN_NAME
183+
constraintColumn.TABLE_SCHEMA +
184+
'.' +
185+
constraintColumn.TABLE_NAME +
186+
'.' +
187+
constraintColumn.COLUMN_NAME
188188
];
189189
if (currentColumn && constraintColumn.REFERENCED_COLUMN_NAME) {
190190
currentColumn.definition.references = {
@@ -377,7 +377,7 @@ export class MySQLConnection extends SqlConnection {
377377
);
378378
}
379379

380-
async connect(): Promise<any> {}
380+
async connect(): Promise<any> { }
381381
async disconnect(): Promise<any> {
382382
this.conn.destroy();
383383
}

src/connections/postgre/postgresql.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client } from 'pg';
1+
import { Client, types } from 'pg';
22
import { QueryResult } from '..';
33
import { Query } from '../../query';
44
import { AbstractDialect } from './../../query-builder';
@@ -17,6 +17,13 @@ export class PostgreSQLConnection extends PostgreBaseConnection {
1717
constructor(pgClient: any) {
1818
super();
1919
this.client = pgClient;
20+
21+
this.client.setTypeParser(types.builtins.TIMESTAMP, str => str)
22+
this.client.setTypeParser(types.builtins.DATE, str => str)
23+
this.client.setTypeParser(types.builtins.TIMESTAMPTZ, str => str)
24+
this.client.setTypeParser(types.builtins.TIME, str => str)
25+
this.client.setTypeParser(types.builtins.TIMETZ, str => str)
26+
this.client.setTypeParser(types.builtins.JSON, str => str);
2027
}
2128

2229
async connect() {

tests/connections/connection.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ jest.setTimeout(10000);
88
beforeAll(async () => {
99
await db.connect();
1010

11-
// It is better to cleanup here in case any previous test failed
12-
await db.dropTable(DEFAULT_SCHEMA, 'persons');
13-
await db.dropTable(DEFAULT_SCHEMA, 'people');
14-
await db.dropTable(DEFAULT_SCHEMA, 'teams');
11+
// Clean up all tables
12+
const schemaList = await db.fetchDatabaseSchema();
13+
const currentSchema = schemaList[DEFAULT_SCHEMA] ?? {};
14+
15+
for (const table of Object.values(currentSchema)) {
16+
await db.dropTable(DEFAULT_SCHEMA, table.name)
17+
}
1518
});
1619

1720
afterAll(async () => {

tests/connections/postgres.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import createTestClient from './create-test-connection';
2+
const { client: db, defaultSchema: DEFAULT_SCHEMA } = createTestClient();
3+
4+
beforeAll(async () => {
5+
if (process.env.CONNECTION_TYPE !== 'postgres') return;
6+
await db.connect();
7+
});
8+
9+
afterAll(async () => {
10+
if (process.env.CONNECTION_TYPE !== 'postgres') return;
11+
await db.disconnect();
12+
});
13+
14+
describe("Postgres Specified Tests", () => {
15+
test("Test timestamp data type", async () => {
16+
if (process.env.CONNECTION_TYPE !== 'postgres') return;
17+
18+
await db.raw(`CREATE TABLE table_ts(
19+
id SERIAL PRIMARY KEY,
20+
ts TIMESTAMP,
21+
date_column DATE
22+
)`)
23+
24+
await db.insert(DEFAULT_SCHEMA, 'table_ts', {
25+
id: 123,
26+
ts: '2022-10-10 11:30:30',
27+
date_column: '2022-10-10 00:00:00'
28+
});
29+
30+
await db.insert(DEFAULT_SCHEMA, 'table_ts', {
31+
id: 124,
32+
ts: null,
33+
date_column: null
34+
});
35+
36+
const rows = await db.select(DEFAULT_SCHEMA, 'table_ts', {});
37+
38+
expect(rows.data.find(row => row.id === 123)).toEqual({
39+
id: 123,
40+
date_column: '2022-10-10',
41+
ts:
42+
'2022-10-10 11:30:30'
43+
});
44+
45+
expect(rows.data.find(row => row.id === 124)).toEqual({
46+
id: 124,
47+
date_column: null,
48+
ts: null
49+
});
50+
});
51+
52+
test("Test JSON data type", async () => {
53+
if (process.env.CONNECTION_TYPE !== 'postgres') return;
54+
55+
await db.raw(`CREATE TABLE table_json(
56+
id SERIAL PRIMARY KEY,
57+
data_json JSON
58+
)`)
59+
60+
const jsonData = JSON.stringify({
61+
name: 'Outerbase',
62+
age: 1000
63+
})
64+
65+
await db.insert(DEFAULT_SCHEMA, 'table_json', {
66+
id: 123,
67+
data_json: jsonData
68+
});
69+
70+
await db.insert(DEFAULT_SCHEMA, 'table_json', {
71+
id: 124,
72+
data_json: null
73+
});
74+
75+
const rows = await db.select(DEFAULT_SCHEMA, 'table_json', {});
76+
77+
expect(rows.data.find(row => row.id === 123)).toEqual({
78+
id: 123,
79+
data_json: jsonData
80+
});
81+
82+
expect(rows.data.find(row => row.id === 124)).toEqual({
83+
id: 124,
84+
data_json: null,
85+
});
86+
});
87+
})

0 commit comments

Comments
 (0)