[fabric/sqlite-store] Refactor SQL key handling functions for consistency and clarity
This commit is contained in:
parent
9f9419c2b6
commit
523578e310
@ -14,23 +14,23 @@ export function recordToSQLKeys(record: Record<string, any>) {
|
||||
/**
|
||||
* Unfold a record into a string of it's keys separated by commas.
|
||||
*/
|
||||
export function recordToSQLKeyParams(record: Record<string, any>) {
|
||||
export function recordToSQLParamKeys(record: Record<string, any>) {
|
||||
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<string, any>,
|
||||
) {
|
||||
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<string, any>) {
|
||||
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}`;
|
||||
}
|
||||
|
||||
@ -26,8 +26,9 @@ export class SQLiteDatabase {
|
||||
this.run("BEGIN TRANSACTION");
|
||||
await fn();
|
||||
this.run("COMMIT");
|
||||
} catch {
|
||||
} catch (e) {
|
||||
this.run("ROLLBACK");
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -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<TModel extends AggregateModel>
|
||||
export class SQLiteStateStore<TModel extends Model>
|
||||
implements WritableStateStore<TModel> {
|
||||
private schema: ModelSchemaFromModels<TModel>;
|
||||
private db: SQLiteDatabase;
|
||||
@ -47,8 +47,8 @@ export class SQLiteStateStore<TModel extends AggregateModel>
|
||||
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<TModel extends AggregateModel>
|
||||
|
||||
return AsyncResult.tryFrom(
|
||||
() => {
|
||||
const params = recordToSQLParams(model, {
|
||||
const params = recordToSQLParamRecord(model, {
|
||||
...record,
|
||||
id,
|
||||
});
|
||||
@ -81,7 +81,7 @@ export class SQLiteStateStore<TModel extends AggregateModel>
|
||||
recordToSQLSet(
|
||||
record,
|
||||
)
|
||||
} WHERE id = ${keyToParam("id")}`,
|
||||
} WHERE id = ${keyToParamKey("id")}`,
|
||||
params,
|
||||
);
|
||||
},
|
||||
@ -98,8 +98,10 @@ export class SQLiteStateStore<TModel extends AggregateModel>
|
||||
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<TModel extends AggregateModel>
|
||||
migrate(): AsyncResult<void, StoreQueryError> {
|
||||
return AsyncResult.tryFrom(
|
||||
async () => {
|
||||
await this.db.init();
|
||||
this.db.init();
|
||||
await this.db.withTransaction(() => {
|
||||
for (const modelKey in this.schema) {
|
||||
const model =
|
||||
|
||||
Loading…
Reference in New Issue
Block a user