Skip to content
This repository was archived by the owner on Apr 17, 2020. It is now read-only.
Open
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
24 changes: 24 additions & 0 deletions lib/KnormPostgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,30 @@ class KnormPostgres {
return super.prepareSql(sql, options);
}

getWhereColumnAndValue(where) {
const [fieldName, value] = where;
const field = this.config.fields[fieldName];

if (
(field.type !== 'json' && field.type !== 'jsonb') ||
typeof value !== 'object'
) {
return super.getWhereColumnAndValue(where);
}

const entries = Object.entries(value);
const [[jsonKey, jsonValue]] = entries;

let column = this.getColumn(fieldName);
column = `${column}->'${jsonKey}'`;

if (field.type === 'json') {
column = `(${column})::jsonb`;
}

return [column, JSON.stringify(jsonValue)];
}

// TODO: strict mode: warn/throw if a patch field is invalid
// TODO: strict mode: warn/throw if a patch field is not a json(b) field
isPatchedField(field) {
Expand Down
Loading