drizzle-orm 1.0.0-beta.8-c55bf0f → 1.0.0-beta.8-e601f80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/effect-postgres/migrator.cjs +1 -1
- package/effect-postgres/migrator.cjs.map +1 -1
- package/effect-postgres/migrator.d.cts +1 -1
- package/effect-postgres/migrator.d.ts +1 -1
- package/effect-postgres/migrator.js +1 -1
- package/effect-postgres/migrator.js.map +1 -1
- package/effect-postgres/session.cjs +29 -56
- package/effect-postgres/session.cjs.map +1 -1
- package/effect-postgres/session.d.cts +10 -7
- package/effect-postgres/session.d.ts +10 -7
- package/effect-postgres/session.js +30 -57
- package/effect-postgres/session.js.map +1 -1
- package/package.json +1 -1
- package/pg-core/effect/session.cjs +3 -3
- package/pg-core/effect/session.cjs.map +1 -1
- package/pg-core/effect/session.d.cts +1 -1
- package/pg-core/effect/session.d.ts +1 -1
- package/pg-core/effect/session.js +3 -3
- package/pg-core/effect/session.js.map +1 -1
|
@@ -23,7 +23,7 @@ __export(migrator_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(migrator_exports);
|
|
24
24
|
var import_migrator = require("../migrator.cjs");
|
|
25
25
|
var import_session = require("../pg-core/effect/session.cjs");
|
|
26
|
-
|
|
26
|
+
function migrate(db, config) {
|
|
27
27
|
const migrations = (0, import_migrator.readMigrationFiles)(config);
|
|
28
28
|
return (0, import_session.migrate)(migrations, db.session, config);
|
|
29
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/effect-postgres/migrator.ts"],"sourcesContent":["import type { MigrationConfig } from '~/migrator.ts';\nimport { readMigrationFiles } from '~/migrator.ts';\nimport { migrate as coreMigrate } from '~/pg-core/effect/session.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport type { EffectPgDatabase } from './driver.ts';\n\nexport
|
|
1
|
+
{"version":3,"sources":["../../src/effect-postgres/migrator.ts"],"sourcesContent":["import type { MigrationConfig } from '~/migrator.ts';\nimport { readMigrationFiles } from '~/migrator.ts';\nimport { migrate as coreMigrate } from '~/pg-core/effect/session.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport type { EffectPgDatabase } from './driver.ts';\n\nexport function migrate<TSchema extends Record<string, unknown>, TRelations extends AnyRelations>(\n\tdb: EffectPgDatabase<TSchema, TRelations>,\n\tconfig: MigrationConfig,\n) {\n\tconst migrations = readMigrationFiles(config);\n\treturn coreMigrate(migrations, db.session, config);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,sBAAmC;AACnC,qBAAuC;AAIhC,SAAS,QACf,IACA,QACC;AACD,QAAM,iBAAa,oCAAmB,MAAM;AAC5C,aAAO,eAAAA,SAAY,YAAY,GAAG,SAAS,MAAM;AAClD;","names":["coreMigrate"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { MigrationConfig } from "../migrator.cjs";
|
|
2
2
|
import type { AnyRelations } from "../relations.cjs";
|
|
3
3
|
import type { EffectPgDatabase } from "./driver.cjs";
|
|
4
|
-
export declare function migrate<TSchema extends Record<string, unknown>, TRelations extends AnyRelations>(db: EffectPgDatabase<TSchema, TRelations>, config: MigrationConfig):
|
|
4
|
+
export declare function migrate<TSchema extends Record<string, unknown>, TRelations extends AnyRelations>(db: EffectPgDatabase<TSchema, TRelations>, config: MigrationConfig): import("effect/Effect").Effect<void | import("../migrator.ts").MigratorInitFailResponse, import("../errors.ts").DrizzleQueryError, never>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { MigrationConfig } from "../migrator.js";
|
|
2
2
|
import type { AnyRelations } from "../relations.js";
|
|
3
3
|
import type { EffectPgDatabase } from "./driver.js";
|
|
4
|
-
export declare function migrate<TSchema extends Record<string, unknown>, TRelations extends AnyRelations>(db: EffectPgDatabase<TSchema, TRelations>, config: MigrationConfig):
|
|
4
|
+
export declare function migrate<TSchema extends Record<string, unknown>, TRelations extends AnyRelations>(db: EffectPgDatabase<TSchema, TRelations>, config: MigrationConfig): import("effect/Effect").Effect<void | import("../migrator.js").MigratorInitFailResponse, import("../errors.js").DrizzleQueryError, never>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readMigrationFiles } from "../migrator.js";
|
|
2
2
|
import { migrate as coreMigrate } from "../pg-core/effect/session.js";
|
|
3
|
-
|
|
3
|
+
function migrate(db, config) {
|
|
4
4
|
const migrations = readMigrationFiles(config);
|
|
5
5
|
return coreMigrate(migrations, db.session, config);
|
|
6
6
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/effect-postgres/migrator.ts"],"sourcesContent":["import type { MigrationConfig } from '~/migrator.ts';\nimport { readMigrationFiles } from '~/migrator.ts';\nimport { migrate as coreMigrate } from '~/pg-core/effect/session.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport type { EffectPgDatabase } from './driver.ts';\n\nexport
|
|
1
|
+
{"version":3,"sources":["../../src/effect-postgres/migrator.ts"],"sourcesContent":["import type { MigrationConfig } from '~/migrator.ts';\nimport { readMigrationFiles } from '~/migrator.ts';\nimport { migrate as coreMigrate } from '~/pg-core/effect/session.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport type { EffectPgDatabase } from './driver.ts';\n\nexport function migrate<TSchema extends Record<string, unknown>, TRelations extends AnyRelations>(\n\tdb: EffectPgDatabase<TSchema, TRelations>,\n\tconfig: MigrationConfig,\n) {\n\tconst migrations = readMigrationFiles(config);\n\treturn coreMigrate(migrations, db.session, config);\n}\n"],"mappings":"AACA,SAAS,0BAA0B;AACnC,SAAS,WAAW,mBAAmB;AAIhC,SAAS,QACf,IACA,QACC;AACD,QAAM,aAAa,mBAAmB,MAAM;AAC5C,SAAO,YAAY,YAAY,GAAG,SAAS,MAAM;AAClD;","names":[]}
|
|
@@ -31,7 +31,7 @@ var import_session = require("../pg-core/effect/session.cjs");
|
|
|
31
31
|
var import_sql = require("../sql/sql.cjs");
|
|
32
32
|
var import_utils = require("../utils.cjs");
|
|
33
33
|
class EffectPgPreparedQuery extends import_session.PgEffectPreparedQuery {
|
|
34
|
-
constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, name, _isResponseInArrayMode, customResultMapper, isRqbV2Query
|
|
34
|
+
constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, name, _isResponseInArrayMode, customResultMapper, isRqbV2Query) {
|
|
35
35
|
super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);
|
|
36
36
|
this.client = client;
|
|
37
37
|
this.queryString = queryString;
|
|
@@ -41,7 +41,6 @@ class EffectPgPreparedQuery extends import_session.PgEffectPreparedQuery {
|
|
|
41
41
|
this._isResponseInArrayMode = _isResponseInArrayMode;
|
|
42
42
|
this.customResultMapper = customResultMapper;
|
|
43
43
|
this.isRqbV2Query = isRqbV2Query;
|
|
44
|
-
this.wrap = wrap;
|
|
45
44
|
}
|
|
46
45
|
static [import_entity.entityKind] = "EffectPgPreparedQuery";
|
|
47
46
|
execute(placeholderValues) {
|
|
@@ -59,35 +58,31 @@ class EffectPgPreparedQuery extends import_session.PgEffectPreparedQuery {
|
|
|
59
58
|
return this.queryWithCache(
|
|
60
59
|
query.sql,
|
|
61
60
|
params,
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
))
|
|
75
|
-
)
|
|
61
|
+
client.unsafe(query.sql, params).values.pipe(import_effect.Effect.andThen(
|
|
62
|
+
(rows) => {
|
|
63
|
+
if (customResultMapper) return customResultMapper(rows);
|
|
64
|
+
return rows.map(
|
|
65
|
+
(row) => (0, import_utils.mapResultRow)(
|
|
66
|
+
fields,
|
|
67
|
+
row,
|
|
68
|
+
joinsNotNullableMap
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
))
|
|
76
73
|
);
|
|
77
74
|
}
|
|
78
75
|
executeRqbV2(placeholderValues) {
|
|
79
76
|
const { query, logger, customResultMapper, client } = this;
|
|
80
77
|
const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
|
|
81
78
|
logger.logQuery(query.sql, params);
|
|
82
|
-
return
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}))
|
|
90
|
-
);
|
|
79
|
+
return client.unsafe(query.sql, params).withoutTransform.pipe(
|
|
80
|
+
import_effect.Effect.andThen(
|
|
81
|
+
(v) => customResultMapper(v)
|
|
82
|
+
)
|
|
83
|
+
).pipe(import_effect.Effect.catchAll((e) => {
|
|
84
|
+
return import_effect.Effect.fail(new import_errors.DrizzleQueryError(query.sql, params, e instanceof Error ? e : void 0));
|
|
85
|
+
}));
|
|
91
86
|
}
|
|
92
87
|
all(placeholderValues) {
|
|
93
88
|
const { query, logger, client } = this;
|
|
@@ -96,7 +91,7 @@ class EffectPgPreparedQuery extends import_session.PgEffectPreparedQuery {
|
|
|
96
91
|
return this.queryWithCache(
|
|
97
92
|
query.sql,
|
|
98
93
|
params,
|
|
99
|
-
|
|
94
|
+
client.unsafe(query.sql, params).withoutTransform
|
|
100
95
|
);
|
|
101
96
|
}
|
|
102
97
|
/** @internal */
|
|
@@ -105,13 +100,11 @@ class EffectPgPreparedQuery extends import_session.PgEffectPreparedQuery {
|
|
|
105
100
|
}
|
|
106
101
|
}
|
|
107
102
|
class EffectPgSession extends import_session.PgEffectSession {
|
|
108
|
-
constructor(client, dialect, relations, schema, options = {}
|
|
103
|
+
constructor(client, dialect, relations, schema, options = {}) {
|
|
109
104
|
super(dialect);
|
|
110
105
|
this.client = client;
|
|
111
106
|
this.relations = relations;
|
|
112
107
|
this.schema = schema;
|
|
113
|
-
this.wrap = wrap;
|
|
114
|
-
this.nestedIndex = nestedIndex;
|
|
115
108
|
this.logger = options.logger ?? new import_logger.NoopLogger();
|
|
116
109
|
this.cache = options.cache;
|
|
117
110
|
}
|
|
@@ -131,8 +124,7 @@ class EffectPgSession extends import_session.PgEffectSession {
|
|
|
131
124
|
name,
|
|
132
125
|
isResponseInArrayMode,
|
|
133
126
|
customResultMapper,
|
|
134
|
-
false
|
|
135
|
-
this.wrap
|
|
127
|
+
false
|
|
136
128
|
);
|
|
137
129
|
}
|
|
138
130
|
prepareRelationalQuery(query, fields, name, customResultMapper) {
|
|
@@ -148,8 +140,7 @@ class EffectPgSession extends import_session.PgEffectSession {
|
|
|
148
140
|
name,
|
|
149
141
|
false,
|
|
150
142
|
customResultMapper,
|
|
151
|
-
true
|
|
152
|
-
this.wrap
|
|
143
|
+
true
|
|
153
144
|
);
|
|
154
145
|
}
|
|
155
146
|
execute(query) {
|
|
@@ -169,34 +160,16 @@ class EffectPgSession extends import_session.PgEffectSession {
|
|
|
169
160
|
).all();
|
|
170
161
|
}
|
|
171
162
|
transaction(transaction) {
|
|
172
|
-
const { dialect, relations, schema
|
|
173
|
-
const
|
|
174
|
-
return this.client.withTransaction(import_effect.Effect.gen(function* (
|
|
175
|
-
const session = new EffectPgSession(
|
|
176
|
-
client,
|
|
177
|
-
dialect,
|
|
178
|
-
relations,
|
|
179
|
-
schema,
|
|
180
|
-
{ logger, cache },
|
|
181
|
-
txAdapter,
|
|
182
|
-
nestedIndex + 1
|
|
183
|
-
);
|
|
163
|
+
const { dialect, relations, schema } = this;
|
|
164
|
+
const session = this;
|
|
165
|
+
return this.client.withTransaction(import_effect.Effect.gen(function* () {
|
|
184
166
|
const tx = new EffectPgTransaction(
|
|
185
167
|
dialect,
|
|
186
168
|
session,
|
|
187
169
|
relations,
|
|
188
|
-
schema
|
|
189
|
-
nestedIndex + 1
|
|
170
|
+
schema
|
|
190
171
|
);
|
|
191
|
-
|
|
192
|
-
const res = yield* transaction(tx).pipe(import_effect.Effect.catchAll(
|
|
193
|
-
(e) => import_effect.Effect.gen(function* () {
|
|
194
|
-
if (nestedIndex) yield* tx.execute(import_sql.sql.raw(`rollback to savepoint ${sp}`));
|
|
195
|
-
return yield* import_effect.Effect.fail(e);
|
|
196
|
-
})
|
|
197
|
-
));
|
|
198
|
-
if (nestedIndex) yield* tx.execute(import_sql.sql.raw(`release savepoint ${sp}`));
|
|
199
|
-
return res;
|
|
172
|
+
return yield* transaction(tx);
|
|
200
173
|
}));
|
|
201
174
|
}
|
|
202
175
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/effect-postgres/session.ts"],"sourcesContent":["import type { PgClient } from '@effect/sql-pg/PgClient';\nimport { Effect } from 'effect';\nimport type * as V1 from '~/_relations.ts';\nimport type { EffectCache } from '~/cache/core/cache-effect.ts';\nimport type { WithCacheConfig } from '~/cache/core/types.ts';\nimport { entityKind } from '~/entity.ts';\nimport { DrizzleQueryError, type TransactionRollbackError } from '~/errors.ts';\nimport { type Logger, NoopLogger } from '~/logger.ts';\nimport type { PgDialect } from '~/pg-core/dialect.ts';\nimport { PgEffectPreparedQuery, PgEffectSession, PgEffectTransaction } from '~/pg-core/effect/session.ts';\nimport type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.types.ts';\nimport type { PgQueryResultHKT, PreparedQueryConfig } from '~/pg-core/session.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport type { Query, SQL } from '~/sql/sql.ts';\nimport { fillPlaceholders, sql } from '~/sql/sql.ts';\nimport { mapResultRow } from '~/utils.ts';\nexport interface EffectPgQueryResultHKT extends PgQueryResultHKT {\n\ttype: readonly object[];\n}\n\nexport class EffectPgPreparedQuery<T extends PreparedQueryConfig, TIsRqbV2 extends boolean = false>\n\textends PgEffectPreparedQuery<T>\n{\n\tstatic override readonly [entityKind]: string = 'EffectPgPreparedQuery';\n\n\tconstructor(\n\t\tprivate client: PgClient,\n\t\tprivate queryString: string,\n\t\tprivate params: unknown[],\n\t\tprivate logger: Logger,\n\t\tcache: EffectCache | undefined,\n\t\tqueryMetadata: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t} | undefined,\n\t\tcacheConfig: WithCacheConfig | undefined,\n\t\tprivate fields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tprivate _isResponseInArrayMode: boolean,\n\t\tprivate customResultMapper?: (\n\t\t\trows: TIsRqbV2 extends true ? Record<string, unknown>[] : unknown[][],\n\t\t) => T['execute'],\n\t\tprivate isRqbV2Query?: TIsRqbV2,\n\t\tprivate wrap: Effect.Adapter = <T>(t: T): T => t,\n\t) {\n\t\tsuper({ sql: queryString, params }, cache, queryMetadata, cacheConfig);\n\t}\n\n\texecute(placeholderValues?: Record<string, unknown>): Effect.Effect<T['execute'], DrizzleQueryError> {\n\t\tif (this.isRqbV2Query) return this.executeRqbV2(placeholderValues);\n\n\t\tconst { query, logger, customResultMapper, fields, joinsNotNullableMap, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\t\tlogger.logQuery(query.sql, params);\n\n\t\tif (!fields && !customResultMapper) {\n\t\t\treturn this.queryWithCache(\n\t\t\t\tquery.sql,\n\t\t\t\tparams,\n\t\t\t\tthis.client.unsafe(query.sql, params as any).withoutTransform,\n\t\t\t);\n\t\t}\n\n\t\treturn this.queryWithCache(\n\t\t\tquery.sql,\n\t\t\tparams,\n\t\t\tthis.wrap(\n\t\t\t\tclient.unsafe(query.sql, params as any).values.pipe(Effect.andThen(\n\t\t\t\t\t(rows) => {\n\t\t\t\t\t\tif (customResultMapper) return (customResultMapper as (rows: unknown[][]) => unknown)(rows as unknown[][]);\n\n\t\t\t\t\t\treturn rows.map((row) =>\n\t\t\t\t\t\t\tmapResultRow(\n\t\t\t\t\t\t\t\tfields!,\n\t\t\t\t\t\t\t\trow as unknown[],\n\t\t\t\t\t\t\t\tjoinsNotNullableMap,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t);\n\t\t\t\t\t},\n\t\t\t\t)),\n\t\t\t),\n\t\t);\n\t}\n\n\tprivate executeRqbV2(\n\t\tplaceholderValues?: Record<string, unknown>,\n\t): Effect.Effect<T['execute'], DrizzleQueryError> {\n\t\tconst { query, logger, customResultMapper, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\n\t\tlogger.logQuery(query.sql, params);\n\t\treturn this.wrap(\n\t\t\tclient.unsafe(query.sql, params as any).withoutTransform.pipe(\n\t\t\t\tEffect.andThen((v) =>\n\t\t\t\t\t(customResultMapper as (\n\t\t\t\t\t\trows: Record<string, unknown>[],\n\t\t\t\t\t\tmapColumnValue?: (value: unknown) => unknown,\n\t\t\t\t\t) => unknown)(v as Record<string, unknown>[])\n\t\t\t\t),\n\t\t\t).pipe(Effect.catchAll((e) => {\n\t\t\t\t// eslint-disable-next-line @drizzle-internal/no-instanceof\n\t\t\t\treturn Effect.fail(new DrizzleQueryError(query.sql, params, e instanceof Error ? e : undefined));\n\t\t\t})),\n\t\t);\n\t}\n\n\toverride all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError, never> {\n\t\tconst { query, logger, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\n\t\tlogger.logQuery(query.sql, params);\n\n\t\treturn this.queryWithCache(\n\t\t\tquery.sql,\n\t\t\tparams,\n\t\t\tthis.wrap(client.unsafe(query.sql, params as any).withoutTransform),\n\t\t);\n\t}\n\n\t/** @internal */\n\tisResponseInArrayMode(): boolean {\n\t\treturn this._isResponseInArrayMode;\n\t}\n}\n\nexport class EffectPgSession<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTRelations extends AnyRelations,\n\tTSchema extends V1.TablesRelationalConfig,\n> extends PgEffectSession<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'EffectPgSession';\n\n\tprivate logger: Logger;\n\tprivate cache: EffectCache | undefined;\n\n\tconstructor(\n\t\tprivate client: PgClient,\n\t\tdialect: PgDialect,\n\t\tprotected relations: TRelations,\n\t\tprotected schema: V1.RelationalSchemaConfig<TSchema> | undefined,\n\t\toptions: { logger?: Logger; cache?: EffectCache } = {},\n\t\tprivate wrap: Effect.Adapter = <T>(t: T): T => t,\n\t\tprivate nestedIndex = 0,\n\t) {\n\t\tsuper(dialect);\n\t\tthis.logger = options.logger ?? new NoopLogger();\n\t\tthis.cache = options.cache;\n\t}\n\n\tprepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tisResponseInArrayMode: boolean,\n\t\tcustomResultMapper?: (rows: unknown[][]) => T['execute'],\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t\tcacheConfig?: WithCacheConfig,\n\t) {\n\t\treturn new EffectPgPreparedQuery(\n\t\t\tthis.client,\n\t\t\tquery.sql,\n\t\t\tquery.params,\n\t\t\tthis.logger,\n\t\t\tthis.cache,\n\t\t\tqueryMetadata,\n\t\t\tcacheConfig,\n\t\t\tfields,\n\t\t\tname,\n\t\t\tisResponseInArrayMode,\n\t\t\tcustomResultMapper,\n\t\t\tfalse,\n\t\t\tthis.wrap,\n\t\t);\n\t}\n\n\tprepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tcustomResultMapper?: (rows: Record<string, unknown>[]) => T['execute'],\n\t): EffectPgPreparedQuery<T, true> {\n\t\treturn new EffectPgPreparedQuery<T, true>(\n\t\t\tthis.client,\n\t\t\tquery.sql,\n\t\t\tquery.params,\n\t\t\tthis.logger,\n\t\t\tthis.cache,\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfields,\n\t\t\tname,\n\t\t\tfalse,\n\t\t\tcustomResultMapper,\n\t\t\ttrue,\n\t\t\tthis.wrap,\n\t\t);\n\t}\n\n\toverride execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\treturn this.prepareQuery<PreparedQueryConfig & { execute: T }>(\n\t\t\tthis.dialect.sqlToQuery(query),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfalse,\n\t\t).execute();\n\t}\n\n\toverride all<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\treturn this.prepareQuery<PreparedQueryConfig & { all: T }>(\n\t\t\tthis.dialect.sqlToQuery(query),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfalse,\n\t\t).all();\n\t}\n\n\toverride transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: EffectPgTransaction<\n\t\t\t\tTQueryResult,\n\t\t\t\tTFullSchema,\n\t\t\t\tTRelations,\n\t\t\t\tTSchema\n\t\t\t>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never> {\n\t\tconst { dialect, relations, schema, client, logger, cache, nestedIndex } = this;\n\t\tconst sp = `sp${this.nestedIndex}`;\n\n\t\treturn this.client.withTransaction(Effect.gen(function*(txAdapter) {\n\t\t\tconst session = new EffectPgSession(\n\t\t\t\tclient,\n\t\t\t\tdialect,\n\t\t\t\trelations,\n\t\t\t\tschema,\n\t\t\t\t{ logger, cache },\n\t\t\t\ttxAdapter,\n\t\t\t\tnestedIndex + 1,\n\t\t\t);\n\n\t\t\tconst tx = new EffectPgTransaction<TQueryResult, TFullSchema, TRelations, TSchema>(\n\t\t\t\tdialect,\n\t\t\t\tsession,\n\t\t\t\trelations,\n\t\t\t\tschema,\n\t\t\t\tnestedIndex + 1,\n\t\t\t);\n\n\t\t\tif (nestedIndex) yield* tx.execute(sql.raw(`savepoint ${sp}`));\n\n\t\t\tconst res = yield* transaction(tx).pipe(Effect.catchAll((e) =>\n\t\t\t\tEffect.gen(function*() {\n\t\t\t\t\tif (nestedIndex) yield* (tx.execute(sql.raw(`rollback to savepoint ${sp}`)));\n\t\t\t\t\treturn yield* Effect.fail(e);\n\t\t\t\t})\n\t\t\t));\n\t\t\tif (nestedIndex) yield* (tx.execute(sql.raw(`release savepoint ${sp}`)));\n\n\t\t\treturn res;\n\t\t}));\n\t}\n}\n\nexport class EffectPgTransaction<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTRelations extends AnyRelations,\n\tTSchema extends V1.TablesRelationalConfig,\n> extends PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'EffectPgTransaction';\n\n\toverride transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never> {\n\t\treturn this.session.transaction(transaction);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAuB;AAIvB,oBAA2B;AAC3B,oBAAiE;AACjE,oBAAwC;AAExC,qBAA4E;AAK5E,iBAAsC;AACtC,mBAA6B;AAKtB,MAAM,8BACJ,qCACT;AAAA,EAGC,YACS,QACA,aACA,QACA,QACR,OACA,eAIA,aACQ,QACR,MACQ,wBACA,oBAGA,cACA,OAAuB,CAAI,MAAY,GAC9C;AACD,UAAM,EAAE,KAAK,aAAa,OAAO,GAAG,OAAO,eAAe,WAAW;AAnB7D;AACA;AACA;AACA;AAOA;AAEA;AACA;AAGA;AACA;AAAA,EAGT;AAAA,EAvBA,QAA0B,wBAAU,IAAY;AAAA,EAyBhD,QAAQ,mBAA6F;AACpG,QAAI,KAAK,aAAc,QAAO,KAAK,aAAa,iBAAiB;AAEjE,UAAM,EAAE,OAAO,QAAQ,oBAAoB,QAAQ,qBAAqB,OAAO,IAAI;AACnF,UAAM,aAAS,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sql-pg/PgClient';\nimport { Effect } from 'effect';\nimport type * as V1 from '~/_relations.ts';\nimport type { EffectCache } from '~/cache/core/cache-effect.ts';\nimport type { WithCacheConfig } from '~/cache/core/types.ts';\nimport { entityKind } from '~/entity.ts';\nimport { DrizzleQueryError, type TransactionRollbackError } from '~/errors.ts';\nimport { type Logger, NoopLogger } from '~/logger.ts';\nimport type { PgDialect } from '~/pg-core/dialect.ts';\nimport { PgEffectPreparedQuery, PgEffectSession, PgEffectTransaction } from '~/pg-core/effect/session.ts';\nimport type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.types.ts';\nimport type { PgQueryResultHKT, PreparedQueryConfig } from '~/pg-core/session.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport type { Query, SQL } from '~/sql/sql.ts';\nimport { fillPlaceholders } from '~/sql/sql.ts';\nimport { type Assume, mapResultRow } from '~/utils.ts';\n\nexport interface EffectPgQueryResultHKT extends PgQueryResultHKT {\n\ttype: readonly Assume<this['row'], object>[];\n}\n\nexport class EffectPgPreparedQuery<T extends PreparedQueryConfig, TIsRqbV2 extends boolean = false>\n\textends PgEffectPreparedQuery<T>\n{\n\tstatic override readonly [entityKind]: string = 'EffectPgPreparedQuery';\n\n\tconstructor(\n\t\tprivate client: PgClient,\n\t\tprivate queryString: string,\n\t\tprivate params: unknown[],\n\t\tprivate logger: Logger,\n\t\tcache: EffectCache | undefined,\n\t\tqueryMetadata: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t} | undefined,\n\t\tcacheConfig: WithCacheConfig | undefined,\n\t\tprivate fields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tprivate _isResponseInArrayMode: boolean,\n\t\tprivate customResultMapper?: (\n\t\t\trows: TIsRqbV2 extends true ? Record<string, unknown>[] : unknown[][],\n\t\t) => T['execute'],\n\t\tprivate isRqbV2Query?: TIsRqbV2,\n\t) {\n\t\tsuper({ sql: queryString, params }, cache, queryMetadata, cacheConfig);\n\t}\n\n\texecute(placeholderValues?: Record<string, unknown>): Effect.Effect<T['execute'], DrizzleQueryError> {\n\t\tif (this.isRqbV2Query) return this.executeRqbV2(placeholderValues);\n\n\t\tconst { query, logger, customResultMapper, fields, joinsNotNullableMap, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\t\tlogger.logQuery(query.sql, params);\n\n\t\tif (!fields && !customResultMapper) {\n\t\t\treturn this.queryWithCache(\n\t\t\t\tquery.sql,\n\t\t\t\tparams,\n\t\t\t\tthis.client.unsafe(query.sql, params as any).withoutTransform,\n\t\t\t);\n\t\t}\n\n\t\treturn this.queryWithCache(\n\t\t\tquery.sql,\n\t\t\tparams,\n\t\t\tclient.unsafe(query.sql, params as any).values.pipe(Effect.andThen(\n\t\t\t\t(rows) => {\n\t\t\t\t\tif (customResultMapper) return (customResultMapper as (rows: unknown[][]) => unknown)(rows as unknown[][]);\n\n\t\t\t\t\treturn rows.map((row) =>\n\t\t\t\t\t\tmapResultRow(\n\t\t\t\t\t\t\tfields!,\n\t\t\t\t\t\t\trow as unknown[],\n\t\t\t\t\t\t\tjoinsNotNullableMap,\n\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t},\n\t\t\t)),\n\t\t);\n\t}\n\n\tprivate executeRqbV2(\n\t\tplaceholderValues?: Record<string, unknown>,\n\t): Effect.Effect<T['execute'], DrizzleQueryError> {\n\t\tconst { query, logger, customResultMapper, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\n\t\tlogger.logQuery(query.sql, params);\n\t\treturn (\n\t\t\tclient.unsafe(query.sql, params as any).withoutTransform.pipe(\n\t\t\t\tEffect.andThen((v) =>\n\t\t\t\t\t(customResultMapper as (\n\t\t\t\t\t\trows: Record<string, unknown>[],\n\t\t\t\t\t\tmapColumnValue?: (value: unknown) => unknown,\n\t\t\t\t\t) => unknown)(v as Record<string, unknown>[])\n\t\t\t\t),\n\t\t\t).pipe(Effect.catchAll((e) => {\n\t\t\t\t// eslint-disable-next-line @drizzle-internal/no-instanceof\n\t\t\t\treturn Effect.fail(new DrizzleQueryError(query.sql, params, e instanceof Error ? e : undefined));\n\t\t\t}))\n\t\t);\n\t}\n\n\toverride all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError, never> {\n\t\tconst { query, logger, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\n\t\tlogger.logQuery(query.sql, params);\n\n\t\treturn this.queryWithCache(\n\t\t\tquery.sql,\n\t\t\tparams,\n\t\t\tclient.unsafe(query.sql, params as any).withoutTransform,\n\t\t);\n\t}\n\n\t/** @internal */\n\tisResponseInArrayMode(): boolean {\n\t\treturn this._isResponseInArrayMode;\n\t}\n}\n\nexport class EffectPgSession<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTRelations extends AnyRelations,\n\tTSchema extends V1.TablesRelationalConfig,\n> extends PgEffectSession<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'EffectPgSession';\n\n\tprivate logger: Logger;\n\tprivate cache: EffectCache | undefined;\n\n\tconstructor(\n\t\tprivate client: PgClient,\n\t\tdialect: PgDialect,\n\t\tprotected relations: TRelations,\n\t\tprotected schema: V1.RelationalSchemaConfig<TSchema> | undefined,\n\t\toptions: { logger?: Logger; cache?: EffectCache } = {},\n\t) {\n\t\tsuper(dialect);\n\t\tthis.logger = options.logger ?? new NoopLogger();\n\t\tthis.cache = options.cache;\n\t}\n\n\tprepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tisResponseInArrayMode: boolean,\n\t\tcustomResultMapper?: (rows: unknown[][]) => T['execute'],\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t\tcacheConfig?: WithCacheConfig,\n\t) {\n\t\treturn new EffectPgPreparedQuery(\n\t\t\tthis.client,\n\t\t\tquery.sql,\n\t\t\tquery.params,\n\t\t\tthis.logger,\n\t\t\tthis.cache,\n\t\t\tqueryMetadata,\n\t\t\tcacheConfig,\n\t\t\tfields,\n\t\t\tname,\n\t\t\tisResponseInArrayMode,\n\t\t\tcustomResultMapper,\n\t\t\tfalse,\n\t\t);\n\t}\n\n\tprepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tcustomResultMapper?: (rows: Record<string, unknown>[]) => T['execute'],\n\t): EffectPgPreparedQuery<T, true> {\n\t\treturn new EffectPgPreparedQuery<T, true>(\n\t\t\tthis.client,\n\t\t\tquery.sql,\n\t\t\tquery.params,\n\t\t\tthis.logger,\n\t\t\tthis.cache,\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfields,\n\t\t\tname,\n\t\t\tfalse,\n\t\t\tcustomResultMapper,\n\t\t\ttrue,\n\t\t);\n\t}\n\n\toverride execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\treturn this.prepareQuery<PreparedQueryConfig & { execute: T }>(\n\t\t\tthis.dialect.sqlToQuery(query),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfalse,\n\t\t).execute();\n\t}\n\n\toverride all<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\treturn this.prepareQuery<PreparedQueryConfig & { all: T }>(\n\t\t\tthis.dialect.sqlToQuery(query),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfalse,\n\t\t).all();\n\t}\n\n\toverride transaction<T>(\n\t\ttransaction: (\n\t\t\t/** Every query within transaction Effect is completed in transaction, regardless of database instance used\n\t\t\t *\n\t\t\t * `tx` argument is only required for `tx.rollback()`\n\t\t\t */\n\t\t\ttx: EffectPgTransaction<\n\t\t\t\tTQueryResult,\n\t\t\t\tTFullSchema,\n\t\t\t\tTRelations,\n\t\t\t\tTSchema\n\t\t\t>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never> {\n\t\tconst { dialect, relations, schema } = this;\n\t\tconst session = this;\n\n\t\treturn this.client.withTransaction(Effect.gen(function*() {\n\t\t\tconst tx = new EffectPgTransaction<TQueryResult, TFullSchema, TRelations, TSchema>(\n\t\t\t\tdialect,\n\t\t\t\tsession,\n\t\t\t\trelations,\n\t\t\t\tschema,\n\t\t\t);\n\n\t\t\treturn yield* transaction(tx);\n\t\t}));\n\t}\n}\n\nexport class EffectPgTransaction<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTRelations extends AnyRelations,\n\tTSchema extends V1.TablesRelationalConfig,\n> extends PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'EffectPgTransaction';\n\n\toverride transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never> {\n\t\treturn this.session.transaction(transaction);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAuB;AAIvB,oBAA2B;AAC3B,oBAAiE;AACjE,oBAAwC;AAExC,qBAA4E;AAK5E,iBAAiC;AACjC,mBAA0C;AAMnC,MAAM,8BACJ,qCACT;AAAA,EAGC,YACS,QACA,aACA,QACA,QACR,OACA,eAIA,aACQ,QACR,MACQ,wBACA,oBAGA,cACP;AACD,UAAM,EAAE,KAAK,aAAa,OAAO,GAAG,OAAO,eAAe,WAAW;AAlB7D;AACA;AACA;AACA;AAOA;AAEA;AACA;AAGA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AAA,IACD,EAAE,QAAQ;AAAA,EACX;AAAA,EAES,IAAO,OAAiD;AAChE,WAAO,KAAK;AAAA,MACX,KAAK,QAAQ,WAAW,KAAK;AAAA,MAC7B;AAAA,MACA;AAAA,MACA;AAAA,IACD,EAAE,IAAI;AAAA,EACP;AAAA,EAES,YACR,aAYwE;AACxE,UAAM,EAAE,SAAS,WAAW,OAAO,IAAI;AACvC,UAAM,UAAU;AAEhB,WAAO,KAAK,OAAO,gBAAgB,qBAAO,IAAI,aAAY;AACzD,YAAM,KAAK,IAAI;AAAA,QACd;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAEA,aAAO,OAAO,YAAY,EAAE;AAAA,IAC7B,CAAC,CAAC;AAAA,EACH;AACD;AAEO,MAAM,4BAKH,mCAAoE;AAAA,EAC7E,QAA0B,wBAAU,IAAY;AAAA,EAEvC,YACR,aAGwE;AACxE,WAAO,KAAK,QAAQ,YAAY,WAAW;AAAA,EAC5C;AACD;","names":[]}
|
|
@@ -12,8 +12,9 @@ import type { SelectedFieldsOrdered } from "../pg-core/query-builders/select.typ
|
|
|
12
12
|
import type { PgQueryResultHKT, PreparedQueryConfig } from "../pg-core/session.cjs";
|
|
13
13
|
import type { AnyRelations } from "../relations.cjs";
|
|
14
14
|
import type { Query, SQL } from "../sql/sql.cjs";
|
|
15
|
+
import { type Assume } from "../utils.cjs";
|
|
15
16
|
export interface EffectPgQueryResultHKT extends PgQueryResultHKT {
|
|
16
|
-
type: readonly object[];
|
|
17
|
+
type: readonly Assume<this['row'], object>[];
|
|
17
18
|
}
|
|
18
19
|
export declare class EffectPgPreparedQuery<T extends PreparedQueryConfig, TIsRqbV2 extends boolean = false> extends PgEffectPreparedQuery<T> {
|
|
19
20
|
private client;
|
|
@@ -24,12 +25,11 @@ export declare class EffectPgPreparedQuery<T extends PreparedQueryConfig, TIsRqb
|
|
|
24
25
|
private _isResponseInArrayMode;
|
|
25
26
|
private customResultMapper?;
|
|
26
27
|
private isRqbV2Query?;
|
|
27
|
-
private wrap;
|
|
28
28
|
static readonly [entityKind]: string;
|
|
29
29
|
constructor(client: PgClient, queryString: string, params: unknown[], logger: Logger, cache: EffectCache | undefined, queryMetadata: {
|
|
30
30
|
type: 'select' | 'update' | 'delete' | 'insert';
|
|
31
31
|
tables: string[];
|
|
32
|
-
} | undefined, cacheConfig: WithCacheConfig | undefined, fields: SelectedFieldsOrdered | undefined, name: string | undefined, _isResponseInArrayMode: boolean, customResultMapper?: ((rows: TIsRqbV2 extends true ? Record<string, unknown>[] : unknown[][]) => T["execute"]) | undefined, isRqbV2Query?: TIsRqbV2 | undefined
|
|
32
|
+
} | undefined, cacheConfig: WithCacheConfig | undefined, fields: SelectedFieldsOrdered | undefined, name: string | undefined, _isResponseInArrayMode: boolean, customResultMapper?: ((rows: TIsRqbV2 extends true ? Record<string, unknown>[] : unknown[][]) => T["execute"]) | undefined, isRqbV2Query?: TIsRqbV2 | undefined);
|
|
33
33
|
execute(placeholderValues?: Record<string, unknown>): Effect.Effect<T['execute'], DrizzleQueryError>;
|
|
34
34
|
private executeRqbV2;
|
|
35
35
|
all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError, never>;
|
|
@@ -38,15 +38,13 @@ export declare class EffectPgSession<TQueryResult extends PgQueryResultHKT, TFul
|
|
|
38
38
|
private client;
|
|
39
39
|
protected relations: TRelations;
|
|
40
40
|
protected schema: V1.RelationalSchemaConfig<TSchema> | undefined;
|
|
41
|
-
private wrap;
|
|
42
|
-
private nestedIndex;
|
|
43
41
|
static readonly [entityKind]: string;
|
|
44
42
|
private logger;
|
|
45
43
|
private cache;
|
|
46
44
|
constructor(client: PgClient, dialect: PgDialect, relations: TRelations, schema: V1.RelationalSchemaConfig<TSchema> | undefined, options?: {
|
|
47
45
|
logger?: Logger;
|
|
48
46
|
cache?: EffectCache;
|
|
49
|
-
}
|
|
47
|
+
});
|
|
50
48
|
prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(query: Query, fields: SelectedFieldsOrdered | undefined, name: string | undefined, isResponseInArrayMode: boolean, customResultMapper?: (rows: unknown[][]) => T['execute'], queryMetadata?: {
|
|
51
49
|
type: 'select' | 'update' | 'delete' | 'insert';
|
|
52
50
|
tables: string[];
|
|
@@ -54,7 +52,12 @@ export declare class EffectPgSession<TQueryResult extends PgQueryResultHKT, TFul
|
|
|
54
52
|
prepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(query: Query, fields: SelectedFieldsOrdered | undefined, name: string | undefined, customResultMapper?: (rows: Record<string, unknown>[]) => T['execute']): EffectPgPreparedQuery<T, true>;
|
|
55
53
|
execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError>;
|
|
56
54
|
all<T>(query: SQL): Effect.Effect<T, DrizzleQueryError>;
|
|
57
|
-
transaction<T>(transaction: (
|
|
55
|
+
transaction<T>(transaction: (
|
|
56
|
+
/** Every query within transaction Effect is completed in transaction, regardless of database instance used
|
|
57
|
+
*
|
|
58
|
+
* `tx` argument is only required for `tx.rollback()`
|
|
59
|
+
*/
|
|
60
|
+
tx: EffectPgTransaction<TQueryResult, TFullSchema, TRelations, TSchema>) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>;
|
|
58
61
|
}
|
|
59
62
|
export declare class EffectPgTransaction<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown>, TRelations extends AnyRelations, TSchema extends V1.TablesRelationalConfig> extends PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema> {
|
|
60
63
|
static readonly [entityKind]: string;
|
|
@@ -12,8 +12,9 @@ import type { SelectedFieldsOrdered } from "../pg-core/query-builders/select.typ
|
|
|
12
12
|
import type { PgQueryResultHKT, PreparedQueryConfig } from "../pg-core/session.js";
|
|
13
13
|
import type { AnyRelations } from "../relations.js";
|
|
14
14
|
import type { Query, SQL } from "../sql/sql.js";
|
|
15
|
+
import { type Assume } from "../utils.js";
|
|
15
16
|
export interface EffectPgQueryResultHKT extends PgQueryResultHKT {
|
|
16
|
-
type: readonly object[];
|
|
17
|
+
type: readonly Assume<this['row'], object>[];
|
|
17
18
|
}
|
|
18
19
|
export declare class EffectPgPreparedQuery<T extends PreparedQueryConfig, TIsRqbV2 extends boolean = false> extends PgEffectPreparedQuery<T> {
|
|
19
20
|
private client;
|
|
@@ -24,12 +25,11 @@ export declare class EffectPgPreparedQuery<T extends PreparedQueryConfig, TIsRqb
|
|
|
24
25
|
private _isResponseInArrayMode;
|
|
25
26
|
private customResultMapper?;
|
|
26
27
|
private isRqbV2Query?;
|
|
27
|
-
private wrap;
|
|
28
28
|
static readonly [entityKind]: string;
|
|
29
29
|
constructor(client: PgClient, queryString: string, params: unknown[], logger: Logger, cache: EffectCache | undefined, queryMetadata: {
|
|
30
30
|
type: 'select' | 'update' | 'delete' | 'insert';
|
|
31
31
|
tables: string[];
|
|
32
|
-
} | undefined, cacheConfig: WithCacheConfig | undefined, fields: SelectedFieldsOrdered | undefined, name: string | undefined, _isResponseInArrayMode: boolean, customResultMapper?: ((rows: TIsRqbV2 extends true ? Record<string, unknown>[] : unknown[][]) => T["execute"]) | undefined, isRqbV2Query?: TIsRqbV2 | undefined
|
|
32
|
+
} | undefined, cacheConfig: WithCacheConfig | undefined, fields: SelectedFieldsOrdered | undefined, name: string | undefined, _isResponseInArrayMode: boolean, customResultMapper?: ((rows: TIsRqbV2 extends true ? Record<string, unknown>[] : unknown[][]) => T["execute"]) | undefined, isRqbV2Query?: TIsRqbV2 | undefined);
|
|
33
33
|
execute(placeholderValues?: Record<string, unknown>): Effect.Effect<T['execute'], DrizzleQueryError>;
|
|
34
34
|
private executeRqbV2;
|
|
35
35
|
all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError, never>;
|
|
@@ -38,15 +38,13 @@ export declare class EffectPgSession<TQueryResult extends PgQueryResultHKT, TFul
|
|
|
38
38
|
private client;
|
|
39
39
|
protected relations: TRelations;
|
|
40
40
|
protected schema: V1.RelationalSchemaConfig<TSchema> | undefined;
|
|
41
|
-
private wrap;
|
|
42
|
-
private nestedIndex;
|
|
43
41
|
static readonly [entityKind]: string;
|
|
44
42
|
private logger;
|
|
45
43
|
private cache;
|
|
46
44
|
constructor(client: PgClient, dialect: PgDialect, relations: TRelations, schema: V1.RelationalSchemaConfig<TSchema> | undefined, options?: {
|
|
47
45
|
logger?: Logger;
|
|
48
46
|
cache?: EffectCache;
|
|
49
|
-
}
|
|
47
|
+
});
|
|
50
48
|
prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(query: Query, fields: SelectedFieldsOrdered | undefined, name: string | undefined, isResponseInArrayMode: boolean, customResultMapper?: (rows: unknown[][]) => T['execute'], queryMetadata?: {
|
|
51
49
|
type: 'select' | 'update' | 'delete' | 'insert';
|
|
52
50
|
tables: string[];
|
|
@@ -54,7 +52,12 @@ export declare class EffectPgSession<TQueryResult extends PgQueryResultHKT, TFul
|
|
|
54
52
|
prepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(query: Query, fields: SelectedFieldsOrdered | undefined, name: string | undefined, customResultMapper?: (rows: Record<string, unknown>[]) => T['execute']): EffectPgPreparedQuery<T, true>;
|
|
55
53
|
execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError>;
|
|
56
54
|
all<T>(query: SQL): Effect.Effect<T, DrizzleQueryError>;
|
|
57
|
-
transaction<T>(transaction: (
|
|
55
|
+
transaction<T>(transaction: (
|
|
56
|
+
/** Every query within transaction Effect is completed in transaction, regardless of database instance used
|
|
57
|
+
*
|
|
58
|
+
* `tx` argument is only required for `tx.rollback()`
|
|
59
|
+
*/
|
|
60
|
+
tx: EffectPgTransaction<TQueryResult, TFullSchema, TRelations, TSchema>) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>;
|
|
58
61
|
}
|
|
59
62
|
export declare class EffectPgTransaction<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown>, TRelations extends AnyRelations, TSchema extends V1.TablesRelationalConfig> extends PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema> {
|
|
60
63
|
static readonly [entityKind]: string;
|
|
@@ -3,10 +3,10 @@ import { entityKind } from "../entity.js";
|
|
|
3
3
|
import { DrizzleQueryError } from "../errors.js";
|
|
4
4
|
import { NoopLogger } from "../logger.js";
|
|
5
5
|
import { PgEffectPreparedQuery, PgEffectSession, PgEffectTransaction } from "../pg-core/effect/session.js";
|
|
6
|
-
import { fillPlaceholders
|
|
6
|
+
import { fillPlaceholders } from "../sql/sql.js";
|
|
7
7
|
import { mapResultRow } from "../utils.js";
|
|
8
8
|
class EffectPgPreparedQuery extends PgEffectPreparedQuery {
|
|
9
|
-
constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, name, _isResponseInArrayMode, customResultMapper, isRqbV2Query
|
|
9
|
+
constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, name, _isResponseInArrayMode, customResultMapper, isRqbV2Query) {
|
|
10
10
|
super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);
|
|
11
11
|
this.client = client;
|
|
12
12
|
this.queryString = queryString;
|
|
@@ -16,7 +16,6 @@ class EffectPgPreparedQuery extends PgEffectPreparedQuery {
|
|
|
16
16
|
this._isResponseInArrayMode = _isResponseInArrayMode;
|
|
17
17
|
this.customResultMapper = customResultMapper;
|
|
18
18
|
this.isRqbV2Query = isRqbV2Query;
|
|
19
|
-
this.wrap = wrap;
|
|
20
19
|
}
|
|
21
20
|
static [entityKind] = "EffectPgPreparedQuery";
|
|
22
21
|
execute(placeholderValues) {
|
|
@@ -34,35 +33,31 @@ class EffectPgPreparedQuery extends PgEffectPreparedQuery {
|
|
|
34
33
|
return this.queryWithCache(
|
|
35
34
|
query.sql,
|
|
36
35
|
params,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
))
|
|
50
|
-
)
|
|
36
|
+
client.unsafe(query.sql, params).values.pipe(Effect.andThen(
|
|
37
|
+
(rows) => {
|
|
38
|
+
if (customResultMapper) return customResultMapper(rows);
|
|
39
|
+
return rows.map(
|
|
40
|
+
(row) => mapResultRow(
|
|
41
|
+
fields,
|
|
42
|
+
row,
|
|
43
|
+
joinsNotNullableMap
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
))
|
|
51
48
|
);
|
|
52
49
|
}
|
|
53
50
|
executeRqbV2(placeholderValues) {
|
|
54
51
|
const { query, logger, customResultMapper, client } = this;
|
|
55
52
|
const params = fillPlaceholders(query.params, placeholderValues ?? {});
|
|
56
53
|
logger.logQuery(query.sql, params);
|
|
57
|
-
return
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}))
|
|
65
|
-
);
|
|
54
|
+
return client.unsafe(query.sql, params).withoutTransform.pipe(
|
|
55
|
+
Effect.andThen(
|
|
56
|
+
(v) => customResultMapper(v)
|
|
57
|
+
)
|
|
58
|
+
).pipe(Effect.catchAll((e) => {
|
|
59
|
+
return Effect.fail(new DrizzleQueryError(query.sql, params, e instanceof Error ? e : void 0));
|
|
60
|
+
}));
|
|
66
61
|
}
|
|
67
62
|
all(placeholderValues) {
|
|
68
63
|
const { query, logger, client } = this;
|
|
@@ -71,7 +66,7 @@ class EffectPgPreparedQuery extends PgEffectPreparedQuery {
|
|
|
71
66
|
return this.queryWithCache(
|
|
72
67
|
query.sql,
|
|
73
68
|
params,
|
|
74
|
-
|
|
69
|
+
client.unsafe(query.sql, params).withoutTransform
|
|
75
70
|
);
|
|
76
71
|
}
|
|
77
72
|
/** @internal */
|
|
@@ -80,13 +75,11 @@ class EffectPgPreparedQuery extends PgEffectPreparedQuery {
|
|
|
80
75
|
}
|
|
81
76
|
}
|
|
82
77
|
class EffectPgSession extends PgEffectSession {
|
|
83
|
-
constructor(client, dialect, relations, schema, options = {}
|
|
78
|
+
constructor(client, dialect, relations, schema, options = {}) {
|
|
84
79
|
super(dialect);
|
|
85
80
|
this.client = client;
|
|
86
81
|
this.relations = relations;
|
|
87
82
|
this.schema = schema;
|
|
88
|
-
this.wrap = wrap;
|
|
89
|
-
this.nestedIndex = nestedIndex;
|
|
90
83
|
this.logger = options.logger ?? new NoopLogger();
|
|
91
84
|
this.cache = options.cache;
|
|
92
85
|
}
|
|
@@ -106,8 +99,7 @@ class EffectPgSession extends PgEffectSession {
|
|
|
106
99
|
name,
|
|
107
100
|
isResponseInArrayMode,
|
|
108
101
|
customResultMapper,
|
|
109
|
-
false
|
|
110
|
-
this.wrap
|
|
102
|
+
false
|
|
111
103
|
);
|
|
112
104
|
}
|
|
113
105
|
prepareRelationalQuery(query, fields, name, customResultMapper) {
|
|
@@ -123,8 +115,7 @@ class EffectPgSession extends PgEffectSession {
|
|
|
123
115
|
name,
|
|
124
116
|
false,
|
|
125
117
|
customResultMapper,
|
|
126
|
-
true
|
|
127
|
-
this.wrap
|
|
118
|
+
true
|
|
128
119
|
);
|
|
129
120
|
}
|
|
130
121
|
execute(query) {
|
|
@@ -144,34 +135,16 @@ class EffectPgSession extends PgEffectSession {
|
|
|
144
135
|
).all();
|
|
145
136
|
}
|
|
146
137
|
transaction(transaction) {
|
|
147
|
-
const { dialect, relations, schema
|
|
148
|
-
const
|
|
149
|
-
return this.client.withTransaction(Effect.gen(function* (
|
|
150
|
-
const session = new EffectPgSession(
|
|
151
|
-
client,
|
|
152
|
-
dialect,
|
|
153
|
-
relations,
|
|
154
|
-
schema,
|
|
155
|
-
{ logger, cache },
|
|
156
|
-
txAdapter,
|
|
157
|
-
nestedIndex + 1
|
|
158
|
-
);
|
|
138
|
+
const { dialect, relations, schema } = this;
|
|
139
|
+
const session = this;
|
|
140
|
+
return this.client.withTransaction(Effect.gen(function* () {
|
|
159
141
|
const tx = new EffectPgTransaction(
|
|
160
142
|
dialect,
|
|
161
143
|
session,
|
|
162
144
|
relations,
|
|
163
|
-
schema
|
|
164
|
-
nestedIndex + 1
|
|
145
|
+
schema
|
|
165
146
|
);
|
|
166
|
-
|
|
167
|
-
const res = yield* transaction(tx).pipe(Effect.catchAll(
|
|
168
|
-
(e) => Effect.gen(function* () {
|
|
169
|
-
if (nestedIndex) yield* tx.execute(sql.raw(`rollback to savepoint ${sp}`));
|
|
170
|
-
return yield* Effect.fail(e);
|
|
171
|
-
})
|
|
172
|
-
));
|
|
173
|
-
if (nestedIndex) yield* tx.execute(sql.raw(`release savepoint ${sp}`));
|
|
174
|
-
return res;
|
|
147
|
+
return yield* transaction(tx);
|
|
175
148
|
}));
|
|
176
149
|
}
|
|
177
150
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/effect-postgres/session.ts"],"sourcesContent":["import type { PgClient } from '@effect/sql-pg/PgClient';\nimport { Effect } from 'effect';\nimport type * as V1 from '~/_relations.ts';\nimport type { EffectCache } from '~/cache/core/cache-effect.ts';\nimport type { WithCacheConfig } from '~/cache/core/types.ts';\nimport { entityKind } from '~/entity.ts';\nimport { DrizzleQueryError, type TransactionRollbackError } from '~/errors.ts';\nimport { type Logger, NoopLogger } from '~/logger.ts';\nimport type { PgDialect } from '~/pg-core/dialect.ts';\nimport { PgEffectPreparedQuery, PgEffectSession, PgEffectTransaction } from '~/pg-core/effect/session.ts';\nimport type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.types.ts';\nimport type { PgQueryResultHKT, PreparedQueryConfig } from '~/pg-core/session.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport type { Query, SQL } from '~/sql/sql.ts';\nimport { fillPlaceholders, sql } from '~/sql/sql.ts';\nimport { mapResultRow } from '~/utils.ts';\nexport interface EffectPgQueryResultHKT extends PgQueryResultHKT {\n\ttype: readonly object[];\n}\n\nexport class EffectPgPreparedQuery<T extends PreparedQueryConfig, TIsRqbV2 extends boolean = false>\n\textends PgEffectPreparedQuery<T>\n{\n\tstatic override readonly [entityKind]: string = 'EffectPgPreparedQuery';\n\n\tconstructor(\n\t\tprivate client: PgClient,\n\t\tprivate queryString: string,\n\t\tprivate params: unknown[],\n\t\tprivate logger: Logger,\n\t\tcache: EffectCache | undefined,\n\t\tqueryMetadata: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t} | undefined,\n\t\tcacheConfig: WithCacheConfig | undefined,\n\t\tprivate fields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tprivate _isResponseInArrayMode: boolean,\n\t\tprivate customResultMapper?: (\n\t\t\trows: TIsRqbV2 extends true ? Record<string, unknown>[] : unknown[][],\n\t\t) => T['execute'],\n\t\tprivate isRqbV2Query?: TIsRqbV2,\n\t\tprivate wrap: Effect.Adapter = <T>(t: T): T => t,\n\t) {\n\t\tsuper({ sql: queryString, params }, cache, queryMetadata, cacheConfig);\n\t}\n\n\texecute(placeholderValues?: Record<string, unknown>): Effect.Effect<T['execute'], DrizzleQueryError> {\n\t\tif (this.isRqbV2Query) return this.executeRqbV2(placeholderValues);\n\n\t\tconst { query, logger, customResultMapper, fields, joinsNotNullableMap, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\t\tlogger.logQuery(query.sql, params);\n\n\t\tif (!fields && !customResultMapper) {\n\t\t\treturn this.queryWithCache(\n\t\t\t\tquery.sql,\n\t\t\t\tparams,\n\t\t\t\tthis.client.unsafe(query.sql, params as any).withoutTransform,\n\t\t\t);\n\t\t}\n\n\t\treturn this.queryWithCache(\n\t\t\tquery.sql,\n\t\t\tparams,\n\t\t\tthis.wrap(\n\t\t\t\tclient.unsafe(query.sql, params as any).values.pipe(Effect.andThen(\n\t\t\t\t\t(rows) => {\n\t\t\t\t\t\tif (customResultMapper) return (customResultMapper as (rows: unknown[][]) => unknown)(rows as unknown[][]);\n\n\t\t\t\t\t\treturn rows.map((row) =>\n\t\t\t\t\t\t\tmapResultRow(\n\t\t\t\t\t\t\t\tfields!,\n\t\t\t\t\t\t\t\trow as unknown[],\n\t\t\t\t\t\t\t\tjoinsNotNullableMap,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t);\n\t\t\t\t\t},\n\t\t\t\t)),\n\t\t\t),\n\t\t);\n\t}\n\n\tprivate executeRqbV2(\n\t\tplaceholderValues?: Record<string, unknown>,\n\t): Effect.Effect<T['execute'], DrizzleQueryError> {\n\t\tconst { query, logger, customResultMapper, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\n\t\tlogger.logQuery(query.sql, params);\n\t\treturn this.wrap(\n\t\t\tclient.unsafe(query.sql, params as any).withoutTransform.pipe(\n\t\t\t\tEffect.andThen((v) =>\n\t\t\t\t\t(customResultMapper as (\n\t\t\t\t\t\trows: Record<string, unknown>[],\n\t\t\t\t\t\tmapColumnValue?: (value: unknown) => unknown,\n\t\t\t\t\t) => unknown)(v as Record<string, unknown>[])\n\t\t\t\t),\n\t\t\t).pipe(Effect.catchAll((e) => {\n\t\t\t\t// eslint-disable-next-line @drizzle-internal/no-instanceof\n\t\t\t\treturn Effect.fail(new DrizzleQueryError(query.sql, params, e instanceof Error ? e : undefined));\n\t\t\t})),\n\t\t);\n\t}\n\n\toverride all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError, never> {\n\t\tconst { query, logger, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\n\t\tlogger.logQuery(query.sql, params);\n\n\t\treturn this.queryWithCache(\n\t\t\tquery.sql,\n\t\t\tparams,\n\t\t\tthis.wrap(client.unsafe(query.sql, params as any).withoutTransform),\n\t\t);\n\t}\n\n\t/** @internal */\n\tisResponseInArrayMode(): boolean {\n\t\treturn this._isResponseInArrayMode;\n\t}\n}\n\nexport class EffectPgSession<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTRelations extends AnyRelations,\n\tTSchema extends V1.TablesRelationalConfig,\n> extends PgEffectSession<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'EffectPgSession';\n\n\tprivate logger: Logger;\n\tprivate cache: EffectCache | undefined;\n\n\tconstructor(\n\t\tprivate client: PgClient,\n\t\tdialect: PgDialect,\n\t\tprotected relations: TRelations,\n\t\tprotected schema: V1.RelationalSchemaConfig<TSchema> | undefined,\n\t\toptions: { logger?: Logger; cache?: EffectCache } = {},\n\t\tprivate wrap: Effect.Adapter = <T>(t: T): T => t,\n\t\tprivate nestedIndex = 0,\n\t) {\n\t\tsuper(dialect);\n\t\tthis.logger = options.logger ?? new NoopLogger();\n\t\tthis.cache = options.cache;\n\t}\n\n\tprepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tisResponseInArrayMode: boolean,\n\t\tcustomResultMapper?: (rows: unknown[][]) => T['execute'],\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t\tcacheConfig?: WithCacheConfig,\n\t) {\n\t\treturn new EffectPgPreparedQuery(\n\t\t\tthis.client,\n\t\t\tquery.sql,\n\t\t\tquery.params,\n\t\t\tthis.logger,\n\t\t\tthis.cache,\n\t\t\tqueryMetadata,\n\t\t\tcacheConfig,\n\t\t\tfields,\n\t\t\tname,\n\t\t\tisResponseInArrayMode,\n\t\t\tcustomResultMapper,\n\t\t\tfalse,\n\t\t\tthis.wrap,\n\t\t);\n\t}\n\n\tprepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tcustomResultMapper?: (rows: Record<string, unknown>[]) => T['execute'],\n\t): EffectPgPreparedQuery<T, true> {\n\t\treturn new EffectPgPreparedQuery<T, true>(\n\t\t\tthis.client,\n\t\t\tquery.sql,\n\t\t\tquery.params,\n\t\t\tthis.logger,\n\t\t\tthis.cache,\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfields,\n\t\t\tname,\n\t\t\tfalse,\n\t\t\tcustomResultMapper,\n\t\t\ttrue,\n\t\t\tthis.wrap,\n\t\t);\n\t}\n\n\toverride execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\treturn this.prepareQuery<PreparedQueryConfig & { execute: T }>(\n\t\t\tthis.dialect.sqlToQuery(query),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfalse,\n\t\t).execute();\n\t}\n\n\toverride all<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\treturn this.prepareQuery<PreparedQueryConfig & { all: T }>(\n\t\t\tthis.dialect.sqlToQuery(query),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfalse,\n\t\t).all();\n\t}\n\n\toverride transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: EffectPgTransaction<\n\t\t\t\tTQueryResult,\n\t\t\t\tTFullSchema,\n\t\t\t\tTRelations,\n\t\t\t\tTSchema\n\t\t\t>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never> {\n\t\tconst { dialect, relations, schema, client, logger, cache, nestedIndex } = this;\n\t\tconst sp = `sp${this.nestedIndex}`;\n\n\t\treturn this.client.withTransaction(Effect.gen(function*(txAdapter) {\n\t\t\tconst session = new EffectPgSession(\n\t\t\t\tclient,\n\t\t\t\tdialect,\n\t\t\t\trelations,\n\t\t\t\tschema,\n\t\t\t\t{ logger, cache },\n\t\t\t\ttxAdapter,\n\t\t\t\tnestedIndex + 1,\n\t\t\t);\n\n\t\t\tconst tx = new EffectPgTransaction<TQueryResult, TFullSchema, TRelations, TSchema>(\n\t\t\t\tdialect,\n\t\t\t\tsession,\n\t\t\t\trelations,\n\t\t\t\tschema,\n\t\t\t\tnestedIndex + 1,\n\t\t\t);\n\n\t\t\tif (nestedIndex) yield* tx.execute(sql.raw(`savepoint ${sp}`));\n\n\t\t\tconst res = yield* transaction(tx).pipe(Effect.catchAll((e) =>\n\t\t\t\tEffect.gen(function*() {\n\t\t\t\t\tif (nestedIndex) yield* (tx.execute(sql.raw(`rollback to savepoint ${sp}`)));\n\t\t\t\t\treturn yield* Effect.fail(e);\n\t\t\t\t})\n\t\t\t));\n\t\t\tif (nestedIndex) yield* (tx.execute(sql.raw(`release savepoint ${sp}`)));\n\n\t\t\treturn res;\n\t\t}));\n\t}\n}\n\nexport class EffectPgTransaction<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTRelations extends AnyRelations,\n\tTSchema extends V1.TablesRelationalConfig,\n> extends PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'EffectPgTransaction';\n\n\toverride transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never> {\n\t\treturn this.session.transaction(transaction);\n\t}\n}\n"],"mappings":"AACA,SAAS,cAAc;AAIvB,SAAS,kBAAkB;AAC3B,SAAS,yBAAwD;AACjE,SAAsB,kBAAkB;AAExC,SAAS,uBAAuB,iBAAiB,2BAA2B;AAK5E,SAAS,kBAAkB,WAAW;AACtC,SAAS,oBAAoB;AAKtB,MAAM,8BACJ,sBACT;AAAA,EAGC,YACS,QACA,aACA,QACA,QACR,OACA,eAIA,aACQ,QACR,MACQ,wBACA,oBAGA,cACA,OAAuB,CAAI,MAAY,GAC9C;AACD,UAAM,EAAE,KAAK,aAAa,OAAO,GAAG,OAAO,eAAe,WAAW;AAnB7D;AAC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AAAA,EACH;AACD;AAEO,MAAM,4BAKH,oBAAoE;AAAA,EAC7E,QAA0B,UAAU,IAAY;AAAA,EAEvC,YACR,aAGwE;AACxE,WAAO,KAAK,QAAQ,YAAY,WAAW;AAAA,EAC5C;AACD;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/effect-postgres/session.ts"],"sourcesContent":["import type { PgClient } from '@effect/sql-pg/PgClient';\nimport { Effect } from 'effect';\nimport type * as V1 from '~/_relations.ts';\nimport type { EffectCache } from '~/cache/core/cache-effect.ts';\nimport type { WithCacheConfig } from '~/cache/core/types.ts';\nimport { entityKind } from '~/entity.ts';\nimport { DrizzleQueryError, type TransactionRollbackError } from '~/errors.ts';\nimport { type Logger, NoopLogger } from '~/logger.ts';\nimport type { PgDialect } from '~/pg-core/dialect.ts';\nimport { PgEffectPreparedQuery, PgEffectSession, PgEffectTransaction } from '~/pg-core/effect/session.ts';\nimport type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.types.ts';\nimport type { PgQueryResultHKT, PreparedQueryConfig } from '~/pg-core/session.ts';\nimport type { AnyRelations } from '~/relations.ts';\nimport type { Query, SQL } from '~/sql/sql.ts';\nimport { fillPlaceholders } from '~/sql/sql.ts';\nimport { type Assume, mapResultRow } from '~/utils.ts';\n\nexport interface EffectPgQueryResultHKT extends PgQueryResultHKT {\n\ttype: readonly Assume<this['row'], object>[];\n}\n\nexport class EffectPgPreparedQuery<T extends PreparedQueryConfig, TIsRqbV2 extends boolean = false>\n\textends PgEffectPreparedQuery<T>\n{\n\tstatic override readonly [entityKind]: string = 'EffectPgPreparedQuery';\n\n\tconstructor(\n\t\tprivate client: PgClient,\n\t\tprivate queryString: string,\n\t\tprivate params: unknown[],\n\t\tprivate logger: Logger,\n\t\tcache: EffectCache | undefined,\n\t\tqueryMetadata: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t} | undefined,\n\t\tcacheConfig: WithCacheConfig | undefined,\n\t\tprivate fields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tprivate _isResponseInArrayMode: boolean,\n\t\tprivate customResultMapper?: (\n\t\t\trows: TIsRqbV2 extends true ? Record<string, unknown>[] : unknown[][],\n\t\t) => T['execute'],\n\t\tprivate isRqbV2Query?: TIsRqbV2,\n\t) {\n\t\tsuper({ sql: queryString, params }, cache, queryMetadata, cacheConfig);\n\t}\n\n\texecute(placeholderValues?: Record<string, unknown>): Effect.Effect<T['execute'], DrizzleQueryError> {\n\t\tif (this.isRqbV2Query) return this.executeRqbV2(placeholderValues);\n\n\t\tconst { query, logger, customResultMapper, fields, joinsNotNullableMap, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\t\tlogger.logQuery(query.sql, params);\n\n\t\tif (!fields && !customResultMapper) {\n\t\t\treturn this.queryWithCache(\n\t\t\t\tquery.sql,\n\t\t\t\tparams,\n\t\t\t\tthis.client.unsafe(query.sql, params as any).withoutTransform,\n\t\t\t);\n\t\t}\n\n\t\treturn this.queryWithCache(\n\t\t\tquery.sql,\n\t\t\tparams,\n\t\t\tclient.unsafe(query.sql, params as any).values.pipe(Effect.andThen(\n\t\t\t\t(rows) => {\n\t\t\t\t\tif (customResultMapper) return (customResultMapper as (rows: unknown[][]) => unknown)(rows as unknown[][]);\n\n\t\t\t\t\treturn rows.map((row) =>\n\t\t\t\t\t\tmapResultRow(\n\t\t\t\t\t\t\tfields!,\n\t\t\t\t\t\t\trow as unknown[],\n\t\t\t\t\t\t\tjoinsNotNullableMap,\n\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t},\n\t\t\t)),\n\t\t);\n\t}\n\n\tprivate executeRqbV2(\n\t\tplaceholderValues?: Record<string, unknown>,\n\t): Effect.Effect<T['execute'], DrizzleQueryError> {\n\t\tconst { query, logger, customResultMapper, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\n\t\tlogger.logQuery(query.sql, params);\n\t\treturn (\n\t\t\tclient.unsafe(query.sql, params as any).withoutTransform.pipe(\n\t\t\t\tEffect.andThen((v) =>\n\t\t\t\t\t(customResultMapper as (\n\t\t\t\t\t\trows: Record<string, unknown>[],\n\t\t\t\t\t\tmapColumnValue?: (value: unknown) => unknown,\n\t\t\t\t\t) => unknown)(v as Record<string, unknown>[])\n\t\t\t\t),\n\t\t\t).pipe(Effect.catchAll((e) => {\n\t\t\t\t// eslint-disable-next-line @drizzle-internal/no-instanceof\n\t\t\t\treturn Effect.fail(new DrizzleQueryError(query.sql, params, e instanceof Error ? e : undefined));\n\t\t\t}))\n\t\t);\n\t}\n\n\toverride all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError, never> {\n\t\tconst { query, logger, client } = this;\n\t\tconst params = fillPlaceholders(query.params, placeholderValues ?? {});\n\n\t\tlogger.logQuery(query.sql, params);\n\n\t\treturn this.queryWithCache(\n\t\t\tquery.sql,\n\t\t\tparams,\n\t\t\tclient.unsafe(query.sql, params as any).withoutTransform,\n\t\t);\n\t}\n\n\t/** @internal */\n\tisResponseInArrayMode(): boolean {\n\t\treturn this._isResponseInArrayMode;\n\t}\n}\n\nexport class EffectPgSession<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTRelations extends AnyRelations,\n\tTSchema extends V1.TablesRelationalConfig,\n> extends PgEffectSession<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'EffectPgSession';\n\n\tprivate logger: Logger;\n\tprivate cache: EffectCache | undefined;\n\n\tconstructor(\n\t\tprivate client: PgClient,\n\t\tdialect: PgDialect,\n\t\tprotected relations: TRelations,\n\t\tprotected schema: V1.RelationalSchemaConfig<TSchema> | undefined,\n\t\toptions: { logger?: Logger; cache?: EffectCache } = {},\n\t) {\n\t\tsuper(dialect);\n\t\tthis.logger = options.logger ?? new NoopLogger();\n\t\tthis.cache = options.cache;\n\t}\n\n\tprepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tisResponseInArrayMode: boolean,\n\t\tcustomResultMapper?: (rows: unknown[][]) => T['execute'],\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t\tcacheConfig?: WithCacheConfig,\n\t) {\n\t\treturn new EffectPgPreparedQuery(\n\t\t\tthis.client,\n\t\t\tquery.sql,\n\t\t\tquery.params,\n\t\t\tthis.logger,\n\t\t\tthis.cache,\n\t\t\tqueryMetadata,\n\t\t\tcacheConfig,\n\t\t\tfields,\n\t\t\tname,\n\t\t\tisResponseInArrayMode,\n\t\t\tcustomResultMapper,\n\t\t\tfalse,\n\t\t);\n\t}\n\n\tprepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tcustomResultMapper?: (rows: Record<string, unknown>[]) => T['execute'],\n\t): EffectPgPreparedQuery<T, true> {\n\t\treturn new EffectPgPreparedQuery<T, true>(\n\t\t\tthis.client,\n\t\t\tquery.sql,\n\t\t\tquery.params,\n\t\t\tthis.logger,\n\t\t\tthis.cache,\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfields,\n\t\t\tname,\n\t\t\tfalse,\n\t\t\tcustomResultMapper,\n\t\t\ttrue,\n\t\t);\n\t}\n\n\toverride execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\treturn this.prepareQuery<PreparedQueryConfig & { execute: T }>(\n\t\t\tthis.dialect.sqlToQuery(query),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfalse,\n\t\t).execute();\n\t}\n\n\toverride all<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\treturn this.prepareQuery<PreparedQueryConfig & { all: T }>(\n\t\t\tthis.dialect.sqlToQuery(query),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tfalse,\n\t\t).all();\n\t}\n\n\toverride transaction<T>(\n\t\ttransaction: (\n\t\t\t/** Every query within transaction Effect is completed in transaction, regardless of database instance used\n\t\t\t *\n\t\t\t * `tx` argument is only required for `tx.rollback()`\n\t\t\t */\n\t\t\ttx: EffectPgTransaction<\n\t\t\t\tTQueryResult,\n\t\t\t\tTFullSchema,\n\t\t\t\tTRelations,\n\t\t\t\tTSchema\n\t\t\t>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never> {\n\t\tconst { dialect, relations, schema } = this;\n\t\tconst session = this;\n\n\t\treturn this.client.withTransaction(Effect.gen(function*() {\n\t\t\tconst tx = new EffectPgTransaction<TQueryResult, TFullSchema, TRelations, TSchema>(\n\t\t\t\tdialect,\n\t\t\t\tsession,\n\t\t\t\trelations,\n\t\t\t\tschema,\n\t\t\t);\n\n\t\t\treturn yield* transaction(tx);\n\t\t}));\n\t}\n}\n\nexport class EffectPgTransaction<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTRelations extends AnyRelations,\n\tTSchema extends V1.TablesRelationalConfig,\n> extends PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'EffectPgTransaction';\n\n\toverride transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError, never> {\n\t\treturn this.session.transaction(transaction);\n\t}\n}\n"],"mappings":"AACA,SAAS,cAAc;AAIvB,SAAS,kBAAkB;AAC3B,SAAS,yBAAwD;AACjE,SAAsB,kBAAkB;AAExC,SAAS,uBAAuB,iBAAiB,2BAA2B;AAK5E,SAAS,wBAAwB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AA,MACL,KAAK;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAAA,EAES,QAAW,OAAiD;AACpE,WAAO,KAAK;AAAA,MACX,KAAK,QAAQ,WAAW,KAAK;AAAA,MAC7B;AAAA,MACA;AAAA,MACA;AAAA,IACD,EAAE,QAAQ;AAAA,EACX;AAAA,EAES,IAAO,OAAiD;AAChE,WAAO,KAAK;AAAA,MACX,KAAK,QAAQ,WAAW,KAAK;AAAA,MAC7B;AAAA,MACA;AAAA,MACA;AAAA,IACD,EAAE,IAAI;AAAA,EACP;AAAA,EAES,YACR,aAYwE;AACxE,UAAM,EAAE,SAAS,WAAW,OAAO,IAAI;AACvC,UAAM,UAAU;AAEhB,WAAO,KAAK,OAAO,gBAAgB,OAAO,IAAI,aAAY;AACzD,YAAM,KAAK,IAAI;AAAA,QACd;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAEA,aAAO,OAAO,YAAY,EAAE;AAAA,IAC7B,CAAC,CAAC;AAAA,EACH;AACD;AAEO,MAAM,4BAKH,oBAAoE;AAAA,EAC7E,QAA0B,UAAU,IAAY;AAAA,EAEvC,YACR,aAGwE;AACxE,WAAO,KAAK,QAAQ,YAAY,WAAW;AAAA,EAC5C;AACD;","names":[]}
|
package/package.json
CHANGED
|
@@ -107,9 +107,9 @@ class PgEffectTransaction extends import_db.PgEffectDatabase {
|
|
|
107
107
|
this.nestedIndex = nestedIndex;
|
|
108
108
|
}
|
|
109
109
|
static [import_entity.entityKind] = "PgEffectTransaction";
|
|
110
|
-
rollback() {
|
|
111
|
-
|
|
112
|
-
}
|
|
110
|
+
rollback = function* () {
|
|
111
|
+
yield* import_effect.Effect.fail(new import_errors.TransactionRollbackError());
|
|
112
|
+
};
|
|
113
113
|
/** @internal */
|
|
114
114
|
getTransactionConfigSQL(config) {
|
|
115
115
|
const chunks = [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/pg-core/effect/session.ts"],"sourcesContent":["import type { SqlError } from '@effect/sql/SqlError';\nimport { Effect } from 'effect';\nimport type * as V1 from '~/_relations.ts';\nimport type { EffectCache } from '~/cache/core/cache-effect.ts';\nimport { strategyFor } from '~/cache/core/cache.ts';\nimport type { WithCacheConfig } from '~/cache/core/types.ts';\nimport { entityKind } from '~/entity.ts';\nimport { DrizzleQueryError, TransactionRollbackError } from '~/errors.ts';\nimport type { MigrationConfig, MigrationMeta, MigratorInitFailResponse } from '~/migrator.ts';\nimport type { AnyRelations, EmptyRelations } from '~/relations.ts';\nimport { type Query, type SQL, sql } from '~/sql/sql.ts';\nimport { assertUnreachable } from '~/utils.ts';\nimport type { PgDialect } from '../dialect.ts';\nimport type { SelectedFieldsOrdered } from '../query-builders/select.types.ts';\nimport {\n\tPgBasePreparedQuery,\n\ttype PgQueryResultHKT,\n\tPgSession,\n\ttype PgTransactionConfig,\n\ttype PreparedQueryConfig,\n} from '../session.ts';\nimport { PgEffectDatabase } from './db.ts';\n\nexport abstract class PgEffectPreparedQuery<T extends PreparedQueryConfig> extends PgBasePreparedQuery {\n\tstatic override readonly [entityKind]: string = 'PgEffectPreparedQuery';\n\n\tconstructor(\n\t\tquery: Query,\n\t\tprivate cache: EffectCache | undefined,\n\t\tprivate queryMetadata: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t} | undefined,\n\t\tprivate cacheConfig?: WithCacheConfig,\n\t) {\n\t\tsuper(query);\n\n\t\tif (cache && cache.strategy() === 'all' && cacheConfig === undefined) {\n\t\t\tthis.cacheConfig = { enabled: true, autoInvalidate: true };\n\t\t}\n\t\tif (!this.cacheConfig?.enabled) {\n\t\t\tthis.cacheConfig = undefined;\n\t\t}\n\t}\n\n\tprotected override queryWithCache<T>(\n\t\tqueryString: string,\n\t\tparams: any[],\n\t\tquery: Effect.Effect<T, SqlError>,\n\t): Effect.Effect<T, DrizzleQueryError> {\n\t\tconst { cache, cacheConfig, queryMetadata } = this;\n\t\treturn Effect.gen(function*() {\n\t\t\tconst cacheStrat: Awaited<ReturnType<typeof strategyFor>> = cache\n\t\t\t\t? yield* Effect.tryPromise(\n\t\t\t\t\t() => strategyFor(queryString, params, queryMetadata, cacheConfig),\n\t\t\t\t)\n\t\t\t\t: { type: 'skip' as const };\n\n\t\t\tif (cacheStrat.type === 'skip') {\n\t\t\t\treturn yield* query;\n\t\t\t}\n\n\t\t\t// For mutate queries, we should query the database, wait for a response, and then perform invalidation\n\t\t\tif (cacheStrat.type === 'invalidate') {\n\t\t\t\tconst result = yield* query;\n\t\t\t\tyield* cache!.onMutate({ tables: cacheStrat.tables });\n\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tif (cacheStrat.type === 'try') {\n\t\t\t\tconst { tables, key, isTag, autoInvalidate, config } = cacheStrat;\n\t\t\t\tconst fromCache: any[] | undefined = yield* cache!.get(\n\t\t\t\t\tkey,\n\t\t\t\t\ttables,\n\t\t\t\t\tisTag,\n\t\t\t\t\tautoInvalidate,\n\t\t\t\t);\n\n\t\t\t\tif (typeof fromCache !== 'undefined') return fromCache as unknown as T;\n\n\t\t\t\tconst result = yield* query;\n\n\t\t\t\tyield* cache!.put(\n\t\t\t\t\tkey,\n\t\t\t\t\tresult,\n\t\t\t\t\tautoInvalidate ? tables : [],\n\t\t\t\t\tisTag,\n\t\t\t\t\tconfig,\n\t\t\t\t);\n\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tassertUnreachable(cacheStrat);\n\t\t}).pipe(Effect.catchAll((e) => {\n\t\t\t// eslint-disable-next-line @drizzle-internal/no-instanceof\n\t\t\treturn Effect.fail(new DrizzleQueryError(queryString, params, e instanceof Error ? e : undefined));\n\t\t}));\n\t}\n\n\tabstract override execute(\n\t\tplaceholderValues?: Record<string, unknown>,\n\t): Effect.Effect<T['execute'], DrizzleQueryError>;\n\n\t/** @internal */\n\tabstract override all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError>;\n}\n\nexport abstract class PgEffectSession<\n\tTQueryResult extends PgQueryResultHKT = PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTRelations extends AnyRelations = EmptyRelations,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> extends PgSession {\n\tstatic override readonly [entityKind]: string = 'PgEffectSession';\n\n\tconstructor(dialect: PgDialect) {\n\t\tsuper(dialect);\n\t}\n\n\tabstract override prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tisResponseInArrayMode: boolean,\n\t\tcustomResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => T['execute'],\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t\tcacheConfig?: WithCacheConfig,\n\t): PgEffectPreparedQuery<T>;\n\n\tabstract override prepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tcustomResultMapper: (\n\t\t\trows: Record<string, unknown>[],\n\t\t\tmapColumnValue?: (value: unknown) => unknown,\n\t\t) => T['execute'],\n\t): PgEffectPreparedQuery<T>;\n\n\toverride execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\tconst { sql, params } = this.dialect.sqlToQuery(query);\n\t\treturn this.prepareQuery<PreparedQueryConfig & { execute: T }>({ sql, params }, undefined, undefined, false)\n\t\t\t.execute();\n\t}\n\n\toverride all<T>(query: SQL): Effect.Effect<T[], DrizzleQueryError> {\n\t\tconst { sql, params } = this.dialect.sqlToQuery(query);\n\t\treturn this.prepareQuery<PreparedQueryConfig & { all: T[] }>({ sql, params }, undefined, undefined, false)\n\t\t\t.all();\n\t}\n\n\tabstract transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;\n}\n\nexport abstract class PgEffectTransaction<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTRelations extends AnyRelations = EmptyRelations,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> extends PgEffectDatabase<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'PgEffectTransaction';\n\n\tconstructor(\n\t\tdialect: PgDialect,\n\t\tsession: PgEffectSession<any, any, any, any>,\n\t\tprotected relations: TRelations,\n\t\tprotected schema: {\n\t\t\tfullSchema: Record<string, unknown>;\n\t\t\tschema: TSchema;\n\t\t\ttableNamesMap: Record<string, string>;\n\t\t} | undefined,\n\t\tprotected readonly nestedIndex = 0,\n\t\tparseRqbJson?: boolean,\n\t) {\n\t\tsuper(dialect, session, relations, schema, parseRqbJson);\n\t}\n\n\trollback(): never {\n\t\tthrow new TransactionRollbackError();\n\t}\n\n\t/** @internal */\n\tgetTransactionConfigSQL(config: PgTransactionConfig): SQL {\n\t\tconst chunks: string[] = [];\n\t\tif (config.isolationLevel) {\n\t\t\tchunks.push(`isolation level ${config.isolationLevel}`);\n\t\t}\n\t\tif (config.accessMode) {\n\t\t\tchunks.push(config.accessMode);\n\t\t}\n\t\tif (typeof config.deferrable === 'boolean') {\n\t\t\tchunks.push(config.deferrable ? 'deferrable' : 'not deferrable');\n\t\t}\n\t\treturn sql.raw(chunks.join(' '));\n\t}\n\n\tsetTransaction(config: PgTransactionConfig): Effect.Effect<void, DrizzleQueryError> {\n\t\treturn this.session.execute(sql`set transaction ${this.getTransactionConfigSQL(config)}`);\n\t}\n\n\tabstract override transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;\n}\n\nexport function migrate(\n\tmigrations: MigrationMeta[],\n\tsession: PgEffectSession,\n\tconfig: string | MigrationConfig,\n): Effect.Effect<void | MigratorInitFailResponse, DrizzleQueryError, never> {\n\treturn Effect.gen(function*() {\n\t\tconst migrationsTable = typeof config === 'string'\n\t\t\t? '__drizzle_migrations'\n\t\t\t: config.migrationsTable ?? '__drizzle_migrations';\n\t\tconst migrationsSchema = typeof config === 'string' ? 'drizzle' : config.migrationsSchema ?? 'drizzle';\n\t\tconst migrationTableCreate = sql`\n\t\t\tCREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsSchema)}.${sql.identifier(migrationsTable)} (\n\t\t\t\tid SERIAL PRIMARY KEY,\n\t\t\t\thash text NOT NULL,\n\t\t\t\tcreated_at bigint\n\t\t\t)\n\t\t`;\n\t\tyield* session.execute(sql`CREATE SCHEMA IF NOT EXISTS ${sql.identifier(migrationsSchema)}`);\n\t\tyield* session.execute(migrationTableCreate);\n\n\t\tconst dbMigrations = yield* session.all<{ id: number; hash: string; created_at: string }>(\n\t\t\tsql`select id, hash, created_at from ${sql.identifier(migrationsSchema)}.${\n\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t} order by created_at desc limit 1`,\n\t\t);\n\n\t\tif (typeof config === 'object' && config.init) {\n\t\t\tif (dbMigrations.length) {\n\t\t\t\treturn { exitCode: 'databaseMigrations' as const };\n\t\t\t}\n\n\t\t\tif (migrations.length > 1) {\n\t\t\t\treturn { exitCode: 'localMigrations' as const };\n\t\t\t}\n\n\t\t\tconst [migration] = migrations;\n\n\t\t\tif (!migration) return;\n\n\t\t\tyield* session.execute(\n\t\t\t\tsql`insert into ${sql.identifier(migrationsSchema)}.${\n\t\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t\t} (\"hash\", \"created_at\") values(${migration.hash}, ${migration.folderMillis})`,\n\t\t\t);\n\n\t\t\treturn;\n\t\t}\n\n\t\tconst lastDbMigration = dbMigrations[0];\n\t\tyield* session.transaction((tx) =>\n\t\t\tEffect.gen(function*() {\n\t\t\t\tfor (const migration of migrations) {\n\t\t\t\t\tif (\n\t\t\t\t\t\t!lastDbMigration\n\t\t\t\t\t\t|| Number(lastDbMigration.created_at) < migration.folderMillis\n\t\t\t\t\t) {\n\t\t\t\t\t\tfor (const stmt of migration.sql) {\n\t\t\t\t\t\t\tyield* tx.execute(sql.raw(stmt));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tyield* tx.execute(\n\t\t\t\t\t\t\tsql`insert into ${sql.identifier(migrationsSchema)}.${\n\t\t\t\t\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t\t\t\t\t} (\"hash\", \"created_at\") values(${migration.hash}, ${migration.folderMillis})`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t);\n\n\t\treturn;\n\t}) as Effect.Effect<void | MigratorInitFailResponse, DrizzleQueryError, never>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAuB;AAGvB,mBAA4B;AAE5B,oBAA2B;AAC3B,oBAA4D;AAG5D,iBAA0C;AAC1C,mBAAkC;AAGlC,qBAMO;AACP,gBAAiC;AAE1B,MAAe,8BAA6D,mCAAoB;AAAA,EAGtG,YACC,OACQ,OACA,eAIA,aACP;AACD,UAAM,KAAK;AAPH;AACA;AAIA;AAIR,QAAI,SAAS,MAAM,SAAS,MAAM,SAAS,gBAAgB,QAAW;AACrE,WAAK,cAAc,EAAE,SAAS,MAAM,gBAAgB,KAAK;AAAA,IAC1D;AACA,QAAI,CAAC,KAAK,aAAa,SAAS;AAC/B,WAAK,cAAc;AAAA,IACpB;AAAA,EACD;AAAA,E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D,CAAC;AACF;","names":["sql"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/pg-core/effect/session.ts"],"sourcesContent":["import type { SqlError } from '@effect/sql/SqlError';\nimport { Effect } from 'effect';\nimport type * as V1 from '~/_relations.ts';\nimport type { EffectCache } from '~/cache/core/cache-effect.ts';\nimport { strategyFor } from '~/cache/core/cache.ts';\nimport type { WithCacheConfig } from '~/cache/core/types.ts';\nimport { entityKind } from '~/entity.ts';\nimport { DrizzleQueryError, TransactionRollbackError } from '~/errors.ts';\nimport type { MigrationConfig, MigrationMeta, MigratorInitFailResponse } from '~/migrator.ts';\nimport type { AnyRelations, EmptyRelations } from '~/relations.ts';\nimport { type Query, type SQL, sql } from '~/sql/sql.ts';\nimport { assertUnreachable } from '~/utils.ts';\nimport type { PgDialect } from '../dialect.ts';\nimport type { SelectedFieldsOrdered } from '../query-builders/select.types.ts';\nimport {\n\tPgBasePreparedQuery,\n\ttype PgQueryResultHKT,\n\tPgSession,\n\ttype PgTransactionConfig,\n\ttype PreparedQueryConfig,\n} from '../session.ts';\nimport { PgEffectDatabase } from './db.ts';\n\nexport abstract class PgEffectPreparedQuery<T extends PreparedQueryConfig> extends PgBasePreparedQuery {\n\tstatic override readonly [entityKind]: string = 'PgEffectPreparedQuery';\n\n\tconstructor(\n\t\tquery: Query,\n\t\tprivate cache: EffectCache | undefined,\n\t\tprivate queryMetadata: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t} | undefined,\n\t\tprivate cacheConfig?: WithCacheConfig,\n\t) {\n\t\tsuper(query);\n\n\t\tif (cache && cache.strategy() === 'all' && cacheConfig === undefined) {\n\t\t\tthis.cacheConfig = { enabled: true, autoInvalidate: true };\n\t\t}\n\t\tif (!this.cacheConfig?.enabled) {\n\t\t\tthis.cacheConfig = undefined;\n\t\t}\n\t}\n\n\tprotected override queryWithCache<T>(\n\t\tqueryString: string,\n\t\tparams: any[],\n\t\tquery: Effect.Effect<T, SqlError>,\n\t): Effect.Effect<T, DrizzleQueryError> {\n\t\tconst { cache, cacheConfig, queryMetadata } = this;\n\t\treturn Effect.gen(function*() {\n\t\t\tconst cacheStrat: Awaited<ReturnType<typeof strategyFor>> = cache\n\t\t\t\t? yield* Effect.tryPromise(\n\t\t\t\t\t() => strategyFor(queryString, params, queryMetadata, cacheConfig),\n\t\t\t\t)\n\t\t\t\t: { type: 'skip' as const };\n\n\t\t\tif (cacheStrat.type === 'skip') {\n\t\t\t\treturn yield* query;\n\t\t\t}\n\n\t\t\t// For mutate queries, we should query the database, wait for a response, and then perform invalidation\n\t\t\tif (cacheStrat.type === 'invalidate') {\n\t\t\t\tconst result = yield* query;\n\t\t\t\tyield* cache!.onMutate({ tables: cacheStrat.tables });\n\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tif (cacheStrat.type === 'try') {\n\t\t\t\tconst { tables, key, isTag, autoInvalidate, config } = cacheStrat;\n\t\t\t\tconst fromCache: any[] | undefined = yield* cache!.get(\n\t\t\t\t\tkey,\n\t\t\t\t\ttables,\n\t\t\t\t\tisTag,\n\t\t\t\t\tautoInvalidate,\n\t\t\t\t);\n\n\t\t\t\tif (typeof fromCache !== 'undefined') return fromCache as unknown as T;\n\n\t\t\t\tconst result = yield* query;\n\n\t\t\t\tyield* cache!.put(\n\t\t\t\t\tkey,\n\t\t\t\t\tresult,\n\t\t\t\t\tautoInvalidate ? tables : [],\n\t\t\t\t\tisTag,\n\t\t\t\t\tconfig,\n\t\t\t\t);\n\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tassertUnreachable(cacheStrat);\n\t\t}).pipe(Effect.catchAll((e) => {\n\t\t\t// eslint-disable-next-line @drizzle-internal/no-instanceof\n\t\t\treturn Effect.fail(new DrizzleQueryError(queryString, params, e instanceof Error ? e : undefined));\n\t\t}));\n\t}\n\n\tabstract override execute(\n\t\tplaceholderValues?: Record<string, unknown>,\n\t): Effect.Effect<T['execute'], DrizzleQueryError>;\n\n\t/** @internal */\n\tabstract override all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError>;\n}\n\nexport abstract class PgEffectSession<\n\tTQueryResult extends PgQueryResultHKT = PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTRelations extends AnyRelations = EmptyRelations,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> extends PgSession {\n\tstatic override readonly [entityKind]: string = 'PgEffectSession';\n\n\tconstructor(dialect: PgDialect) {\n\t\tsuper(dialect);\n\t}\n\n\tabstract override prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tisResponseInArrayMode: boolean,\n\t\tcustomResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => T['execute'],\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t\tcacheConfig?: WithCacheConfig,\n\t): PgEffectPreparedQuery<T>;\n\n\tabstract override prepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tcustomResultMapper: (\n\t\t\trows: Record<string, unknown>[],\n\t\t\tmapColumnValue?: (value: unknown) => unknown,\n\t\t) => T['execute'],\n\t): PgEffectPreparedQuery<T>;\n\n\toverride execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\tconst { sql, params } = this.dialect.sqlToQuery(query);\n\t\treturn this.prepareQuery<PreparedQueryConfig & { execute: T }>({ sql, params }, undefined, undefined, false)\n\t\t\t.execute();\n\t}\n\n\toverride all<T>(query: SQL): Effect.Effect<T[], DrizzleQueryError> {\n\t\tconst { sql, params } = this.dialect.sqlToQuery(query);\n\t\treturn this.prepareQuery<PreparedQueryConfig & { all: T[] }>({ sql, params }, undefined, undefined, false)\n\t\t\t.all();\n\t}\n\n\tabstract transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;\n}\n\nexport abstract class PgEffectTransaction<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTRelations extends AnyRelations = EmptyRelations,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> extends PgEffectDatabase<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'PgEffectTransaction';\n\n\tconstructor(\n\t\tdialect: PgDialect,\n\t\tsession: PgEffectSession<any, any, any, any>,\n\t\tprotected relations: TRelations,\n\t\tprotected schema: {\n\t\t\tfullSchema: Record<string, unknown>;\n\t\t\tschema: TSchema;\n\t\t\ttableNamesMap: Record<string, string>;\n\t\t} | undefined,\n\t\tprotected readonly nestedIndex = 0,\n\t\tparseRqbJson?: boolean,\n\t) {\n\t\tsuper(dialect, session, relations, schema, parseRqbJson);\n\t}\n\n\trollback = function*() {\n\t\tyield* Effect.fail(new TransactionRollbackError());\n\t};\n\n\t/** @internal */\n\tgetTransactionConfigSQL(config: PgTransactionConfig): SQL {\n\t\tconst chunks: string[] = [];\n\t\tif (config.isolationLevel) {\n\t\t\tchunks.push(`isolation level ${config.isolationLevel}`);\n\t\t}\n\t\tif (config.accessMode) {\n\t\t\tchunks.push(config.accessMode);\n\t\t}\n\t\tif (typeof config.deferrable === 'boolean') {\n\t\t\tchunks.push(config.deferrable ? 'deferrable' : 'not deferrable');\n\t\t}\n\t\treturn sql.raw(chunks.join(' '));\n\t}\n\n\tsetTransaction(config: PgTransactionConfig): Effect.Effect<void, DrizzleQueryError> {\n\t\treturn this.session.execute(sql`set transaction ${this.getTransactionConfigSQL(config)}`);\n\t}\n\n\tabstract override transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;\n}\n\nexport function migrate(\n\tmigrations: MigrationMeta[],\n\tsession: PgEffectSession,\n\tconfig: string | MigrationConfig,\n): Effect.Effect<void | MigratorInitFailResponse, DrizzleQueryError, never> {\n\treturn Effect.gen(function*() {\n\t\tconst migrationsTable = typeof config === 'string'\n\t\t\t? '__drizzle_migrations'\n\t\t\t: config.migrationsTable ?? '__drizzle_migrations';\n\t\tconst migrationsSchema = typeof config === 'string' ? 'drizzle' : config.migrationsSchema ?? 'drizzle';\n\t\tconst migrationTableCreate = sql`\n\t\t\tCREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsSchema)}.${sql.identifier(migrationsTable)} (\n\t\t\t\tid SERIAL PRIMARY KEY,\n\t\t\t\thash text NOT NULL,\n\t\t\t\tcreated_at bigint\n\t\t\t)\n\t\t`;\n\t\tyield* session.execute(sql`CREATE SCHEMA IF NOT EXISTS ${sql.identifier(migrationsSchema)}`);\n\t\tyield* session.execute(migrationTableCreate);\n\n\t\tconst dbMigrations = yield* session.all<{ id: number; hash: string; created_at: string }>(\n\t\t\tsql`select id, hash, created_at from ${sql.identifier(migrationsSchema)}.${\n\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t} order by created_at desc limit 1`,\n\t\t);\n\n\t\tif (typeof config === 'object' && config.init) {\n\t\t\tif (dbMigrations.length) {\n\t\t\t\treturn { exitCode: 'databaseMigrations' as const };\n\t\t\t}\n\n\t\t\tif (migrations.length > 1) {\n\t\t\t\treturn { exitCode: 'localMigrations' as const };\n\t\t\t}\n\n\t\t\tconst [migration] = migrations;\n\n\t\t\tif (!migration) return;\n\n\t\t\tyield* session.execute(\n\t\t\t\tsql`insert into ${sql.identifier(migrationsSchema)}.${\n\t\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t\t} (\"hash\", \"created_at\") values(${migration.hash}, ${migration.folderMillis})`,\n\t\t\t);\n\n\t\t\treturn;\n\t\t}\n\n\t\tconst lastDbMigration = dbMigrations[0];\n\t\tyield* session.transaction((tx) =>\n\t\t\tEffect.gen(function*() {\n\t\t\t\tfor (const migration of migrations) {\n\t\t\t\t\tif (\n\t\t\t\t\t\t!lastDbMigration\n\t\t\t\t\t\t|| Number(lastDbMigration.created_at) < migration.folderMillis\n\t\t\t\t\t) {\n\t\t\t\t\t\tfor (const stmt of migration.sql) {\n\t\t\t\t\t\t\tyield* tx.execute(sql.raw(stmt));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tyield* tx.execute(\n\t\t\t\t\t\t\tsql`insert into ${sql.identifier(migrationsSchema)}.${\n\t\t\t\t\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t\t\t\t\t} (\"hash\", \"created_at\") values(${migration.hash}, ${migration.folderMillis})`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t);\n\n\t\treturn;\n\t}) as Effect.Effect<void | MigratorInitFailResponse, DrizzleQueryError, never>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAuB;AAGvB,mBAA4B;AAE5B,oBAA2B;AAC3B,oBAA4D;AAG5D,iBAA0C;AAC1C,mBAAkC;AAGlC,qBAMO;AACP,gBAAiC;AAE1B,MAAe,8BAA6D,mCAAoB;AAAA,EAGtG,YACC,OACQ,OACA,eAIA,aACP;AACD,UAAM,KAAK;AAPH;AACA;AAIA;AAIR,QAAI,SAAS,MAAM,SAAS,MAAM,SAAS,gBAAgB,QAAW;AACrE,WAAK,cAAc,EAAE,SAAS,MAAM,gBAAgB,KAAK;AAAA,IAC1D;AACA,QAAI,CAAC,KAAK,aAAa,SAAS;AAC/B,WAAK,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A,UACD;AAAA,QACD;AAAA,MACD,CAAC;AAAA,IACF;AAEA;AAAA,EACD,CAAC;AACF;","names":["sql"]}
|
|
@@ -50,7 +50,7 @@ export declare abstract class PgEffectTransaction<TQueryResult extends PgQueryRe
|
|
|
50
50
|
schema: TSchema;
|
|
51
51
|
tableNamesMap: Record<string, string>;
|
|
52
52
|
} | undefined, nestedIndex?: number, parseRqbJson?: boolean);
|
|
53
|
-
rollback()
|
|
53
|
+
rollback: () => Generator<import("effect/Utils").YieldWrap<Effect.Effect<never, TransactionRollbackError, never>>, void, any>;
|
|
54
54
|
setTransaction(config: PgTransactionConfig): Effect.Effect<void, DrizzleQueryError>;
|
|
55
55
|
abstract transaction<T>(transaction: (tx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;
|
|
56
56
|
}
|
|
@@ -50,7 +50,7 @@ export declare abstract class PgEffectTransaction<TQueryResult extends PgQueryRe
|
|
|
50
50
|
schema: TSchema;
|
|
51
51
|
tableNamesMap: Record<string, string>;
|
|
52
52
|
} | undefined, nestedIndex?: number, parseRqbJson?: boolean);
|
|
53
|
-
rollback()
|
|
53
|
+
rollback: () => Generator<import("effect/Utils").YieldWrap<Effect.Effect<never, TransactionRollbackError, never>>, void, any>;
|
|
54
54
|
setTransaction(config: PgTransactionConfig): Effect.Effect<void, DrizzleQueryError>;
|
|
55
55
|
abstract transaction<T>(transaction: (tx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;
|
|
56
56
|
}
|
|
@@ -84,9 +84,9 @@ class PgEffectTransaction extends PgEffectDatabase {
|
|
|
84
84
|
this.nestedIndex = nestedIndex;
|
|
85
85
|
}
|
|
86
86
|
static [entityKind] = "PgEffectTransaction";
|
|
87
|
-
rollback() {
|
|
88
|
-
|
|
89
|
-
}
|
|
87
|
+
rollback = function* () {
|
|
88
|
+
yield* Effect.fail(new TransactionRollbackError());
|
|
89
|
+
};
|
|
90
90
|
/** @internal */
|
|
91
91
|
getTransactionConfigSQL(config) {
|
|
92
92
|
const chunks = [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/pg-core/effect/session.ts"],"sourcesContent":["import type { SqlError } from '@effect/sql/SqlError';\nimport { Effect } from 'effect';\nimport type * as V1 from '~/_relations.ts';\nimport type { EffectCache } from '~/cache/core/cache-effect.ts';\nimport { strategyFor } from '~/cache/core/cache.ts';\nimport type { WithCacheConfig } from '~/cache/core/types.ts';\nimport { entityKind } from '~/entity.ts';\nimport { DrizzleQueryError, TransactionRollbackError } from '~/errors.ts';\nimport type { MigrationConfig, MigrationMeta, MigratorInitFailResponse } from '~/migrator.ts';\nimport type { AnyRelations, EmptyRelations } from '~/relations.ts';\nimport { type Query, type SQL, sql } from '~/sql/sql.ts';\nimport { assertUnreachable } from '~/utils.ts';\nimport type { PgDialect } from '../dialect.ts';\nimport type { SelectedFieldsOrdered } from '../query-builders/select.types.ts';\nimport {\n\tPgBasePreparedQuery,\n\ttype PgQueryResultHKT,\n\tPgSession,\n\ttype PgTransactionConfig,\n\ttype PreparedQueryConfig,\n} from '../session.ts';\nimport { PgEffectDatabase } from './db.ts';\n\nexport abstract class PgEffectPreparedQuery<T extends PreparedQueryConfig> extends PgBasePreparedQuery {\n\tstatic override readonly [entityKind]: string = 'PgEffectPreparedQuery';\n\n\tconstructor(\n\t\tquery: Query,\n\t\tprivate cache: EffectCache | undefined,\n\t\tprivate queryMetadata: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t} | undefined,\n\t\tprivate cacheConfig?: WithCacheConfig,\n\t) {\n\t\tsuper(query);\n\n\t\tif (cache && cache.strategy() === 'all' && cacheConfig === undefined) {\n\t\t\tthis.cacheConfig = { enabled: true, autoInvalidate: true };\n\t\t}\n\t\tif (!this.cacheConfig?.enabled) {\n\t\t\tthis.cacheConfig = undefined;\n\t\t}\n\t}\n\n\tprotected override queryWithCache<T>(\n\t\tqueryString: string,\n\t\tparams: any[],\n\t\tquery: Effect.Effect<T, SqlError>,\n\t): Effect.Effect<T, DrizzleQueryError> {\n\t\tconst { cache, cacheConfig, queryMetadata } = this;\n\t\treturn Effect.gen(function*() {\n\t\t\tconst cacheStrat: Awaited<ReturnType<typeof strategyFor>> = cache\n\t\t\t\t? yield* Effect.tryPromise(\n\t\t\t\t\t() => strategyFor(queryString, params, queryMetadata, cacheConfig),\n\t\t\t\t)\n\t\t\t\t: { type: 'skip' as const };\n\n\t\t\tif (cacheStrat.type === 'skip') {\n\t\t\t\treturn yield* query;\n\t\t\t}\n\n\t\t\t// For mutate queries, we should query the database, wait for a response, and then perform invalidation\n\t\t\tif (cacheStrat.type === 'invalidate') {\n\t\t\t\tconst result = yield* query;\n\t\t\t\tyield* cache!.onMutate({ tables: cacheStrat.tables });\n\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tif (cacheStrat.type === 'try') {\n\t\t\t\tconst { tables, key, isTag, autoInvalidate, config } = cacheStrat;\n\t\t\t\tconst fromCache: any[] | undefined = yield* cache!.get(\n\t\t\t\t\tkey,\n\t\t\t\t\ttables,\n\t\t\t\t\tisTag,\n\t\t\t\t\tautoInvalidate,\n\t\t\t\t);\n\n\t\t\t\tif (typeof fromCache !== 'undefined') return fromCache as unknown as T;\n\n\t\t\t\tconst result = yield* query;\n\n\t\t\t\tyield* cache!.put(\n\t\t\t\t\tkey,\n\t\t\t\t\tresult,\n\t\t\t\t\tautoInvalidate ? tables : [],\n\t\t\t\t\tisTag,\n\t\t\t\t\tconfig,\n\t\t\t\t);\n\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tassertUnreachable(cacheStrat);\n\t\t}).pipe(Effect.catchAll((e) => {\n\t\t\t// eslint-disable-next-line @drizzle-internal/no-instanceof\n\t\t\treturn Effect.fail(new DrizzleQueryError(queryString, params, e instanceof Error ? e : undefined));\n\t\t}));\n\t}\n\n\tabstract override execute(\n\t\tplaceholderValues?: Record<string, unknown>,\n\t): Effect.Effect<T['execute'], DrizzleQueryError>;\n\n\t/** @internal */\n\tabstract override all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError>;\n}\n\nexport abstract class PgEffectSession<\n\tTQueryResult extends PgQueryResultHKT = PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTRelations extends AnyRelations = EmptyRelations,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> extends PgSession {\n\tstatic override readonly [entityKind]: string = 'PgEffectSession';\n\n\tconstructor(dialect: PgDialect) {\n\t\tsuper(dialect);\n\t}\n\n\tabstract override prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tisResponseInArrayMode: boolean,\n\t\tcustomResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => T['execute'],\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t\tcacheConfig?: WithCacheConfig,\n\t): PgEffectPreparedQuery<T>;\n\n\tabstract override prepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tcustomResultMapper: (\n\t\t\trows: Record<string, unknown>[],\n\t\t\tmapColumnValue?: (value: unknown) => unknown,\n\t\t) => T['execute'],\n\t): PgEffectPreparedQuery<T>;\n\n\toverride execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\tconst { sql, params } = this.dialect.sqlToQuery(query);\n\t\treturn this.prepareQuery<PreparedQueryConfig & { execute: T }>({ sql, params }, undefined, undefined, false)\n\t\t\t.execute();\n\t}\n\n\toverride all<T>(query: SQL): Effect.Effect<T[], DrizzleQueryError> {\n\t\tconst { sql, params } = this.dialect.sqlToQuery(query);\n\t\treturn this.prepareQuery<PreparedQueryConfig & { all: T[] }>({ sql, params }, undefined, undefined, false)\n\t\t\t.all();\n\t}\n\n\tabstract transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;\n}\n\nexport abstract class PgEffectTransaction<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTRelations extends AnyRelations = EmptyRelations,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> extends PgEffectDatabase<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'PgEffectTransaction';\n\n\tconstructor(\n\t\tdialect: PgDialect,\n\t\tsession: PgEffectSession<any, any, any, any>,\n\t\tprotected relations: TRelations,\n\t\tprotected schema: {\n\t\t\tfullSchema: Record<string, unknown>;\n\t\t\tschema: TSchema;\n\t\t\ttableNamesMap: Record<string, string>;\n\t\t} | undefined,\n\t\tprotected readonly nestedIndex = 0,\n\t\tparseRqbJson?: boolean,\n\t) {\n\t\tsuper(dialect, session, relations, schema, parseRqbJson);\n\t}\n\n\trollback(): never {\n\t\tthrow new TransactionRollbackError();\n\t}\n\n\t/** @internal */\n\tgetTransactionConfigSQL(config: PgTransactionConfig): SQL {\n\t\tconst chunks: string[] = [];\n\t\tif (config.isolationLevel) {\n\t\t\tchunks.push(`isolation level ${config.isolationLevel}`);\n\t\t}\n\t\tif (config.accessMode) {\n\t\t\tchunks.push(config.accessMode);\n\t\t}\n\t\tif (typeof config.deferrable === 'boolean') {\n\t\t\tchunks.push(config.deferrable ? 'deferrable' : 'not deferrable');\n\t\t}\n\t\treturn sql.raw(chunks.join(' '));\n\t}\n\n\tsetTransaction(config: PgTransactionConfig): Effect.Effect<void, DrizzleQueryError> {\n\t\treturn this.session.execute(sql`set transaction ${this.getTransactionConfigSQL(config)}`);\n\t}\n\n\tabstract override transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;\n}\n\nexport function migrate(\n\tmigrations: MigrationMeta[],\n\tsession: PgEffectSession,\n\tconfig: string | MigrationConfig,\n): Effect.Effect<void | MigratorInitFailResponse, DrizzleQueryError, never> {\n\treturn Effect.gen(function*() {\n\t\tconst migrationsTable = typeof config === 'string'\n\t\t\t? '__drizzle_migrations'\n\t\t\t: config.migrationsTable ?? '__drizzle_migrations';\n\t\tconst migrationsSchema = typeof config === 'string' ? 'drizzle' : config.migrationsSchema ?? 'drizzle';\n\t\tconst migrationTableCreate = sql`\n\t\t\tCREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsSchema)}.${sql.identifier(migrationsTable)} (\n\t\t\t\tid SERIAL PRIMARY KEY,\n\t\t\t\thash text NOT NULL,\n\t\t\t\tcreated_at bigint\n\t\t\t)\n\t\t`;\n\t\tyield* session.execute(sql`CREATE SCHEMA IF NOT EXISTS ${sql.identifier(migrationsSchema)}`);\n\t\tyield* session.execute(migrationTableCreate);\n\n\t\tconst dbMigrations = yield* session.all<{ id: number; hash: string; created_at: string }>(\n\t\t\tsql`select id, hash, created_at from ${sql.identifier(migrationsSchema)}.${\n\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t} order by created_at desc limit 1`,\n\t\t);\n\n\t\tif (typeof config === 'object' && config.init) {\n\t\t\tif (dbMigrations.length) {\n\t\t\t\treturn { exitCode: 'databaseMigrations' as const };\n\t\t\t}\n\n\t\t\tif (migrations.length > 1) {\n\t\t\t\treturn { exitCode: 'localMigrations' as const };\n\t\t\t}\n\n\t\t\tconst [migration] = migrations;\n\n\t\t\tif (!migration) return;\n\n\t\t\tyield* session.execute(\n\t\t\t\tsql`insert into ${sql.identifier(migrationsSchema)}.${\n\t\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t\t} (\"hash\", \"created_at\") values(${migration.hash}, ${migration.folderMillis})`,\n\t\t\t);\n\n\t\t\treturn;\n\t\t}\n\n\t\tconst lastDbMigration = dbMigrations[0];\n\t\tyield* session.transaction((tx) =>\n\t\t\tEffect.gen(function*() {\n\t\t\t\tfor (const migration of migrations) {\n\t\t\t\t\tif (\n\t\t\t\t\t\t!lastDbMigration\n\t\t\t\t\t\t|| Number(lastDbMigration.created_at) < migration.folderMillis\n\t\t\t\t\t) {\n\t\t\t\t\t\tfor (const stmt of migration.sql) {\n\t\t\t\t\t\t\tyield* tx.execute(sql.raw(stmt));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tyield* tx.execute(\n\t\t\t\t\t\t\tsql`insert into ${sql.identifier(migrationsSchema)}.${\n\t\t\t\t\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t\t\t\t\t} (\"hash\", \"created_at\") values(${migration.hash}, ${migration.folderMillis})`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t);\n\n\t\treturn;\n\t}) as Effect.Effect<void | MigratorInitFailResponse, DrizzleQueryError, never>;\n}\n"],"mappings":"AACA,SAAS,cAAc;AAGvB,SAAS,mBAAmB;AAE5B,SAAS,kBAAkB;AAC3B,SAAS,mBAAmB,gCAAgC;AAG5D,SAA+B,WAAW;AAC1C,SAAS,yBAAyB;AAGlC;AAAA,EACC;AAAA,EAEA;AAAA,OAGM;AACP,SAAS,wBAAwB;AAE1B,MAAe,8BAA6D,oBAAoB;AAAA,EAGtG,YACC,OACQ,OACA,eAIA,aACP;AACD,UAAM,KAAK;AAPH;AACA;AAIA;A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AA,cACT,kBAAkB,IAAI,WAAW,gBAAgB,CAAC,IACjD,IAAI,WAAW,eAAe,CAC/B,kCAAkC,UAAU,IAAI,KAAK,UAAU,YAAY;AAAA,YAC5E;AAAA,UACD;AAAA,QACD;AAAA,MACD,CAAC;AAAA,IACF;AAEA;AAAA,EACD,CAAC;AACF;","names":["sql"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/pg-core/effect/session.ts"],"sourcesContent":["import type { SqlError } from '@effect/sql/SqlError';\nimport { Effect } from 'effect';\nimport type * as V1 from '~/_relations.ts';\nimport type { EffectCache } from '~/cache/core/cache-effect.ts';\nimport { strategyFor } from '~/cache/core/cache.ts';\nimport type { WithCacheConfig } from '~/cache/core/types.ts';\nimport { entityKind } from '~/entity.ts';\nimport { DrizzleQueryError, TransactionRollbackError } from '~/errors.ts';\nimport type { MigrationConfig, MigrationMeta, MigratorInitFailResponse } from '~/migrator.ts';\nimport type { AnyRelations, EmptyRelations } from '~/relations.ts';\nimport { type Query, type SQL, sql } from '~/sql/sql.ts';\nimport { assertUnreachable } from '~/utils.ts';\nimport type { PgDialect } from '../dialect.ts';\nimport type { SelectedFieldsOrdered } from '../query-builders/select.types.ts';\nimport {\n\tPgBasePreparedQuery,\n\ttype PgQueryResultHKT,\n\tPgSession,\n\ttype PgTransactionConfig,\n\ttype PreparedQueryConfig,\n} from '../session.ts';\nimport { PgEffectDatabase } from './db.ts';\n\nexport abstract class PgEffectPreparedQuery<T extends PreparedQueryConfig> extends PgBasePreparedQuery {\n\tstatic override readonly [entityKind]: string = 'PgEffectPreparedQuery';\n\n\tconstructor(\n\t\tquery: Query,\n\t\tprivate cache: EffectCache | undefined,\n\t\tprivate queryMetadata: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t} | undefined,\n\t\tprivate cacheConfig?: WithCacheConfig,\n\t) {\n\t\tsuper(query);\n\n\t\tif (cache && cache.strategy() === 'all' && cacheConfig === undefined) {\n\t\t\tthis.cacheConfig = { enabled: true, autoInvalidate: true };\n\t\t}\n\t\tif (!this.cacheConfig?.enabled) {\n\t\t\tthis.cacheConfig = undefined;\n\t\t}\n\t}\n\n\tprotected override queryWithCache<T>(\n\t\tqueryString: string,\n\t\tparams: any[],\n\t\tquery: Effect.Effect<T, SqlError>,\n\t): Effect.Effect<T, DrizzleQueryError> {\n\t\tconst { cache, cacheConfig, queryMetadata } = this;\n\t\treturn Effect.gen(function*() {\n\t\t\tconst cacheStrat: Awaited<ReturnType<typeof strategyFor>> = cache\n\t\t\t\t? yield* Effect.tryPromise(\n\t\t\t\t\t() => strategyFor(queryString, params, queryMetadata, cacheConfig),\n\t\t\t\t)\n\t\t\t\t: { type: 'skip' as const };\n\n\t\t\tif (cacheStrat.type === 'skip') {\n\t\t\t\treturn yield* query;\n\t\t\t}\n\n\t\t\t// For mutate queries, we should query the database, wait for a response, and then perform invalidation\n\t\t\tif (cacheStrat.type === 'invalidate') {\n\t\t\t\tconst result = yield* query;\n\t\t\t\tyield* cache!.onMutate({ tables: cacheStrat.tables });\n\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tif (cacheStrat.type === 'try') {\n\t\t\t\tconst { tables, key, isTag, autoInvalidate, config } = cacheStrat;\n\t\t\t\tconst fromCache: any[] | undefined = yield* cache!.get(\n\t\t\t\t\tkey,\n\t\t\t\t\ttables,\n\t\t\t\t\tisTag,\n\t\t\t\t\tautoInvalidate,\n\t\t\t\t);\n\n\t\t\t\tif (typeof fromCache !== 'undefined') return fromCache as unknown as T;\n\n\t\t\t\tconst result = yield* query;\n\n\t\t\t\tyield* cache!.put(\n\t\t\t\t\tkey,\n\t\t\t\t\tresult,\n\t\t\t\t\tautoInvalidate ? tables : [],\n\t\t\t\t\tisTag,\n\t\t\t\t\tconfig,\n\t\t\t\t);\n\n\t\t\t\treturn result;\n\t\t\t}\n\n\t\t\tassertUnreachable(cacheStrat);\n\t\t}).pipe(Effect.catchAll((e) => {\n\t\t\t// eslint-disable-next-line @drizzle-internal/no-instanceof\n\t\t\treturn Effect.fail(new DrizzleQueryError(queryString, params, e instanceof Error ? e : undefined));\n\t\t}));\n\t}\n\n\tabstract override execute(\n\t\tplaceholderValues?: Record<string, unknown>,\n\t): Effect.Effect<T['execute'], DrizzleQueryError>;\n\n\t/** @internal */\n\tabstract override all(placeholderValues?: Record<string, unknown>): Effect.Effect<T['all'], DrizzleQueryError>;\n}\n\nexport abstract class PgEffectSession<\n\tTQueryResult extends PgQueryResultHKT = PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTRelations extends AnyRelations = EmptyRelations,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> extends PgSession {\n\tstatic override readonly [entityKind]: string = 'PgEffectSession';\n\n\tconstructor(dialect: PgDialect) {\n\t\tsuper(dialect);\n\t}\n\n\tabstract override prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tisResponseInArrayMode: boolean,\n\t\tcustomResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => T['execute'],\n\t\tqueryMetadata?: {\n\t\t\ttype: 'select' | 'update' | 'delete' | 'insert';\n\t\t\ttables: string[];\n\t\t},\n\t\tcacheConfig?: WithCacheConfig,\n\t): PgEffectPreparedQuery<T>;\n\n\tabstract override prepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(\n\t\tquery: Query,\n\t\tfields: SelectedFieldsOrdered | undefined,\n\t\tname: string | undefined,\n\t\tcustomResultMapper: (\n\t\t\trows: Record<string, unknown>[],\n\t\t\tmapColumnValue?: (value: unknown) => unknown,\n\t\t) => T['execute'],\n\t): PgEffectPreparedQuery<T>;\n\n\toverride execute<T>(query: SQL): Effect.Effect<T, DrizzleQueryError> {\n\t\tconst { sql, params } = this.dialect.sqlToQuery(query);\n\t\treturn this.prepareQuery<PreparedQueryConfig & { execute: T }>({ sql, params }, undefined, undefined, false)\n\t\t\t.execute();\n\t}\n\n\toverride all<T>(query: SQL): Effect.Effect<T[], DrizzleQueryError> {\n\t\tconst { sql, params } = this.dialect.sqlToQuery(query);\n\t\treturn this.prepareQuery<PreparedQueryConfig & { all: T[] }>({ sql, params }, undefined, undefined, false)\n\t\t\t.all();\n\t}\n\n\tabstract transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;\n}\n\nexport abstract class PgEffectTransaction<\n\tTQueryResult extends PgQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTRelations extends AnyRelations = EmptyRelations,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> extends PgEffectDatabase<TQueryResult, TFullSchema, TRelations, TSchema> {\n\tstatic override readonly [entityKind]: string = 'PgEffectTransaction';\n\n\tconstructor(\n\t\tdialect: PgDialect,\n\t\tsession: PgEffectSession<any, any, any, any>,\n\t\tprotected relations: TRelations,\n\t\tprotected schema: {\n\t\t\tfullSchema: Record<string, unknown>;\n\t\t\tschema: TSchema;\n\t\t\ttableNamesMap: Record<string, string>;\n\t\t} | undefined,\n\t\tprotected readonly nestedIndex = 0,\n\t\tparseRqbJson?: boolean,\n\t) {\n\t\tsuper(dialect, session, relations, schema, parseRqbJson);\n\t}\n\n\trollback = function*() {\n\t\tyield* Effect.fail(new TransactionRollbackError());\n\t};\n\n\t/** @internal */\n\tgetTransactionConfigSQL(config: PgTransactionConfig): SQL {\n\t\tconst chunks: string[] = [];\n\t\tif (config.isolationLevel) {\n\t\t\tchunks.push(`isolation level ${config.isolationLevel}`);\n\t\t}\n\t\tif (config.accessMode) {\n\t\t\tchunks.push(config.accessMode);\n\t\t}\n\t\tif (typeof config.deferrable === 'boolean') {\n\t\t\tchunks.push(config.deferrable ? 'deferrable' : 'not deferrable');\n\t\t}\n\t\treturn sql.raw(chunks.join(' '));\n\t}\n\n\tsetTransaction(config: PgTransactionConfig): Effect.Effect<void, DrizzleQueryError> {\n\t\treturn this.session.execute(sql`set transaction ${this.getTransactionConfigSQL(config)}`);\n\t}\n\n\tabstract override transaction<T>(\n\t\ttransaction: (\n\t\t\ttx: PgEffectTransaction<TQueryResult, TFullSchema, TRelations, TSchema>,\n\t\t) => Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>,\n\t): Effect.Effect<T, DrizzleQueryError | TransactionRollbackError>;\n}\n\nexport function migrate(\n\tmigrations: MigrationMeta[],\n\tsession: PgEffectSession,\n\tconfig: string | MigrationConfig,\n): Effect.Effect<void | MigratorInitFailResponse, DrizzleQueryError, never> {\n\treturn Effect.gen(function*() {\n\t\tconst migrationsTable = typeof config === 'string'\n\t\t\t? '__drizzle_migrations'\n\t\t\t: config.migrationsTable ?? '__drizzle_migrations';\n\t\tconst migrationsSchema = typeof config === 'string' ? 'drizzle' : config.migrationsSchema ?? 'drizzle';\n\t\tconst migrationTableCreate = sql`\n\t\t\tCREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsSchema)}.${sql.identifier(migrationsTable)} (\n\t\t\t\tid SERIAL PRIMARY KEY,\n\t\t\t\thash text NOT NULL,\n\t\t\t\tcreated_at bigint\n\t\t\t)\n\t\t`;\n\t\tyield* session.execute(sql`CREATE SCHEMA IF NOT EXISTS ${sql.identifier(migrationsSchema)}`);\n\t\tyield* session.execute(migrationTableCreate);\n\n\t\tconst dbMigrations = yield* session.all<{ id: number; hash: string; created_at: string }>(\n\t\t\tsql`select id, hash, created_at from ${sql.identifier(migrationsSchema)}.${\n\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t} order by created_at desc limit 1`,\n\t\t);\n\n\t\tif (typeof config === 'object' && config.init) {\n\t\t\tif (dbMigrations.length) {\n\t\t\t\treturn { exitCode: 'databaseMigrations' as const };\n\t\t\t}\n\n\t\t\tif (migrations.length > 1) {\n\t\t\t\treturn { exitCode: 'localMigrations' as const };\n\t\t\t}\n\n\t\t\tconst [migration] = migrations;\n\n\t\t\tif (!migration) return;\n\n\t\t\tyield* session.execute(\n\t\t\t\tsql`insert into ${sql.identifier(migrationsSchema)}.${\n\t\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t\t} (\"hash\", \"created_at\") values(${migration.hash}, ${migration.folderMillis})`,\n\t\t\t);\n\n\t\t\treturn;\n\t\t}\n\n\t\tconst lastDbMigration = dbMigrations[0];\n\t\tyield* session.transaction((tx) =>\n\t\t\tEffect.gen(function*() {\n\t\t\t\tfor (const migration of migrations) {\n\t\t\t\t\tif (\n\t\t\t\t\t\t!lastDbMigration\n\t\t\t\t\t\t|| Number(lastDbMigration.created_at) < migration.folderMillis\n\t\t\t\t\t) {\n\t\t\t\t\t\tfor (const stmt of migration.sql) {\n\t\t\t\t\t\t\tyield* tx.execute(sql.raw(stmt));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tyield* tx.execute(\n\t\t\t\t\t\t\tsql`insert into ${sql.identifier(migrationsSchema)}.${\n\t\t\t\t\t\t\t\tsql.identifier(migrationsTable)\n\t\t\t\t\t\t\t} (\"hash\", \"created_at\") values(${migration.hash}, ${migration.folderMillis})`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t);\n\n\t\treturn;\n\t}) as Effect.Effect<void | MigratorInitFailResponse, DrizzleQueryError, never>;\n}\n"],"mappings":"AACA,SAAS,cAAc;AAGvB,SAAS,mBAAmB;AAE5B,SAAS,kBAAkB;AAC3B,SAAS,mBAAmB,gCAAgC;AAG5D,SAA+B,WAAW;AAC1C,SAAS,yBAAyB;AAGlC;AAAA,EACC;AAAA,EAEA;AAAA,OAGM;AACP,SAAS,wBAAwB;AAE1B,MAAe,8BAA6D,oBAAoB;AAAA,EAGtG,YACC,OACQ,OACA,eAIA,aACP;AACD,U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