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

Implement missing JSON operators #305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
48 changes: 48 additions & 0 deletions src/schema/pg-catalog/binary-operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,54 @@ function registerJsonOperators(schema: _ISchema) {
implementation: (a, b) => queryJson(b, a),
});

// ======= "json <@ json" query operator
schema.registerOperator({
operator: '<@',
left: Types.jsonb,
right: Types.jsonb,
returns: Types.bool,
implementation: (a, b) => queryJson(a, b),
});

// ======= "json ? text"
schema.registerOperator({
operator: '?',
left: Types.jsonb,
right: Types.text(),
returns: Types.bool,
implementation: (a: Record<string, unknown> | unknown[], b: string) => {
if (a instanceof Array) {
return a.includes(b);
} else {
return Object.keys(a).includes(b);
}
},
})

// ======= "json ?| text[]"
schema.registerOperator({
operator: '?|',
left: Types.jsonb,
right: Types.text().asArray(),
returns: Types.bool,
implementation: (a: Record<string, unknown>, b: string[]) => b.some((value) => Object.keys(a).includes(value)),
})

// ======= "json ?& text[]"
schema.registerOperator({
operator: '?&',
left: Types.jsonb,
right: Types.text().asArray(),
returns: Types.bool,
implementation: (a: Record<string, unknown> | unknown[], b: string[]) => {
if (a instanceof Array) {
return b.every((value) => a.includes(value));
} else {
return b.every((value) => Object.keys(a).includes(value));
}
},
})

// ======= "json - text" (remove key)
schema.registerOperator({
operator: '-',
Expand Down
43 changes: 42 additions & 1 deletion src/tests/binary-operators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ describe('Binary operators', () => {
.to.deep.equal({ test: ['b', 'b', 'c'] });
expect(one(`select '["a", "b", "b", "c"]'::jsonb - 3 as test`))
.to.deep.equal({ test: ['a', 'b', 'b'] });
})
});


// https://www.postgresql.org/docs/15/functions-json.html

it('supports Is the first JSON value contained in the second?', () => {
expect(one(`select '{"b":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb as test`))
.to.deep.equal({ test: true });
expect(one(`select '{"a":1, "b":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb as test`))
.to.deep.equal({ test: true });
expect(one(`select '{"c":3}'::jsonb <@ '{"a":1, "b":2}'::jsonb as test`))
.to.deep.equal({ test: false });
});

it('supports Does the text string exist as a top-level key or array element within the JSON value?', () => {
expect(one(`select '{"a":1, "b":2}'::jsonb ? 'a' as test`))
.to.deep.equal({ test: true });
expect(one(`select '{"a":1, "b":2}'::jsonb ? 'b' as test`))
.to.deep.equal({ test: true });
expect(one(`select '{"a":1, "b":2}'::jsonb ? 'c' as test`))
.to.deep.equal({ test: false });

expect(one(`select '["a", "b", "c"]'::jsonb ? 'b' as test`))
.to.deep.equal({ test: true });
});

it('supports Do any of the strings in the text array exist as top-level keys or array elements?', () => {
expect(one(`select '{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'd'] as test`))
.to.deep.equal({ test: true });
expect(one(`select '{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'c'] as test`))
.to.deep.equal({ test: true });
expect(one(`select '{"a":1, "b":2, "c":3}'::jsonb ?| array['d', 'e'] as test`))
.to.deep.equal({ test: false });
});

it('supports Do all of the strings in the text array exist as top-level keys or array elements?', () => {
expect(one(`select '{"a":1, "b":2, "c":3}'::jsonb ?& array['a', 'b'] as test`))
.to.deep.equal({ test: true });

expect(one(`select '["a", "b", "c"]'::jsonb ?& array['a', 'b'] as test`))
.to.deep.equal({ test: true });
});

});