Skip to content

Commit

Permalink
fix: switch Postgres from json to jsonb (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobdenOs authored Jan 9, 2024
1 parent 4ba4f32 commit c98a964
Showing 1 changed file with 7 additions and 7 deletions.
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

0 comments on commit c98a964

Please sign in to comment.