diff --git a/README.md b/README.md index 1f0dc05..73de8cb 100644 --- a/README.md +++ b/README.md @@ -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', @@ -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 diff --git a/src/operator.ts b/src/operator.ts index d86a5f3..dce6269 100644 --- a/src/operator.ts +++ b/src/operator.ts @@ -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 ?'); diff --git a/test/client.test.ts b/test/client.test.ts index e1160f1..c0215ed 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -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 + 'm@fengmk2.com' ]); await db.query(`insert into ??(name, email, gmt_create, gmt_modified) values(?, ?, now(), now())`, [ table, prefix + 'fengmk3', prefix + 'm@fengmk2.com' ]); diff --git a/test/operator.test.ts b/test/operator.test.ts index d421a7d..e8aa3a5 100644 --- a/test/operator.test.ts +++ b/test/operator.test.ts @@ -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 {} @@ -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'); }); });