Check support in columns

This commit is contained in:
Endeavorance 2025-04-18 10:39:23 -04:00
parent 153725f785
commit 98a5b9c0e1
3 changed files with 27 additions and 11 deletions

View file

@ -2,12 +2,19 @@ import type { ColumnShorthand } from "./columns";
import type { Table } from "./table"; import type { Table } from "./table";
export const ColumnOf = { export const ColumnOf = {
PrimaryKey: { Primary: {
Int: {
type: "INTEGER", type: "INTEGER",
primary: true, primary: true,
autoincrement: true, autoincrement: true,
}, },
Text: {
type: "TEXT",
primary: true,
},
},
Text: { Text: {
type: "TEXT", type: "TEXT",
nullable: false, nullable: false,

View file

@ -20,6 +20,7 @@ export interface Column {
default: string | number | undefined; default: string | number | undefined;
unique: boolean; unique: boolean;
autoincrement: boolean; autoincrement: boolean;
check: string;
} }
export type ColumnShorthand = DataType | Partial<Column>; export type ColumnShorthand = DataType | Partial<Column>;
@ -61,6 +62,10 @@ export function generateColumnDefinitionSQL(
} }
} }
if (colDef.check) {
parts.push(`CHECK (${colDef.check})`);
}
if (colDef.misc.length) { if (colDef.misc.length) {
parts.push(colDef.misc); parts.push(colDef.misc);
} }
@ -80,6 +85,7 @@ function expandColumnShorthand(col: ColumnShorthand): Column {
default: undefined, default: undefined,
unique: false, unique: false,
autoincrement: false, autoincrement: false,
check: "",
}; };
} }
@ -93,6 +99,7 @@ function expandColumnShorthand(col: ColumnShorthand): Column {
default: col.default ?? undefined, default: col.default ?? undefined,
unique: col.unique ?? false, unique: col.unique ?? false,
autoincrement: col.autoincrement ?? false, autoincrement: col.autoincrement ?? false,
check: col.check ?? "",
}; };
} }

View file

@ -77,11 +77,7 @@ export class PrequelKVStore {
* @returns `true` if the key exists, `false` otherwise. * @returns `true` if the key exists, `false` otherwise.
*/ */
has(key: string): boolean { has(key: string): boolean {
return ( return this.table.existsWhere({ key });
this.table.findOneWhere({
key,
}) !== null
);
} }
/** /**
@ -89,7 +85,10 @@ export class PrequelKVStore {
* @returns The keys in the store. * @returns The keys in the store.
*/ */
keys(): string[] { 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. * @returns The values in the store.
*/ */
values(): unknown[] { 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));
} }
} }