[fabric/domain] Simplify model definition

This commit is contained in:
Pablo Baleztena 2024-10-20 23:12:07 -03:00
parent 25870c8558
commit 9f9419c2b6
12 changed files with 74 additions and 130 deletions

View File

@ -1,37 +0,0 @@
import type { Keyof } from "../../core/index.ts";
import type { CustomModelFields } from "./custom-model-fields.ts";
import { DefaultEntityFields, type EntityModel } from "./entity-model.ts";
import { Field } from "./fields/index.ts";
export const DefaultAggregateFields = {
...DefaultEntityFields,
streamId: Field.uuid({ isIndexed: true }),
streamVersion: Field.integer({
isUnsigned: true,
hasArbitraryPrecision: true,
}),
deletedAt: Field.timestamp({ isOptional: true }),
};
export interface AggregateModel<
TName extends string = string,
TFields extends CustomModelFields = CustomModelFields,
> extends EntityModel<TName, TFields> {
fields: typeof DefaultAggregateFields & TFields;
}
export function defineAggregateModel<
TName extends string,
TFields extends CustomModelFields,
>(name: TName, fields: TFields): AggregateModel<TName, TFields> {
return {
name,
fields: { ...DefaultAggregateFields, ...fields },
} as const;
}
export type ModelAddressableFields<TModel extends AggregateModel> = {
[K in Keyof<TModel["fields"]>]: TModel["fields"][K] extends { isUnique: true }
? K
: never;
}[Keyof<TModel["fields"]>];

View File

@ -1,3 +0,0 @@
import type { FieldDefinition } from "./fields/index.ts";
export type CustomModelFields = Record<string, FieldDefinition>;

View File

@ -1,14 +0,0 @@
import type { CustomModelFields } from "./custom-model-fields.ts";
import { Field } from "./fields/index.ts";
import type { Model } from "./model.ts";
export const DefaultEntityFields = {
id: Field.uuid({ isPrimaryKey: true }),
};
export interface EntityModel<
TName extends string = string,
TFields extends CustomModelFields = CustomModelFields,
> extends Model<TName, TFields> {
fields: typeof DefaultEntityFields & TFields;
}

View File

