8000 fix: #143, #174, #175 by soedirgo · Pull Request #176 · supabase/postgres-meta · GitHub
[go: up one dir, main page]

Skip to content

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

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: multi-statement queries & arrayMode
  • Loading branch information
soedirgo committed Jan 26, 2022
commit f397839ee5506cb7ee4501e7c6435496a66c2d49
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)
}
}
40 changes: 40 additions & 0 deletions src/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 @@ -38,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
0