Compare commits

...

3 Commits

31 changed files with 968 additions and 143 deletions

View File

@ -26,7 +26,7 @@
"lint": "eslint . --fix --report-unused-disable-directives",
"format": "prettier --write .",
"test": "yarn workspaces foreach -vvpA run test --run --clearScreen false",
"build": "yarn workspaces foreach -vvpA --topological-dev run build",
"build": "yarn workspaces foreach -vvpA --topological run build",
"add-package": "tsx ./scripts/add-package.ts",
"postinstall": "husky"
}

View File

@ -9,11 +9,13 @@
"private": true,
"packageManager": "yarn@4.1.1",
"devDependencies": {
"@fabric/store-sqlite": "workspace:^",
"typescript": "^5.6.2",
"vitest": "^2.1.1"
},
"dependencies": {
"@fabric/core": "workspace:^"
"@fabric/core": "workspace:^",
"decimal.js": "^10.4.3"
},
"scripts": {
"test": "vitest",

View File

@ -3,6 +3,7 @@ export * from "./events/index.js";
export * from "./files/index.js";
export * from "./models/index.js";
export * from "./security/index.js";
export * from "./services/index.js";
export * from "./storage/index.js";
export * from "./types/index.js";
export * from "./use-case/index.js";

View File

@ -0,0 +1 @@
export * from "./services/mocks.js";

View File

@ -0,0 +1,21 @@
import { TaggedVariant, VariantTag } from "@fabric/core";
import { BaseField } from "./base-field.js";
export interface DecimalFieldOptions extends BaseField {
isUnsigned?: boolean;
precision?: number;
scale?: number;
}
export interface DecimalField
extends TaggedVariant<"DecimalField">,
DecimalFieldOptions {}
export function createDecimalField<T extends DecimalFieldOptions>(
opts: T = {} as T,
): DecimalField & T {
return {
[VariantTag]: "DecimalField",
...opts,
} as const;
}

View File

@ -1,21 +1,32 @@
import { Decimal } from "decimal.js";
import { UUID } from "../../types/uuid.js";
import { DecimalField } from "./decimal.js";
import { FloatField } from "./float.js";
import { IntegerField } from "./integer.js";
import { ReferenceField } from "./reference-field.js";
import { StringField } from "./string-field.js";
import { UUIDField } from "./uuid-field.js";
/**
* Converts a field definition to its corresponding TypeScript type.
*/
export type FieldToType<TField> = TField extends StringField
? ToOptional<TField, string>
: TField extends UUIDField
? ToOptional<TField, UUID>
: TField extends IntegerField
? TField["hasArbitraryPrecision"] extends true
? ToOptional<TField, bigint>
: ToOptional<TField, number>
: never;
//prettier-ignore
export type FieldToType<TField> =
TField extends StringField ? MaybeOptional<TField, string>
: TField extends UUIDField ? MaybeOptional<TField, UUID>
: TField extends IntegerField ? IntegerFieldToType<TField>
: TField extends ReferenceField ? MaybeOptional<TField, UUID>
: TField extends DecimalField ? MaybeOptional<TField, Decimal>
: TField extends FloatField ? MaybeOptional<TField, number>
: never;
type ToOptional<TField, TType> = TField extends { isOptional: true }
//prettier-ignore
type IntegerFieldToType<TField extends IntegerField> = TField["hasArbitraryPrecision"] extends true
? MaybeOptional<TField, bigint>
: TField["hasArbitraryPrecision"] extends false
? MaybeOptional<TField, number>
: MaybeOptional<TField, number | bigint>;
type MaybeOptional<TField, TType> = TField extends { isOptional: true }
? TType | null
: TType;

View File

@ -0,0 +1,18 @@
import { TaggedVariant, VariantTag } from "@fabric/core";
import { BaseField } from "./base-field.js";
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface FloatFieldOptions extends BaseField {}
export interface FloatField
extends TaggedVariant<"FloatField">,
FloatFieldOptions {}
export function createFloatField<T extends FloatFieldOptions>(
opts: T = {} as T,
): FloatField & T {
return {
[VariantTag]: "FloatField",
...opts,
} as const;
}

View File

@ -1,14 +1,19 @@
import { createDecimalField, DecimalField } from "./decimal.js";
import { createFloatField, FloatField } from "./float.js";
import { createIntegerField, IntegerField } from "./integer.js";
import { createReferenceField, ReferenceField } from "./reference-field.js";
import { createStringField, StringField } from "./string-field.js";
import { createUUIDField, UUIDField } from "./uuid-field.js";
export * from "./base-field.js";
export * from "./field-to-type.js";
export * from "./reference-field.js";
export type FieldDefinition =
| StringField
| UUIDField
| IntegerField
| FloatField
| DecimalField
| ReferenceField;
export namespace Field {
@ -16,4 +21,6 @@ export namespace Field {
export const uuid = createUUIDField;
export const integer = createIntegerField;
export const reference = createReferenceField;
export const decimal = createDecimalField;
export const float = createFloatField;
}

View File

@ -1,3 +1,7 @@
import { Model } from "./model.js";
export type ModelSchema = Record<string, Model>;
export type ModelSchemaFromModels<TModels extends Model> = {
[K in TModels["name"]]: Extract<TModels, { name: K }>;
};

View File

@ -4,6 +4,14 @@ import { Field, FieldDefinition } from "./fields/index.js";
export type CustomModelFields = Record<string, FieldDefinition>;
export interface Collection<
TName extends string = string,
TFields extends CustomModelFields = CustomModelFields,
> {
name: TName;
fields: TFields;
}
export const DefaultModelFields = {
id: Field.uuid({ isPrimaryKey: true }),
streamId: Field.uuid({ isIndexed: true }),
@ -12,11 +20,11 @@ export const DefaultModelFields = {
hasArbitraryPrecision: true,
}),
};
export interface Model<
TName extends string = string,
TFields extends CustomModelFields = CustomModelFields,
> {
name: TName;
> extends Collection<TName, TFields> {
fields: typeof DefaultModelFields & TFields;
}
@ -30,6 +38,16 @@ export function defineModel<
} as const;
}
export function defineCollection<
TName extends string,
TFields extends CustomModelFields,
>(name: TName, fields: TFields): Collection<TName, TFields> {
return {
name,
fields,
} as const;
}
export type ModelToType<TModel extends Model> = {
[K in Keyof<TModel["fields"]>]: FieldToType<TModel["fields"][K]>;
};

