kysely
Version:
Type safe SQL query builder
28 lines (27 loc) • 1.18 kB
JavaScript
/// <reference types="./safe-null-comparison-plugin.d.ts" />
import { SafeNullComparisonTransformer } from './safe-null-comparison-transformer.js';
/**
* Plugin that handles NULL comparisons to prevent common SQL mistakes.
*
* In SQL, comparing values with NULL using standard comparison operators (=, !=, <>)
* always yields NULL, which is usually not what developers expect. The correct way
* to compare with NULL is using IS NULL and IS NOT NULL.
*
* When working with nullable variables (e.g. string | null), you need to be careful to
* manually handle these cases with conditional WHERE clauses. This plugins automatically
* applies the correct operator based on the value, allowing you to simply write `query.where('name', '=', name)`.
*
* The plugin transforms the following operators when comparing with NULL:
* - `=` becomes `IS`
* - `!=` becomes `IS NOT`
* - `<>` becomes `IS NOT`
*/
export class SafeNullComparisonPlugin {
#transformer = new SafeNullComparisonTransformer();
transformQuery(args) {
return this.#transformer.transformNode(args.node);
}
transformResult(args) {
return Promise.resolve(args.result);
}
}