Compare commits

..

6 Commits

19 changed files with 326 additions and 164 deletions

17
.vscode/settings.json vendored
View File

@ -1,24 +1,17 @@
{ {
"cSpell.enabled": true, "cSpell.enabled": true,
"cSpell.language": "en,es", "cSpell.language": "en,es",
"editor.defaultFormatter": "esbenp.prettier-vscode", "editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true, "editor.formatOnSave": true,
"files.autoSave": "off", "files.autoSave": "off",
"files.eol": "\n", "files.eol": "\n",
"javascript.preferences.importModuleSpecifierEnding": "js",
"editor.codeActionsOnSave": { "editor.codeActionsOnSave": {
"source.fixAll": "always", "source.fixAll": "always",
"source.organizeImports": "always" "source.organizeImports": "always"
}, },
"search.exclude": {
"**/.yarn": true,
"**/node_modules": true,
"packages/frontend/{android,ios}": true
},
"typescript.preferences.importModuleSpecifierEnding": "js",
"cSpell.words": ["autodocs", "Syntropy"], "cSpell.words": ["autodocs", "Syntropy"],
"typescript.preferences.autoImportFileExcludePatterns": ["**/chai/**"], "deno.enable": true,
"[typescript]": { "deno.lint": true,
"editor.defaultFormatter": "denoland.vscode-deno" "deno.unstable": [],
} "deno.suggest.imports.autoDiscover": true
} }

View File

@ -1,6 +1,6 @@
// deno-lint-ignore-file no-namespace no-explicit-any // deno-lint-ignore-file no-namespace no-explicit-any no-async-promise-executor
import { UnexpectedError } from "@fabric/core";
import type { TaggedError } from "../error/tagged-error.ts"; import type { TaggedError } from "../error/tagged-error.ts";
import { UnexpectedError } from "../error/unexpected-error.ts";
import type { MaybePromise } from "../types/maybe-promise.ts"; import type { MaybePromise } from "../types/maybe-promise.ts";
import { Result } from "./result.ts"; import { Result } from "./result.ts";
@ -8,24 +8,123 @@ import { Result } from "./result.ts";
* An AsyncResult represents the result of an asynchronous operation that can * An AsyncResult represents the result of an asynchronous operation that can
* resolve to a value of type `TValue` or an error of type `TError`. * resolve to a value of type `TValue` or an error of type `TError`.
*/ */
export type AsyncResult< export class AsyncResult<
TValue = any, TValue = any,
TError extends TaggedError = never, TError extends TaggedError = never,
> = Promise<Result<TValue, TError>>; > {
static tryFrom<T, TError extends TaggedError>(
export namespace AsyncResult {
export async function tryFrom<T, TError extends TaggedError>(
fn: () => MaybePromise<T>, fn: () => MaybePromise<T>,
errorMapper: (error: any) => TError, errorMapper: (error: any) => TError,
): AsyncResult<T, TError> { ): AsyncResult<T, TError> {
try { return new AsyncResult(
return Result.succeedWith(await fn()); new Promise<Result<T, TError>>(async (resolve) => {
} catch (error) { try {
return Result.failWith(errorMapper(error)); const value = await fn();
} resolve(Result.ok(value));
} catch (error) {
resolve(Result.failWith(errorMapper(error)));
}
}),
);
} }
export function from<T>(fn: () => MaybePromise<T>): AsyncResult<T, never> { static from<T>(fn: () => MaybePromise<T>): AsyncResult<T, never> {
return tryFrom(fn, (error) => new UnexpectedError(error) as never); return AsyncResult.tryFrom(
fn,
(error) => new UnexpectedError(error) as never,
);
}
static ok<T>(value: T): AsyncResult<T, never> {
return new AsyncResult(Promise.resolve(Result.ok(value)));
}
static succeedWith = AsyncResult.ok;
static failWith<TError extends TaggedError>(
error: TError,
): AsyncResult<never, TError> {
return new AsyncResult(Promise.resolve(Result.failWith(error)));
}
private constructor(private r: Promise<Result<TValue, TError>>) {
}
promise(): Promise<Result<TValue, TError>> {
return this.r;
}
async unwrapOrThrow(): Promise<TValue> {
return (await this.r).unwrapOrThrow();
}
async orThrow(): Promise<void> {
return (await this.r).orThrow();
}
async unwrapErrorOrThrow(): Promise<TError> {
return (await this.r).unwrapErrorOrThrow();
}
/**
* Map a function over the value of the result.
*/
map<TMappedValue>(
fn: (value: TValue) => TMappedValue,
): AsyncResult<TMappedValue, TError> {
return new AsyncResult(
this.r.then((result) => result.map(fn)),
);
}
/**
* Maps a function over the value of the result and flattens the result.
*/
flatMap<TMappedValue, TMappedError extends TaggedError>(
fn: (value: TValue) => AsyncResult<TMappedValue, TMappedError>,
): AsyncResult<TMappedValue, TError | TMappedError> {
return new AsyncResult(
this.r.then((result) => {
if (result.isError()) {
return result as any;
}
return (fn(result.unwrapOrThrow())).promise();
}),
);
}
/**
* Try to map a function over the value of the result.
* If the function throws an error, the result will be a failure.
*/
tryMap<TMappedValue>(
fn: (value: TValue) => TMappedValue,
errMapper: (error: any) => TError,
): AsyncResult<TMappedValue, TError> {
return new AsyncResult(
this.r.then((result) => result.tryMap(fn, errMapper)),
);
}
/**
* Map a function over the error of the result.
*/
mapError<TMappedError extends TaggedError>(
fn: (error: TError) => TMappedError,
): AsyncResult<TValue, TMappedError> {
return new AsyncResult(
this.r.then((result) => result.mapError(fn)),
);
}
/**
* Taps a function if the result is a success.
* This is useful for side effects that do not modify the result.
*/
tap(fn: (value: TValue) => void): AsyncResult<TValue, TError> {
return new AsyncResult(
this.r.then((result) => result.tap(fn)),
);
} }
} }

View File

@ -1,27 +1,26 @@
// deno-lint-ignore-file require-await import { AsyncResult } from "@fabric/core";
import { describe, expect, test } from "@fabric/testing"; import { describe, expect, test } from "@fabric/testing";
import { UnexpectedError } from "../error/unexpected-error.ts"; import { UnexpectedError } from "../error/unexpected-error.ts";
import { Result } from "../result/result.ts";
import { Run } from "./run.ts"; import { Run } from "./run.ts";
describe("Run", () => { describe("Run", () => {
describe("In Sequence", () => { describe("In Sequence", () => {
test("should pipe the results of multiple async functions", async () => { test("should pipe the results of multiple async functions", async () => {
const result = await Run.seq( const result = Run.seq(
async () => Result.succeedWith(1), () => AsyncResult.succeedWith(1),
async (x) => Result.succeedWith(x + 1), (x) => AsyncResult.succeedWith(x + 1),
async (x) => Result.succeedWith(x * 2), (x) => AsyncResult.succeedWith(x * 2),
); );
expect(result.unwrapOrThrow()).toEqual(4); expect(await result.unwrapOrThrow()).toEqual(4);
}); });
test("should return the first error if one of the functions fails", async () => { test("should return the first error if one of the functions fails", async () => {
const result = await Run.seq( const result = await Run.seq(
async () => Result.succeedWith(1), () => AsyncResult.succeedWith(1),
async () => Result.failWith(new UnexpectedError()), () => AsyncResult.failWith(new UnexpectedError()),
async (x) => Result.succeedWith(x * 2), (x) => AsyncResult.succeedWith(x * 2),
); ).promise();
expect(result.isError()).toBe(true); expect(result.isError()).toBe(true);
}); });

View File

@ -4,7 +4,7 @@ import type { AsyncResult } from "../result/async-result.ts";
export namespace Run { export namespace Run {
// prettier-ignore // prettier-ignore
export async function seq< export function seq<
T1, T1,
TE1 extends TaggedError, TE1 extends TaggedError,
T2, T2,
@ -14,7 +14,7 @@ export namespace Run {
fn2: (value: T1) => AsyncResult<T2, TE2>, fn2: (value: T1) => AsyncResult<T2, TE2>,
): AsyncResult<T2, TE1 | TE2>; ): AsyncResult<T2, TE1 | TE2>;
// prettier-ignore // prettier-ignore
export async function seq< export function seq<
T1, T1,
TE1 extends TaggedError, TE1 extends TaggedError,
T2, T2,
@ -27,7 +27,7 @@ export namespace Run {
fn3: (value: T2) => AsyncResult<T3, TE3>, fn3: (value: T2) => AsyncResult<T3, TE3>,
): AsyncResult<T3, TE1 | TE2 | TE3>; ): AsyncResult<T3, TE1 | TE2 | TE3>;
// prettier-ignore // prettier-ignore
export async function seq< export function seq<
T1, T1,
TE1 extends TaggedError, TE1 extends TaggedError,
T2, T2,
@ -42,24 +42,20 @@ export namespace Run {
fn3: (value: T2) => AsyncResult<T3, TE3>, fn3: (value: T2) => AsyncResult<T3, TE3>,
fn4: (value: T3) => AsyncResult<T4, TE4>, fn4: (value: T3) => AsyncResult<T4, TE4>,
): AsyncResult<T4, TE1 | TE2 | TE3 | TE4>; ): AsyncResult<T4, TE1 | TE2 | TE3 | TE4>;
export async function seq( export function seq(
...fns: ((...args: any[]) => AsyncResult<any, any>)[] ...fns: ((...args: any[]) => AsyncResult<any, any>)[]
): AsyncResult<any, any> { ): AsyncResult<any, any> {
let result = await fns[0]!(); let result = fns[0]!();
for (let i = 1; i < fns.length; i++) { for (let i = 1; i < fns.length; i++) {
if (result.isError()) { result = result.flatMap((value) => fns[i]!(value));
return result;
}
result = await fns[i]!(result.unwrapOrThrow());
} }
return result; return result;
} }
// prettier-ignore // prettier-ignore
export async function seqUNSAFE< export function seqOrThrow<
T1, T1,
TE1 extends TaggedError, TE1 extends TaggedError,
T2, T2,
@ -69,7 +65,7 @@ export namespace Run {
fn2: (value: T1) => AsyncResult<T2, TE2>, fn2: (value: T1) => AsyncResult<T2, TE2>,
): Promise<T2>; ): Promise<T2>;
// prettier-ignore // prettier-ignore
export async function seqUNSAFE< export function seqOrThrow<
T1, T1,
TE1 extends TaggedError, TE1 extends TaggedError,
T2, T2,
@ -81,21 +77,11 @@ export namespace Run {
fn2: (value: T1) => AsyncResult<T2, TE2>, fn2: (value: T1) => AsyncResult<T2, TE2>,
fn3: (value: T2) => AsyncResult<T3, TE3>, fn3: (value: T2) => AsyncResult<T3, TE3>,
): Promise<T2>; ): Promise<T2>;
export async function seqUNSAFE( export function seqOrThrow(
...fns: ((...args: any[]) => AsyncResult<any, any>)[] ...fns: ((...args: any[]) => AsyncResult<any, any>)[]
): Promise<any> { ): Promise<any> {
const result = await (seq as any)(...fns); const result = (seq as any)(...fns);
if (result.isError()) {
throw result.unwrapOrThrow();
}
return result.unwrapOrThrow(); return result.unwrapOrThrow();
} }
export async function UNSAFE<T, TError extends TaggedError>(
fn: () => AsyncResult<T, TError>,
): Promise<T> {
return (await fn()).unwrapOrThrow();
}
} }

View File

@ -1,5 +1,5 @@
export * from "./fields/index.ts"; export * from "./fields/index.ts";
export * from "./model-schema.ts"; export * from "./model-schema.ts";
export * from "./model.ts"; export * from "./model.ts";
export * from "./query/index.ts";
export * from "./state-store.ts"; export * from "./state-store.ts";
export * from "./store-query/index.ts";

View File

@ -2,7 +2,7 @@ import type { AsyncResult } from "@fabric/core";
import type { StoreQueryError } from "../errors/query-error.ts"; import type { StoreQueryError } from "../errors/query-error.ts";
import type { ModelSchemaFromModels } from "./model-schema.ts"; import type { ModelSchemaFromModels } from "./model-schema.ts";
import type { Model, ModelToType } from "./model.ts"; import type { Model, ModelToType } from "./model.ts";
import type { StoreQuery } from "./query/query.ts"; import type { StoreQuery } from "./store-query/store-query.ts";
export interface ReadonlyStateStore<TModel extends Model> { export interface ReadonlyStateStore<TModel extends Model> {
from<T extends keyof ModelSchemaFromModels<TModel>>( from<T extends keyof ModelSchemaFromModels<TModel>>(

View File

@ -1,3 +1,3 @@
export * from "./filter-options.ts"; export * from "./filter-options.ts";
export * from "./order-by-options.ts"; export * from "./order-by-options.ts";
export * from "./query.ts"; export * from "./store-query.ts";

View File

@ -1,5 +1,10 @@
// deno-lint-ignore-file no-explicit-any // deno-lint-ignore-file no-explicit-any
import type { AsyncResult, Keyof, Optional } from "@fabric/core"; import {
type AsyncResult,
type Keyof,
type Optional,
TaggedError,
} from "@fabric/core";
import type { StoreQueryError } from "../../errors/query-error.ts"; import type { StoreQueryError } from "../../errors/query-error.ts";
import type { FilterOptions } from "./filter-options.ts"; import type { FilterOptions } from "./filter-options.ts";
import type { OrderByOptions } from "./order-by-options.ts"; import type { OrderByOptions } from "./order-by-options.ts";
@ -18,6 +23,11 @@ export interface StoreQuery<T> {
selectOne<K extends Keyof<T>>( selectOne<K extends Keyof<T>>(
keys: K[], keys: K[],
): AsyncResult<Optional<Pick<T, K>>, StoreQueryError>; ): AsyncResult<Optional<Pick<T, K>>, StoreQueryError>;
selectOneOrFail(): AsyncResult<T, StoreQueryError | NotFoundError>;
selectOneOrFail<K extends Keyof<T>>(
keys: K[],
): AsyncResult<Pick<T, K>, StoreQueryError | NotFoundError>;
} }
export interface StoreSortableQuery<T> { export interface StoreSortableQuery<T> {
@ -33,6 +43,11 @@ export interface StoreSortableQuery<T> {
selectOne<K extends Keyof<T>>( selectOne<K extends Keyof<T>>(
keys: K[], keys: K[],
): AsyncResult<Optional<Pick<T, K>>, StoreQueryError>; ): AsyncResult<Optional<Pick<T, K>>, StoreQueryError>;
selectOneOrFail(): AsyncResult<T, StoreQueryError | NotFoundError>;
selectOneOrFail<K extends Keyof<T>>(
keys: K[],
): AsyncResult<Pick<T, K>, StoreQueryError | NotFoundError>;
} }
export interface StoreLimitableQuery<T> { export interface StoreLimitableQuery<T> {
@ -47,6 +62,11 @@ export interface StoreLimitableQuery<T> {
selectOne<K extends Keyof<T>>( selectOne<K extends Keyof<T>>(
keys: K[], keys: K[],
): AsyncResult<Optional<Pick<T, K>>, StoreQueryError>; ): AsyncResult<Optional<Pick<T, K>>, StoreQueryError>;
selectOneOrFail(): AsyncResult<T, StoreQueryError | NotFoundError>;
selectOneOrFail<K extends Keyof<T>>(
keys: K[],
): AsyncResult<Pick<T, K>, StoreQueryError | NotFoundError>;
} }
export interface SelectableQuery<T> { export interface SelectableQuery<T> {
@ -59,9 +79,14 @@ export interface SelectableQuery<T> {
selectOne<K extends Keyof<T>>( selectOne<K extends Keyof<T>>(
keys: K[], keys: K[],
): AsyncResult<Optional<Pick<T, K>>, StoreQueryError>; ): AsyncResult<Optional<Pick<T, K>>, StoreQueryError>;
selectOneOrFail(): AsyncResult<T, StoreQueryError | NotFoundError>;
selectOneOrFail<K extends Keyof<T>>(
keys: K[],
): AsyncResult<Pick<T, K>, StoreQueryError | NotFoundError>;
} }
export interface QueryDefinition<K extends string = string> { export interface StoreQueryDefinition<K extends string = string> {
from: K; from: K;
where?: FilterOptions<any>; where?: FilterOptions<any>;
orderBy?: OrderByOptions<any>; orderBy?: OrderByOptions<any>;
@ -69,3 +94,9 @@ export interface QueryDefinition<K extends string = string> {
offset?: number; offset?: number;
keys?: string[]; keys?: string[];
} }
export class NotFoundError extends TaggedError<"NotFoundError"> {
constructor() {
super("NotFoundError");
}
}

View File

@ -0,0 +1,37 @@
// deno-lint-ignore-file no-explicit-any
import type { TaggedError } from "@fabric/core";
import type { UseCase } from "./use-case.ts";
export type Command<
TDependencies = any,
TPayload = any,
TEvent extends Event = any,
TErrors extends TaggedError<string> = any,
> = BasicCommandDefinition<TDependencies, TPayload, TEvent, TErrors>;
interface BasicCommandDefinition<
TDependencies,
TPayload,
TEvent extends Event,
TErrors extends TaggedError<string>,
> {
/**
* The use case name.
*/
name: string;
/**
* Whether the use case requires authentication or not.
*/
isAuthRequired: boolean;
/**
* Permissions required to execute the use case.
*/
permissions?: string[];
/**
* The use case function.
*/
useCase: UseCase<TDependencies, TPayload, TEvent, TErrors>;
}

View File

@ -1,2 +1,3 @@
export * from "./use-case-definition.ts"; export * from "./command.ts";
export * from "./query.ts";
export * from "./use-case.ts"; export * from "./use-case.ts";

View File

@ -2,14 +2,14 @@
import type { TaggedError } from "@fabric/core"; import type { TaggedError } from "@fabric/core";
import type { UseCase } from "./use-case.ts"; import type { UseCase } from "./use-case.ts";
export type UseCaseDefinition< export type Query<
TDependencies = any, TDependencies = any,
TPayload = any, TPayload = any,
TOutput = any, TOutput = any,
TErrors extends TaggedError<string> = any, TErrors extends TaggedError<string> = any,
> = BasicUseCaseDefinition<TDependencies, TPayload, TOutput, TErrors>; > = BasicQueryDefinition<TDependencies, TPayload, TOutput, TErrors>;
interface BasicUseCaseDefinition< interface BasicQueryDefinition<
TDependencies, TDependencies,
TPayload, TPayload,
TOutput, TOutput,
@ -25,6 +25,11 @@ interface BasicUseCaseDefinition<
*/ */
isAuthRequired: boolean; isAuthRequired: boolean;
/**
* Permissions required to execute the use case.
*/
permissions?: string[];
/** /**
* The use case function. * The use case function.
*/ */

View File

@ -1,4 +1,4 @@
import { PosixDate, Run } from "@fabric/core"; import { PosixDate } from "@fabric/core";
import { Event } from "@fabric/domain"; import { Event } from "@fabric/domain";
import { UUIDGeneratorMock } from "@fabric/domain/mocks"; import { UUIDGeneratorMock } from "@fabric/domain/mocks";
import { import {
@ -22,11 +22,11 @@ describe("Event Store", () => {
beforeEach(async () => { beforeEach(async () => {
store = new SQLiteEventStore(":memory:"); store = new SQLiteEventStore(":memory:");
await Run.UNSAFE(() => store.migrate()); await store.migrate().orThrow();
}); });
afterEach(async () => { afterEach(async () => {
await Run.UNSAFE(() => store.close()); await store.close().orThrow();
}); });
test("Should append an event", async () => { test("Should append an event", async () => {
@ -39,9 +39,9 @@ describe("Event Store", () => {
payload: { name: "test" }, payload: { name: "test" },
}; };
await Run.UNSAFE(() => store.append(userCreated)); await store.append(userCreated).orThrow();
const events = await Run.UNSAFE(() => store.getEventsFromStream(newUUID)); const events = await store.getEventsFromStream(newUUID).unwrapOrThrow();
expect(events).toHaveLength(1); expect(events).toHaveLength(1);
@ -69,7 +69,7 @@ describe("Event Store", () => {
store.subscribe(["UserCreated"], subscriber); store.subscribe(["UserCreated"], subscriber);
await Run.UNSAFE(() => store.append(userCreated)); await store.append(userCreated).orThrow();
expect(subscriber).toHaveBeenCalledTimes(1); expect(subscriber).toHaveBeenCalledTimes(1);
expect(subscriber).toHaveBeenCalledWith({ expect(subscriber).toHaveBeenCalledWith({

View File

@ -89,10 +89,7 @@ export class SQLiteEventStore<TEvents extends Event>
}), }),
(version) => this.storeEvent(event.streamId, version + 1n, event), (version) => this.storeEvent(event.streamId, version + 1n, event),
(storedEvent) => (storedEvent) =>
AsyncResult.from(async () => { this.notifySubscribers(storedEvent).map(() => storedEvent),
await this.notifySubscribers(storedEvent);
return storedEvent;
}),
); );
} }

View File

@ -5,13 +5,14 @@ import {
FilterOptions, FilterOptions,
ModelSchema, ModelSchema,
OrderByOptions, OrderByOptions,
QueryDefinition,
SelectableQuery, SelectableQuery,
StoreLimitableQuery, StoreLimitableQuery,
StoreQuery, StoreQuery,
StoreQueryDefinition,
StoreQueryError, StoreQueryError,
StoreSortableQuery, StoreSortableQuery,
} from "@fabric/domain"; } from "@fabric/domain";
import { NotFoundError } from "../../domain/models/store-query/store-query.ts";
import { filterToParams, filterToSQL } from "../sqlite/filter-to-sql.ts"; import { filterToParams, filterToSQL } from "../sqlite/filter-to-sql.ts";
import { transformRow } from "../sqlite/sql-to-value.ts"; import { transformRow } from "../sqlite/sql-to-value.ts";
import { SQLiteDatabase } from "../sqlite/sqlite-database.ts"; import { SQLiteDatabase } from "../sqlite/sqlite-database.ts";
@ -20,7 +21,7 @@ export class QueryBuilder<T> implements StoreQuery<T> {
constructor( constructor(
private db: SQLiteDatabase, private db: SQLiteDatabase,
private schema: ModelSchema, private schema: ModelSchema,
private query: QueryDefinition, private query: StoreQueryDefinition,
) {} ) {}
where(where: FilterOptions<T>): StoreSortableQuery<T> { where(where: FilterOptions<T>): StoreSortableQuery<T> {
@ -93,11 +94,42 @@ export class QueryBuilder<T> implements StoreQuery<T> {
(err) => new StoreQueryError(err.message), (err) => new StoreQueryError(err.message),
); );
} }
selectOneOrFail(): AsyncResult<T, StoreQueryError | NotFoundError>;
selectOneOrFail<K extends Extract<keyof T, string>>(
keys: K[],
): AsyncResult<Pick<T, K>, StoreQueryError | NotFoundError>;
selectOneOrFail<K extends Extract<keyof T, string>>(
keys?: K[],
): AsyncResult<any, StoreQueryError | NotFoundError> {
return AsyncResult.tryFrom(
async () => {
const [stmt, params] = getSelectStatement(
this.schema[this.query.from]!,
{
...this.query,
keys: keys!,
limit: 1,
},
);
const result = await this.db.onePrepared(
stmt,
params,
transformRow(this.schema[this.query.from]!),
);
if (!result) {
throw new NotFoundError();
}
return result;
},
(err) => new StoreQueryError(err.message),
);
}
} }
export function getSelectStatement( export function getSelectStatement(
collection: Collection, collection: Collection,
query: QueryDefinition, query: StoreQueryDefinition,
): [string, Record<string, any>] { ): [string, Record<string, any>] {
const selectFields = query.keys ? query.keys.join(", ") : "*"; const selectFields = query.keys ? query.keys.join(", ") : "*";

View File

@ -26,41 +26,37 @@ describe("State Store", () => {
beforeEach(async () => { beforeEach(async () => {
store = new SQLiteStateStore(":memory:", models); store = new SQLiteStateStore(":memory:", models);
await Run.UNSAFE(() => store.migrate()); await store.migrate().orThrow();
}); });
afterEach(async () => { afterEach(async () => {
await Run.UNSAFE(() => store.close()); await store.close().orThrow();
}); });
test("should insert a record", async () => { test("should insert a record", async () => {
const newUUID = UUIDGeneratorMock.generate(); const newUUID = UUIDGeneratorMock.generate();
await Run.UNSAFE(() => await store.insertInto("users", {
store.insertInto("users", { id: newUUID,
id: newUUID, name: "test",
name: "test", streamId: newUUID,
streamId: newUUID, streamVersion: 1n,
streamVersion: 1n, deletedAt: null,
deletedAt: null, }).orThrow();
})
);
}); });
test("should select all records", async () => { test("should select all records", async () => {
const newUUID = UUIDGeneratorMock.generate(); const newUUID = UUIDGeneratorMock.generate();
await Run.UNSAFE(() => await store.insertInto("users", {
store.insertInto("users", { name: "test",
name: "test", id: newUUID,
id: newUUID, streamId: newUUID,
streamId: newUUID, streamVersion: 1n,
streamVersion: 1n, deletedAt: null,
deletedAt: null, }).orThrow();
})
);
const result = await Run.UNSAFE(() => store.from("users").select()); const result = await store.from("users").select().unwrapOrThrow();
expectTypeOf(result).toEqualTypeOf< expectTypeOf(result).toEqualTypeOf<
{ {
@ -86,7 +82,7 @@ describe("State Store", () => {
test("should select records with a filter", async () => { test("should select records with a filter", async () => {
const newUUID = UUIDGeneratorMock.generate(); const newUUID = UUIDGeneratorMock.generate();
await Run.seqUNSAFE( await Run.seqOrThrow(
() => () =>
store.insertInto("users", { store.insertInto("users", {
name: "test", name: "test",
@ -113,14 +109,12 @@ describe("State Store", () => {
}), }),
); );
const result = await Run.UNSAFE(() => const result = await store
store .from("users")
.from("users") .where({
.where({ name: isLike("te%"),
name: isLike("te%"), })
}) .select().unwrapOrThrow();
.select()
);
expectTypeOf(result).toEqualTypeOf< expectTypeOf(result).toEqualTypeOf<
{ {
@ -146,25 +140,20 @@ describe("State Store", () => {
test("should update a record", async () => { test("should update a record", async () => {
const newUUID = UUIDGeneratorMock.generate(); const newUUID = UUIDGeneratorMock.generate();
await Run.UNSAFE(() => await store.insertInto("users", {
store.insertInto("users", { name: "test",
name: "test", id: newUUID,
id: newUUID, streamId: newUUID,
streamId: newUUID, streamVersion: 1n,
streamVersion: 1n, deletedAt: null,
deletedAt: null, }).orThrow();
})
);
await Run.UNSAFE(() => await store.update("users", newUUID, {
store.update("users", newUUID, { name: "updated",
name: "updated", }).orThrow();
})
);
const result = await Run.UNSAFE(() => const result = await store.from("users").where({ id: newUUID }).selectOne()
store.from("users").where({ id: newUUID }).selectOne() .unwrapOrThrow();
);
expect(result).toEqual({ expect(result).toEqual({
id: newUUID, id: newUUID,
@ -178,21 +167,18 @@ describe("State Store", () => {
test("should delete a record", async () => { test("should delete a record", async () => {
const newUUID = UUIDGeneratorMock.generate(); const newUUID = UUIDGeneratorMock.generate();
await Run.UNSAFE(() => await store.insertInto("users", {
store.insertInto("users", { name: "test",
name: "test", id: newUUID,
id: newUUID, streamId: newUUID,
streamId: newUUID, streamVersion: 1n,
streamVersion: 1n, deletedAt: null,
deletedAt: null, }).orThrow();
})
);
await Run.UNSAFE(() => store.delete("users", newUUID)); await store.delete("users", newUUID).orThrow();
const result = await Run.UNSAFE(() => const result = await store.from("users").where({ id: newUUID }).selectOne()
store.from("users").where({ id: newUUID }).selectOne() .unwrapOrThrow();
);
expect(result).toBeUndefined(); expect(result).toBeUndefined();
}); });
@ -203,25 +189,21 @@ describe("State Store", () => {
const newUUID = UUIDGeneratorMock.generate(); const newUUID = UUIDGeneratorMock.generate();
const ownerUUID = UUIDGeneratorMock.generate(); const ownerUUID = UUIDGeneratorMock.generate();
await Run.UNSAFE(() => await store.insertInto("users", {
store.insertInto("users", { id: ownerUUID,
id: ownerUUID, name: "test",
name: "test", streamId: ownerUUID,
streamId: ownerUUID, streamVersion: 1n,
streamVersion: 1n, deletedAt: null,
deletedAt: null, }).orThrow();
})
);
await Run.UNSAFE(() => await store.insertInto("demo", {
store.insertInto("demo", { id: newUUID,
id: newUUID, value: 1.0,
value: 1.0, owner: ownerUUID,
owner: ownerUUID, streamId: newUUID,
streamId: newUUID, streamVersion: 1n,
streamVersion: 1n, deletedAt: null,
deletedAt: null, }).orThrow();
})
);
}); });
}); });

View File

@ -1,5 +1,5 @@
import { UseCaseDefinition } from "@fabric/domain"; import { Query } from "@fabric/domain";
export const UseCases = [] as const satisfies UseCaseDefinition[]; export const UseCases = [] as const satisfies Query[];
export type UseCases = typeof UseCases; export type UseCases = typeof UseCases;