Effect CommunityEC
Effect Community3y ago
7 replies
pixeleet

Modeling Mongoose Schemas with @effect/schema: Best Way to Model References

Trying to model mongoose schemas with @effect/schema
I wonder, what would be the best way to model references?

import {
  getAnnotation,
  IdentifierAnnotationId,
} from "@effect/schema/AST";
import {
  array,
  boolean,
  brand,
  Date as SDate,
  dateFromString,
  filter,
  identifier,
  lazy,
  optional,
  Schema,
  string,
  struct,
  title,
} from "@effect/schema/Schema";
import { Option } from "effect";

const objectId = string.pipe(
  filter((s) => s.length === 24),
  brand("ObjectId"),
  identifier("ObjectId"),
  title("ObjectId"),
);
const dateOrNow = optional(SDate).withDefault(() => new Date());

// TODO: Extend the schema with more fields.
const timestamps = {
  createdAt: dateOrNow,
  updatedAt: dateOrNow,
};

const ref = <A, B>(type: Schema<A, B>) => (field: Extract<keyof B, string>) =>
  type.pipe(
    identifier(field),
  );

export const Account = struct({
  userId: lazy(() => ref(User)("id")),
  type: string,
  provider: string,
  providerAccountId: string,
  refresh_token: optional(string),
  access_token: optional(string),
  expires_at: optional(string),
  token_type: optional(string),
  scope: optional(string),
  id_token: optional(string),
  session_state: optional(string),
  ...timestamps,
});


While trying to inspect the Account AST I'm not finding the annotation that was attached. What am I doing wrong?
Was this page helpful?