@ -1,6 +1,6 @@
import { isError } from "@fabric/core"; import { isError } from "@fabric/core";
import { describe, expect, test } from "@fabric/testing"; import { describe, expect, test } from "@fabric/testing";
import { defineAggregateModel } from "../aggregate-model.ts"; import { Model } from "../model.ts";
import { Field } from "./index.ts"; import { Field } from "./index.ts";
import { import {
InvalidReferenceFieldError, InvalidReferenceFieldError,
@ -9,7 +9,7 @@ import {
describe("Validate Reference Field", () => { describe("Validate Reference Field", () => {
const schema = { const schema = {
User: defineAggregateModel("User", { User: Model.aggregateFrom("User", {
name: Field.string(), name: Field.string(),
password: Field.string(), password: Field.string(),
otherUnique: Field.integer({ isUnique: true }), otherUnique: Field.integer({ isUnique: true }),

View File

@ -1,5 +1,3 @@
export * from "./aggregate-model.ts";
export * from "./custom-model-fields.ts";
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";

View File

@ -1,13 +1,12 @@
import type { UUID } from "@fabric/core"; import type { UUID } from "@fabric/core";
import { describe, expectTypeOf, test } from "@fabric/testing"; import { describe, expectTypeOf, test } from "@fabric/testing";
import type { PosixDate } from "../../core/index.ts"; import type { PosixDate } from "../../core/index.ts";
import { defineAggregateModel } from "./aggregate-model.ts";
import { Field } from "./fields/index.ts"; import { Field } from "./fields/index.ts";
import type { ModelToType } from "./model.ts"; import { Model, type ModelToType } from "./model.ts";
describe("CreateModel", () => { describe("CreateModel", () => {
test("should create a model and it's interface type", () => { test("should create a model and it's interface type", () => {
const User = defineAggregateModel("User", { const User = Model.aggregateFrom("User", {
name: Field.string(), name: Field.string(),
password: Field.string(), password: Field.string(),
phone: Field.string({ isOptional: true }), phone: Field.string({ isOptional: true }),

View File

@ -1,29 +1,73 @@
import type { Keyof } from "@fabric/core"; import type { Keyof } from "@fabric/core";
import type { CustomModelFields } from "./custom-model-fields.ts";
import type { FieldToType } from "./fields/field-to-type.ts"; import type { FieldToType } from "./fields/field-to-type.ts";
import { Field, type FieldDefinition } from "./fields/index.ts";
export interface Model< /**
* A model is a schema definition for some type of structured data.
*/
export class Model<
TName extends string = string, TName extends string = string,
TFields extends CustomModelFields = CustomModelFields, TFields extends ModelFields = ModelFields,
> { > {
name: TName; static from<TName extends string, TFields extends ModelFields>(
fields: TFields; name: TName,
fields: TFields,
) {
return new Model(name, fields);
}
static aggregateFrom<TName extends string, TFields extends ModelFields>(
name: TName,
fields: TFields,
): Model<TName, TFields & typeof DefaultAggregateFields> {
return new Model(name, { ...fields, ...DefaultAggregateFields });
}
static entityFrom<TName extends string, TFields extends ModelFields>(
name: TName,
fields: TFields,
): Model<TName, TFields & typeof DefaultEntityFields> {
return new Model(name, { ...fields, ...DefaultEntityFields });
}
private constructor(readonly name: TName, readonly fields: TFields) {}
} }
export function defineModel< export type EntityModel = Model<
TName extends string, string,
TFields extends CustomModelFields, typeof DefaultEntityFields & ModelFields
>(name: TName, fields: TFields): Model<TName, TFields> { >;
return {
name, export type AggregateModel = Model<
fields, string,
} as const; typeof DefaultAggregateFields & ModelFields
} >;
export type ModelToType<TModel extends Model> = { export type ModelToType<TModel extends Model> = {
[K in Keyof<TModel["fields"]>]: FieldToType<TModel["fields"][K]>; [K in Keyof<TModel["fields"]>]: FieldToType<TModel["fields"][K]>;
}; };
export type ModelFieldNames<TModel extends CustomModelFields> = Keyof< export type ModelFieldNames<TModel extends ModelFields> = Keyof<
TModel["fields"] TModel["fields"]
>; >;
export type ModelAddressableFields<TModel extends Model> = {
[K in Keyof<TModel["fields"]>]: TModel["fields"][K] extends { isUnique: true }
? K
: never;
}[Keyof<TModel["fields"]>];
type ModelFields = Record<string, FieldDefinition>;
const DefaultEntityFields = {
id: Field.uuid({ isPrimaryKey: true }),
} as const;
const DefaultAggregateFields = {
...DefaultEntityFields,
streamId: Field.uuid({ isIndexed: true }),
streamVersion: Field.integer({
isUnsigned: true,
hasArbitraryPrecision: true,
}),
deletedAt: Field.timestamp({ isOptional: true }),
} as const;

View File

@ -1,8 +1,7 @@
import type { VariantTag } from "@fabric/core"; import type { VariantTag } from "@fabric/core";
import type { Event } from "../events/event.ts"; import type { Event } from "../events/event.ts";
import type { StoredEvent } from "../events/stored-event.ts"; import type { StoredEvent } from "../events/stored-event.ts";
import type { AggregateModel } from "../models/aggregate-model.ts"; import type { AggregateModel, ModelToType } from "../models/model.ts";
import type { ModelToType } from "../models/model.ts";
export interface Projection< export interface Projection<
TModel extends AggregateModel, TModel extends AggregateModel,

View File

@ -1,5 +1,4 @@
import { import {
defineModel,
Field, Field,
isGreaterOrEqualTo, isGreaterOrEqualTo,
isGreaterThan, isGreaterThan,
@ -8,12 +7,13 @@ import {
isLessThan, isLessThan,
isLike, isLike,
isNotEqualTo, isNotEqualTo,
Model,
} from "@fabric/domain"; } from "@fabric/domain";
import { describe, expect, test } from "@fabric/testing"; import { describe, expect, test } from "@fabric/testing";
import { filterToParams, filterToSQL } from "./filter-to-sql.ts"; import { filterToParams, filterToSQL } from "./filter-to-sql.ts";
describe("SQL where clause from filter options", () => { describe("SQL where clause from filter options", () => {
const col = defineModel("users", { const col = Model.from("users", {
name: Field.string(), name: Field.string(),
age: Field.integer(), age: Field.integer(),
status: Field.string(), status: Field.string(),

View File

@ -10,7 +10,7 @@ import {
MultiFilterOption, MultiFilterOption,
SingleFilterOption, SingleFilterOption,
} from "@fabric/domain"; } from "@fabric/domain";
import { keyToParam } from "./record-utils.ts"; import { keyToParamKey } from "./record-utils.ts";
import { fieldValueToSQL } from "./value-to-sql.ts"; import { fieldValueToSQL } from "./value-to-sql.ts";
export function filterToSQL(filterOptions?: FilterOptions) { export function filterToSQL(filterOptions?: FilterOptions) {
@ -57,7 +57,7 @@ function getWhereFromSingleOption(
const WHERE_KEY_PREFIX = "where_"; const WHERE_KEY_PREFIX = "where_";
function getWhereParamKey(key: string, opts: { postfix?: string } = {}) { function getWhereParamKey(key: string, opts: { postfix?: string } = {}) {
return keyToParam(`${WHERE_KEY_PREFIX}${key}${opts.postfix ?? ""}`); return keyToParamKey(`${WHERE_KEY_PREFIX}${key}${opts.postfix ?? ""}`);
} }
function getWhereFromKeyValue( function getWhereFromKeyValue(

View File

@ -1,9 +1,9 @@
import { defineModel, Field } from "@fabric/domain"; import { Field, Model } from "@fabric/domain";
import { describe, expect, test } from "@fabric/testing"; import { describe, expect, test } from "@fabric/testing";
import { modelToSql } from "./model-to-sql.ts"; import { modelToSql } from "./model-to-sql.ts";
describe("ModelToSQL", () => { describe("ModelToSQL", () => {
const model = defineModel("something", { const model = Model.from("something", {
id: Field.uuid({ isPrimaryKey: true }), id: Field.uuid({ isPrimaryKey: true }),
name: Field.string(), name: Field.string(),
age: Field.integer(), age: Field.integer(),

View File

@ -1,5 +1,5 @@
import { PosixDate, Run, UUID } from "@fabric/core"; import { Run, UUID } from "@fabric/core";
import { defineAggregateModel, Field, isLike } from "@fabric/domain"; import { Field, isLike, Model } from "@fabric/domain";
import { UUIDGeneratorMock } from "@fabric/domain/mocks"; import { UUIDGeneratorMock } from "@fabric/domain/mocks";
import { import {
afterEach, afterEach,
@ -13,11 +13,11 @@ import { SQLiteStateStore } from "./state-store.ts";
describe("State Store", () => { describe("State Store", () => {
const models = [ const models = [
defineAggregateModel("demo", { Model.entityFrom("demo", {
value: Field.float(), value: Field.float(),
owner: Field.reference({ targetModel: "users" }), owner: Field.reference({ targetModel: "users" }),
}), }),
defineAggregateModel("users", { Model.entityFrom("users", {
name: Field.string(), name: Field.string(),
}), }),
]; ];
@ -39,9 +39,6 @@ describe("State Store", () => {
await store.insertInto("users", { await store.insertInto("users", {
id: newUUID, id: newUUID,
name: "test", name: "test",
streamId: newUUID,
streamVersion: 1n,
deletedAt: null,
}).orThrow(); }).orThrow();
}); });
@ -51,9 +48,6 @@ describe("State Store", () => {
await store.insertInto("users", { await store.insertInto("users", {
name: "test", name: "test",
id: newUUID, id: newUUID,
streamId: newUUID,
streamVersion: 1n,
deletedAt: null,
}).orThrow(); }).orThrow();
const result = await store.from("users").select().unwrapOrThrow(); const result = await store.from("users").select().unwrapOrThrow();
@ -61,20 +55,14 @@ describe("State Store", () => {
expectTypeOf(result).toEqualTypeOf< expectTypeOf(result).toEqualTypeOf<
{ {
id: UUID; id: UUID;
streamId: UUID;
streamVersion: bigint;
name: string; name: string;
deletedAt: PosixDate | null;
}[] }[]
>(); >();
expect(result).toEqual([ expect(result).toEqual([
{ {
id: newUUID, id: newUUID,
streamId: newUUID,
streamVersion: 1n,
name: "test", name: "test",
deletedAt: null,
}, },
]); ]);
}); });
@ -87,25 +75,16 @@ describe("State Store", () => {
store.insertInto("users", { store.insertInto("users", {
name: "test", name: "test",
id: newUUID, id: newUUID,
streamId: newUUID,
streamVersion: 1n,
deletedAt: null,
}), }),
() => () =>
store.insertInto("users", { store.insertInto("users", {
name: "anotherName", name: "anotherName",
id: UUIDGeneratorMock.generate(), id: UUIDGeneratorMock.generate(),
streamId: UUIDGeneratorMock.generate(),
streamVersion: 1n,
deletedAt: null,
}), }),
() => () =>
store.insertInto("users", { store.insertInto("users", {
name: "anotherName2", name: "anotherName2",
id: UUIDGeneratorMock.generate(), id: UUIDGeneratorMock.generate(),
streamId: UUIDGeneratorMock.generate(),
streamVersion: 1n,
deletedAt: null,
}), }),
); );
@ -119,20 +98,14 @@ describe("State Store", () => {
expectTypeOf(result).toEqualTypeOf< expectTypeOf(result).toEqualTypeOf<
{ {
id: UUID; id: UUID;
streamId: UUID;
streamVersion: bigint;
name: string; name: string;
deletedAt: PosixDate | null;
}[] }[]
>(); >();
expect(result).toEqual([ expect(result).toEqual([
{ {
id: newUUID, id: newUUID,
streamId: newUUID,
streamVersion: 1n,
name: "test", name: "test",
deletedAt: null,
}, },
]); ]);
}); });
@ -143,9 +116,6 @@ describe("State Store", () => {
await store.insertInto("users", { await store.insertInto("users", {
name: "test", name: "test",
id: newUUID, id: newUUID,
streamId: newUUID,
streamVersion: 1n,
deletedAt: null,
}).orThrow(); }).orThrow();
await store.update("users", newUUID, { await store.update("users", newUUID, {
@ -157,10 +127,7 @@ describe("State Store", () => {
expect(result).toEqual({ expect(result).toEqual({
id: newUUID, id: newUUID,
streamId: newUUID,
streamVersion: 1n,
name: "updated", name: "updated",
deletedAt: null,
}); });
}); });
@ -170,9 +137,6 @@ describe("State Store", () => {
await store.insertInto("users", { await store.insertInto("users", {
name: "test", name: "test",
id: newUUID, id: newUUID,
streamId: newUUID,
streamVersion: 1n,
deletedAt: null,
}).orThrow(); }).orThrow();
await store.delete("users", newUUID).orThrow(); await store.delete("users", newUUID).orThrow();
@ -192,18 +156,12 @@ describe("State Store", () => {
await store.insertInto("users", { await store.insertInto("users", {
id: ownerUUID, id: ownerUUID,
name: "test", name: "test",
streamId: ownerUUID,
streamVersion: 1n,
deletedAt: null,
}).orThrow(); }).orThrow();
await store.insertInto("demo", { await store.insertInto("demo", {
id: newUUID, id: newUUID,
value: 1.0, value: 1.0,
owner: ownerUUID, owner: ownerUUID,
streamId: newUUID,
streamVersion: 1n,
deletedAt: null,
}).orThrow(); }).orThrow();
}); });
}); });