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

fix: #143, #174, #175 #176

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
30 changes: 23 additions & 7 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
"build:server": "tsc -p tsconfig.server.json && cpy 'src/lib/sql/*.sql' bin/src/lib/sql",
"docs:export": "PG_META_EXPORT_DOCS=true ts-node-dev src/server/app.ts",
"start": "NODE_ENV=production node bin/src/server/app.js",
"dev": "run-s db:clean db:run && NODE_ENV=development ts-node-dev src/server/app.ts | pino-pretty --colorize",
"dev": "trap 'npm run db:clean' INT && run-s db:clean db:run && NODE_ENV=development ts-node-dev src/server/app.ts | pino-pretty --colorize",
"pkg": "run-s clean build:server && pkg --out-path bin .pkg.config.json",
"test": "run-s db:clean db:run test:run db:clean",
"db:clean": "cd test/db && docker-compose down",
"db:run": "cd test/db && docker-compose up --detach && sleep 5",
"test:run": "jest --runInBand"
"test:run": "jest --runInBand",
"test:update": "run-s db:clean db:run && jest --runInBand --updateSnapshot && run-s db:clean"
},
"engines": {
"node": ">=14 <15",
Expand All @@ -38,6 +39,7 @@
"pg-format": "^1.0.4",
"pgsql-parser": "^13.1.11",
"pino": "^7.6.3",
"postgres-array": "^3.0.1",
"prettier": "^2.4.1",
"prettier-plugin-sql": "^0.4.0",
"sql-formatter": "^4.0.2"
Expand Down
33 changes: 16 additions & 17 deletions src/lib/PostgresMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import PostgresMetaTypes from './PostgresMetaTypes'
import PostgresMetaVersion from './PostgresMetaVersion'
import PostgresMetaViews from './PostgresMetaViews'
import { init } from './db'
import { PostgresMetaResult } from './types'
export default class PostgresMeta {
query: (sql: string) => Promise<PostgresMetaResult<any>>
query: (sql: string) => Promise<any>
end: () => Promise<void>
columns: PostgresMetaColumns
config: PostgresMetaConfig
Expand All @@ -37,21 +36,21 @@ export default class PostgresMeta {
format = Parser.Format

constructor(config: PoolConfig) {
const { query, end } = init(config)
this.query = query
const { query, queryArrayMode, end } = init(config)
this.query = queryArrayMode
this.end = end
this.columns = new PostgresMetaColumns(this.query)
this.config = new PostgresMetaConfig(this.query)
this.extensions = new PostgresMetaExtensions(this.query)
this.functions = new PostgresMetaFunctions(this.query)
this.policies = new PostgresMetaPolicies(this.query)
this.publications = new PostgresMetaPublications(this.query)
this.roles = new PostgresMetaRoles(this.query)
this.schemas = new PostgresMetaSchemas(this.query)
this.tables = new PostgresMetaTables(this.query)
this.triggers = new PostgresMetaTriggers(this.query)
this.types = new PostgresMetaTypes(this.query)
this.version = new PostgresMetaVersion(this.query)
this.views = new PostgresMetaViews(this.query)
this.columns = new PostgresMetaColumns(query)
this.config = new PostgresMetaConfig(query)
this.extensions = new PostgresMetaExtensions(query)
this.functions = new PostgresMetaFunctions(query)
this.policies = new PostgresMetaPolicies(query)
this.publications = new PostgresMetaPublications(query)
this.roles = new PostgresMetaRoles(query)
this.schemas = new PostgresMetaSchemas(query)
this.tables = new PostgresMetaTables(query)
this.triggers = new PostgresMetaTriggers(query)
this.types = new PostgresMetaTypes(query)
this.version = new PostgresMetaVersion(query)
this.views = new PostgresMetaViews(query)
}
}
49 changes: 48 additions & 1 deletion src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { types, Pool, PoolConfig } from 'pg'
import { parse as parseArray } from 'postgres-array'
import { PostgresMetaResult } from './types'

types.setTypeParser(20, parseInt)
types.setTypeParser(types.builtins.INT8, parseInt)
types.setTypeParser(types.builtins.DATE, (x) => x)
types.setTypeParser(types.builtins.TIMESTAMP, (x) => x)
types.setTypeParser(types.builtins.TIMESTAMPTZ, (x) => x)
types.setTypeParser(1115, parseArray) // _timestamp
types.setTypeParser(1182, parseArray) // _date
types.setTypeParser(1185, parseArray) // _timestamptz

export const init: (config: PoolConfig) => {
query: (sql: string) => Promise<PostgresMetaResult<any>>
queryArrayMode: (sql: string) => Promise<any>
end: () => Promise<void>
} = (config) => {
// NOTE: Race condition could happen here: one async task may be doing
Expand All @@ -31,6 +39,45 @@ export const init: (config: PoolConfig) => {
}
},

async queryArrayMode(sql) {
try {
if (!pool) {
const pool = new Pool(config)
let res: any = await pool.query({
rowMode: 'array',
text: sql,
})
if (!Array.isArray(res)) {
res = [res]
}
return {
data: res.map(({ fields, rows }: any) => ({
columns: fields.map((x: any) => x.name),
rows,
})),
error: null,
}
}

let res: any = await pool.query({
rowMode: 'array',
text: sql,
})
if (!Array.isArray(res)) {
res = [res]
}
return {
data: res.map(({ fields, rows }: any) => ({
columns: fields.map((x: any) => x.name),
rows,
})),
error: null,
}
} catch (e: any) {
return { data: null, error: { message: e.message } }
}
},

async end() {
const _pool = pool
pool = null
Expand Down
27 changes: 24 additions & 3 deletions test/lib/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,14 @@ AND i.indisprimary;
Object {
"data": Array [
Object {
"attname": "c",
"columns": Array [
"attname",
],
"rows": Array [
Array [
"c",
],
],
},
],
"error": null,
Expand Down Expand Up @@ -267,7 +274,14 @@ AND i.indisunique;
Object {
"data": Array [
Object {
"attname": "c",
"columns": Array [
"attname",
],
"rows": Array [
Array [
"c",
],
],
},
],
"error": null,
Expand Down Expand Up @@ -387,7 +401,14 @@ SELECT pg_get_constraintdef((
Object {
"data": Array [
Object {
"pg_get_constraintdef": null,
"columns": Array [
"pg_get_constraintdef",
],
"rows": Array [
Array [
null,
],
],
},
],
"error": null,
Expand Down
41 changes: 25 additions & 16 deletions test/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ test('query', async () => {
Object {
"data": Array [
Object {
"id": 1,
"name": "Joe Bloggs",
"status": "ACTIVE",
},
Object {
"id": 2,
"name": "Jane Doe",
"status": "ACTIVE",
"columns": Array [
"id",
"name",
"status",
],
"rows": Array [
Array [
1,
"Joe Bloggs",
"ACTIVE",
],
Array [
2,
"Jane Doe",
"ACTIVE",
],
],
},
],
"error": null,
Expand Down Expand Up @@ -463,14 +472,14 @@ CREATE TABLE table_name (

const deparse = pgMeta.deparse(res.data!)
expect(deparse.data).toMatchInlineSnapshot(`
"CREATE TABLE table_name (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
inserted_at pg_catalog.timestamptz DEFAULT ( timezone('utc'::text, now()) ) NOT NULL,
updated_at pg_catalog.timestamptz DEFAULT ( timezone('utc'::text, now()) ) NOT NULL,
data jsonb,
name text
);"
`)
"CREATE TABLE table_name (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
inserted_at pg_catalog.timestamptz DEFAULT ( timezone('utc'::text, now()) ) NOT NULL,
updated_at pg_catalog.timestamptz DEFAULT ( timezone('utc'::text, now()) ) NOT NULL,
data jsonb,
name text
);"
`)
})

test('formatter', async () => {
Expand Down
8 changes: 4 additions & 4 deletions test/lib/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ test('retrieve, create, update, delete', async () => {
"is_superuser": true,
"name": "r",
"password": "********",
"valid_until": 2020-01-01T00:00:00.000Z,
"valid_until": "2020-01-01 00:00:00+00",
},
"error": null,
}
Expand All @@ -467,7 +467,7 @@ test('retrieve, create, update, delete', async () => {
"is_superuser": true,
"name": "r",
"password": "********",
"valid_until": 2020-01-01T00:00:00.000Z,
"valid_until": "2020-01-01 00:00:00+00",
},
"error": null,
}
Expand Down Expand Up @@ -507,7 +507,7 @@ test('retrieve, create, update, delete', async () => {
"is_superuser": true,
"name": "rr",
"password": "********",
"valid_until": 2020-01-01T00:00:00.000Z,
"valid_until": "2020-01-01 00:00:00+00",
},
"error": null,
}
Expand All @@ -532,7 +532,7 @@ test('retrieve, create, update, delete', async () => {
"is_superuser": true,
"name": "rr",
"password": "********",
"valid_until": 2020-01-01T00:00:00.000Z,
"valid_until": "2020-01-01 00:00:00+00",
},
"error": null,
}
Expand Down