Assigning Default Values to PGVectorStoreConfig in TypeScript

Gm. is there a better way i can assign defaults to context?

export class PGVectorStoreConfig extends Context.Tag('PGVectorStoreConfig')<PGVectorStoreConfig, {
  poolConfig?: PoolConfig
  /**
   * @default "public"
   */
  schema?: string
  /**
   * @default "data_llamaindex_embedding"
   */
  tableName?: string
  /**
   * @default "1536"
   */
  dimensions?: string
}>() {}

class _PGVectorStoreConfig extends Context.Tag('_PGVectorStoreConfig')<_PGVectorStoreConfig, {
  poolConfig?: PoolConfig
  schema: string
  tableName: string
  dimensions: string
}>() {}

const PGVectorStoreConfigWithDefaults = Layer.effect(
  _PGVectorStoreConfig,
  Effect.gen(function*() {
    const config = yield* PGVectorStoreConfig

    return {
      poolConfig: config.poolConfig,
      schema: config.schema ?? 'public',
      tableName: config.tableName ?? 'data_llamaindex_embedding',
      dimensions: config.dimensions ?? '1536',
    }
  }),
)

export const PGVectorStore = Layer.effect(
  VectorStore,
  Effect.gen(function*() {
    const config = yield* _PGVectorStoreConfig
    const pool = yield* Effect.sync(() => new Pool(config.poolConfig))

    yield* setupVectorExtension(pool)

    const dialect = yield* Effect.sync(() => new PostgresDialect({ pool }))
    const db = yield* Effect.sync(() => new Kysely<Database>({ dialect }))

    yield* setupDatabase(db)

    return yield* new RuntimeException('Not Implemented')
  }),
).pipe(
  Layer.provide(PGVectorStoreConfigWithDefaults),
)
Was this page helpful?