View File

@ -4,12 +4,14 @@ export type FilterOptions<T = any> =
| SingleFilterOption<T>
| MultiFilterOption<T>;
export type FilterValue<T = any, K extends keyof T = keyof T> =
| T[K]
| LikeFilterOption<T[K]>
| ComparisonFilterOption<T[K]>
| InFilterOption<T[K]>;
export type SingleFilterOption<T = any> = {
[K in keyof T]?:
| T[K]
| LikeFilterOption<T[K]>
| ComparisonFilterOption<T[K]>
| InFilterOption<T[K]>;
[K in keyof T]?: FilterValue<T, K>;
};
export type MultiFilterOption<T = any> = SingleFilterOption<T>[];

View File

@ -1,6 +1,7 @@
import { AsyncResult, Keyof } from "@fabric/core";
import { StoreQueryError } from "../../errors/query-error.js";
import { StorageDriver } from "../../storage/storage-driver.js";
import { ModelSchema } from "../model-schema.js";
import { FilterOptions } from "./filter-options.js";
import { OrderByOptions } from "./order-by-options.js";
import {
@ -14,25 +15,26 @@ import {
export class QueryBuilder<T> implements StoreQuery<T> {
constructor(
private driver: StorageDriver,
private schema: ModelSchema,
private query: QueryDefinition,
) {}
where(where: FilterOptions<T>): StoreSortableQuery<T> {
return new QueryBuilder(this.driver, {
return new QueryBuilder(this.driver, this.schema, {
...this.query,
where,
});
}
orderBy(opts: OrderByOptions<T>): StoreLimitableQuery<T> {
return new QueryBuilder(this.driver, {
return new QueryBuilder(this.driver, this.schema, {
...this.query,
orderBy: opts,
});
}
limit(limit: number, offset?: number | undefined): SelectableQuery<T> {
return new QueryBuilder(this.driver, {
return new QueryBuilder(this.driver, this.schema, {
...this.query,
limit,
offset,
@ -42,7 +44,7 @@ export class QueryBuilder<T> implements StoreQuery<T> {
select<K extends Keyof<T>>(
keys?: K[],
): AsyncResult<Pick<T, K>[], StoreQueryError> {
return this.driver.select({
return this.driver.select(this.schema[this.query.from], {
...this.query,
keys,
});
@ -51,7 +53,7 @@ export class QueryBuilder<T> implements StoreQuery<T> {
selectOne<K extends Keyof<T>>(
keys?: K[],
): AsyncResult<Pick<T, K>, StoreQueryError> {
return this.driver.selectOne({
return this.driver.selectOne(this.schema[this.query.from], {
...this.query,
keys,
});

View File

@ -0,0 +1,133 @@
import { isError } from "@fabric/core";
import { SQLiteStorageDriver } from "@fabric/store-sqlite";
import {
afterEach,
beforeEach,
describe,
expect,
expectTypeOf,
it,
} from "vitest";
import { UUIDGeneratorMock } from "../services/uuid-generator.mock.js";
import { UUID } from "../types/uuid.js";
import { Field } from "./fields/index.js";
import { defineModel } from "./model.js";
import { isLike } from "./query/filter-options.js";
import { StateStore } from "./state-store.js";
describe("State Store", () => {
const models = [
defineModel("users", {
name: Field.string(),
}),
];
let driver: SQLiteStorageDriver;
let store: StateStore<(typeof models)[number]>;
beforeEach(async () => {
driver = new SQLiteStorageDriver(":memory:");
store = new StateStore(driver, models);
const migrationResult = await store.migrate();
if (isError(migrationResult)) throw migrationResult;
});
afterEach(async () => {
await driver.close();
});
it("should insert a record", async () => {
const newUUID = UUIDGeneratorMock.generate();
const insertResult = await store.insertInto("users", {
name: "test",
id: newUUID,
streamId: newUUID,
streamVersion: 1n,
});
if (isError(insertResult)) throw insertResult;
});
it("should query with a basic select", async () => {
const newUUID = UUIDGeneratorMock.generate();
const insertResult = await store.insertInto("users", {
name: "test",
id: newUUID,
streamId: newUUID,
streamVersion: 1n,
});
if (isError(insertResult)) throw insertResult;
const result = await store.from("users").select();
if (isError(result)) throw result;
expectTypeOf(result).toEqualTypeOf<
{
id: UUID;
streamId: UUID;
streamVersion: bigint;
name: string;
}[]
>();
expect(result).toEqual([
{
id: newUUID,
streamId: newUUID,
streamVersion: 1n,
name: "test",
},
]);
});
it("should query with a where clause", async () => {
const newUUID = UUIDGeneratorMock.generate();
await store.insertInto("users", {
name: "test",
id: newUUID,
streamId: newUUID,
streamVersion: 1n,
});
await store.insertInto("users", {
name: "anotherName",
id: UUIDGeneratorMock.generate(),
streamId: UUIDGeneratorMock.generate(),
streamVersion: 1n,
});
await store.insertInto("users", {
name: "anotherName2",
id: UUIDGeneratorMock.generate(),
streamId: UUIDGeneratorMock.generate(),
streamVersion: 1n,
});
const result = await store
.from("users")
.where({
name: isLike("te*"),
})
.select();
if (isError(result)) throw result;
expectTypeOf(result).toEqualTypeOf<
{
id: UUID;
streamId: UUID;
streamVersion: bigint;
name: string;
}[]
>();
expect(result).toEqual([
{
id: newUUID,
streamId: newUUID,
streamVersion: 1n,
name: "test",
},
]);
});
});

View File

@ -1,5 +1,41 @@
import { AsyncResult } from "@fabric/core";
import { StoreQueryError } from "../errors/query-error.js";
import { StorageDriver } from "../storage/storage-driver.js";
import { ModelSchemaFromModels } from "./model-schema.js";
import { Model, ModelToType } from "./model.js";
import { QueryBuilder } from "./query/query-builder.js";
import { StoreQuery } from "./query/query.js";
export class StateStore {
constructor(private driver: StorageDriver) {}
export class StateStore<TModel extends Model> {
private schema: ModelSchemaFromModels<TModel>;
constructor(
private driver: StorageDriver,
models: TModel[],
) {
this.schema = models.reduce((acc, model: TModel) => {
return {
...acc,
[model.name]: model,
};
}, {} as ModelSchemaFromModels<TModel>);
}
async migrate(): AsyncResult<void, StoreQueryError> {
await this.driver.sync(this.schema);
}
async insertInto<T extends keyof ModelSchemaFromModels<TModel>>(
collection: T,
record: ModelToType<ModelSchemaFromModels<TModel>[T]>,
): AsyncResult<void, StoreQueryError> {
return this.driver.insert(this.schema[collection], record);
}
from<T extends keyof ModelSchemaFromModels<TModel>>(
collection: T,
): StoreQuery<ModelToType<ModelSchemaFromModels<TModel>[T]>> {
return new QueryBuilder(this.driver, this.schema, {
from: collection,
}) as StoreQuery<ModelToType<ModelSchemaFromModels<TModel>[T]>>;
}
}

View File

@ -0,0 +1 @@
export * from "./uuid-generator.js";

View File

@ -0,0 +1 @@
export * from "./uuid-generator.mock.js";

View File

@ -0,0 +1,8 @@
import { UUID } from "../types/uuid.js";
import { UUIDGenerator } from "./uuid-generator.js";
export const UUIDGeneratorMock: UUIDGenerator = {
generate(): UUID {
return crypto.randomUUID() as UUID;
},
};

View File

@ -0,0 +1,5 @@
import { UUID } from "../types/uuid.js";
export interface UUIDGenerator {
generate(): UUID;
}

View File

@ -4,6 +4,7 @@ import { AsyncResult, UnexpectedError } from "@fabric/core";
import { CircularDependencyError } from "../errors/circular-dependency-error.js";
import { StoreQueryError } from "../errors/query-error.js";
import { ModelSchema } from "../models/model-schema.js";
import { Collection } from "../models/model.js";
import { QueryDefinition } from "../models/query/query.js";
export interface StorageDriver {
@ -11,19 +12,25 @@ export interface StorageDriver {
* Insert data into the store
*/
insert(
collectionName: string,
model: Collection,
record: Record<string, any>,
): AsyncResult<void, StoreQueryError>;
/**
* Run a select query against the store.
*/
select(query: QueryDefinition): AsyncResult<any[], StoreQueryError>;
select(
model: Collection,
query: QueryDefinition,
): AsyncResult<any[], StoreQueryError>;
/**
* Run a select query against the store.
*/
selectOne(query: QueryDefinition): AsyncResult<any, StoreQueryError>;
selectOne(
model: Collection,
query: QueryDefinition,
): AsyncResult<any, StoreQueryError>;
/**
* Sincronice the store with the schema.
@ -46,7 +53,7 @@ export interface StorageDriver {
* Update a record in the store.
*/
update(
collectionName: string,
model: Collection,
id: string,
record: Record<string, any>,
): AsyncResult<void, StoreQueryError>;
@ -54,8 +61,5 @@ export interface StorageDriver {
/**
* Delete a record from the store.
*/
delete(
collectionName: string,
id: string,
): AsyncResult<void, StoreQueryError>;
delete(model: Collection, id: string): AsyncResult<void, StoreQueryError>;
}

View File

@ -0,0 +1 @@
export { Decimal } from "decimal.js";

View File

@ -1,5 +1,5 @@
{
"name": "@ulthar/store-sqlite",
"name": "@fabric/store-sqlite",
"type": "module",
"module": "dist/index.js",
"main": "dist/index.js",

View File

@ -0,0 +1,150 @@
import {
defineCollection,
Field,
isGreaterOrEqualTo,
isGreaterThan,
isIn,
isLessOrEqualTo,
isLessThan,
isLike,
isNotEqualTo,
} from "@fabric/domain";
import { describe, expect, it } from "vitest";
import { filterToParams, filterToSQL } from "./filter-to-sql.js";
describe("SQL where clause from filter options", () => {
const col = defineCollection("users", {
name: Field.string(),
age: Field.integer(),
status: Field.string(),
salary: Field.decimal(),
rating: Field.float(),
quantity: Field.integer({
isUnsigned: true,
}),
price: Field.decimal(),
});
it("should create a where clause from options with IN option", () => {
const opts = {
name: isIn(["John", "Jane"]),
};
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual("WHERE name IN ($where_name_0,$where_name_1)");
expect(params).toEqual({ $where_name_0: "John", $where_name_1: "Jane" });
});
it("should create a where clause from options with LIKE option", () => {
const opts = {
name: isLike("%John%"),
};
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual("WHERE name LIKE $where_name");
expect(params).toEqual({ $where_name: "%John%" });
});
it("should create a where clause from options with EQUALS option", () => {
const opts = {
age: 25,
};
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual("WHERE age = $where_age");
expect(params).toEqual({ $where_age: 25 });
});
it("should create a where clause from options with NOT EQUALS option", () => {
const opts = {
status: isNotEqualTo("inactive"),
};
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual("WHERE status <> $where_status");
expect(params).toEqual({ $where_status: "inactive" });
});
it("should create a where clause from options with GREATER THAN option", () => {
const opts = {
salary: isGreaterThan(50000),
};
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual("WHERE salary > $where_salary");
expect(params).toEqual({ $where_salary: 50000 });
});
it("should create a where clause from options with LESS THAN option", () => {
const opts = {
rating: isLessThan(4.5),
};
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual("WHERE rating < $where_rating");
expect(params).toEqual({ $where_rating: 4.5 });
});
it("should create a where clause from options with GREATER THAN OR EQUALS option", () => {
const opts = {
quantity: isGreaterOrEqualTo(10),
};
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual("WHERE quantity >= $where_quantity");
expect(params).toEqual({ $where_quantity: 10 });
});
it("should create a where clause from options with LESS THAN OR EQUALS option", () => {
const opts = {
price: isLessOrEqualTo(100),
};
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual("WHERE price <= $where_price");
expect(params).toEqual({ $where_price: 100 });
});
it("should create a where clause from options with IS NULL option", () => {
const opts = {
price: undefined,
};
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual("WHERE price IS NULL");
expect(params).toEqual({});
});
it("should create a where clause from options with OR combination", () => {
const opts = [
{
name: isIn(["John", "Jane"]),
age: isGreaterThan(30),
},
{
status: isNotEqualTo("inactive"),
salary: isGreaterThan(50000),
},
{
rating: isLessThan(4.5),
quantity: isGreaterOrEqualTo(10),
},
];
const result = filterToSQL(opts);
const params = filterToParams(col, opts);
expect(result).toEqual(
"WHERE (name IN ($where_name_0_0,$where_name_0_1) AND age > $where_age_0) OR (status <> $where_status_1 AND salary > $where_salary_1) OR (rating < $where_rating_2 AND quantity >= $where_quantity_2)",
);
expect(params).toEqual({
$where_name_0_0: "John",
$where_name_0_1: "Jane",
$where_age_0: 30,
$where_status_1: "inactive",
$where_salary_1: 50000,
$where_rating_2: 4.5,
$where_quantity_2: 10,
});
});
});

View File

@ -0,0 +1,166 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
Collection,
FieldDefinition,
FILTER_OPTION_OPERATOR_SYMBOL,
FILTER_OPTION_TYPE_SYMBOL,
FILTER_OPTION_VALUE_SYMBOL,
FilterOptions,
FilterValue,
MultiFilterOption,
SingleFilterOption,
} from "@fabric/domain";
import { keyToParam } from "./record-utils.js";
import { fieldValueToSQL } from "./value-to-sql.js";
export function filterToSQL(filterOptions?: FilterOptions) {
if (!filterOptions) return "";
if (Array.isArray(filterOptions))
return `WHERE ${getWhereFromMultiOption(filterOptions)}`;
return `WHERE ${getWhereFromSingleOption(filterOptions)}`;
}
export function filterToParams(
collection: Collection,
filterOptions?: FilterOptions,
) {
if (!filterOptions) return {};
if (Array.isArray(filterOptions))
return getParamsFromMultiFilterOption(collection, filterOptions);
return getParamsFromSingleFilterOption(collection, filterOptions);
}
function getWhereFromMultiOption(filterOptions: MultiFilterOption) {
return filterOptions
.map(
(option, i) =>
`(${getWhereFromSingleOption(option, { postfix: `_${i}` })})`,
)
.join(" OR ");
}
function getWhereFromSingleOption(
filterOptions: SingleFilterOption,
opts: { postfix?: string } = {},
) {
return Object.entries(filterOptions)
.map(([key, value]) => getWhereFromKeyValue(key, value, opts))
.join(" AND ");
}
const WHERE_KEY_PREFIX = "where_";
function getWhereParamKey(key: string, opts: { postfix?: string } = {}) {
return keyToParam(`${WHERE_KEY_PREFIX}${key}${opts.postfix ?? ""}`);
}
function getWhereFromKeyValue(
key: string,
value: FilterValue,
opts: { postfix?: string } = {},
) {
if (value == undefined) {
return `${key} IS NULL`;
}
if (typeof value === "object") {
if (value[FILTER_OPTION_TYPE_SYMBOL] === "like") {
return `${key} LIKE ${getWhereParamKey(key, opts)}`;
}
if (value[FILTER_OPTION_TYPE_SYMBOL] === "in") {
return `${key} IN (${value[FILTER_OPTION_VALUE_SYMBOL].map(
(v: any, i: number) =>
`${getWhereParamKey(key, {
postfix: opts.postfix ? `${opts.postfix}_${i}` : `_${i}`,
})}`,
).join(",")})`;
}
if (value[FILTER_OPTION_TYPE_SYMBOL] === "comparison") {
return `${key} ${value[FILTER_OPTION_OPERATOR_SYMBOL]} ${getWhereParamKey(
key,
opts,
)}`;
}
}
return `${key} = ${getWhereParamKey(key, opts)}`;
}
function getParamsFromMultiFilterOption(
collection: Collection,
filterOptions: MultiFilterOption,
) {
return filterOptions.reduce(
(acc, filterOption, i) => ({
...acc,
...getParamsFromSingleFilterOption(collection, filterOption, {
postfix: `_${i}`,
}),
}),
{},
);
}
function getParamsFromSingleFilterOption(
collection: Collection,
filterOptions: SingleFilterOption,
opts: { postfix?: string } = {},
) {
return Object.entries(filterOptions)
.filter(([, value]) => {
return value !== undefined;
})
.reduce(
(acc, [key, value]) => ({
...acc,
...getParamsForFilterKeyValue(collection.fields[key], key, value, opts),
}),
{},
);
}
function getParamValueFromOptionValue(field: FieldDefinition, value: any) {
if (typeof value === "object") {
if (value[FILTER_OPTION_TYPE_SYMBOL] === "like") {
return value[FILTER_OPTION_VALUE_SYMBOL];
}
if (value[FILTER_OPTION_TYPE_SYMBOL] === "comparison") {
return fieldValueToSQL(field, value[FILTER_OPTION_VALUE_SYMBOL]);
}
}
return fieldValueToSQL(field, value);
}
function getParamsForFilterKeyValue(
field: FieldDefinition,
key: string,
value: FilterValue,
opts: { postfix?: string } = {},
) {
if (typeof value === "object") {
if (value[FILTER_OPTION_TYPE_SYMBOL] === "in") {
return value[FILTER_OPTION_VALUE_SYMBOL].reduce(
(acc: Record<string, any>, v: any, i: number) => {
return {
...acc,
[getWhereParamKey(key, {
postfix: opts.postfix ? `${opts.postfix}_${i}` : `_${i}`,
})]: value[FILTER_OPTION_VALUE_SYMBOL][i],
};
},
{},
);
}
}
return {
[getWhereParamKey(key, opts)]: getParamValueFromOptionValue(field, value),
};
}

View File

@ -2,14 +2,14 @@
import { Variant, VariantTag } from "@fabric/core";
import { FieldDefinition, getTargetKey, Model } from "@fabric/domain";
type FieldMap = {
type FieldSQLDefinitionMap = {
[K in FieldDefinition[VariantTag]]: (
name: string,
field: Extract<FieldDefinition, { [VariantTag]: K }>,
) => string;
};
const FieldMap: FieldMap = {
const FieldSQLDefinitionMap: FieldSQLDefinitionMap = {
StringField: (n, f) => {
return [n, "TEXT", modifiersFromOpts(f)].join(" ");
},
@ -21,10 +21,10 @@ const FieldMap: FieldMap = {
modifiersFromOpts(f),
].join(" ");
},
IntegerField: function (n, f): string {
IntegerField: (n, f): string => {
return [n, "INTEGER", modifiersFromOpts(f)].join(" ");
},
ReferenceField: function (n, f): string {
ReferenceField: (n, f): string => {
return [
n,
"TEXT",
@ -33,7 +33,16 @@ const FieldMap: FieldMap = {
`FOREIGN KEY (${n}) REFERENCES ${f.targetModel}(${getTargetKey(f)})`,
].join(" ");
},
FloatField: (n, f): string => {
return [n, "REAL", modifiersFromOpts(f)].join(" ");
},
DecimalField: (n, f): string => {
return [n, "REAL", modifiersFromOpts(f)].join(" ");
},
};
function fieldDefinitionToSQL(name: string, field: FieldDefinition) {
return FieldSQLDefinitionMap[field[VariantTag]](name, field as any);
}
function modifiersFromOpts(field: FieldDefinition) {
if (Variant.is(field, "UUIDField") && field.isPrimaryKey) {
@ -45,10 +54,6 @@ function modifiersFromOpts(field: FieldDefinition) {
].join(" ");
}
function fieldDefinitionToSQL(name: string, field: FieldDefinition) {
return FieldMap[field[VariantTag]](name, field as any);
}
export function modelToSql(
model: Model<string, Record<string, FieldDefinition>>,
) {

View File

@ -1,26 +1,44 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Model } from "@fabric/domain";
import { fieldValueToSQL } from "./value-to-sql.js";
/**
* Unfold a record into a string of it's keys separated by commas.
*/
export function recordToKeys(record: Record<string, any>, prefix = "") {
export function recordToSQLKeys(record: Record<string, any>) {
return Object.keys(record)
.map((key) => `${prefix}${key}`)
.map((key) => key)
.join(", ");
}
/**
* Unfold a record into a string of it's keys separated by commas.
*/
export function recordToSQLKeyParams(record: Record<string, any>) {
return Object.keys(record)
.map((key) => keyToParam(key))
.join(", ");
}
/**
* Unfold a record into a string of it's keys separated by commas.
*/
export function recordToParams(record: Record<string, any>) {
export function recordToSQLParams(model: Model, record: Record<string, any>) {
return Object.keys(record).reduce(
(acc, key) => ({ ...acc, [`:${key}`]: record[key] }),
(acc, key) => ({
...acc,
[keyToParam(key)]: fieldValueToSQL(model.fields[key], record[key]),
}),
{},
);
}
export function recordToSQLSet(record: Record<string, any>) {
return Object.keys(record)
.map((key) => `${key} = :${key}`)
.map((key) => `${key} = ${keyToParam(key)}`)
.join(", ");
}
export function keyToParam(key: string) {
return `$${key}`;
}

View File

@ -0,0 +1,39 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { VariantTag } from "@fabric/core";
import { Collection, FieldDefinition, FieldToType } from "@fabric/domain";
export function transformRow(model: Collection) {
return (row: Record<string, any>) => {
const result: Record<string, any> = {};
for (const key in row) {
const field = model.fields[key];
result[key] = valueFromSQL(field, row[key]);
}
return result;
};
}
function valueFromSQL(field: FieldDefinition, value: any): any {
const r = FieldSQLInsertMap[field[VariantTag]];
return r(field as any, value);
}
type FieldSQLInsertMap = {
[K in FieldDefinition[VariantTag]]: (
field: Extract<FieldDefinition, { [VariantTag]: K }>,
value: any,
) => FieldToType<Extract<FieldDefinition, { [VariantTag]: K }>>;
};
const FieldSQLInsertMap: FieldSQLInsertMap = {
StringField: (f, v) => v,
UUIDField: (f, v) => v,
IntegerField: (f, v) => {
if (f.hasArbitraryPrecision) {
return BigInt(v);
}
return v;
},
ReferenceField: (f, v) => v,
FloatField: (f, v) => v,
DecimalField: (f, v) => v,
};

View File

@ -1,6 +1,6 @@
import { isError } from "@fabric/core";
import { defineModel, Field } from "@fabric/domain";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { defineModel, Field, isLike } from "@fabric/domain";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { SQLiteStorageDriver } from "./sqlite-driver.js";
describe("SQLite Store Driver", () => {
@ -21,116 +21,211 @@ describe("SQLite Store Driver", () => {
if (isError(result)) throw result;
});
test("should be able to synchronize the store and insert a record", async () => {
it("should synchronize the store and insert a record", async () => {
const result = await store.sync(schema);
if (isError(result)) throw result;
await store.insert("users", {
const insertResult = await store.insert(schema.users, {
id: "1",
name: "test",
streamId: "1",
streamVersion: 1,
streamVersion: 1n,
});
const records = await store.select({ from: "users" });
if (isError(insertResult)) throw insertResult;
const records = await store.select(schema.users, { from: "users" });
expect(records).toEqual([
{ id: "1", name: "test", streamId: "1", streamVersion: 1 },
{ id: "1", name: "test", streamId: "1", streamVersion: 1n },
]);
});
test("should be able to update a record", async () => {
const result = await store.sync(schema);
it("should be update a record", async () => {
await store.sync(schema);
if (isError(result)) throw result;
await store.insert("users", {
await store.insert(schema.users, {
id: "1",
name: "test",
streamId: "1",
streamVersion: 1,
streamVersion: 1n,
});
await store.update("users", "1", { name: "updated" });
const err = await store.update(schema.users, "1", { name: "updated" });
if (isError(err)) throw err;
const records = await store.select({ from: "users" });
const records = await store.select(schema.users, { from: "users" });
expect(records).toEqual([
{ id: "1", name: "updated", streamId: "1", streamVersion: 1 },
{ id: "1", name: "updated", streamId: "1", streamVersion: 1n },
]);
});
test("should be able to delete a record", async () => {
const result = await store.sync(schema);
it("should be able to delete a record", async () => {
await store.sync(schema);
if (isError(result)) throw result;
await store.insert("users", {
await store.insert(schema.users, {
id: "1",
name: "test",
streamId: "1",
streamVersion: 1,
streamVersion: 1n,
});
await store.delete("users", "1");
await store.delete(schema.users, "1");
const records = await store.select({ from: "users" });
const records = await store.select(schema.users, { from: "users" });
expect(records).toEqual([]);
});
test("should be able to select records", async () => {
const result = await store.sync(schema);
it("should be able to select records", async () => {
await store.sync(schema);
if (isError(result)) throw result;
await store.insert("users", {
await store.insert(schema.users, {
id: "1",
name: "test",
streamId: "1",
streamVersion: 1,
streamVersion: 1n,
});
await store.insert("users", {
await store.insert(schema.users, {
id: "2",
name: "test",
streamId: "2",
streamVersion: 1,
streamVersion: 1n,
});
const records = await store.select({ from: "users" });
const records = await store.select(schema.users, { from: "users" });
expect(records).toEqual([
{ id: "1", name: "test", streamId: "1", streamVersion: 1 },
{ id: "2", name: "test", streamId: "2", streamVersion: 1 },
{ id: "1", name: "test", streamId: "1", streamVersion: 1n },
{ id: "2", name: "test", streamId: "2", streamVersion: 1n },
]);
});
test("should be able to select one record", async () => {
const result = await store.sync(schema);
it("should be able to select one record", async () => {
await store.sync(schema);
if (isError(result)) throw result;
await store.insert("users", {
await store.insert(schema.users, {
id: "1",
name: "test",
streamId: "1",
streamVersion: 1,
streamVersion: 1n,
});
await store.insert("users", {
await store.insert(schema.users, {
id: "2",
name: "test",
streamId: "2",
streamVersion: 1,
streamVersion: 1n,
});
const record = await store.selectOne({ from: "users" });
const record = await store.selectOne(schema.users, { from: "users" });
expect(record).toEqual({
id: "1",
name: "test",
streamId: "1",
streamVersion: 1,
streamVersion: 1n,
});
});
it("should select a record with a where clause", async () => {
await store.sync(schema);
await store.insert(schema.users, {
id: "1",
name: "test",
streamId: "1",
streamVersion: 1n,
});
await store.insert(schema.users, {
id: "2",
name: "jamón",
streamId: "2",
streamVersion: 1n,
});
const result = await store.select(schema.users, {
from: "users",
where: { name: isLike("te%") },
});
expect(result).toEqual([
{
id: "1",
name: "test",
streamId: "1",
streamVersion: 1n,
},
]);
});
it("should select a record with a where clause of a specific type", async () => {
await store.sync(schema);
await store.insert(schema.users, {
id: "1",
name: "test",
streamId: "1",
streamVersion: 1n,
});
await store.insert(schema.users, {
id: "2",
name: "jamón",
streamId: "2",
streamVersion: 1n,
});
const result = await store.select(schema.users, {
from: "users",
where: { streamVersion: 1n },
});
expect(result).toEqual([
{
id: "1",
name: "test",
streamId: "1",
streamVersion: 1n,
},
{
id: "2",
name: "jamón",
streamId: "2",
streamVersion: 1n,
},
]);
});
it("should select with a limit and offset", async () => {
await store.sync(schema);
await store.insert(schema.users, {
id: "1",
name: "test",
streamId: "1",
streamVersion: 1n,
});
await store.insert(schema.users, {
id: "2",
name: "jamón",
streamId: "2",
streamVersion: 1n,
});
const result = await store.select(schema.users, {
from: "users",
limit: 1,
offset: 1,
});
expect(result).toEqual([
{
id: "2",
name: "jamón",
streamId: "2",
streamVersion: 1n,
},
]);
});
});

View File

@ -4,18 +4,24 @@ import { unlink } from "fs/promises";
import {
CircularDependencyError,
Collection,
Model,
ModelSchema,
QueryDefinition,
StorageDriver,
StoreQueryError,
} from "@fabric/domain";
import { Database, Statement } from "sqlite3";
import { filterToParams, filterToSQL } from "./filter-to-sql.js";
import { modelToSql } from "./model-to-sql.js";
import {
recordToKeys,
recordToParams,
keyToParam,
recordToSQLKeyParams,
recordToSQLKeys,
recordToSQLParams,
recordToSQLSet,
} from "./record-utils.js";
import { transformRow } from "./sql-to-value.js";
import {
dbClose,
dbRun,
@ -35,7 +41,7 @@ export class SQLiteStorageDriver implements StorageDriver {
this.db = new Database(path);
// Enable Write-Ahead Logging, which is faster and more reliable.
this.db.run("PRAGMA journal_mode= WAL;");
this.db.run("PRAGMA journal_mode = WAL;");
this.db.run("PRAGMA foreign_keys = ON;");
}
@ -54,21 +60,47 @@ export class SQLiteStorageDriver implements StorageDriver {
return stmt;
}
private async getSelectStatement(
collection: Collection,
query: QueryDefinition,
): Promise<[Statement, Record<string, any>]> {
const selectFields = query.keys ? query.keys.join(", ") : "*";
const queryFilter = filterToSQL(query.where);
const limit = query.limit ? `LIMIT ${query.limit}` : "";
const offset = query.offset ? `OFFSET ${query.offset}` : "";
const sql = [
`SELECT ${selectFields}`,
`FROM ${query.from}`,
queryFilter,
limit,
offset,
].join(" ");
return [
await this.getOrCreatePreparedStatement(sql),
{
...filterToParams(collection, query.where),
},
];
}
/**
* Insert data into the store
*/
async insert(
collectionName: string,
model: Model,
record: Record<string, any>,
): AsyncResult<void, StoreQueryError> {
try {
const sql = `INSERT INTO ${collectionName} (${recordToKeys(record)}) VALUES (${recordToKeys(record, ":")})`;
const sql = `INSERT INTO ${model.name} (${recordToSQLKeys(record)}) VALUES (${recordToSQLKeyParams(record)})`;
const stmt = await this.getOrCreatePreparedStatement(sql);
return await run(stmt, recordToParams(record));
return await run(stmt, recordToSQLParams(model, record));
} catch (error: any) {
return new StoreQueryError(error.message, {
error,
collectionName,
collectionName: model.name,
record,
});
}
@ -77,11 +109,13 @@ export class SQLiteStorageDriver implements StorageDriver {
/**
* Run a select query against the store.
*/
async select(query: QueryDefinition): AsyncResult<any[], StoreQueryError> {
async select(
collection: Collection,
query: QueryDefinition,
): AsyncResult<any[], StoreQueryError> {
try {
const sql = `SELECT * FROM ${query.from}`;
const stmt = await this.getOrCreatePreparedStatement(sql);
return await getAll(stmt);
const [stmt, params] = await this.getSelectStatement(collection, query);
return await getAll(stmt, params, transformRow(collection));
} catch (error: any) {
return new StoreQueryError(error.message, {
error,
@ -93,12 +127,13 @@ export class SQLiteStorageDriver implements StorageDriver {
/**
* Run a select query against the store.
*/
async selectOne(query: QueryDefinition): AsyncResult<any, StoreQueryError> {
async selectOne(
collection: Collection,
query: QueryDefinition,
): AsyncResult<any, StoreQueryError> {
try {
const sql = `SELECT * FROM ${query.from}`;
const stmt = await this.getOrCreatePreparedStatement(sql);
return await getOne(stmt);
const [stmt, params] = await this.getSelectStatement(collection, query);
return await getOne(stmt, params, transformRow(collection));
} catch (error: any) {
return new StoreQueryError(error.message, {
error,
@ -161,24 +196,22 @@ export class SQLiteStorageDriver implements StorageDriver {
* Update a record in the store.
*/
async update(
collectionName: string,
model: Model,
id: string,
record: Record<string, any>,
): AsyncResult<void, StoreQueryError> {
try {
const sql = `UPDATE ${collectionName} SET ${recordToSQLSet(record)} WHERE id = :id`;
const sql = `UPDATE ${model.name} SET ${recordToSQLSet(record)} WHERE id = ${keyToParam("id")}`;
const stmt = await this.getOrCreatePreparedStatement(sql);
return await run(
stmt,
recordToParams({
...record,
id,
}),
);
const params = recordToSQLParams(model, {
...record,
id,
});
return await run(stmt, params);
} catch (error: any) {
return new StoreQueryError(error.message, {
error,
collectionName,
collectionName: model.name,
record,
});
}
@ -188,18 +221,15 @@ export class SQLiteStorageDriver implements StorageDriver {
* Delete a record from the store.
*/
async delete(
collectionName: string,
id: string,
): AsyncResult<void, StoreQueryError> {
async delete(model: Model, id: string): AsyncResult<void, StoreQueryError> {
try {
const sql = `DELETE FROM ${collectionName} WHERE id = :id`;
const sql = `DELETE FROM ${model.name} WHERE id = :id`;
const stmt = await this.getOrCreatePreparedStatement(sql);
return await run(stmt, { ":id": id });
} catch (error: any) {
return new StoreQueryError(error.message, {
error,
collectionName,
collectionName: model.name,
id,
});
}

View File

@ -52,25 +52,33 @@ export function run(
});
}
export function getAll(stmt: Statement): Promise<Record<string, any>[]> {
export function getAll(
stmt: Statement,
params: Record<string, any>,
transformer: (row: any) => any,
): Promise<Record<string, any>[]> {
return new Promise((resolve, reject) => {
stmt.all((err: Error | null, rows: Record<string, any>[]) => {
stmt.all(params, (err: Error | null, rows: Record<string, any>[]) => {
if (err) {
reject(err);
} else {
resolve(rows);
resolve(rows.map(transformer));
}
});
});
}
export function getOne(stmt: Statement): Promise<Record<string, any>> {
export function getOne(
stmt: Statement,
params: Record<string, any>,
transformer: (row: any) => any,
): Promise<Record<string, any>> {
return new Promise((resolve, reject) => {
stmt.get((err: Error | null, row: Record<string, any>) => {
stmt.all(params, (err: Error | null, rows: Record<string, any>[]) => {
if (err) {
reject(err);
} else {
resolve(row);
resolve(rows.map(transformer)[0]);
}
});
});

View File

@ -0,0 +1,28 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { VariantTag } from "@fabric/core";
import { FieldDefinition, FieldToType } from "@fabric/domain";
type FieldSQLInsertMap = {
[K in FieldDefinition[VariantTag]]: (
field: Extract<FieldDefinition, { [VariantTag]: K }>,
value: FieldToType<Extract<FieldDefinition, { [VariantTag]: K }>>,
) => any;
};
const FieldSQLInsertMap: FieldSQLInsertMap = {
StringField: (f, v) => v,
UUIDField: (f, v) => v,
IntegerField: (f, v: number | bigint) => {
if (f.hasArbitraryPrecision) {
return String(v);
}
return v as number;
},
ReferenceField: (f, v) => v,
FloatField: (f, v) => v,
DecimalField: (f, v) => v,
};
export function fieldValueToSQL(field: FieldDefinition, value: any) {
const r = FieldSQLInsertMap[field[VariantTag]] as any;
return r(field as any, value);
}

View File

@ -419,6 +419,20 @@ __metadata:
resolution: "@fabric/domain@workspace:packages/fabric/domain"
dependencies:
"@fabric/core": "workspace:^"
"@fabric/store-sqlite": "workspace:^"
decimal.js: "npm:^10.4.3"
typescript: "npm:^5.6.2"
vitest: "npm:^2.1.1"
languageName: unknown
linkType: soft
"@fabric/store-sqlite@workspace:^, @fabric/store-sqlite@workspace:packages/fabric/store-sqlite":
version: 0.0.0-use.local
resolution: "@fabric/store-sqlite@workspace:packages/fabric/store-sqlite"
dependencies:
"@fabric/core": "workspace:^"
"@fabric/domain": "workspace:^"
sqlite3: "npm:^5.1.7"
typescript: "npm:^5.6.2"
vitest: "npm:^2.1.1"
languageName: unknown
@ -855,23 +869,12 @@ __metadata:
languageName: unknown
linkType: soft
"@ulthar/store-sqlite@workspace:packages/fabric/store-sqlite":
version: 0.0.0-use.local
resolution: "@ulthar/store-sqlite@workspace:packages/fabric/store-sqlite"
dependencies:
"@fabric/core": "workspace:^"
"@fabric/domain": "workspace:^"
sqlite3: "npm:^5.1.7"
typescript: "npm:^5.6.2"
vitest: "npm:^2.1.1"
languageName: unknown
linkType: soft
"@ulthar/template-domain@workspace:packages/templates/domain":
version: 0.0.0-use.local
resolution: "@ulthar/template-domain@workspace:packages/templates/domain"
dependencies:
"@fabric/core": "workspace:^"
"@fabric/domain": "workspace:^"
typescript: "npm:^5.6.2"
vitest: "npm:^2.1.1"
languageName: unknown
@ -1407,6 +1410,13 @@ __metadata:
languageName: node
linkType: hard
"decimal.js@npm:^10.4.3":
version: 10.4.3
resolution: "decimal.js@npm:10.4.3"
checksum: 10c0/6d60206689ff0911f0ce968d40f163304a6c1bc739927758e6efc7921cfa630130388966f16bf6ef6b838cb33679fbe8e7a78a2f3c478afce841fd55ac8fb8ee
languageName: node
linkType: hard
"decompress-response@npm:^6.0.0":
version: 6.0.0
resolution: "decompress-response@npm:6.0.0"