Defining Payload as an Effect Service with Hooks Integration

I'm working on a project which uses Payload as it's CMS. I want to be able to define Payload as an effect service, but also use effect inside of the "hooks" on a bunch of collections. When initializing payload, you have to provide the configuration of these collections, including the hooks. Does the following setup make any sense at all?

function createConfig(config: PayloadConfig) {
  return Effect.promise(() => buildConfig(config));
}

export const config = Effect.gen(function* () {
  const db = yield* DbClient;
  const secret = yield* Config.redacted('PAYLOAD_SECRET');
  const vercelEnv = yield* Config.string('VERCEL_ENV').pipe(Config.option);

  const runtime = yield* Effect.runtime<DbClient>();

  return yield* createConfig({
    debug: Option.getOrUndefined(vercelEnv) !== 'production',
    secret: Redacted.value(secret),

    collections: [
      {
        slug: 'test',
        fields: [],
        hooks: {
          beforeRead: [
            async ({ doc }) => {
              await Effect.gen(function* () {
                const dbClient = yield* DbClient;

                yield* Effect.log('Test collection:', dbClient, doc);
              }).pipe(Effect.provide(Logger.logFmt), Runtime.runPromise(runtime));
            },
          ],
        },
      },
    ],

    db,
    email: emailAdapter,
  });
});
Was this page helpful?