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: switch Postgres from json to jsonb #402

Merged
merged 2 commits into from
Jan 9, 2024
Merged
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
14 changes: 7 additions & 7 deletions postgres/lib/PostgresService.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class PostgresService extends SQLService {
}

return Promise.all([
(await this.prepare(`SELECT set_config(key::text,$1->>key,false) FROM json_each($1);`)).run([
(await this.prepare(`SELECT set_config(key::text,$1->>key,false) FROM jsonb_each($1);`)).run([
JSON.stringify(env),
]),
...(this.options?.credentials?.schema
Expand Down Expand Up @@ -339,9 +339,9 @@ GROUP BY k
return col
})
// REVISIT: Remove SELECT ${cols} by adjusting SELECT_columns
let obj = `row_to_json(${queryAlias}.*)`
let obj = `to_jsonb(${queryAlias}.*)`
return `SELECT ${
SELECT.one || SELECT.expand === 'root' ? obj : `coalesce(json_agg(${obj}),'[]'::json)`
SELECT.one || SELECT.expand === 'root' ? obj : `coalesce(jsonb_agg (${obj}),'[]'::jsonb)`
} as _json_ FROM (SELECT ${cols} FROM (${sql}) as ${queryAlias}) as ${queryAlias}`
}

Expand All @@ -357,8 +357,8 @@ GROUP BY k
// Adjusts json path expressions to be postgres specific
.replace(/->>'\$(?:(?:\."(.*?)")|(?:\[(\d*)\]))'/g, (a, b, c) => (b ? `->>'${b}'` : `->>${c}`))
// Adjusts json function to be postgres specific
.replace('json_each(?)', 'json_array_elements($1::JSON)')
.replace(/json_type\((\w+),'\$\."(\w+)"'\)/g, (_a, b, c) => `json_typeof(${b}->'${c}')`))
.replace('json_each(?)', 'jsonb_array_elements($1::jsonb)')
.replace(/json_type\((\w+),'\$\."(\w+)"'\)/g, (_a, b, c) => `jsonb_typeof(${b}->'${c}')`))
}

param({ ref }) {
Expand Down Expand Up @@ -430,8 +430,8 @@ GROUP BY k
Timestamp: e => `to_char(${e}, 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')`,
UTCDateTime: e => `to_char(${e}, 'YYYY-MM-DD"T"HH24:MI:SS"Z"')`,
UTCTimestamp: e => `to_char(${e}, 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')`,
struct: e => `json(${e})`,
array: e => `json(${e})`,
struct: e => `jsonb(${e})`,
array: e => `jsonb(${e})`,
}
}

Expand Down
Loading