Check support in columns
This commit is contained in:
parent
153725f785
commit
98a5b9c0e1
3 changed files with 27 additions and 11 deletions
|
@ -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: {
|
||||
|
|
|
@ -20,6 +20,7 @@ export interface Column {
|
|||
default: string | number | undefined;
|
||||
unique: boolean;
|
||||
autoincrement: boolean;
|
||||
check: string;
|
||||
}
|
||||
|
||||
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) {
|
||||
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 ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue