MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeRepository

Function makeRepository

packages/effect/src/unstable/sql/SqlModel.ts:33–221  ·  view source on GitHub ↗
(Model: S, options: {
  readonly tableName: string
  readonly spanPrefix: string
  readonly idColumn: Id
  readonly softDeleteColumn?: SoftDelete | undefined
})

Source from the content-addressed store, hash-verified

31 * @since 4.0.0
32 */
33export const makeRepository = <
34 S extends Model.Any,
35 Id extends (keyof S["Type"]) & (keyof S["update"]["Type"]) & (keyof S["fields"]),
36 SoftDelete extends keyof S["fields"] = never
37>(Model: S, options: {
38 readonly tableName: string
39 readonly spanPrefix: string
40 readonly idColumn: Id
41 readonly softDeleteColumn?: SoftDelete | undefined
42}): Effect.Effect<
43 {
44 readonly insert: (
45 insert: S["insert"]["Type"]
46 ) => Effect.Effect<
47 S["Type"],
48 Schema.SchemaError | SqlError,
49 S["DecodingServices"] | S["insert"]["EncodingServices"]
50 >
51 readonly insertVoid: (
52 insert: S["insert"]["Type"]
53 ) => Effect.Effect<void, Schema.SchemaError | SqlError, S["insert"]["EncodingServices"]>
54 readonly update: (
55 update: S["update"]["Type"]
56 ) => Effect.Effect<
57 S["Type"],
58 Schema.SchemaError | SqlError,
59 S["DecodingServices"] | S["update"]["EncodingServices"]
60 >
61 readonly updateVoid: (
62 update: S["update"]["Type"]
63 ) => Effect.Effect<void, Schema.SchemaError | SqlError, S["update"]["EncodingServices"]>
64 readonly findById: (
65 id: S["fields"][Id]["Type"]
66 ) => Effect.Effect<
67 S["Type"],
68 Cause.NoSuchElementError | Schema.SchemaError | SqlError,
69 S["DecodingServices"] | S["fields"][Id]["EncodingServices"]
70 >
71 readonly delete: (
72 id: S["fields"][Id]["Type"]
73 ) => Effect.Effect<void, Schema.SchemaError | SqlError, S["fields"][Id]["EncodingServices"]>
74 },
75 never,
76 SqlClient
77> =>
78 Effect.gen(function*() {
79 const sql = yield* SqlClient
80 const idSchema = Model.fields[options.idColumn]
81 const idColumn = options.idColumn as string
82 const softDeleteColumn = options.softDeleteColumn as string | undefined
83 const withSoftDeleteFilter = (where: any) =>
84 softDeleteColumn === undefined ? where : sql.and([where, sql`${sql(softDeleteColumn)} is null`])
85 const setSoftDeleted = softDeleteColumn === undefined
86 ? undefined
87 : sql`${sql(softDeleteColumn)} = CURRENT_TIMESTAMP`
88
89 const insertSchema = SqlSchema.findOne({
90 Request: Model.insert,

Callers

nothing calls this directly

Calls 4

withSoftDeleteFilterFunction · 0.85
pipeMethod · 0.65
mapMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected