From 523578e310134b9f0e110f02d5d010a9d8fff554 Mon Sep 17 00:00:00 2001 From: Pablo Baleztena Date: Sun, 20 Oct 2024 23:13:41 -0300 Subject: [PATCH] [fabric/sqlite-store] Refactor SQL key handling functions for consistency and clarity --- .../sqlite-store/sqlite/record-utils.ts | 12 ++++----- .../sqlite-store/sqlite/sqlite-database.ts | 3 ++- .../sqlite-store/state/query-builder.ts | 4 +-- .../fabric/sqlite-store/state/state-store.ts | 26 ++++++++++--------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/packages/fabric/sqlite-store/sqlite/record-utils.ts b/packages/fabric/sqlite-store/sqlite/record-utils.ts index fec48e0..19fb908 100644 --- a/packages/fabric/sqlite-store/sqlite/record-utils.ts +++ b/packages/fabric/sqlite-store/sqlite/record-utils.ts @@ -14,23 +14,23 @@ export function recordToSQLKeys(record: Record) { /** * Unfold a record into a string of it's keys separated by commas. */ -export function recordToSQLKeyParams(record: Record) { +export function recordToSQLParamKeys(record: Record) { return Object.keys(record) - .map((key) => keyToParam(key)) + .map((key) => keyToParamKey(key)) .join(", "); } /** * Unfold a record into a string of it's keys separated by commas. */ -export function recordToSQLParams( +export function recordToSQLParamRecord( model: Model, record: Record, ) { return Object.keys(record).reduce( (acc, key) => ({ ...acc, - [keyToParam(key)]: fieldValueToSQL(model.fields[key]!, record[key]), + [keyToParamKey(key)]: fieldValueToSQL(model.fields[key]!, record[key]), }), {}, ); @@ -38,10 +38,10 @@ export function recordToSQLParams( export function recordToSQLSet(record: Record) { return Object.keys(record) - .map((key) => `${key} = ${keyToParam(key)}`) + .map((key) => `${key} = ${keyToParamKey(key)}`) .join(", "); } -export function keyToParam(key: string) { +export function keyToParamKey(key: string) { return `$${key}`; } diff --git a/packages/fabric/sqlite-store/sqlite/sqlite-database.ts b/packages/fabric/sqlite-store/sqlite/sqlite-database.ts index dfee30a..bdc5279 100644 --- a/packages/fabric/sqlite-store/sqlite/sqlite-database.ts +++ b/packages/fabric/sqlite-store/sqlite/sqlite-database.ts @@ -26,8 +26,9 @@ export class SQLiteDatabase { this.run("BEGIN TRANSACTION"); await fn(); this.run("COMMIT"); - } catch { + } catch (e) { this.run("ROLLBACK"); + throw e; } } diff --git a/packages/fabric/sqlite-store/state/query-builder.ts b/packages/fabric/sqlite-store/state/query-builder.ts index 3c197d5..35bc3eb 100644 --- a/packages/fabric/sqlite-store/state/query-builder.ts +++ b/packages/fabric/sqlite-store/state/query-builder.ts @@ -3,7 +3,8 @@ import { AsyncResult, Keyof, Optional } from "@fabric/core"; import { FilterOptions, Model, - ModelSchema, + type ModelSchema, + NotFoundError, OrderByOptions, SelectableQuery, StoreLimitableQuery, @@ -12,7 +13,6 @@ import { StoreQueryError, StoreSortableQuery, } from "@fabric/domain"; -import { NotFoundError } from "../../domain/models/store-query/store-query.ts"; import { filterToParams, filterToSQL } from "../sqlite/filter-to-sql.ts"; import { transformRow } from "../sqlite/sql-to-value.ts"; import { SQLiteDatabase } from "../sqlite/sqlite-database.ts"; diff --git a/packages/fabric/sqlite-store/state/state-store.ts b/packages/fabric/sqlite-store/state/state-store.ts index 035f9cb..6ba71c3 100644 --- a/packages/fabric/sqlite-store/state/state-store.ts +++ b/packages/fabric/sqlite-store/state/state-store.ts @@ -1,6 +1,6 @@ import { AsyncResult, UnexpectedError, UUID } from "@fabric/core"; import { - type AggregateModel, + Model, ModelSchemaFromModels, ModelToType, StoreQuery, @@ -9,16 +9,16 @@ import { } from "@fabric/domain"; import { modelToSql } from "../sqlite/model-to-sql.ts"; import { - keyToParam, - recordToSQLKeyParams, + keyToParamKey, recordToSQLKeys, - recordToSQLParams, + recordToSQLParamKeys, + recordToSQLParamRecord, recordToSQLSet, } from "../sqlite/record-utils.ts"; import { SQLiteDatabase } from "../sqlite/sqlite-database.ts"; import { QueryBuilder } from "./query-builder.ts"; -export class SQLiteStateStore +export class SQLiteStateStore implements WritableStateStore { private schema: ModelSchemaFromModels; private db: SQLiteDatabase; @@ -47,8 +47,8 @@ export class SQLiteStateStore recordToSQLKeys( record, ) - }) VALUES (${recordToSQLKeyParams(record)})`, - recordToSQLParams(model, record), + }) VALUES (${recordToSQLParamKeys(record)})`, + recordToSQLParamRecord(model, record), ); }, (error) => new StoreQueryError(error.message), @@ -72,7 +72,7 @@ export class SQLiteStateStore return AsyncResult.tryFrom( () => { - const params = recordToSQLParams(model, { + const params = recordToSQLParamRecord(model, { ...record, id, }); @@ -81,7 +81,7 @@ export class SQLiteStateStore recordToSQLSet( record, ) - } WHERE id = ${keyToParam("id")}`, + } WHERE id = ${keyToParamKey("id")}`, params, ); }, @@ -98,8 +98,10 @@ export class SQLiteStateStore return AsyncResult.tryFrom( () => { this.db.runPrepared( - `DELETE FROM ${model.name} WHERE id = ${keyToParam("id")}`, - { $id: id }, + `DELETE FROM ${model.name} WHERE id = ${keyToParamKey("id")}`, + { + [keyToParamKey("id")]: id, + }, ); }, (error) => new StoreQueryError(error.message), @@ -109,7 +111,7 @@ export class SQLiteStateStore migrate(): AsyncResult { return AsyncResult.tryFrom( async () => { - await this.db.init(); + this.db.init(); await this.db.withTransaction(() => { for (const modelKey in this.schema) { const model =