feat(effect-drizzle-sqlite): add vendored sqlite adapter (#28547)
This commit is contained in:
parent
7b9d7a7b7d
commit
5381795844
28 changed files with 3697 additions and 0 deletions
|
|
@ -0,0 +1,58 @@
|
|||
/* oxlint-disable */
|
||||
import type * as Effect from "effect/Effect"
|
||||
import { applyEffectWrapper, type QueryEffectHKTBase } from "drizzle-orm/effect-core/query-effect"
|
||||
import { entityKind } from "drizzle-orm/entity"
|
||||
import { SQL, sql, type SQLWrapper } from "drizzle-orm/sql/sql"
|
||||
import type { SQLiteTable } from "drizzle-orm/sqlite-core/table"
|
||||
import type { SQLiteView } from "drizzle-orm/sqlite-core/view"
|
||||
import type { SQLiteEffectSession } from "./session"
|
||||
|
||||
function buildSQLiteEmbeddedCount(source: SQLiteTable | SQLiteView | SQL | SQLWrapper, filters?: SQL<unknown>) {
|
||||
return sql<number>`(select count(*) from ${source}${sql.raw(" where ").if(filters)}${filters})`
|
||||
}
|
||||
|
||||
function buildSQLiteCount(source: SQLiteTable | SQLiteView | SQL | SQLWrapper, filters?: SQL<unknown>) {
|
||||
return sql<number>`select count(*) from ${source}${sql.raw(" where ").if(filters)}${filters}`
|
||||
}
|
||||
|
||||
export interface SQLiteEffectCountBuilder<TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase>
|
||||
extends SQL<number>,
|
||||
SQLWrapper<number>,
|
||||
Effect.Effect<number, TEffectHKT["error"], TEffectHKT["context"]> {}
|
||||
|
||||
export class SQLiteEffectCountBuilder<TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase> extends SQL<number> {
|
||||
static override readonly [entityKind]: string = "SQLiteEffectCountBuilder"
|
||||
|
||||
private sql: SQL<number>
|
||||
private session: SQLiteEffectSession<TEffectHKT, any, any>
|
||||
|
||||
constructor(params: {
|
||||
source: SQLiteTable | SQLiteView | SQL | SQLWrapper
|
||||
filters?: SQL<unknown>
|
||||
session: SQLiteEffectSession<TEffectHKT, any, any>
|
||||
}) {
|
||||
super(buildSQLiteEmbeddedCount(params.source, params.filters).queryChunks)
|
||||
|
||||
this.session = params.session
|
||||
this.sql = buildSQLiteCount(params.source, params.filters)
|
||||
}
|
||||
|
||||
execute(placeholderValues?: Record<string, unknown>) {
|
||||
return this.session
|
||||
.prepareQuery<{
|
||||
type: "async"
|
||||
execute: number
|
||||
run: unknown
|
||||
all: unknown
|
||||
get: unknown
|
||||
values: unknown
|
||||
}>(this.session.dialect.sqlToQuery(this.sql), undefined, "all", (rows) => {
|
||||
const v = rows[0]?.[0]
|
||||
if (typeof v === "number") return v
|
||||
return v ? Number(v) : 0
|
||||
})
|
||||
.execute(placeholderValues)
|
||||
}
|
||||
}
|
||||
|
||||
applyEffectWrapper(SQLiteEffectCountBuilder)
|
||||
296
packages/effect-drizzle-sqlite/src/sqlite-core/effect/db.ts
Normal file
296
packages/effect-drizzle-sqlite/src/sqlite-core/effect/db.ts
Normal file
|
|
@ -0,0 +1,296 @@
|
|||
/* 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>
|
||||
261
packages/effect-drizzle-sqlite/src/sqlite-core/effect/delete.ts
Normal file
261
packages/effect-drizzle-sqlite/src/sqlite-core/effect/delete.ts
Normal file
|
|
@ -0,0 +1,261 @@
|
|||
/* oxlint-disable */
|
||||
import type * as Effect from "effect/Effect"
|
||||
import { applyEffectWrapper, type QueryEffectHKTBase } from "drizzle-orm/effect-core/query-effect"
|
||||
import { entityKind } from "drizzle-orm/entity"
|
||||
import type { SelectResultFields } from "drizzle-orm/query-builders/select.types"
|
||||
import type { RunnableQuery } from "drizzle-orm/runnable-query"
|
||||
import { SelectionProxyHandler } from "drizzle-orm/selection-proxy"
|
||||
import type { Placeholder, Query, SQL, SQLWrapper } from "drizzle-orm/sql/sql"
|
||||
import type { SQLiteDialect } from "drizzle-orm/sqlite-core/dialect"
|
||||
import type { SQLiteDeleteConfig } from "drizzle-orm/sqlite-core/query-builders/delete"
|
||||
import type { SelectedFieldsFlat } from "drizzle-orm/sqlite-core/query-builders/select.types"
|
||||
import type { PreparedQueryConfig } from "drizzle-orm/sqlite-core/session"
|
||||
import { SQLiteTable } from "drizzle-orm/sqlite-core/table"
|
||||
import { extractUsedTable } from "drizzle-orm/sqlite-core/utils"
|
||||
import type { Subquery } from "drizzle-orm/subquery"
|
||||
import { type DrizzleTypeError, type ValueOrArray } from "drizzle-orm/utils"
|
||||
import type { SQLiteColumn } from "drizzle-orm/sqlite-core/columns/common"
|
||||
import { getTableColumnsRuntime, orderSelectedFields } from "../../internal/drizzle-utils"
|
||||
import type { SQLiteEffectPreparedQuery, SQLiteEffectSession } from "./session"
|
||||
|
||||
export type SQLiteEffectDeleteWithout<
|
||||
T extends AnySQLiteEffectDelete,
|
||||
TDynamic extends boolean,
|
||||
K extends keyof T & string,
|
||||
> = TDynamic extends true
|
||||
? T
|
||||
: Omit<
|
||||
SQLiteEffectDeleteBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["returning"],
|
||||
TDynamic,
|
||||
T["_"]["excludedMethods"] | K,
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
T["_"]["excludedMethods"] | K
|
||||
>
|
||||
|
||||
export type SQLiteEffectDeleteReturningAll<
|
||||
T extends AnySQLiteEffectDelete,
|
||||
TDynamic extends boolean,
|
||||
> = SQLiteEffectDeleteWithout<
|
||||
SQLiteEffectDeleteBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["table"]["$inferSelect"],
|
||||
T["_"]["dynamic"],
|
||||
T["_"]["excludedMethods"],
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
TDynamic,
|
||||
"returning"
|
||||
>
|
||||
|
||||
export type SQLiteEffectDeleteReturning<
|
||||
T extends AnySQLiteEffectDelete,
|
||||
TDynamic extends boolean,
|
||||
TSelectedFields extends SelectedFieldsFlat,
|
||||
> = SQLiteEffectDeleteWithout<
|
||||
SQLiteEffectDeleteBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
SelectResultFields<TSelectedFields>,
|
||||
T["_"]["dynamic"],
|
||||
T["_"]["excludedMethods"],
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
TDynamic,
|
||||
"returning"
|
||||
>
|
||||
|
||||
export type SQLiteEffectDeleteExecute<T extends AnySQLiteEffectDelete> = T["_"]["returning"] extends undefined
|
||||
? T["_"]["runResult"]
|
||||
: T["_"]["returning"][]
|
||||
|
||||
export type SQLiteEffectDeletePrepare<
|
||||
T extends AnySQLiteEffectDelete,
|
||||
TEffectHKT extends QueryEffectHKTBase = T["_"]["effectHKT"],
|
||||
> = SQLiteEffectPreparedQuery<
|
||||
PreparedQueryConfig & {
|
||||
run: T["_"]["runResult"]
|
||||
all: T["_"]["returning"] extends undefined
|
||||
? DrizzleTypeError<".all() cannot be used without .returning()">
|
||||
: T["_"]["returning"][]
|
||||
get: T["_"]["returning"] extends undefined
|
||||
? DrizzleTypeError<".get() cannot be used without .returning()">
|
||||
: T["_"]["returning"] | undefined
|
||||
values: T["_"]["returning"] extends undefined
|
||||
? DrizzleTypeError<".values() cannot be used without .returning()">
|
||||
: any[][]
|
||||
execute: SQLiteEffectDeleteExecute<T>
|
||||
},
|
||||
TEffectHKT
|
||||
>
|
||||
|
||||
export type SQLiteEffectDeleteDynamic<T extends AnySQLiteEffectDelete> = SQLiteEffectDelete<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["returning"],
|
||||
T["_"]["effectHKT"]
|
||||
>
|
||||
|
||||
export type SQLiteEffectDelete<
|
||||
TTable extends SQLiteTable = SQLiteTable,
|
||||
TRunResult = unknown,
|
||||
TReturning extends Record<string, unknown> | undefined = undefined,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> = SQLiteEffectDeleteBase<TTable, TRunResult, TReturning, true, never, TEffectHKT>
|
||||
|
||||
export type AnySQLiteEffectDelete = SQLiteEffectDeleteBase<any, any, any, any, any, any>
|
||||
|
||||
export interface SQLiteEffectDeleteBase<
|
||||
TTable extends SQLiteTable,
|
||||
TRunResult,
|
||||
TReturning extends Record<string, unknown> | undefined = undefined,
|
||||
TDynamic extends boolean = false,
|
||||
_TExcludedMethods extends string = never,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> extends RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], "sqlite">,
|
||||
SQLWrapper,
|
||||
Effect.Effect<
|
||||
TReturning extends undefined ? TRunResult : TReturning[],
|
||||
TEffectHKT["error"],
|
||||
TEffectHKT["context"]
|
||||
> {
|
||||
readonly _: {
|
||||
dialect: "sqlite"
|
||||
readonly table: TTable
|
||||
readonly resultType: "async"
|
||||
readonly runResult: TRunResult
|
||||
readonly returning: TReturning
|
||||
readonly dynamic: TDynamic
|
||||
readonly excludedMethods: _TExcludedMethods
|
||||
readonly result: TReturning extends undefined ? TRunResult : TReturning[]
|
||||
readonly effectHKT: TEffectHKT
|
||||
}
|
||||
}
|
||||
|
||||
export class SQLiteEffectDeleteBase<
|
||||
TTable extends SQLiteTable,
|
||||
TRunResult,
|
||||
TReturning extends Record<string, unknown> | undefined = undefined,
|
||||
TDynamic extends boolean = false,
|
||||
_TExcludedMethods extends string = never,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
>
|
||||
implements RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], "sqlite">, SQLWrapper
|
||||
{
|
||||
static readonly [entityKind]: string = "SQLiteEffectDelete"
|
||||
|
||||
/** @internal */
|
||||
config: SQLiteDeleteConfig
|
||||
|
||||
constructor(
|
||||
private table: TTable,
|
||||
private effectSession: SQLiteEffectSession<TEffectHKT, TRunResult, any>,
|
||||
private effectDialect: SQLiteDialect,
|
||||
withList?: Subquery[],
|
||||
) {
|
||||
this.config = { table, withList }
|
||||
}
|
||||
|
||||
where(where: SQL | undefined): SQLiteEffectDeleteWithout<this, TDynamic, "where"> {
|
||||
this.config.where = where
|
||||
return this as any
|
||||
}
|
||||
|
||||
orderBy(
|
||||
builder: (deleteTable: TTable) => ValueOrArray<SQLiteColumn | SQL | SQL.Aliased>,
|
||||
): SQLiteEffectDeleteWithout<this, TDynamic, "orderBy">
|
||||
orderBy(...columns: (SQLiteColumn | SQL | SQL.Aliased)[]): SQLiteEffectDeleteWithout<this, TDynamic, "orderBy">
|
||||
orderBy(
|
||||
...columns:
|
||||
| [(deleteTable: TTable) => ValueOrArray<SQLiteColumn | SQL | SQL.Aliased>]
|
||||
| (SQLiteColumn | SQL | SQL.Aliased)[]
|
||||
): SQLiteEffectDeleteWithout<this, TDynamic, "orderBy"> {
|
||||
if (typeof columns[0] === "function") {
|
||||
const orderBy = columns[0](
|
||||
new Proxy(
|
||||
getTableColumnsRuntime(this.config.table),
|
||||
new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" }),
|
||||
) as any,
|
||||
)
|
||||
|
||||
this.config.orderBy = Array.isArray(orderBy) ? orderBy : [orderBy]
|
||||
return this as any
|
||||
}
|
||||
|
||||
this.config.orderBy = columns as (SQLiteColumn | SQL | SQL.Aliased)[]
|
||||
return this as any
|
||||
}
|
||||
|
||||
limit(limit: number | Placeholder): SQLiteEffectDeleteWithout<this, TDynamic, "limit"> {
|
||||
this.config.limit = limit
|
||||
return this as any
|
||||
}
|
||||
|
||||
returning(): SQLiteEffectDeleteReturningAll<this, TDynamic>
|
||||
returning<TSelectedFields extends SelectedFieldsFlat>(
|
||||
fields: TSelectedFields,
|
||||
): SQLiteEffectDeleteReturning<this, TDynamic, TSelectedFields>
|
||||
returning(
|
||||
fields: SelectedFieldsFlat = getTableColumnsRuntime(this.table),
|
||||
): SQLiteEffectDeleteReturning<this, TDynamic, any> | SQLiteEffectDeleteReturningAll<this, TDynamic> {
|
||||
this.config.returning = orderSelectedFields<SQLiteColumn>(fields)
|
||||
return this as any
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
getSQL(): SQL {
|
||||
return this.effectDialect.buildDeleteQuery(this.config)
|
||||
}
|
||||
|
||||
toSQL(): Query {
|
||||
return this.effectDialect.sqlToQuery(this.getSQL())
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_prepare(isOneTimeQuery = true): SQLiteEffectDeletePrepare<this, TEffectHKT> {
|
||||
return this.effectSession[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
|
||||
this.effectDialect.sqlToQuery(this.getSQL()),
|
||||
this.config.returning,
|
||||
this.config.returning ? "all" : "run",
|
||||
undefined,
|
||||
{
|
||||
type: "delete",
|
||||
tables: extractUsedTable(this.config.table),
|
||||
},
|
||||
) as SQLiteEffectDeletePrepare<this, TEffectHKT>
|
||||
}
|
||||
|
||||
prepare(): SQLiteEffectDeletePrepare<this, TEffectHKT> {
|
||||
return this._prepare(false)
|
||||
}
|
||||
|
||||
run: ReturnType<this["prepare"]>["run"] = (placeholderValues) => {
|
||||
return this._prepare().run(placeholderValues)
|
||||
}
|
||||
|
||||
all: ReturnType<this["prepare"]>["all"] = (placeholderValues) => {
|
||||
return this._prepare().all(placeholderValues)
|
||||
}
|
||||
|
||||
get: ReturnType<this["prepare"]>["get"] = (placeholderValues) => {
|
||||
return this._prepare().get(placeholderValues)
|
||||
}
|
||||
|
||||
values: ReturnType<this["prepare"]>["values"] = (placeholderValues) => {
|
||||
return this._prepare().values(placeholderValues)
|
||||
}
|
||||
|
||||
execute: ReturnType<this["prepare"]>["execute"] = (placeholderValues) => {
|
||||
return this._prepare().execute(placeholderValues)
|
||||
}
|
||||
|
||||
$dynamic(): SQLiteEffectDeleteDynamic<this> {
|
||||
return this as any
|
||||
}
|
||||
}
|
||||
|
||||
applyEffectWrapper(SQLiteEffectDeleteBase)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
/* oxlint-disable */
|
||||
export * from "./count"
|
||||
export * from "./db"
|
||||
export * from "./delete"
|
||||
export * from "./insert"
|
||||
export * from "./query"
|
||||
export * from "./raw"
|
||||
export * from "./select"
|
||||
export * from "./session"
|
||||
export * from "./update"
|
||||
349
packages/effect-drizzle-sqlite/src/sqlite-core/effect/insert.ts
Normal file
349
packages/effect-drizzle-sqlite/src/sqlite-core/effect/insert.ts
Normal file
|
|
@ -0,0 +1,349 @@
|
|||
/* oxlint-disable */
|
||||
import type * as Effect from "effect/Effect"
|
||||
import { applyEffectWrapper, type QueryEffectHKTBase } from "drizzle-orm/effect-core/query-effect"
|
||||
import { entityKind, is } from "drizzle-orm/entity"
|
||||
import type { SelectResultFields } from "drizzle-orm/query-builders/select.types"
|
||||
import type { RunnableQuery } from "drizzle-orm/runnable-query"
|
||||
import type { Query, SQLWrapper } from "drizzle-orm/sql/sql"
|
||||
import { Param, SQL, sql } from "drizzle-orm/sql/sql"
|
||||
import type { SQLiteDialect } from "drizzle-orm/sqlite-core/dialect"
|
||||
import type { IndexColumn } from "drizzle-orm/sqlite-core/indexes"
|
||||
import type {
|
||||
SQLiteInsertConfig,
|
||||
SQLiteInsertSelectQueryBuilder,
|
||||
SQLiteInsertValue,
|
||||
} from "drizzle-orm/sqlite-core/query-builders/insert"
|
||||
import type { SelectedFieldsFlat } from "drizzle-orm/sqlite-core/query-builders/select.types"
|
||||
import type { PreparedQueryConfig } from "drizzle-orm/sqlite-core/session"
|
||||
import { SQLiteTable } from "drizzle-orm/sqlite-core/table"
|
||||
import { extractUsedTable } from "drizzle-orm/sqlite-core/utils"
|
||||
import type { Subquery } from "drizzle-orm/subquery"
|
||||
import { type DrizzleTypeError, haveSameKeys } from "drizzle-orm/utils"
|
||||
import type { SQLiteColumn } from "drizzle-orm/sqlite-core/columns/common"
|
||||
import { QueryBuilder } from "drizzle-orm/sqlite-core/query-builders/query-builder"
|
||||
import type { SQLiteUpdateSetSource } from "drizzle-orm/sqlite-core/query-builders/update"
|
||||
import { getTableColumnsRuntime, mapUpdateSet, orderSelectedFields } from "../../internal/drizzle-utils"
|
||||
import type { SQLiteEffectPreparedQuery, SQLiteEffectSession } from "./session"
|
||||
|
||||
export type SQLiteEffectInsertWithout<
|
||||
T extends AnySQLiteEffectInsert,
|
||||
TDynamic extends boolean,
|
||||
K extends keyof T & string,
|
||||
> = TDynamic extends true
|
||||
? T
|
||||
: Omit<
|
||||
SQLiteEffectInsertBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["returning"],
|
||||
TDynamic,
|
||||
T["_"]["excludedMethods"] | K,
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
T["_"]["excludedMethods"] | K
|
||||
>
|
||||
|
||||
export type SQLiteEffectInsertReturning<
|
||||
T extends AnySQLiteEffectInsert,
|
||||
TDynamic extends boolean,
|
||||
TSelectedFields extends SelectedFieldsFlat,
|
||||
> = SQLiteEffectInsertWithout<
|
||||
SQLiteEffectInsertBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
SelectResultFields<TSelectedFields>,
|
||||
TDynamic,
|
||||
T["_"]["excludedMethods"],
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
TDynamic,
|
||||
"returning"
|
||||
>
|
||||
|
||||
export type SQLiteEffectInsertReturningAll<
|
||||
T extends AnySQLiteEffectInsert,
|
||||
TDynamic extends boolean,
|
||||
> = SQLiteEffectInsertWithout<
|
||||
SQLiteEffectInsertBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["table"]["$inferSelect"],
|
||||
TDynamic,
|
||||
T["_"]["excludedMethods"],
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
TDynamic,
|
||||
"returning"
|
||||
>
|
||||
|
||||
export type SQLiteEffectInsertDynamic<T extends AnySQLiteEffectInsert> = SQLiteEffectInsert<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["returning"],
|
||||
T["_"]["effectHKT"]
|
||||
>
|
||||
|
||||
export type SQLiteEffectInsertOnConflictDoUpdateConfig<T extends AnySQLiteEffectInsert> = {
|
||||
target: IndexColumn | IndexColumn[]
|
||||
/** @deprecated - use either `targetWhere` or `setWhere` */
|
||||
where?: SQL
|
||||
targetWhere?: SQL
|
||||
setWhere?: SQL
|
||||
set: SQLiteUpdateSetSource<T["_"]["table"]>
|
||||
}
|
||||
|
||||
export type SQLiteEffectInsertExecute<T extends AnySQLiteEffectInsert> = T["_"]["returning"] extends undefined
|
||||
? T["_"]["runResult"]
|
||||
: T["_"]["returning"][]
|
||||
|
||||
export type SQLiteEffectInsertPrepare<
|
||||
T extends AnySQLiteEffectInsert,
|
||||
TEffectHKT extends QueryEffectHKTBase = T["_"]["effectHKT"],
|
||||
> = SQLiteEffectPreparedQuery<
|
||||
PreparedQueryConfig & {
|
||||
run: T["_"]["runResult"]
|
||||
all: T["_"]["returning"] extends undefined
|
||||
? DrizzleTypeError<".all() cannot be used without .returning()">
|
||||
: T["_"]["returning"][]
|
||||
get: T["_"]["returning"] extends undefined
|
||||
? DrizzleTypeError<".get() cannot be used without .returning()">
|
||||
: T["_"]["returning"]
|
||||
values: T["_"]["returning"] extends undefined
|
||||
? DrizzleTypeError<".values() cannot be used without .returning()">
|
||||
: any[][]
|
||||
execute: SQLiteEffectInsertExecute<T>
|
||||
},
|
||||
TEffectHKT
|
||||
>
|
||||
|
||||
export type SQLiteEffectInsert<
|
||||
TTable extends SQLiteTable = SQLiteTable,
|
||||
TRunResult = unknown,
|
||||
TReturning = any,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> = SQLiteEffectInsertBase<TTable, TRunResult, TReturning, true, never, TEffectHKT>
|
||||
|
||||
export type AnySQLiteEffectInsert = SQLiteEffectInsertBase<any, any, any, any, any, any>
|
||||
|
||||
export class SQLiteEffectInsertBuilder<
|
||||
TTable extends SQLiteTable,
|
||||
TRunResult,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> {
|
||||
static readonly [entityKind]: string = "SQLiteEffectInsertBuilder"
|
||||
|
||||
constructor(
|
||||
protected table: TTable,
|
||||
protected session: SQLiteEffectSession<TEffectHKT, TRunResult, any>,
|
||||
protected dialect: SQLiteDialect,
|
||||
private withList?: Subquery[],
|
||||
) {}
|
||||
|
||||
values(
|
||||
value: SQLiteInsertValue<TTable>,
|
||||
): SQLiteEffectInsertBase<TTable, TRunResult, undefined, false, never, TEffectHKT>
|
||||
values(
|
||||
values: SQLiteInsertValue<TTable>[],
|
||||
): SQLiteEffectInsertBase<TTable, TRunResult, undefined, false, never, TEffectHKT>
|
||||
values(
|
||||
values: SQLiteInsertValue<TTable> | SQLiteInsertValue<TTable>[],
|
||||
): SQLiteEffectInsertBase<TTable, TRunResult, undefined, false, never, TEffectHKT> {
|
||||
values = Array.isArray(values) ? values : [values]
|
||||
if (values.length === 0) {
|
||||
throw new Error("values() must be called with at least one value")
|
||||
}
|
||||
const mappedValues = values.map((entry) => {
|
||||
const result: Record<string, Param | SQL> = {}
|
||||
const cols = getTableColumnsRuntime(this.table)
|
||||
for (const colKey of Object.keys(entry)) {
|
||||
const colValue = entry[colKey as keyof typeof entry]
|
||||
result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey])
|
||||
}
|
||||
return result
|
||||
})
|
||||
|
||||
return new SQLiteEffectInsertBase(this.table, mappedValues, this.session, this.dialect, this.withList)
|
||||
}
|
||||
|
||||
select(
|
||||
selectQuery: (qb: QueryBuilder) => SQLiteInsertSelectQueryBuilder<TTable>,
|
||||
): SQLiteEffectInsertBase<TTable, TRunResult, undefined, false, never, TEffectHKT>
|
||||
select(
|
||||
selectQuery: (qb: QueryBuilder) => SQL,
|
||||
): SQLiteEffectInsertBase<TTable, TRunResult, undefined, false, never, TEffectHKT>
|
||||
select(selectQuery: SQL): SQLiteEffectInsertBase<TTable, TRunResult, undefined, false, never, TEffectHKT>
|
||||
select(
|
||||
selectQuery: SQLiteInsertSelectQueryBuilder<TTable>,
|
||||
): SQLiteEffectInsertBase<TTable, TRunResult, undefined, false, never, TEffectHKT>
|
||||
select(
|
||||
selectQuery:
|
||||
| SQL
|
||||
| SQLiteInsertSelectQueryBuilder<TTable>
|
||||
| ((qb: QueryBuilder) => SQLiteInsertSelectQueryBuilder<TTable> | SQL),
|
||||
): SQLiteEffectInsertBase<TTable, TRunResult, undefined, false, never, TEffectHKT> {
|
||||
const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery
|
||||
|
||||
if (!is(select, SQL) && !haveSameKeys(getTableColumnsRuntime(this.table), select._.selectedFields)) {
|
||||
throw new Error(
|
||||
"Insert select error: selected fields are not the same or are in a different order compared to the table definition",
|
||||
)
|
||||
}
|
||||
|
||||
return new SQLiteEffectInsertBase(this.table, select, this.session, this.dialect, this.withList, true)
|
||||
}
|
||||
}
|
||||
|
||||
export interface SQLiteEffectInsertBase<
|
||||
TTable extends SQLiteTable,
|
||||
TRunResult,
|
||||
TReturning = undefined,
|
||||
TDynamic extends boolean = false,
|
||||
_TExcludedMethods extends string = never,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> extends SQLWrapper,
|
||||
RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], "sqlite">,
|
||||
Effect.Effect<
|
||||
TReturning extends undefined ? TRunResult : TReturning[],
|
||||
TEffectHKT["error"],
|
||||
TEffectHKT["context"]
|
||||
> {
|
||||
readonly _: {
|
||||
readonly dialect: "sqlite"
|
||||
readonly table: TTable
|
||||
readonly resultType: "async"
|
||||
readonly runResult: TRunResult
|
||||
readonly returning: TReturning
|
||||
readonly dynamic: TDynamic
|
||||
readonly excludedMethods: _TExcludedMethods
|
||||
readonly result: TReturning extends undefined ? TRunResult : TReturning[]
|
||||
readonly effectHKT: TEffectHKT
|
||||
}
|
||||
}
|
||||
|
||||
export class SQLiteEffectInsertBase<
|
||||
TTable extends SQLiteTable,
|
||||
TRunResult,
|
||||
TReturning = undefined,
|
||||
TDynamic extends boolean = false,
|
||||
_TExcludedMethods extends string = never,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
>
|
||||
implements RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], "sqlite">, SQLWrapper
|
||||
{
|
||||
static readonly [entityKind]: string = "SQLiteEffectInsert"
|
||||
|
||||
/** @internal */
|
||||
config: SQLiteInsertConfig<TTable>
|
||||
|
||||
constructor(
|
||||
private table: TTable,
|
||||
values: SQLiteInsertConfig["values"],
|
||||
private effectSession: SQLiteEffectSession<TEffectHKT, TRunResult, any>,
|
||||
private effectDialect: SQLiteDialect,
|
||||
withList?: Subquery[],
|
||||
select?: boolean,
|
||||
) {
|
||||
this.config = { table, values: values as any, withList, select }
|
||||
}
|
||||
|
||||
returning(): SQLiteEffectInsertReturningAll<this, TDynamic>
|
||||
returning<TSelectedFields extends SelectedFieldsFlat>(
|
||||
fields: TSelectedFields,
|
||||
): SQLiteEffectInsertReturning<this, TDynamic, TSelectedFields>
|
||||
returning(
|
||||
fields: SelectedFieldsFlat = getTableColumnsRuntime(this.config.table),
|
||||
): SQLiteEffectInsertWithout<AnySQLiteEffectInsert, TDynamic, "returning"> {
|
||||
this.config.returning = orderSelectedFields<SQLiteColumn>(fields)
|
||||
return this as any
|
||||
}
|
||||
|
||||
onConflictDoNothing(config: { target?: IndexColumn | IndexColumn[]; where?: SQL } = {}): this {
|
||||
if (!this.config.onConflict) this.config.onConflict = []
|
||||
|
||||
if (config.target === undefined) {
|
||||
this.config.onConflict.push(sql` on conflict do nothing`)
|
||||
return this
|
||||
}
|
||||
|
||||
const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`
|
||||
const whereSql = config.where ? sql` where ${config.where}` : sql``
|
||||
this.config.onConflict.push(sql` on conflict ${targetSql} do nothing${whereSql}`)
|
||||
return this
|
||||
}
|
||||
|
||||
onConflictDoUpdate(config: SQLiteEffectInsertOnConflictDoUpdateConfig<this>): this {
|
||||
if (config.where && (config.targetWhere || config.setWhere)) {
|
||||
throw new Error(
|
||||
'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.',
|
||||
)
|
||||
}
|
||||
|
||||
if (!this.config.onConflict) this.config.onConflict = []
|
||||
|
||||
const whereSql = config.where ? sql` where ${config.where}` : undefined
|
||||
const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : undefined
|
||||
const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : undefined
|
||||
const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`
|
||||
const setSql = this.effectDialect.buildUpdateSet(
|
||||
this.config.table,
|
||||
mapUpdateSet(this.config.table, config.set as SQLiteUpdateSetSource<TTable>),
|
||||
)
|
||||
this.config.onConflict.push(
|
||||
sql` on conflict ${targetSql}${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`,
|
||||
)
|
||||
return this
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
getSQL(): SQL {
|
||||
return this.effectDialect.buildInsertQuery(this.config)
|
||||
}
|
||||
|
||||
toSQL(): Query {
|
||||
return this.effectDialect.sqlToQuery(this.getSQL())
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_prepare(isOneTimeQuery = true): SQLiteEffectInsertPrepare<this, TEffectHKT> {
|
||||
return this.effectSession[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
|
||||
this.effectDialect.sqlToQuery(this.getSQL()),
|
||||
this.config.returning,
|
||||
this.config.returning ? "all" : "run",
|
||||
undefined,
|
||||
{
|
||||
type: "insert",
|
||||
tables: extractUsedTable(this.config.table),
|
||||
},
|
||||
) as SQLiteEffectInsertPrepare<this, TEffectHKT>
|
||||
}
|
||||
|
||||
prepare(): SQLiteEffectInsertPrepare<this, TEffectHKT> {
|
||||
return this._prepare(false)
|
||||
}
|
||||
|
||||
run: ReturnType<this["prepare"]>["run"] = (placeholderValues) => {
|
||||
return this._prepare().run(placeholderValues)
|
||||
}
|
||||
|
||||
all: ReturnType<this["prepare"]>["all"] = (placeholderValues) => {
|
||||
return this._prepare().all(placeholderValues)
|
||||
}
|
||||
|
||||
get: ReturnType<this["prepare"]>["get"] = (placeholderValues) => {
|
||||
return this._prepare().get(placeholderValues)
|
||||
}
|
||||
|
||||
values: ReturnType<this["prepare"]>["values"] = (placeholderValues) => {
|
||||
return this._prepare().values(placeholderValues)
|
||||
}
|
||||
|
||||
execute: ReturnType<this["prepare"]>["execute"] = (placeholderValues) => {
|
||||
return this._prepare().execute(placeholderValues)
|
||||
}
|
||||
|
||||
$dynamic(): SQLiteEffectInsertDynamic<this> {
|
||||
return this as any
|
||||
}
|
||||
}
|
||||
|
||||
applyEffectWrapper(SQLiteEffectInsertBase)
|
||||
198
packages/effect-drizzle-sqlite/src/sqlite-core/effect/query.ts
Normal file
198
packages/effect-drizzle-sqlite/src/sqlite-core/effect/query.ts
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
/* oxlint-disable */
|
||||
import type * as Effect from "effect/Effect"
|
||||
import { applyEffectWrapper, type QueryEffectHKTBase } from "drizzle-orm/effect-core/query-effect"
|
||||
import { entityKind } from "drizzle-orm/entity"
|
||||
import {
|
||||
type BuildQueryResult,
|
||||
type BuildRelationalQueryResult,
|
||||
type DBQueryConfig,
|
||||
makeDefaultRqbMapper,
|
||||
type TableRelationalConfig,
|
||||
type TablesRelationalConfig,
|
||||
} from "drizzle-orm/relations"
|
||||
import type { RunnableQuery } from "drizzle-orm/runnable-query"
|
||||
import { type Query, type SQL, sql, type SQLWrapper } from "drizzle-orm/sql/sql"
|
||||
import type { KnownKeysOnly } from "drizzle-orm/utils"
|
||||
import type { SQLiteDialect } from "drizzle-orm/sqlite-core/dialect"
|
||||
import type { PreparedQueryConfig } from "drizzle-orm/sqlite-core/session"
|
||||
import type { SQLiteTable } from "drizzle-orm/sqlite-core/table"
|
||||
import type { SQLiteEffectPreparedQuery, SQLiteEffectSession } from "./session"
|
||||
|
||||
export class SQLiteEffectRelationalQueryBuilder<
|
||||
TSchema extends TablesRelationalConfig,
|
||||
TFields extends TableRelationalConfig,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> {
|
||||
static readonly [entityKind]: string = "SQLiteEffectRelationalQueryBuilderV2"
|
||||
|
||||
constructor(
|
||||
private schema: TSchema,
|
||||
private table: SQLiteTable,
|
||||
private tableConfig: TableRelationalConfig,
|
||||
private dialect: SQLiteDialect,
|
||||
private session: SQLiteEffectSession<TEffectHKT, any, any>,
|
||||
private rowMode?: boolean,
|
||||
private forbidJsonb?: boolean,
|
||||
) {}
|
||||
|
||||
findMany<TConfig extends DBQueryConfig<"many", TSchema, TFields>>(
|
||||
config?: KnownKeysOnly<TConfig, DBQueryConfig<"many", TSchema, TFields>>,
|
||||
): SQLiteEffectRelationalQuery<BuildQueryResult<TSchema, TFields, TConfig>[], TEffectHKT> {
|
||||
return new SQLiteEffectRelationalQuery(
|
||||
this.schema,
|
||||
this.table,
|
||||
this.tableConfig,
|
||||
this.dialect,
|
||||
this.session,
|
||||
(config as DBQueryConfig<"many"> | undefined) ?? true,
|
||||
"many",
|
||||
this.rowMode,
|
||||
this.forbidJsonb,
|
||||
)
|
||||
}
|
||||
|
||||
findFirst<TConfig extends DBQueryConfig<"one", TSchema, TFields>>(
|
||||
config?: KnownKeysOnly<TConfig, DBQueryConfig<"one", TSchema, TFields>>,
|
||||
): SQLiteEffectRelationalQuery<BuildQueryResult<TSchema, TFields, TConfig> | undefined, TEffectHKT> {
|
||||
return new SQLiteEffectRelationalQuery(
|
||||
this.schema,
|
||||
this.table,
|
||||
this.tableConfig,
|
||||
this.dialect,
|
||||
this.session,
|
||||
(config as DBQueryConfig<"one"> | undefined) ?? true,
|
||||
"first",
|
||||
this.rowMode,
|
||||
this.forbidJsonb,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export interface SQLiteEffectRelationalQuery<TResult, TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase>
|
||||
extends Effect.Effect<TResult, TEffectHKT["error"], TEffectHKT["context"]>,
|
||||
RunnableQuery<TResult, "sqlite">,
|
||||
SQLWrapper {}
|
||||
|
||||
export class SQLiteEffectRelationalQuery<TResult, TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase>
|
||||
implements RunnableQuery<TResult, "sqlite">, SQLWrapper
|
||||
{
|
||||
static readonly [entityKind]: string = "SQLiteEffectRelationalQueryV2"
|
||||
|
||||
declare readonly _: {
|
||||
readonly dialect: "sqlite"
|
||||
readonly type: "async"
|
||||
readonly result: TResult
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
mode: "many" | "first"
|
||||
/** @internal */
|
||||
table: SQLiteTable
|
||||
|
||||
constructor(
|
||||
private schema: TablesRelationalConfig,
|
||||
table: SQLiteTable,
|
||||
private tableConfig: TableRelationalConfig,
|
||||
private dialect: SQLiteDialect,
|
||||
private session: SQLiteEffectSession<TEffectHKT, any, any>,
|
||||
private config: DBQueryConfig<"many" | "one"> | true,
|
||||
mode: "many" | "first",
|
||||
private rowMode?: boolean,
|
||||
private forbidJsonb?: boolean,
|
||||
) {
|
||||
this.mode = mode
|
||||
this.table = table
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
getSQL(): SQL {
|
||||
return this._getQuery().sql
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_prepare(
|
||||
isOneTimeQuery = true,
|
||||
): SQLiteEffectPreparedQuery<
|
||||
PreparedQueryConfig & { all: TResult; get: TResult; execute: TResult },
|
||||
TEffectHKT,
|
||||
true
|
||||
> {
|
||||
const { query, builtQuery } = this._toSQL()
|
||||
const mapperConfig = {
|
||||
isFirst: this.mode === "first",
|
||||
parseJson: !this.rowMode,
|
||||
parseJsonIfString: false,
|
||||
rootJsonMappers: true,
|
||||
selection: query.selection,
|
||||
}
|
||||
|
||||
return this.session[isOneTimeQuery ? "prepareOneTimeRelationalQuery" : "prepareRelationalQuery"](
|
||||
builtQuery,
|
||||
undefined,
|
||||
this.mode === "first" ? "get" : "all",
|
||||
makeDefaultRqbMapper(mapperConfig),
|
||||
mapperConfig,
|
||||
) as SQLiteEffectPreparedQuery<
|
||||
PreparedQueryConfig & { all: TResult; get: TResult; execute: TResult },
|
||||
TEffectHKT,
|
||||
true
|
||||
>
|
||||
}
|
||||
|
||||
prepare(): SQLiteEffectPreparedQuery<
|
||||
PreparedQueryConfig & { all: TResult; get: TResult; execute: TResult },
|
||||
TEffectHKT,
|
||||
true
|
||||
> {
|
||||
return this._prepare(false)
|
||||
}
|
||||
|
||||
private _getQuery() {
|
||||
const jsonb = this.forbidJsonb ? sql`json` : sql`jsonb`
|
||||
|
||||
const query = this.dialect.buildRelationalQuery({
|
||||
schema: this.schema,
|
||||
table: this.table,
|
||||
tableConfig: this.tableConfig,
|
||||
queryConfig: this.config,
|
||||
mode: this.mode,
|
||||
isNested: this.rowMode,
|
||||
jsonb,
|
||||
})
|
||||
|
||||
if (this.rowMode) {
|
||||
const jsonColumns = sql.join(
|
||||
query.selection.map((s) => {
|
||||
return sql`${sql.raw(this.dialect.escapeString(s.key))}, ${
|
||||
s.selection ? sql`${jsonb}(${sql.identifier(s.key)})` : sql.identifier(s.key)
|
||||
}`
|
||||
}),
|
||||
sql`, `,
|
||||
)
|
||||
|
||||
query.sql = sql`select json_object(${jsonColumns}) as ${sql.identifier("r")} from (${query.sql}) as ${sql.identifier(
|
||||
"t",
|
||||
)}`
|
||||
}
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
private _toSQL(): { query: BuildRelationalQueryResult; builtQuery: Query } {
|
||||
const query = this._getQuery()
|
||||
|
||||
const builtQuery = this.dialect.sqlToQuery(query.sql)
|
||||
|
||||
return { query, builtQuery }
|
||||
}
|
||||
|
||||
toSQL(): Query {
|
||||
return this._toSQL().builtQuery
|
||||
}
|
||||
|
||||
execute(placeholderValues?: Record<string, unknown>) {
|
||||
return this.mode === "first" ? this._prepare().get(placeholderValues) : this._prepare().all(placeholderValues)
|
||||
}
|
||||
}
|
||||
|
||||
applyEffectWrapper(SQLiteEffectRelationalQuery)
|
||||
49
packages/effect-drizzle-sqlite/src/sqlite-core/effect/raw.ts
Normal file
49
packages/effect-drizzle-sqlite/src/sqlite-core/effect/raw.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/* oxlint-disable */
|
||||
import type * as Effect from "effect/Effect"
|
||||
import { applyEffectWrapper, type QueryEffectHKTBase } from "drizzle-orm/effect-core/query-effect"
|
||||
import { entityKind } from "drizzle-orm/entity"
|
||||
import type { RunnableQuery } from "drizzle-orm/runnable-query"
|
||||
import type { PreparedQuery } from "drizzle-orm/session"
|
||||
import type { Query, SQL, SQLWrapper } from "drizzle-orm/sql/sql"
|
||||
import type { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core/dialect"
|
||||
|
||||
type SQLiteEffectRawAction = "all" | "get" | "values" | "run"
|
||||
|
||||
export interface SQLiteEffectRaw<TResult, TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase>
|
||||
extends Effect.Effect<TResult, TEffectHKT["error"], TEffectHKT["context"]>,
|
||||
RunnableQuery<TResult, "sqlite">,
|
||||
SQLWrapper {}
|
||||
|
||||
export class SQLiteEffectRaw<TResult, TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase>
|
||||
implements RunnableQuery<TResult, "sqlite">, SQLWrapper, PreparedQuery
|
||||
{
|
||||
static readonly [entityKind]: string = "SQLiteEffectRaw"
|
||||
|
||||
declare readonly _: {
|
||||
readonly dialect: "sqlite"
|
||||
readonly result: TResult
|
||||
}
|
||||
|
||||
constructor(
|
||||
public execute: () => Effect.Effect<TResult, TEffectHKT["error"], TEffectHKT["context"]>,
|
||||
/** @internal */
|
||||
public getSQL: () => SQL,
|
||||
private action: SQLiteEffectRawAction,
|
||||
private dialect: SQLiteAsyncDialect,
|
||||
private mapBatchResult: (result: unknown) => unknown,
|
||||
) {}
|
||||
|
||||
getQuery(): Query & { method: SQLiteEffectRawAction } {
|
||||
return { ...this.dialect.sqlToQuery(this.getSQL()), method: this.action }
|
||||
}
|
||||
|
||||
mapResult(result: unknown, isFromBatch?: boolean) {
|
||||
return isFromBatch ? this.mapBatchResult(result) : result
|
||||
}
|
||||
|
||||
_prepare(): PreparedQuery {
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
applyEffectWrapper(SQLiteEffectRaw)
|
||||
279
packages/effect-drizzle-sqlite/src/sqlite-core/effect/select.ts
Normal file
279
packages/effect-drizzle-sqlite/src/sqlite-core/effect/select.ts
Normal file
|
|
@ -0,0 +1,279 @@
|
|||
/* oxlint-disable */
|
||||
import type * as Effect from "effect/Effect"
|
||||
import type { CacheConfig } from "drizzle-orm/cache/core/types"
|
||||
import { applyEffectWrapper, type QueryEffectHKTBase } from "drizzle-orm/effect-core/query-effect"
|
||||
import { entityKind, is } from "drizzle-orm/entity"
|
||||
import type {
|
||||
BuildSubquerySelection,
|
||||
GetSelectTableName,
|
||||
GetSelectTableSelection,
|
||||
JoinNullability,
|
||||
SelectMode,
|
||||
SelectResult,
|
||||
} from "drizzle-orm/query-builders/select.types"
|
||||
import { SQL } from "drizzle-orm/sql/sql"
|
||||
import type { ColumnsSelection, SQLWrapper } from "drizzle-orm/sql/sql"
|
||||
import type { SQLiteColumn } from "drizzle-orm/sqlite-core/columns"
|
||||
import type { SQLiteDialect } from "drizzle-orm/sqlite-core/dialect"
|
||||
import { SQLiteSelectQueryBuilderBase } from "drizzle-orm/sqlite-core/query-builders/select"
|
||||
import type {
|
||||
CreateSQLiteSelectFromBuilderMode,
|
||||
SelectedFields,
|
||||
SQLiteSelectConfig,
|
||||
SQLiteSelectHKTBase,
|
||||
} from "drizzle-orm/sqlite-core/query-builders/select.types"
|
||||
import type { SQLiteTable } from "drizzle-orm/sqlite-core/table"
|
||||
import { SQLiteViewBase } from "drizzle-orm/sqlite-core/view-base"
|
||||
import { Subquery } from "drizzle-orm/subquery"
|
||||
import { type Assume, getTableColumns } from "drizzle-orm/utils"
|
||||
import { getViewSelectedFieldsRuntime, orderSelectedFields } from "../../internal/drizzle-utils"
|
||||
import type { SQLiteEffectPreparedQuery, SQLiteEffectSession } from "./session"
|
||||
|
||||
export type SQLiteEffectSelectPrepare<
|
||||
T extends AnySQLiteEffectSelect,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> = SQLiteEffectPreparedQuery<
|
||||
{
|
||||
type: "async"
|
||||
run: T["_"]["runResult"]
|
||||
all: T["_"]["result"]
|
||||
get: T["_"]["result"][number] | undefined
|
||||
values: any[][]
|
||||
execute: T["_"]["result"]
|
||||
},
|
||||
TEffectHKT
|
||||
>
|
||||
|
||||
export class SQLiteEffectSelectBuilder<
|
||||
TSelection extends SelectedFields | undefined,
|
||||
TRunResult,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
TBuilderMode extends "db" | "qb" = "db",
|
||||
> {
|
||||
static readonly [entityKind]: string = "SQLiteEffectSelectBuilder"
|
||||
|
||||
private fields: TSelection
|
||||
private session: SQLiteEffectSession<TEffectHKT, TRunResult, any> | undefined
|
||||
private dialect: SQLiteDialect
|
||||
private withList: Subquery[] | undefined
|
||||
private distinct: boolean | undefined
|
||||
|
||||
constructor(config: {
|
||||
fields: TSelection
|
||||
session: SQLiteEffectSession<TEffectHKT, TRunResult, any> | undefined
|
||||
dialect: SQLiteDialect
|
||||
withList?: Subquery[]
|
||||
distinct?: boolean
|
||||
}) {
|
||||
this.fields = config.fields
|
||||
this.session = config.session
|
||||
this.dialect = config.dialect
|
||||
this.withList = config.withList
|
||||
this.distinct = config.distinct
|
||||
}
|
||||
|
||||
from<TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL>(
|
||||
source: TFrom,
|
||||
): TBuilderMode extends "db"
|
||||
? SQLiteEffectSelectBase<
|
||||
GetSelectTableName<TFrom>,
|
||||
TRunResult,
|
||||
TSelection extends undefined ? GetSelectTableSelection<TFrom> : TSelection,
|
||||
TSelection extends undefined ? "single" : "partial",
|
||||
GetSelectTableName<TFrom> extends string ? Record<GetSelectTableName<TFrom>, "not-null"> : {},
|
||||
false,
|
||||
never,
|
||||
SelectResult<
|
||||
TSelection extends undefined ? GetSelectTableSelection<TFrom> : TSelection,
|
||||
TSelection extends undefined ? "single" : "partial",
|
||||
GetSelectTableName<TFrom> extends string ? Record<GetSelectTableName<TFrom>, "not-null"> : {}
|
||||
>[],
|
||||
BuildSubquerySelection<
|
||||
TSelection extends undefined ? GetSelectTableSelection<TFrom> : TSelection,
|
||||
GetSelectTableName<TFrom> extends string ? Record<GetSelectTableName<TFrom>, "not-null"> : {}
|
||||
>,
|
||||
TEffectHKT
|
||||
>
|
||||
: CreateSQLiteSelectFromBuilderMode<
|
||||
TBuilderMode,
|
||||
GetSelectTableName<TFrom>,
|
||||
"async",
|
||||
TRunResult,
|
||||
TSelection extends undefined ? GetSelectTableSelection<TFrom> : TSelection,
|
||||
TSelection extends undefined ? "single" : "partial"
|
||||
> {
|
||||
const isPartialSelect = !!this.fields
|
||||
|
||||
let fields: SelectedFields
|
||||
if (this.fields) {
|
||||
fields = this.fields
|
||||
} else if (is(source, Subquery)) {
|
||||
fields = Object.fromEntries(
|
||||
Object.keys(source._.selectedFields).map((key) => [
|
||||
key,
|
||||
source[key as unknown as keyof typeof source] as unknown as SelectedFields[string],
|
||||
]),
|
||||
)
|
||||
} else if (is(source, SQLiteViewBase)) {
|
||||
fields = getViewSelectedFieldsRuntime(source).selectedFields as SelectedFields
|
||||
} else if (is(source, SQL)) {
|
||||
fields = {}
|
||||
} else {
|
||||
fields = getTableColumns<SQLiteTable>(source)
|
||||
}
|
||||
|
||||
return new SQLiteEffectSelectBase({
|
||||
table: source,
|
||||
fields,
|
||||
isPartialSelect,
|
||||
session: this.session as any,
|
||||
dialect: this.dialect,
|
||||
withList: this.withList,
|
||||
distinct: this.distinct,
|
||||
}) as any
|
||||
}
|
||||
}
|
||||
|
||||
export interface SQLiteEffectSelectHKT<TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase>
|
||||
extends SQLiteSelectHKTBase {
|
||||
_type: SQLiteEffectSelectBase<
|
||||
this["tableName"],
|
||||
this["runResult"],
|
||||
Assume<this["selection"], ColumnsSelection>,
|
||||
this["selectMode"],
|
||||
Assume<this["nullabilityMap"], Record<string, JoinNullability>>,
|
||||
this["dynamic"],
|
||||
this["excludedMethods"],
|
||||
Assume<this["result"], any[]>,
|
||||
Assume<this["selectedFields"], ColumnsSelection>,
|
||||
TEffectHKT
|
||||
>
|
||||
}
|
||||
|
||||
export interface SQLiteEffectSelectBase<
|
||||
TTableName extends string | undefined,
|
||||
TRunResult,
|
||||
TSelection extends ColumnsSelection,
|
||||
TSelectMode extends SelectMode = "single",
|
||||
TNullabilityMap extends Record<string, JoinNullability> = TTableName extends string
|
||||
? Record<TTableName, "not-null">
|
||||
: {},
|
||||
TDynamic extends boolean = false,
|
||||
TExcludedMethods extends string = never,
|
||||
TResult extends any[] = SelectResult<TSelection, TSelectMode, TNullabilityMap>[],
|
||||
TSelectedFields extends ColumnsSelection = BuildSubquerySelection<TSelection, TNullabilityMap>,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> extends SQLiteSelectQueryBuilderBase<
|
||||
SQLiteEffectSelectHKT<TEffectHKT>,
|
||||
TTableName,
|
||||
"async",
|
||||
TRunResult,
|
||||
TSelection,
|
||||
TSelectMode,
|
||||
TNullabilityMap,
|
||||
TDynamic,
|
||||
TExcludedMethods,
|
||||
TResult,
|
||||
TSelectedFields
|
||||
>,
|
||||
Effect.Effect<TResult, TEffectHKT["error"], TEffectHKT["context"]> {}
|
||||
|
||||
export class SQLiteEffectSelectBase<
|
||||
TTableName extends string | undefined,
|
||||
TRunResult,
|
||||
TSelection extends ColumnsSelection,
|
||||
TSelectMode extends SelectMode = "single",
|
||||
TNullabilityMap extends Record<string, JoinNullability> = TTableName extends string
|
||||
? Record<TTableName, "not-null">
|
||||
: {},
|
||||
TDynamic extends boolean = false,
|
||||
TExcludedMethods extends string = never,
|
||||
TResult extends any[] = SelectResult<TSelection, TSelectMode, TNullabilityMap>[],
|
||||
TSelectedFields extends ColumnsSelection = BuildSubquerySelection<TSelection, TNullabilityMap>,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
>
|
||||
extends SQLiteSelectQueryBuilderBase<
|
||||
SQLiteEffectSelectHKT<TEffectHKT>,
|
||||
TTableName,
|
||||
"async",
|
||||
TRunResult,
|
||||
TSelection,
|
||||
TSelectMode,
|
||||
TNullabilityMap,
|
||||
TDynamic,
|
||||
TExcludedMethods,
|
||||
TResult,
|
||||
TSelectedFields
|
||||
>
|
||||
implements SQLWrapper
|
||||
{
|
||||
static override readonly [entityKind]: string = "SQLiteEffectSelect"
|
||||
|
||||
private get effectConfig() {
|
||||
return (this as unknown as { config: SQLiteSelectConfig }).config
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
getSQL(): SQL {
|
||||
return this.dialect.buildSelectQuery(this.effectConfig)
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_prepare(isOneTimeQuery = true): SQLiteEffectSelectPrepare<this, TEffectHKT> {
|
||||
if (!this.session) {
|
||||
throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.")
|
||||
}
|
||||
const session = this.session as unknown as SQLiteEffectSession<TEffectHKT, TRunResult, any>
|
||||
const query = session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
|
||||
this.dialect.sqlToQuery(this.getSQL()),
|
||||
orderSelectedFields<SQLiteColumn>(this.effectConfig.fields),
|
||||
"all",
|
||||
undefined,
|
||||
{
|
||||
type: "select",
|
||||
tables: [...this.usedTables],
|
||||
},
|
||||
this.cacheConfig,
|
||||
)
|
||||
query.joinsNotNullableMap = this.joinsNotNullableMap
|
||||
return query as ReturnType<this["prepare"]>
|
||||
}
|
||||
|
||||
$withCache(config?: { config?: CacheConfig; tag?: string; autoInvalidate?: boolean } | false) {
|
||||
this.cacheConfig =
|
||||
config === undefined
|
||||
? { config: {}, enabled: true, autoInvalidate: true }
|
||||
: config === false
|
||||
? { enabled: false }
|
||||
: { enabled: true, autoInvalidate: true, ...config }
|
||||
return this
|
||||
}
|
||||
|
||||
prepare(): SQLiteEffectSelectPrepare<this, TEffectHKT> {
|
||||
return this._prepare(false)
|
||||
}
|
||||
|
||||
run: ReturnType<this["prepare"]>["run"] = (placeholderValues) => {
|
||||
return this._prepare().run(placeholderValues)
|
||||
}
|
||||
|
||||
all: ReturnType<this["prepare"]>["all"] = (placeholderValues) => {
|
||||
return this._prepare().all(placeholderValues)
|
||||
}
|
||||
|
||||
get: ReturnType<this["prepare"]>["get"] = (placeholderValues) => {
|
||||
return this._prepare().get(placeholderValues)
|
||||
}
|
||||
|
||||
values: ReturnType<this["prepare"]>["values"] = (placeholderValues) => {
|
||||
return this._prepare().values(placeholderValues)
|
||||
}
|
||||
|
||||
execute: ReturnType<this["prepare"]>["execute"] = (placeholderValues) => {
|
||||
return this._prepare().execute(placeholderValues)
|
||||
}
|
||||
}
|
||||
|
||||
applyEffectWrapper(SQLiteEffectSelectBase)
|
||||
|
||||
export type AnySQLiteEffectSelect = SQLiteEffectSelectBase<any, any, any, any, any, any, any, any, any, any>
|
||||
490
packages/effect-drizzle-sqlite/src/sqlite-core/effect/session.ts
Normal file
490
packages/effect-drizzle-sqlite/src/sqlite-core/effect/session.ts
Normal file
|
|
@ -0,0 +1,490 @@
|
|||
/* oxlint-disable */
|
||||
import * as Cause from "effect/Cause"
|
||||
import * as Effect from "effect/Effect"
|
||||
import type { SqlError } from "effect/unstable/sql/SqlError"
|
||||
import type { EffectCacheShape } from "drizzle-orm/cache/core/cache-effect"
|
||||
import { NoopCache, strategyFor } from "drizzle-orm/cache/core/cache"
|
||||
import type { WithCacheConfig } from "drizzle-orm/cache/core/types"
|
||||
import { MigratorInitError } from "drizzle-orm/effect-core/errors"
|
||||
import { EffectDrizzleQueryError, EffectTransactionRollbackError } from "drizzle-orm/effect-core/errors"
|
||||
import type { EffectLoggerShape } from "drizzle-orm/effect-core/logger"
|
||||
import type { QueryEffectHKTBase, QueryEffectKind } from "drizzle-orm/effect-core/query-effect"
|
||||
import { entityKind, is } from "drizzle-orm/entity"
|
||||
import type { MigrationConfig, MigrationMeta } from "drizzle-orm/migrator"
|
||||
import { getMigrationsToRun } from "drizzle-orm/migrator.utils"
|
||||
import type {
|
||||
AnyRelations,
|
||||
EmptyRelations,
|
||||
RelationalQueryMapperConfig,
|
||||
RelationalRowsMapper,
|
||||
} from "drizzle-orm/relations"
|
||||
import { makeJitRqbMapper } from "drizzle-orm/relations"
|
||||
import type { PreparedQuery } from "drizzle-orm/session"
|
||||
import { fillPlaceholders, type Query, type SQL, sql } from "drizzle-orm/sql/sql"
|
||||
import type { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core/dialect"
|
||||
import type { SelectedFieldsOrdered } from "drizzle-orm/sqlite-core/query-builders/select.types"
|
||||
import type { PreparedQueryConfig, SQLiteExecuteMethod, SQLiteTransactionConfig } from "drizzle-orm/sqlite-core/session"
|
||||
import { upgradeIfNeeded } from "../../up-migrations/effect-sqlite"
|
||||
import { assertUnreachable, makeJitQueryMapper, type RowsMapper } from "drizzle-orm/utils"
|
||||
import { mapResultRow } from "../../internal/drizzle-utils"
|
||||
import { SQLiteEffectDatabase } from "./db"
|
||||
|
||||
type MigrationConfigWithInit = MigrationConfig & { init?: boolean }
|
||||
|
||||
type SQLiteEffectExecuteMethod = SQLiteExecuteMethod | "values"
|
||||
|
||||
export class SQLiteEffectPreparedQuery<
|
||||
T extends PreparedQueryConfig,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
TIsRqbV2 extends boolean = false,
|
||||
> implements PreparedQuery
|
||||
{
|
||||
static readonly [entityKind]: string = "SQLiteEffectPreparedQuery"
|
||||
|
||||
/** @internal */
|
||||
joinsNotNullableMap?: Record<string, boolean>
|
||||
private jitMapper?: RowsMapper<any> | RelationalRowsMapper<any>
|
||||
private cacheConfig: WithCacheConfig | undefined
|
||||
private effectExecuteMethod: SQLiteExecuteMethod
|
||||
|
||||
constructor(
|
||||
private executor: (
|
||||
params: unknown[],
|
||||
executeMethod: SQLiteEffectExecuteMethod,
|
||||
) => Effect.Effect<unknown, unknown, unknown>,
|
||||
protected query: Query,
|
||||
private logger: EffectLoggerShape,
|
||||
private cache: EffectCacheShape,
|
||||
private queryMetadata:
|
||||
| {
|
||||
type: "select" | "update" | "delete" | "insert"
|
||||
tables: string[]
|
||||
}
|
||||
| undefined,
|
||||
cacheConfig: WithCacheConfig | undefined,
|
||||
private fields: SelectedFieldsOrdered | undefined,
|
||||
executeMethod: SQLiteExecuteMethod,
|
||||
private useJitMappers: boolean | undefined,
|
||||
private customResultMapper?: (
|
||||
rows: TIsRqbV2 extends true ? Record<string, unknown>[] : unknown[][],
|
||||
mapColumnValue?: (value: unknown) => unknown,
|
||||
) => unknown,
|
||||
private isRqbV2Query?: TIsRqbV2,
|
||||
private rqbConfig?: RelationalQueryMapperConfig,
|
||||
private isInTransaction: Effect.Effect<boolean> = Effect.succeed(false),
|
||||
) {
|
||||
this.effectExecuteMethod = executeMethod
|
||||
this.cacheConfig =
|
||||
cache.strategy() === "all" && cacheConfig === undefined ? { enabled: true, autoInvalidate: true } : cacheConfig
|
||||
if (!this.cacheConfig?.enabled) {
|
||||
this.cacheConfig = undefined
|
||||
}
|
||||
}
|
||||
|
||||
run(placeholderValues?: Record<string, unknown>): QueryEffectKind<TEffectHKT, T["run"]>
|
||||
run(placeholderValues?: Record<string, unknown>): any {
|
||||
return this.executeWithCache<T["run"]>(placeholderValues, "run")
|
||||
}
|
||||
|
||||
all(placeholderValues?: Record<string, unknown>): QueryEffectKind<TEffectHKT, T["all"]>
|
||||
all(placeholderValues?: Record<string, unknown>): any {
|
||||
if (this.isRqbV2Query) return this.allRqbV2(placeholderValues)
|
||||
|
||||
if (!this.fields && !this.customResultMapper) {
|
||||
return this.executeWithCache<T["all"]>(placeholderValues, "all")
|
||||
}
|
||||
|
||||
return this.executeWithCache<T["values"], T["all"]>(
|
||||
placeholderValues,
|
||||
"values",
|
||||
(rows) => this.mapAllResult(rows) as T["all"],
|
||||
)
|
||||
}
|
||||
|
||||
get(placeholderValues?: Record<string, unknown>): QueryEffectKind<TEffectHKT, T["get"]>
|
||||
get(placeholderValues?: Record<string, unknown>): any {
|
||||
if (this.isRqbV2Query) return this.getRqbV2(placeholderValues)
|
||||
|
||||
if (!this.fields && !this.customResultMapper) {
|
||||
return this.executeWithCache<T["get"]>(placeholderValues, "get")
|
||||
}
|
||||
|
||||
return this.executeWithCache<T["values"], T["get"]>(
|
||||
placeholderValues,
|
||||
"values",
|
||||
(rows) => this.mapGetResult(rows) as T["get"],
|
||||
)
|
||||
}
|
||||
|
||||
values(placeholderValues?: Record<string, unknown>): QueryEffectKind<TEffectHKT, T["values"]>
|
||||
values(placeholderValues?: Record<string, unknown>): any {
|
||||
return this.executeWithCache<T["values"]>(placeholderValues, "values")
|
||||
}
|
||||
|
||||
execute(placeholderValues?: Record<string, unknown>): QueryEffectKind<TEffectHKT, T["execute"]>
|
||||
execute(placeholderValues?: Record<string, unknown>): any {
|
||||
return this[this.effectExecuteMethod](placeholderValues) as QueryEffectKind<TEffectHKT, T["execute"]>
|
||||
}
|
||||
|
||||
mapRunResult(result: unknown, _isFromBatch?: boolean): unknown {
|
||||
return result
|
||||
}
|
||||
|
||||
mapAllResult(rows: unknown, isFromBatch?: boolean): unknown {
|
||||
if (isFromBatch) {
|
||||
rows = Array.isArray(rows) ? rows : []
|
||||
}
|
||||
|
||||
if (!this.fields && !this.customResultMapper) {
|
||||
return rows
|
||||
}
|
||||
|
||||
if (this.isRqbV2Query) {
|
||||
return this.useJitMappers
|
||||
? (this.jitMapper =
|
||||
(this.jitMapper as RelationalRowsMapper<T["all"]>) ?? makeJitRqbMapper<T["all"]>(this.rqbConfig!))(
|
||||
rows as Record<string, unknown>[],
|
||||
)
|
||||
: (this.customResultMapper as (rows: Record<string, unknown>[]) => unknown)(rows as Record<string, unknown>[])
|
||||
}
|
||||
|
||||
if (this.customResultMapper) {
|
||||
return (this.customResultMapper as (rows: unknown[][]) => unknown)(rows as unknown[][]) as T["all"]
|
||||
}
|
||||
|
||||
return this.useJitMappers
|
||||
? (this.jitMapper =
|
||||
(this.jitMapper as RowsMapper<T["all"]>) ??
|
||||
makeJitQueryMapper<T["all"]>(this.fields!, this.joinsNotNullableMap))(rows as unknown[][])
|
||||
: (rows as unknown[][]).map((row) => mapResultRow(this.fields!, row, this.joinsNotNullableMap))
|
||||
}
|
||||
|
||||
mapGetResult(rows: unknown, isFromBatch?: boolean): unknown {
|
||||
if (isFromBatch) {
|
||||
rows = Array.isArray(rows) ? rows : []
|
||||
}
|
||||
|
||||
if (!this.fields && !this.customResultMapper) {
|
||||
return Array.isArray(rows) ? rows[0] : rows
|
||||
}
|
||||
|
||||
const row = Array.isArray(rows) ? rows[0] : rows
|
||||
if (!row) return undefined
|
||||
|
||||
if (this.isRqbV2Query) {
|
||||
return this.useJitMappers
|
||||
? (this.jitMapper =
|
||||
(this.jitMapper as RelationalRowsMapper<T["get"][]>) ?? makeJitRqbMapper<T["get"][]>(this.rqbConfig!))([
|
||||
row as Record<string, unknown>,
|
||||
])
|
||||
: (this.customResultMapper as (rows: Record<string, unknown>[]) => unknown)([row as Record<string, unknown>])
|
||||
}
|
||||
|
||||
if (this.customResultMapper) {
|
||||
return (this.customResultMapper as (rows: unknown[][]) => unknown)([row as unknown[]]) as T["get"]
|
||||
}
|
||||
|
||||
return this.useJitMappers
|
||||
? (this.jitMapper =
|
||||
(this.jitMapper as RowsMapper<T["get"][]>) ??
|
||||
makeJitQueryMapper<T["get"][]>(this.fields!, this.joinsNotNullableMap))([row as unknown[]])[0]
|
||||
: mapResultRow(this.fields!, row as unknown[], this.joinsNotNullableMap)
|
||||
}
|
||||
|
||||
private allRqbV2(placeholderValues?: Record<string, unknown>) {
|
||||
return this.executeWithCache<unknown[], T["all"]>(
|
||||
placeholderValues,
|
||||
"all",
|
||||
(rows) => this.mapAllResult(rows) as T["all"],
|
||||
)
|
||||
}
|
||||
|
||||
private getRqbV2(placeholderValues?: Record<string, unknown>) {
|
||||
return this.executeWithCache<unknown, T["get"] | undefined>(placeholderValues, "get", (row) =>
|
||||
row === undefined ? undefined : (this.mapGetResult(row) as T["get"]),
|
||||
)
|
||||
}
|
||||
|
||||
private executeWithCache<A, B = A>(
|
||||
placeholderValues: Record<string, unknown> | undefined,
|
||||
executeMethod: SQLiteEffectExecuteMethod,
|
||||
mapResult?: (result: A) => B,
|
||||
) {
|
||||
return Effect.gen({ self: this }, function* () {
|
||||
const params = fillPlaceholders(this.query.params, placeholderValues ?? {})
|
||||
|
||||
yield* this.logger.logQuery(this.query.sql, params)
|
||||
|
||||
return yield* this.queryWithCache(
|
||||
this.query.sql,
|
||||
params,
|
||||
Effect.suspend(() => this.executor(params, executeMethod) as Effect.Effect<A, unknown, unknown>),
|
||||
mapResult,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
private mapCachedResult<A, B>(result: A, mapResult: ((result: A) => B) | undefined) {
|
||||
if (!mapResult) return Effect.succeed(result as unknown as B)
|
||||
return Effect.try({
|
||||
try: () => mapResult(result),
|
||||
catch: (cause) => cause,
|
||||
})
|
||||
}
|
||||
|
||||
private queryWithCache<A, E, R, B = A>(
|
||||
queryString: string,
|
||||
params: unknown[],
|
||||
query: Effect.Effect<A, E, R>,
|
||||
mapResult?: (result: A) => B,
|
||||
) {
|
||||
return Effect.gen({ self: this }, function* () {
|
||||
if (this.queryMetadata?.type === "select" && this.cacheConfig?.enabled && (yield* this.isInTransaction)) {
|
||||
return yield* this.mapCachedResult(yield* query, mapResult)
|
||||
}
|
||||
|
||||
const cacheStrat: Awaited<ReturnType<typeof strategyFor>> = !is(this.cache.cache, NoopCache)
|
||||
? yield* Effect.tryPromise(() => strategyFor(queryString, params, this.queryMetadata, this.cacheConfig))
|
||||
: { type: "skip" as const }
|
||||
|
||||
if (cacheStrat.type === "skip") {
|
||||
return yield* this.mapCachedResult(yield* query, mapResult)
|
||||
}
|
||||
|
||||
if (cacheStrat.type === "invalidate") {
|
||||
const result = yield* query
|
||||
yield* this.cache.onMutate({ tables: cacheStrat.tables })
|
||||
return yield* this.mapCachedResult(result, mapResult)
|
||||
}
|
||||
|
||||
if (cacheStrat.type === "try") {
|
||||
if (yield* this.isInTransaction) {
|
||||
return yield* this.mapCachedResult(yield* query, mapResult)
|
||||
}
|
||||
|
||||
const { tables, key, isTag, autoInvalidate, config } = cacheStrat
|
||||
const fromCache: any[] | undefined = yield* this.cache.get(key, tables, isTag, autoInvalidate)
|
||||
|
||||
if (typeof fromCache !== "undefined") {
|
||||
return yield* this.mapCachedResult(fromCache as unknown as A, mapResult)
|
||||
}
|
||||
|
||||
const result = yield* query
|
||||
|
||||
yield* this.cache.put(key, result, autoInvalidate ? tables : [], isTag, config)
|
||||
|
||||
return yield* this.mapCachedResult(result, mapResult)
|
||||
}
|
||||
|
||||
assertUnreachable(cacheStrat)
|
||||
}).pipe(
|
||||
Effect.catch((e) => {
|
||||
return Effect.fail(new EffectDrizzleQueryError({ query: queryString, params, cause: Cause.fail(e) }))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
getQuery(): Query {
|
||||
return this.query
|
||||
}
|
||||
|
||||
mapResult(response: unknown, isFromBatch?: boolean) {
|
||||
switch (this.effectExecuteMethod) {
|
||||
case "run": {
|
||||
return this.mapRunResult(response, isFromBatch)
|
||||
}
|
||||
case "all": {
|
||||
return this.mapAllResult(response, isFromBatch)
|
||||
}
|
||||
case "get": {
|
||||
return this.mapGetResult(response, isFromBatch)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class SQLiteEffectSession<
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
TRunResult = unknown,
|
||||
TRelations extends AnyRelations = EmptyRelations,
|
||||
> {
|
||||
static readonly [entityKind]: string = "SQLiteEffectSession"
|
||||
|
||||
constructor(readonly dialect: SQLiteAsyncDialect) {}
|
||||
|
||||
abstract prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(
|
||||
query: Query,
|
||||
fields: SelectedFieldsOrdered | undefined,
|
||||
executeMethod: SQLiteExecuteMethod,
|
||||
customResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => unknown,
|
||||
queryMetadata?: {
|
||||
type: "select" | "update" | "delete" | "insert"
|
||||
tables: string[]
|
||||
},
|
||||
cacheConfig?: WithCacheConfig,
|
||||
): SQLiteEffectPreparedQuery<T, TEffectHKT>
|
||||
|
||||
prepareOneTimeQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(
|
||||
query: Query,
|
||||
fields: SelectedFieldsOrdered | undefined,
|
||||
executeMethod: SQLiteExecuteMethod,
|
||||
customResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => unknown,
|
||||
queryMetadata?: {
|
||||
type: "select" | "update" | "delete" | "insert"
|
||||
tables: string[]
|
||||
},
|
||||
cacheConfig?: WithCacheConfig,
|
||||
): SQLiteEffectPreparedQuery<T, TEffectHKT> {
|
||||
return this.prepareQuery(query, fields, executeMethod, customResultMapper, queryMetadata, cacheConfig)
|
||||
}
|
||||
|
||||
abstract prepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(
|
||||
query: Query,
|
||||
fields: SelectedFieldsOrdered | undefined,
|
||||
executeMethod: SQLiteExecuteMethod,
|
||||
customResultMapper: (rows: Record<string, unknown>[], mapColumnValue?: (value: unknown) => unknown) => unknown,
|
||||
config: RelationalQueryMapperConfig,
|
||||
): SQLiteEffectPreparedQuery<T, TEffectHKT, true>
|
||||
|
||||
prepareOneTimeRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(
|
||||
query: Query,
|
||||
fields: SelectedFieldsOrdered | undefined,
|
||||
executeMethod: SQLiteExecuteMethod,
|
||||
customResultMapper: (rows: Record<string, unknown>[], mapColumnValue?: (value: unknown) => unknown) => unknown,
|
||||
config: RelationalQueryMapperConfig,
|
||||
): SQLiteEffectPreparedQuery<T, TEffectHKT, true> {
|
||||
return this.prepareRelationalQuery(query, fields, executeMethod, customResultMapper, config)
|
||||
}
|
||||
|
||||
run(query: SQL): QueryEffectKind<TEffectHKT, TRunResult>
|
||||
run(query: SQL): any {
|
||||
return this.prepareQuery<PreparedQueryConfig & { run: TRunResult; execute: TRunResult }>(
|
||||
this.dialect.sqlToQuery(query),
|
||||
undefined,
|
||||
"run",
|
||||
).run()
|
||||
}
|
||||
|
||||
all<T = unknown>(query: SQL): QueryEffectKind<TEffectHKT, T[]>
|
||||
all<T = unknown>(query: SQL): any {
|
||||
return this.prepareQuery<PreparedQueryConfig & { all: T[]; execute: T[] }>(
|
||||
this.dialect.sqlToQuery(query),
|
||||
undefined,
|
||||
"all",
|
||||
).all()
|
||||
}
|
||||
|
||||
get<T = unknown>(query: SQL): QueryEffectKind<TEffectHKT, T | undefined>
|
||||
get<T = unknown>(query: SQL): any {
|
||||
return this.prepareQuery<PreparedQueryConfig & { get: T | undefined; execute: T | undefined }>(
|
||||
this.dialect.sqlToQuery(query),
|
||||
undefined,
|
||||
"get",
|
||||
).get()
|
||||
}
|
||||
|
||||
values<T extends unknown[] = unknown[]>(query: SQL): QueryEffectKind<TEffectHKT, T[]>
|
||||
values<T extends unknown[] = unknown[]>(query: SQL): any {
|
||||
return this.prepareQuery<PreparedQueryConfig & { values: T[]; execute: T[] }>(
|
||||
this.dialect.sqlToQuery(query),
|
||||
undefined,
|
||||
"all",
|
||||
).values()
|
||||
}
|
||||
|
||||
count(query: SQL): QueryEffectKind<TEffectHKT, number>
|
||||
count(query: SQL): any {
|
||||
return this.values<[number]>(query).pipe(Effect.map((result) => result[0]?.[0] ?? 0))
|
||||
}
|
||||
|
||||
abstract transaction<A, E, R>(
|
||||
transaction: (tx: SQLiteEffectTransaction<TEffectHKT, TRunResult, TRelations>) => Effect.Effect<A, E, R>,
|
||||
config?: SQLiteTransactionConfig,
|
||||
): Effect.Effect<A, E | SqlError, R>
|
||||
}
|
||||
|
||||
export abstract class SQLiteEffectTransaction<
|
||||
TEffectHKT extends QueryEffectHKTBase,
|
||||
TRunResult,
|
||||
TRelations extends AnyRelations = EmptyRelations,
|
||||
> extends SQLiteEffectDatabase<TEffectHKT, TRunResult, TRelations> {
|
||||
static override readonly [entityKind]: string = "SQLiteEffectTransaction"
|
||||
|
||||
constructor(
|
||||
dialect: SQLiteAsyncDialect,
|
||||
session: SQLiteEffectSession<TEffectHKT, TRunResult, TRelations>,
|
||||
protected relations: TRelations,
|
||||
) {
|
||||
super(dialect, session, relations)
|
||||
}
|
||||
|
||||
rollback() {
|
||||
return new EffectTransactionRollbackError()
|
||||
}
|
||||
}
|
||||
|
||||
export const migrate = Effect.fn("migrate")(function* <TEffectHKT extends QueryEffectHKTBase>(
|
||||
migrations: MigrationMeta[],
|
||||
session: SQLiteEffectSession<TEffectHKT>,
|
||||
config: string | MigrationConfigWithInit,
|
||||
) {
|
||||
const migrationsTable =
|
||||
typeof config === "string" ? "__drizzle_migrations" : (config.migrationsTable ?? "__drizzle_migrations")
|
||||
|
||||
const { newDb } = yield* upgradeIfNeeded(migrationsTable, session, migrations)
|
||||
|
||||
if (newDb) {
|
||||
yield* session.run(sql`
|
||||
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
|
||||
id INTEGER PRIMARY KEY,
|
||||
hash text NOT NULL,
|
||||
created_at numeric,
|
||||
name text,
|
||||
applied_at TEXT
|
||||
)
|
||||
`)
|
||||
}
|
||||
|
||||
const dbMigrations = yield* session.all<{ id: number; hash: string; created_at: string; name: string | null }>(
|
||||
sql`SELECT id, hash, created_at, name FROM ${sql.identifier(migrationsTable)}`,
|
||||
)
|
||||
|
||||
if (typeof config === "object" && config.init) {
|
||||
if (dbMigrations.length) {
|
||||
return yield* new MigratorInitError({ exitCode: "databaseMigrations" })
|
||||
}
|
||||
|
||||
if (migrations.length > 1) {
|
||||
return yield* new MigratorInitError({ exitCode: "localMigrations" })
|
||||
}
|
||||
|
||||
const [migration] = migrations
|
||||
if (!migration) return
|
||||
|
||||
yield* session.run(
|
||||
sql`insert into ${sql.identifier(
|
||||
migrationsTable,
|
||||
)} ("hash", "created_at", "name", "applied_at") values(${migration.hash}, ${migration.folderMillis}, ${migration.name}, ${new Date().toISOString()})`,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const migrationsToRun = getMigrationsToRun({ localMigrations: migrations, dbMigrations })
|
||||
if (migrationsToRun.length === 0) return
|
||||
|
||||
yield* session.transaction((tx) =>
|
||||
Effect.gen(function* () {
|
||||
for (const migration of migrationsToRun) {
|
||||
for (const stmt of migration.sql) {
|
||||
yield* tx.run(sql.raw(stmt))
|
||||
}
|
||||
yield* tx.run(
|
||||
sql`insert into ${sql.identifier(
|
||||
migrationsTable,
|
||||
)} ("hash", "created_at", "name", "applied_at") values(${migration.hash}, ${migration.folderMillis}, ${migration.name}, ${new Date().toISOString()})`,
|
||||
)
|
||||
}
|
||||
}),
|
||||
)
|
||||
})
|
||||
402
packages/effect-drizzle-sqlite/src/sqlite-core/effect/update.ts
Normal file
402
packages/effect-drizzle-sqlite/src/sqlite-core/effect/update.ts
Normal file
|
|
@ -0,0 +1,402 @@
|
|||
/* oxlint-disable */
|
||||
import type * as Effect from "effect/Effect"
|
||||
import { applyEffectWrapper, type QueryEffectHKTBase } from "drizzle-orm/effect-core/query-effect"
|
||||
import { entityKind, is } from "drizzle-orm/entity"
|
||||
import type { SelectResultFields } from "drizzle-orm/query-builders/select.types"
|
||||
import type { RunnableQuery } from "drizzle-orm/runnable-query"
|
||||
import { SelectionProxyHandler } from "drizzle-orm/selection-proxy"
|
||||
import type { Placeholder, Query, SQL, SQLWrapper } from "drizzle-orm/sql/sql"
|
||||
import type { SQLiteDialect } from "drizzle-orm/sqlite-core/dialect"
|
||||
import type { SelectedFields, SQLiteSelectJoinConfig } from "drizzle-orm/sqlite-core/query-builders/select.types"
|
||||
import type { SQLiteUpdateConfig, SQLiteUpdateSetSource } from "drizzle-orm/sqlite-core/query-builders/update"
|
||||
import type { PreparedQueryConfig } from "drizzle-orm/sqlite-core/session"
|
||||
import { SQLiteTable } from "drizzle-orm/sqlite-core/table"
|
||||
import { extractUsedTable } from "drizzle-orm/sqlite-core/utils"
|
||||
import { SQLiteViewBase } from "drizzle-orm/sqlite-core/view-base"
|
||||
import { Subquery } from "drizzle-orm/subquery"
|
||||
import { type DrizzleTypeError, type UpdateSet, type ValueOrArray } from "drizzle-orm/utils"
|
||||
import type { SQLiteColumn } from "drizzle-orm/sqlite-core/columns/common"
|
||||
import {
|
||||
getTableColumnsRuntime,
|
||||
getTableLikeName,
|
||||
getViewSelectedFieldsRuntime,
|
||||
mapUpdateSet,
|
||||
orderSelectedFields,
|
||||
} from "../../internal/drizzle-utils"
|
||||
import type { SQLiteEffectPreparedQuery, SQLiteEffectSession } from "./session"
|
||||
|
||||
export type SQLiteEffectUpdateWithout<
|
||||
T extends AnySQLiteEffectUpdate,
|
||||
TDynamic extends boolean,
|
||||
K extends keyof T & string,
|
||||
> = TDynamic extends true
|
||||
? T
|
||||
: Omit<
|
||||
SQLiteEffectUpdateBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["from"],
|
||||
T["_"]["returning"],
|
||||
TDynamic,
|
||||
T["_"]["excludedMethods"] | K,
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
T["_"]["excludedMethods"] | K
|
||||
>
|
||||
|
||||
export type SQLiteEffectUpdateWithJoins<
|
||||
T extends AnySQLiteEffectUpdate,
|
||||
TDynamic extends boolean,
|
||||
TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL,
|
||||
> = TDynamic extends true
|
||||
? T
|
||||
: Omit<
|
||||
SQLiteEffectUpdateBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
TFrom,
|
||||
T["_"]["returning"],
|
||||
TDynamic,
|
||||
Exclude<T["_"]["excludedMethods"] | "from", "leftJoin" | "rightJoin" | "innerJoin" | "fullJoin">,
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
Exclude<T["_"]["excludedMethods"] | "from", "leftJoin" | "rightJoin" | "innerJoin" | "fullJoin">
|
||||
>
|
||||
|
||||
export type SQLiteEffectUpdateReturningAll<
|
||||
T extends AnySQLiteEffectUpdate,
|
||||
TDynamic extends boolean,
|
||||
> = SQLiteEffectUpdateWithout<
|
||||
SQLiteEffectUpdateBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["from"],
|
||||
T["_"]["table"]["$inferSelect"],
|
||||
TDynamic,
|
||||
T["_"]["excludedMethods"],
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
TDynamic,
|
||||
"returning"
|
||||
>
|
||||
|
||||
export type SQLiteEffectUpdateReturning<
|
||||
T extends AnySQLiteEffectUpdate,
|
||||
TDynamic extends boolean,
|
||||
TSelectedFields extends SelectedFields,
|
||||
> = SQLiteEffectUpdateWithout<
|
||||
SQLiteEffectUpdateBase<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["from"],
|
||||
SelectResultFields<TSelectedFields>,
|
||||
TDynamic,
|
||||
T["_"]["excludedMethods"],
|
||||
T["_"]["effectHKT"]
|
||||
>,
|
||||
TDynamic,
|
||||
"returning"
|
||||
>
|
||||
|
||||
export type SQLiteEffectUpdateExecute<T extends AnySQLiteEffectUpdate> = T["_"]["returning"] extends undefined
|
||||
? T["_"]["runResult"]
|
||||
: T["_"]["returning"][]
|
||||
|
||||
export type SQLiteEffectUpdatePrepare<
|
||||
T extends AnySQLiteEffectUpdate,
|
||||
TEffectHKT extends QueryEffectHKTBase = T["_"]["effectHKT"],
|
||||
> = SQLiteEffectPreparedQuery<
|
||||
PreparedQueryConfig & {
|
||||
run: T["_"]["runResult"]
|
||||
all: T["_"]["returning"] extends undefined
|
||||
? DrizzleTypeError<".all() cannot be used without .returning()">
|
||||
: T["_"]["returning"][]
|
||||
get: T["_"]["returning"] extends undefined
|
||||
? DrizzleTypeError<".get() cannot be used without .returning()">
|
||||
: T["_"]["returning"]
|
||||
values: T["_"]["returning"] extends undefined
|
||||
? DrizzleTypeError<".values() cannot be used without .returning()">
|
||||
: any[][]
|
||||
execute: SQLiteEffectUpdateExecute<T>
|
||||
},
|
||||
TEffectHKT
|
||||
>
|
||||
|
||||
export type SQLiteEffectUpdateDynamic<T extends AnySQLiteEffectUpdate> = SQLiteEffectUpdate<
|
||||
T["_"]["table"],
|
||||
T["_"]["runResult"],
|
||||
T["_"]["from"],
|
||||
T["_"]["returning"],
|
||||
T["_"]["effectHKT"]
|
||||
>
|
||||
|
||||
export type SQLiteEffectUpdate<
|
||||
TTable extends SQLiteTable = SQLiteTable,
|
||||
TRunResult = unknown,
|
||||
TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL | undefined = undefined,
|
||||
TReturning extends Record<string, unknown> | undefined = Record<string, unknown> | undefined,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> = SQLiteEffectUpdateBase<TTable, TRunResult, TFrom, TReturning, true, never, TEffectHKT>
|
||||
|
||||
export type AnySQLiteEffectUpdate = SQLiteEffectUpdateBase<any, any, any, any, any, any, any>
|
||||
|
||||
export type SQLiteEffectUpdateJoinFn<T extends AnySQLiteEffectUpdate> = <
|
||||
TJoinedTable extends SQLiteTable | Subquery | SQLiteViewBase | SQL,
|
||||
>(
|
||||
table: TJoinedTable,
|
||||
on:
|
||||
| ((
|
||||
updateTable: T["_"]["table"]["_"]["columns"],
|
||||
from: T["_"]["from"] extends SQLiteTable
|
||||
? T["_"]["from"]["_"]["columns"]
|
||||
: T["_"]["from"] extends Subquery | SQLiteViewBase
|
||||
? T["_"]["from"]["_"]["selectedFields"]
|
||||
: never,
|
||||
) => SQL | undefined)
|
||||
| SQL
|
||||
| undefined,
|
||||
) => T
|
||||
|
||||
export class SQLiteEffectUpdateBuilder<
|
||||
TTable extends SQLiteTable,
|
||||
TRunResult,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> {
|
||||
static readonly [entityKind]: string = "SQLiteEffectUpdateBuilder"
|
||||
|
||||
declare readonly _: {
|
||||
readonly table: TTable
|
||||
}
|
||||
|
||||
constructor(
|
||||
protected table: TTable,
|
||||
protected session: SQLiteEffectSession<TEffectHKT, TRunResult, any>,
|
||||
protected dialect: SQLiteDialect,
|
||||
private withList?: Subquery[],
|
||||
) {}
|
||||
|
||||
set(
|
||||
values: SQLiteUpdateSetSource<TTable>,
|
||||
): SQLiteEffectUpdateWithout<
|
||||
SQLiteEffectUpdateBase<TTable, TRunResult, undefined, undefined, false, never, TEffectHKT>,
|
||||
false,
|
||||
"leftJoin" | "rightJoin" | "innerJoin" | "fullJoin"
|
||||
> {
|
||||
return new SQLiteEffectUpdateBase(
|
||||
this.table,
|
||||
mapUpdateSet(this.table, values),
|
||||
this.session,
|
||||
this.dialect,
|
||||
this.withList,
|
||||
) as any
|
||||
}
|
||||
}
|
||||
|
||||
export interface SQLiteEffectUpdateBase<
|
||||
TTable extends SQLiteTable = SQLiteTable,
|
||||
TRunResult = unknown,
|
||||
TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL | undefined = undefined,
|
||||
TReturning = undefined,
|
||||
TDynamic extends boolean = false,
|
||||
_TExcludedMethods extends string = never,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
> extends SQLWrapper,
|
||||
RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], "sqlite">,
|
||||
Effect.Effect<
|
||||
TReturning extends undefined ? TRunResult : TReturning[],
|
||||
TEffectHKT["error"],
|
||||
TEffectHKT["context"]
|
||||
> {
|
||||
readonly _: {
|
||||
readonly dialect: "sqlite"
|
||||
readonly table: TTable
|
||||
readonly resultType: "async"
|
||||
readonly runResult: TRunResult
|
||||
readonly from: TFrom
|
||||
readonly returning: TReturning
|
||||
readonly dynamic: TDynamic
|
||||
readonly excludedMethods: _TExcludedMethods
|
||||
readonly result: TReturning extends undefined ? TRunResult : TReturning[]
|
||||
readonly effectHKT: TEffectHKT
|
||||
}
|
||||
}
|
||||
|
||||
export class SQLiteEffectUpdateBase<
|
||||
TTable extends SQLiteTable = SQLiteTable,
|
||||
TRunResult = unknown,
|
||||
TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL | undefined = undefined,
|
||||
TReturning = undefined,
|
||||
TDynamic extends boolean = false,
|
||||
_TExcludedMethods extends string = never,
|
||||
TEffectHKT extends QueryEffectHKTBase = QueryEffectHKTBase,
|
||||
>
|
||||
implements RunnableQuery<TReturning extends undefined ? TRunResult : TReturning[], "sqlite">, SQLWrapper
|
||||
{
|
||||
static readonly [entityKind]: string = "SQLiteEffectUpdate"
|
||||
|
||||
/** @internal */
|
||||
config: SQLiteUpdateConfig
|
||||
|
||||
constructor(
|
||||
table: TTable,
|
||||
set: UpdateSet,
|
||||
private effectSession: SQLiteEffectSession<TEffectHKT, TRunResult, any>,
|
||||
private effectDialect: SQLiteDialect,
|
||||
withList?: Subquery[],
|
||||
) {
|
||||
this.config = { set, table, withList, joins: [] }
|
||||
}
|
||||
|
||||
from<TFrom extends SQLiteTable | Subquery | SQLiteViewBase | SQL>(
|
||||
source: TFrom,
|
||||
): SQLiteEffectUpdateWithJoins<this, TDynamic, TFrom> {
|
||||
this.config.from = source
|
||||
return this as any
|
||||
}
|
||||
|
||||
private createJoin<TJoinType extends SQLiteSelectJoinConfig["joinType"]>(
|
||||
joinType: TJoinType,
|
||||
): SQLiteEffectUpdateJoinFn<this> {
|
||||
return ((
|
||||
table: SQLiteTable | Subquery | SQLiteViewBase | SQL,
|
||||
on: ((updateTable: TTable, from: TFrom) => SQL | undefined) | SQL | undefined,
|
||||
) => {
|
||||
const tableName = getTableLikeName(table)
|
||||
|
||||
if (typeof tableName === "string" && this.config.joins.some((join) => join.alias === tableName)) {
|
||||
throw new Error(`Alias "${tableName}" is already used in this query`)
|
||||
}
|
||||
|
||||
if (typeof on === "function") {
|
||||
const from = this.config.from
|
||||
? is(table, SQLiteTable)
|
||||
? getTableColumnsRuntime(table)
|
||||
: is(table, Subquery)
|
||||
? table._.selectedFields
|
||||
: is(table, SQLiteViewBase)
|
||||
? getViewSelectedFieldsRuntime(table).selectedFields
|
||||
: undefined
|
||||
: undefined
|
||||
on = on(
|
||||
new Proxy(
|
||||
this.config.table._.columns,
|
||||
new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" }),
|
||||
) as any,
|
||||
from &&
|
||||
(new Proxy(from, new SelectionProxyHandler({ sqlAliasedBehavior: "sql", sqlBehavior: "sql" })) as any),
|
||||
)
|
||||
}
|
||||
|
||||
this.config.joins.push({ on, table, joinType, alias: tableName })
|
||||
|
||||
return this as any
|
||||
}) as any
|
||||
}
|
||||
|
||||
leftJoin = this.createJoin("left")
|
||||
|
||||
rightJoin = this.createJoin("right")
|
||||
|
||||
innerJoin = this.createJoin("inner")
|
||||
|
||||
fullJoin = this.createJoin("full")
|
||||
|
||||
where(where: SQL | undefined): SQLiteEffectUpdateWithout<this, TDynamic, "where"> {
|
||||
this.config.where = where
|
||||
return this as any
|
||||
}
|
||||
|
||||
orderBy(
|
||||
builder: (updateTable: TTable) => ValueOrArray<SQLiteColumn | SQL | SQL.Aliased>,
|
||||
): SQLiteEffectUpdateWithout<this, TDynamic, "orderBy">
|
||||
orderBy(...columns: (SQLiteColumn | SQL | SQL.Aliased)[]): SQLiteEffectUpdateWithout<this, TDynamic, "orderBy">
|
||||
orderBy(
|
||||
...columns:
|
||||
| [(updateTable: TTable) => ValueOrArray<SQLiteColumn | SQL | SQL.Aliased>]
|
||||
| (SQLiteColumn | SQL | SQL.Aliased)[]
|
||||
): SQLiteEffectUpdateWithout<this, TDynamic, "orderBy"> {
|
||||
if (typeof columns[0] === "function") {
|
||||
const orderBy = columns[0](
|
||||
new Proxy(
|
||||
getTableColumnsRuntime(this.config.table),
|
||||
new SelectionProxyHandler({ sqlAliasedBehavior: "alias", sqlBehavior: "sql" }),
|
||||
) as any,
|
||||
)
|
||||
|
||||
this.config.orderBy = Array.isArray(orderBy) ? orderBy : [orderBy]
|
||||
return this as any
|
||||
}
|
||||
|
||||
this.config.orderBy = columns as (SQLiteColumn | SQL | SQL.Aliased)[]
|
||||
return this as any
|
||||
}
|
||||
|
||||
limit(limit: number | Placeholder): SQLiteEffectUpdateWithout<this, TDynamic, "limit"> {
|
||||
this.config.limit = limit
|
||||
return this as any
|
||||
}
|
||||
|
||||
returning(): SQLiteEffectUpdateReturningAll<this, TDynamic>
|
||||
returning<TSelectedFields extends SelectedFields>(
|
||||
fields: TSelectedFields,
|
||||
): SQLiteEffectUpdateReturning<this, TDynamic, TSelectedFields>
|
||||
returning(
|
||||
fields: SelectedFields = getTableColumnsRuntime(this.config.table),
|
||||
): SQLiteEffectUpdateWithout<AnySQLiteEffectUpdate, TDynamic, "returning"> {
|
||||
this.config.returning = orderSelectedFields<SQLiteColumn>(fields)
|
||||
return this as any
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
getSQL(): SQL {
|
||||
return this.effectDialect.buildUpdateQuery(this.config)
|
||||
}
|
||||
|
||||
toSQL(): Query {
|
||||
return this.effectDialect.sqlToQuery(this.getSQL())
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_prepare(isOneTimeQuery = true): SQLiteEffectUpdatePrepare<this, TEffectHKT> {
|
||||
return this.effectSession[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
|
||||
this.effectDialect.sqlToQuery(this.getSQL()),
|
||||
this.config.returning,
|
||||
this.config.returning ? "all" : "run",
|
||||
undefined,
|
||||
{
|
||||
type: "update",
|
||||
tables: extractUsedTable(this.config.table),
|
||||
},
|
||||
) as SQLiteEffectUpdatePrepare<this, TEffectHKT>
|
||||
}
|
||||
|
||||
prepare(): SQLiteEffectUpdatePrepare<this, TEffectHKT> {
|
||||
return this._prepare(false)
|
||||
}
|
||||
|
||||
run: ReturnType<this["prepare"]>["run"] = (placeholderValues) => {
|
||||
return this._prepare().run(placeholderValues)
|
||||
}
|
||||
|
||||
all: ReturnType<this["prepare"]>["all"] = (placeholderValues) => {
|
||||
return this._prepare().all(placeholderValues)
|
||||
}
|
||||
|
||||
get: ReturnType<this["prepare"]>["get"] = (placeholderValues) => {
|
||||
return this._prepare().get(placeholderValues)
|
||||
}
|
||||
|
||||
values: ReturnType<this["prepare"]>["values"] = (placeholderValues) => {
|
||||
return this._prepare().values(placeholderValues)
|
||||
}
|
||||
|
||||
execute: ReturnType<this["prepare"]>["execute"] = (placeholderValues) => {
|
||||
return this._prepare().execute(placeholderValues)
|
||||
}
|
||||
|
||||
$dynamic(): SQLiteEffectUpdateDynamic<this> {
|
||||
return this as any
|
||||
}
|
||||
}
|
||||
|
||||
applyEffectWrapper(SQLiteEffectUpdateBase)
|
||||
Loading…
Add table
Add a link
Reference in a new issue