Skip to content

Commit

Permalink
feat: support literal in where options (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
rockdai committed Jul 8, 2024
1 parent 70869e2 commit 271bd68
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const options = [{
email: 'm@fengmk2_2.com',
otherField: 'other field value2',
modifiedAt: db.literals.now, // `now()` on db server
},
},
where: {
id: 124,
name: 'fengmk2_2',
Expand Down Expand Up @@ -408,6 +408,12 @@ INSERT INTO `user` SET `name` = 'fengmk2', `createdAt` = now()
const session = new db.literals.Literal('session()');
```

```js
const row = await db.get('table-name', { deletedAt: new db.literals.Literal('IS NOT NULL') });

=> SELECT * FROM `table-name` WHERE `deletedAt` IS NOT NULL
```

## Class Relation

```txt
Expand Down
2 changes: 2 additions & 0 deletions src/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ export abstract class Operator {
const value = where[key];
if (Array.isArray(value)) {
wheres.push('?? IN (?)');
} else if (value instanceof literals.Literal) {
wheres.push('?? ?');
} else {
if (value === null || value === undefined) {
wheres.push('?? IS ?');
Expand Down
2 changes: 1 addition & 1 deletion test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('test/client.test.ts', () => {

describe('query(), queryOne()', () => {
before(async () => {
await db.query(`insert into ??(name, email, gmt_create, gmt_modified)
await db.query(`insert into ??(name, email, gmt_create, gmt_modified)
values(?, ?, now(), now())`, [ table, prefix + 'fengmk2', prefix + '[email protected]' ]);
await db.query(`insert into ??(name, email, gmt_create, gmt_modified)
values(?, ?, now(), now())`, [ table, prefix + 'fengmk3', prefix + '[email protected]' ]);
Expand Down
2 changes: 2 additions & 0 deletions test/operator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { strict as assert } from 'node:assert';
import { Operator } from '../src/operator';
import { Literal } from '../src/literals';

class CustomOperator extends Operator {}

Expand All @@ -21,6 +22,7 @@ describe('test/operator.test.ts', () => {
assert.equal(op._where({ id: 1, name: 'foo\'\"' }), ' WHERE `id` = 1 AND `name` = \'foo\\\'\\\"\'');
assert.equal(op._where({ id: 1, name: 'foo\'\"', user: 'fengmk2' }),
' WHERE `id` = 1 AND `name` = \'foo\\\'\\\"\' AND `user` = \'fengmk2\'');
assert.equal(op._where({ name: new Literal('IS NOT NULL') }), ' WHERE `name` IS NOT NULL');
});
});

Expand Down

0 comments on commit 271bd68

Please sign in to comment.