Effect CommunityEC
Effect Community16mo ago
9 replies
shoki

Troubleshooting Effect/Schema Integration with Effect/SQL for User Creation

Hello, I am trying to use effect/schema with effect/sql and I have a small problem:

I have the following schema:
class User extends Schema.Class<User>('User')({
  id: Schema.UUID,
  name: Schema.String,
  email: Schema.String,
  password: Schema.String,
  createdAt: Schema.DateTimeUtc,
  updatedAt: Schema.DateTimeUtc,
}) {}


And the following effect:
const create = (user: CreateUser) =>
  Effect.gen(function* () {
    const InsertUser = yield* SqlResolver.ordered("InsertUser", {
      Request: User,
      Result: User,
      execute: (requests) =>
        sql`
            INSERT INTO users
            ${sql.insert(requests)}
            RETURNING users.*
          `,
    });

    const now = yield* DateTime.now;

    const newUser = yield* InsertUser.execute({
      id: randomUUID(),
      name: user.name,
      email: user.email,
      password: user.password,
      createdAt: now,
      updatedAt: now,
    });

    return newUser;
  });


When a new user is inserted in the database, the date fields (createdAt, updatedAt) are returned as Date , and if I my understanding is good I can't make a DateTimeUtc from a Date, I have the following error: Expected string, actual Thu Sep 12 2024 23:33:03 GMT+0000 (Coordinated Universal Time)

My guess is that I should use transform to manually write the decode/encode functions, maybe I am wrong, can someone help please ? Thanks
Was this page helpful?