Code Review Request for UUIDv7 Domain Objects in TypeScript

👋 Hey, I'd be thankful for a quick code review and some suggestions. I'd like to create domain
objects with a default UUIDv7 value for the id property. I'm using the @typed/id package
to generate the uuid and it works, but:

- see last line: The user.id property is typed as any (I guess this is because of Schema.Schema.All, but if I type it as a branded type, I cannot use Schema.propertySignature anymore).
- The @typed/id package has a makeUuid7 function that returns an Effect and but the withConstructorDefault method expects a value, not an Effect. So I have to run the Effect to get the value, which is not ideal?


import {
    DateTimes,
    GetRandomValues,
    makeUuid7,
    Uuid7,
    Uuid7State,
} from "@typed/id";
import { Console, Effect, pipe, Schema } from "effect";

const defaultUuid = (schema: Schema.Schema.All) => {
  return pipe(
    schema,
    Schema.propertySignature,
    Schema.withConstructorDefault(() =>
      makeUuid7.pipe(
        Effect.provide(Uuid7State.Default),
        Effect.provide([GetRandomValues.CryptoRandom, DateTimes.Default]),
        Effect.runSync
      )
    )
  );
};

// USAGE

export const UserId = Uuid7.pipe(Schema.brand("UserId"));
export type UserId = typeof UserId.Type;

export class User extends Schema.Class<User>("User")({
  id: UserId.pipe(defaultUuid),
  name: Schema.NonEmptyTrimmedString,
}) {}

const user = User.make({ name: "John Doe" });

Console.log(user).pipe(Effect.runSync);
// Output:
// User { id: '0197c78d-8e0f-775a-a795-98938df30727', name: 'John Doe' }

// But:
// user.id is any
Was this page helpful?