This commit is contained in:
Endeavorance 2025-01-01 12:18:54 -05:00
parent 13c3d86a10
commit 20ac2da081
6 changed files with 47 additions and 10 deletions

View file

@ -105,6 +105,8 @@ export class Prequel {
public db: Database;
public kv: PrequelKVStore;
public tables: Record<string, Table<unknown>> = {};
public query: typeof Database.prototype.query;
public uncachedQuery: typeof Database.prototype.prepare;
/**
* Creates a new Prequel database instance.
@ -112,6 +114,8 @@ export class Prequel {
*/
constructor(filename = ":memory:") {
this.db = new Database(filename);
this.query = this.db.query.bind(this.db);
this.uncachedQuery = this.db.prepare.bind(this.db);
this.db.exec("PRAGMA foreign_keys=ON");
this.kv = new PrequelKVStore(this, "PrequelManagedKVStore");
}

View file

@ -27,12 +27,10 @@ export class PrequelEmptyResultsError extends Error {
}
/** An arbitrarily shaped Row */
interface ArbitraryRow {
[key: string]: ColumnValue;
}
type ArbitraryRow = Record<string, ColumnValue>;
function shapeForQuery<T>(obj: Partial<T>): Record<string, ColumnValue> {
const asInsertDataShape: Record<string, ColumnValue> = {};
function shapeForQuery<T>(obj: Partial<T>): ArbitraryRow {
const asInsertDataShape: ArbitraryRow = {};
const keys = Object.keys(obj);
@ -72,6 +70,8 @@ export class Table<RowShape> {
private _sizeQuery: Statement<{ count: number }, []>;
private _truncateQuery: Statement<void, []>;
public query: typeof Database.prototype.query;
constructor(
db: Database | Prequel,
name: string,
@ -85,6 +85,7 @@ export class Table<RowShape> {
this._name = name;
this._columnDefinitions = normalizeColumns(cols);
this._columnNames = Object.keys(this._columnDefinitions);
this.query = this._db.query.bind(this._db);
const columnSQLParts: string[] = [];
const updateColumnSQLParts: string[] = [];
@ -211,6 +212,12 @@ export class Table<RowShape> {
return query.get(asInsertDataShape) as RowShape;
}
public insertWithout<OmitKeys extends keyof RowShape>(
rowWithKeysOmitted: Omit<RowShape, OmitKeys>,
): RowShape {
return this.insertPartial(rowWithKeysOmitted as Partial<RowShape>);
}
/**
* Updates a row in the table with the provided data.
*