Skip to content
This repository was archived by the owner on Apr 17, 2020. It is now read-only.
This repository was archived by the owner on Apr 17, 2020. It is now read-only.

Support json fields in the fields query option #38

@joelmukuthu

Description

@joelmukuthu
Model.fields { foo: 'json' };
Model.table = 'data';
Model.fetch({
  fields: [{ foo: { bar: true } }, { foo: { quux: true } }]
});
// or with an alias:
Model.fetch({
  fields: { alias1: { foo: { bar: true } }, alias2: { foo: { quux: true } } } }
});

that yields:

-- without aliases:
SELECT "foo"->'bar' as "foo.bar", "foo"->'quux' as "foo.quux" FROM "data";
-- with an alias:
SELECT "foo"->'bar' as "alias1", "foo"->'quux' as "alias2" FROM "data";

however, since using an object allows one to use multiple keys, one could add multiple json paths for only one alias when using the aliased form which would result in undesired/unexpected behaviour. therefore, it makes sense to enforce a single key per object (in both the non-aliased and aliased forms for conformity).

note that the field value should get automatically JSON-parsed so it shouldn't be necessary to use the ->> operator:

// only values coming from the database will be strings
if (typeof value === 'string') {
try {
return JSON.parse(value);
} catch (e) {
// root-level string JSON values are already "parsed"
// TODO: should this error be propagated?
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions