Skip to content

Commit

Permalink
feat: enable custom operators through use of sql tokens (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Mar 31, 2021
2 parents f611993 + 0254308 commit c4a77db
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/datasource/DBDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,10 @@ export default class DBDataSource<
...options,
expected: 'any',
where: {
[column]: this.builder.any(
[column]: sql`= ${this.builder.any(
([...args] as unknown) as (string | number | boolean | Date | null)[],
type
),
)}`,
...options?.where,
},
})
Expand Down
8 changes: 8 additions & 0 deletions src/datasource/__tests__/__snapshots__/queries.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ Object {
}
`;
exports[`QueryBuilder clause generators where enables custom operators through use of sql tokens 1`] = `
Object {
"sql": "WHERE (\\"any_table\\".\\"id\\" > 1)",
"type": "SLONIK_TOKEN_SQL",
"values": Array [],
}
`;
exports[`QueryBuilder clause generators where handles multiple values for a column 1`] = `
Object {
"sql": "WHERE (\\"any_table\\".\\"id\\" = ANY($1::these[]))",
Expand Down
4 changes: 4 additions & 0 deletions src/datasource/__tests__/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ describe(QueryBuilder, () => {
expect(builder.where(conditions)).toMatchSnapshot()
})

it('enables custom operators through use of sql tokens', () => {
expect(builder.where({ id: sql`> 1` })).toMatchSnapshot()
})

it('produces a valid clause with no conditions', () => {
expect(builder.where({})).toMatchSnapshot()
expect(builder.where([])).toMatchSnapshot()
Expand Down
2 changes: 2 additions & 0 deletions src/datasource/queries/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ export default class QueryBuilder<
} else {
return condition
}
} else if (isSqlSqlTokenType(value)) {
return sql`${this.identifier(column)} ${value}`
}

// We've already filtered out value === undefined above
Expand Down

0 comments on commit c4a77db

Please sign in to comment.