From 98a5b9c0e1e10a2c0a7a66f53557247e1f12bbf2 Mon Sep 17 00:00:00 2001 From: Endeavorance Date: Fri, 18 Apr 2025 10:39:23 -0400 Subject: [PATCH] Check support in columns --- src/column-presets.ts | 15 +++++++++++---- src/columns.ts | 7 +++++++ src/prequel.ts | 16 +++++++++------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/column-presets.ts b/src/column-presets.ts index 63932e5..c053ebc 100644 --- a/src/column-presets.ts +++ b/src/column-presets.ts @@ -2,10 +2,17 @@ import type { ColumnShorthand } from "./columns"; import type { Table } from "./table"; export const ColumnOf = { - PrimaryKey: { - type: "INTEGER", - primary: true, - autoincrement: true, + Primary: { + Int: { + type: "INTEGER", + primary: true, + autoincrement: true, + }, + + Text: { + type: "TEXT", + primary: true, + }, }, Text: { diff --git a/src/columns.ts b/src/columns.ts index 39e73e4..bf2f502 100644 --- a/src/columns.ts +++ b/src/columns.ts @@ -20,6 +20,7 @@ export interface Column { default: string | number | undefined; unique: boolean; autoincrement: boolean; + check: string; } export type ColumnShorthand = DataType | Partial; @@ -61,6 +62,10 @@ export function generateColumnDefinitionSQL( } } + if (colDef.check) { + parts.push(`CHECK (${colDef.check})`); + } + if (colDef.misc.length) { parts.push(colDef.misc); } @@ -80,6 +85,7 @@ function expandColumnShorthand(col: ColumnShorthand): Column { default: undefined, unique: false, autoincrement: false, + check: "", }; } @@ -93,6 +99,7 @@ function expandColumnShorthand(col: ColumnShorthand): Column { default: col.default ?? undefined, unique: col.unique ?? false, autoincrement: col.autoincrement ?? false, + check: col.check ?? "", }; } diff --git a/src/prequel.ts b/src/prequel.ts index b2c8097..f7d19ed 100644 --- a/src/prequel.ts +++ b/src/prequel.ts @@ -77,11 +77,7 @@ export class PrequelKVStore { * @returns `true` if the key exists, `false` otherwise. */ has(key: string): boolean { - return ( - this.table.findOneWhere({ - key, - }) !== null - ); + return this.table.existsWhere({ key }); } /** @@ -89,7 +85,10 @@ export class PrequelKVStore { * @returns The keys in the store. */ keys(): string[] { - return this.table.findAll().map((row) => row.key); + return this.table + .prepare<{ key: string }, []>(`SELECT key FROM ${this.table}`) + .all() + .map(({ key }) => key); } /** @@ -97,7 +96,10 @@ export class PrequelKVStore { * @returns The values in the store. */ values(): unknown[] { - return this.table.findAll().map((row) => JSON.parse(row.value)); + return this.table + .prepare<{ value: string }, []>(`SELECT value FROM ${this.table}`) + .all() + .map(({ value }) => JSON.parse(value)); } }