File size: 11,343 Bytes
9697ede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* oxlint-disable */
import { Effect } from "effect"
import type { SqlError } from "effect/unstable/sql/SqlError"
import type { EffectCacheShape } from "drizzle-orm/cache/core/cache-effect"
import type { MutationOption } from "drizzle-orm/cache/core/cache"
import type { QueryEffectHKTBase } from "drizzle-orm/effect-core/query-effect"
import { entityKind } from "drizzle-orm/entity"
import type { TypedQueryBuilder } from "drizzle-orm/query-builders/query-builder"
import type { AnyRelations, EmptyRelations } from "drizzle-orm/relations"
import { SelectionProxyHandler } from "drizzle-orm/selection-proxy"
import { type ColumnsSelection, type SQL, sql, type SQLWrapper } from "drizzle-orm/sql/sql"
import type { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core/dialect"
import { QueryBuilder } from "drizzle-orm/sqlite-core/query-builders/query-builder"
import type { SelectedFields } from "drizzle-orm/sqlite-core/query-builders/select.types"
import type { SQLiteTransactionConfig } from "drizzle-orm/sqlite-core/session"
import type { SQLiteTable } from "drizzle-orm/sqlite-core/table"
import type { SQLiteViewBase } from "drizzle-orm/sqlite-core/view-base"
import { WithSubquery } from "drizzle-orm/subquery"
import type { WithBuilder } from "drizzle-orm/sqlite-core/subquery"
import { SQLiteEffectCountBuilder } from "./count"
import { SQLiteEffectDeleteBase } from "./delete"
import { SQLiteEffectInsertBuilder } from "./insert"
import { SQLiteEffectRelationalQueryBuilder } from "./query"
import { SQLiteEffectRaw } from "./raw"
import { SQLiteEffectSelectBuilder } from "./select"
import type { SQLiteEffectSelectBase } from "./select"
import type { SQLiteEffectSession, SQLiteEffectTransaction } from "./session"
import { SQLiteEffectUpdateBuilder } from "./update"

export class SQLiteEffectDatabase<
  TEffectHKT extends QueryEffectHKTBase,
  TRunResult,
  TRelations extends AnyRelations = EmptyRelations,
> {
  static readonly [entityKind]: string = "SQLiteEffectDatabase"

  declare readonly _: {
    readonly relations: TRelations
    readonly session: SQLiteEffectSession<TEffectHKT, TRunResult, TRelations>
  }

  query: {
    [K in keyof TRelations]: SQLiteEffectRelationalQueryBuilder<TRelations, TRelations[K], TEffectHKT>
  }

  constructor(
    /** @internal */
    readonly dialect: SQLiteAsyncDialect,
    /** @internal */
    readonly session: SQLiteEffectSession<TEffectHKT, TRunResult, TRelations>,
    relations: TRelations,
    readonly rowModeRQB?: boolean,
    readonly forbidJsonb?: boolean,
  ) {
    this._ = {
      relations,
      session,
    }

    this.query = {} as (typeof this)["query"]
    for (const [tableName, relation] of Object.entries(relations)) {
      ;(this.query as SQLiteEffectDatabase<TEffectHKT, TRunResult, AnyRelations>["query"])[tableName] =
        new SQLiteEffectRelationalQueryBuilder(
          relations,
          relations[relation.name]!.table as SQLiteTable,
          relation,
          dialect,
          session,
          rowModeRQB,
          forbidJsonb,
        )
    }

    this.$cache = {
      invalidate: (_params: MutationOption) => Effect.void,
    }
  }

  $with: WithBuilder = (alias: string, selection?: ColumnsSelection) => {
    const self = this
    const as = (
      qb:
        | TypedQueryBuilder<ColumnsSelection | undefined>
        | SQL
        | ((qb: QueryBuilder) => TypedQueryBuilder<ColumnsSelection | undefined> | SQL),
    ) => {
      if (typeof qb === "function") {
        qb = qb(new QueryBuilder(self.dialect))
      }

      return new Proxy(
        new WithSubquery(
          qb.getSQL(),
          selection ??
            (("getSelectedFields" in qb
              ? ((qb as { getSelectedFields(): SelectedFields | undefined }).getSelectedFields() ?? {})
              : {}) as SelectedFields),
          alias,
          true,
        ),
        new SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" }),
      )
    }
    return { as }
  }

  $cache: { invalidate: EffectCacheShape["onMutate"] }

  $count(source: SQLiteTable | SQLiteViewBase | SQL | SQLWrapper, filters?: SQL<unknown>) {
    return new SQLiteEffectCountBuilder({ source, filters, session: this.session })
  }

  with(...queries: WithSubquery[]) {
    const self = this

    function select(): SQLiteEffectSelectBuilder<undefined, TRunResult, TEffectHKT>
    function select<TSelection extends SelectedFields>(
      fields: TSelection,
    ): SQLiteEffectSelectBuilder<TSelection, TRunResult, TEffectHKT>
    function select(
      fields?: SelectedFields,
    ): SQLiteEffectSelectBuilder<SelectedFields | undefined, TRunResult, TEffectHKT> {
      return new SQLiteEffectSelectBuilder({
        fields: fields ?? undefined,
        session: self.session,
        dialect: self.dialect,
        withList: queries,
      })
    }

    function selectDistinct(): SQLiteEffectSelectBuilder<undefined, TRunResult, TEffectHKT>
    function selectDistinct<TSelection extends SelectedFields>(
      fields: TSelection,
    ): SQLiteEffectSelectBuilder<TSelection, TRunResult, TEffectHKT>
    function selectDistinct(
      fields?: SelectedFields,
    ): SQLiteEffectSelectBuilder<SelectedFields | undefined, TRunResult, TEffectHKT> {
      return new SQLiteEffectSelectBuilder({
        fields: fields ?? undefined,
        session: self.session,
        dialect: self.dialect,
        withList: queries,
        distinct: true,
      })
    }

    function update<TTable extends SQLiteTable>(
      table: TTable,
    ): SQLiteEffectUpdateBuilder<TTable, TRunResult, TEffectHKT> {
      return new SQLiteEffectUpdateBuilder(table, self.session, self.dialect, queries)
    }

    function insert<TTable extends SQLiteTable>(
      into: TTable,
    ): SQLiteEffectInsertBuilder<TTable, TRunResult, TEffectHKT> {
      return new SQLiteEffectInsertBuilder(into, self.session, self.dialect, queries)
    }

    function delete_<TTable extends SQLiteTable>(
      from: TTable,
    ): SQLiteEffectDeleteBase<TTable, TRunResult, undefined, false, never, TEffectHKT> {
      return new SQLiteEffectDeleteBase(from, self.session, self.dialect, queries)
    }

    return { select, selectDistinct, update, insert, delete: delete_ }
  }

  select(): SQLiteEffectSelectBuilder<undefined, TRunResult, TEffectHKT>
  select<TSelection extends SelectedFields>(
    fields: TSelection,
  ): SQLiteEffectSelectBuilder<TSelection, TRunResult, TEffectHKT>
  select(fields?: SelectedFields): SQLiteEffectSelectBuilder<SelectedFields | undefined, TRunResult, TEffectHKT> {
    return new SQLiteEffectSelectBuilder({ fields: fields ?? undefined, session: this.session, dialect: this.dialect })
  }

  selectDistinct(): SQLiteEffectSelectBuilder<undefined, TRunResult, TEffectHKT>
  selectDistinct<TSelection extends SelectedFields>(
    fields: TSelection,
  ): SQLiteEffectSelectBuilder<TSelection, TRunResult, TEffectHKT>
  selectDistinct(
    fields?: SelectedFields,
  ): SQLiteEffectSelectBuilder<SelectedFields | undefined, TRunResult, TEffectHKT> {
    return new SQLiteEffectSelectBuilder({
      fields: fields ?? undefined,
      session: this.session,
      dialect: this.dialect,
      distinct: true,
    })
  }

  update<TTable extends SQLiteTable>(table: TTable): SQLiteEffectUpdateBuilder<TTable, TRunResult, TEffectHKT> {
    return new SQLiteEffectUpdateBuilder(table, this.session, this.dialect)
  }

  insert<TTable extends SQLiteTable>(into: TTable): SQLiteEffectInsertBuilder<TTable, TRunResult, TEffectHKT> {
    return new SQLiteEffectInsertBuilder(into, this.session, this.dialect)
  }

  delete<TTable extends SQLiteTable>(
    from: TTable,
  ): SQLiteEffectDeleteBase<TTable, TRunResult, undefined, false, never, TEffectHKT> {
    return new SQLiteEffectDeleteBase(from, this.session, this.dialect)
  }

  private raw<TResult>(
    query: SQLWrapper | string,
    action: "all" | "get" | "run" | "values",
    execute: (query: SQL) => Effect.Effect<TResult, TEffectHKT["error"], TEffectHKT["context"]>,
  ): SQLiteEffectRaw<TResult, TEffectHKT> {
    const sequel = typeof query === "string" ? sql.raw(query) : query.getSQL()
    return new SQLiteEffectRaw(
      () => execute(sequel),
      () => sequel,
      action,
      this.dialect,
      (result) => result,
    )
  }

  run(query: SQLWrapper | string): SQLiteEffectRaw<TRunResult, TEffectHKT> {
    return this.raw(query, "run", (sequel) => this.session.run(sequel))
  }

  all<T = unknown>(query: SQLWrapper | string): SQLiteEffectRaw<T[], TEffectHKT> {
    return this.raw(query, "all", (sequel) => this.session.all(sequel))
  }

  get<T = unknown>(query: SQLWrapper | string): SQLiteEffectRaw<T | undefined, TEffectHKT> {
    return this.raw(query, "get", (sequel) => this.session.get(sequel))
  }

  values<T extends unknown[] = unknown[]>(query: SQLWrapper | string): SQLiteEffectRaw<T[], TEffectHKT> {
    return this.raw(query, "values", (sequel) => this.session.values(sequel))
  }

  transaction: <A, E, R>(
    transaction: (tx: SQLiteEffectTransaction<TEffectHKT, TRunResult, TRelations>) => Effect.Effect<A, E, R>,
    config?: SQLiteTransactionConfig,
  ) => Effect.Effect<A, E | SqlError, R> = (tx, config) => this.session.transaction(tx, config)
}

export type SQLiteEffectWithReplicas<Q> = Q & { $primary: Q; $replicas: Q[] }

export const withReplicas = <
  TEffectHKT extends QueryEffectHKTBase,
  TRunResult,
  TRelations extends AnyRelations,
  Q extends SQLiteEffectDatabase<TEffectHKT, TRunResult, TRelations>,
>(
  primary: Q,
  replicas: [Q, ...Q[]],
  getReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,
): SQLiteEffectWithReplicas<Q> => {
  const select: Q["select"] = (...args: []) => getReplica(replicas).select(...args)
  const selectDistinct: Q["selectDistinct"] = (...args: []) => getReplica(replicas).selectDistinct(...args)
  const $count: Q["$count"] = (...args: [any]) => getReplica(replicas).$count(...args)
  const _with: Q["with"] = (...args: []) => getReplica(replicas).with(...args)
  const $with = ((...args: [string] | [string, ColumnsSelection]) =>
    args.length === 1
      ? getReplica(replicas).$with(args[0])
      : getReplica(replicas).$with(args[0], args[1])) as Q["$with"]

  const update: Q["update"] = (...args: [any]) => primary.update(...args)
  const insert: Q["insert"] = (...args: [any]) => primary.insert(...args)
  const $delete: Q["delete"] = (...args: [any]) => primary.delete(...args)
  const run: Q["run"] = (...args: [any]) => primary.run(...args)
  const all: Q["all"] = (...args: [any]) => primary.all(...args)
  const get: Q["get"] = (...args: [any]) => primary.get(...args)
  const values: Q["values"] = (...args: [any]) => primary.values(...args)
  const transaction: Q["transaction"] = (...args: [any]) => primary.transaction(...args)

  return {
    ...primary,
    update,
    insert,
    delete: $delete,
    run,
    all,
    get,
    values,
    transaction,
    $primary: primary,
    $replicas: replicas,
    select,
    selectDistinct,
    $count,
    $with,
    with: _with,
    get query() {
      return getReplica(replicas).query
    },
  }
}

export type AnySQLiteEffectDatabase = SQLiteEffectDatabase<any, any, any>
export type AnySQLiteEffectSelectBase = SQLiteEffectSelectBase<any, any, any, any, any, any, any, any, any, any>