ff-effect 0.0.7 → 0.0.8

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.
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/for/drizzle/index.ts
21
+ var drizzle_exports = {};
22
+ __export(drizzle_exports, {
23
+ DrizzleError: () => DrizzleError,
24
+ TagTypeId: () => TagTypeId,
25
+ createDatabase: () => createDatabase
26
+ });
27
+ module.exports = __toCommonJS(drizzle_exports);
28
+ var import_effect = require("effect");
29
+ var TagTypeId = import_effect.Context.TagTypeId;
30
+ var DrizzleError = class extends import_effect.Data.TaggedError("ff-effect/DrizzleError") {
31
+ };
32
+ var WrappedTxError = class extends Error {
33
+ };
34
+ function createDatabase(tagId, createClient) {
35
+ class Drizzle extends import_effect.Context.Tag(tagId)() {
36
+ }
37
+ const txTag = `${tagId}.tx`;
38
+ class DrizzleTx extends import_effect.Context.Tag(txTag)() {
39
+ }
40
+ const db = (fn) => import_effect.Effect.gen(function* () {
41
+ const client = yield* Drizzle;
42
+ return yield* import_effect.Effect.tryPromise({
43
+ try: () => fn(client),
44
+ catch: (cause) => new DrizzleError({ message: "Database operation failed", cause })
45
+ });
46
+ });
47
+ const tx = (fn) => import_effect.Effect.gen(function* () {
48
+ const client = yield* DrizzleTx;
49
+ return yield* import_effect.Effect.tryPromise({
50
+ try: () => fn(client),
51
+ catch: (cause) => new DrizzleError({ message: "Database operation failed", cause })
52
+ });
53
+ });
54
+ const withTransaction = (effect) => import_effect.Effect.gen(function* () {
55
+ const client = yield* Drizzle;
56
+ const runFork = yield* import_effect.FiberSet.makeRuntimePromise();
57
+ return yield* import_effect.Effect.tryPromise({
58
+ try: () => client.transaction(
59
+ (txClient) => runFork(
60
+ effect.pipe(
61
+ import_effect.Effect.provideService(Drizzle, txClient),
62
+ import_effect.Effect.provideService(DrizzleTx, txClient),
63
+ import_effect.Effect.mapError((e) => new WrappedTxError("", { cause: e }))
64
+ )
65
+ )
66
+ ),
67
+ catch: (error) => {
68
+ if (error instanceof WrappedTxError) return error.cause;
69
+ return new DrizzleError({
70
+ message: "Transaction failed",
71
+ cause: error
72
+ });
73
+ }
74
+ });
75
+ }).pipe(import_effect.Effect.scoped);
76
+ return {
77
+ db,
78
+ tx,
79
+ withTransaction,
80
+ layer: import_effect.Layer.effect(Drizzle, createClient)
81
+ };
82
+ }
83
+ // Annotate the CommonJS export names for ESM import in node:
84
+ 0 && (module.exports = {
85
+ DrizzleError,
86
+ TagTypeId,
87
+ createDatabase
88
+ });
89
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/for/drizzle/index.ts"],"sourcesContent":["import { Context, Data, Effect, FiberSet, Layer } from 'effect';\n\n// TypeScript issue where the return type of createDatabase\n// contains internal Effect types (TagTypeId) that aren't exported\n// from this module,\n// so TypeScript can't \"name\" them in the declaration file.\nexport const TagTypeId = Context.TagTypeId;\n\nexport class DrizzleError extends Data.TaggedError('ff-effect/DrizzleError')<{\n\tmessage: string;\n\tcause?: unknown;\n}> {}\n\ntype AnyDrizzleClient = {\n\ttransaction: (fn: (tx: any) => Promise<any>) => Promise<any>;\n};\n\ntype TxClient<TClient extends AnyDrizzleClient> = Parameters<\n\tParameters<TClient['transaction']>[0]\n>[0];\n\nclass WrappedTxError extends Error {}\n\nexport function createDatabase<\n\tTAG extends string,\n\tTClient extends AnyDrizzleClient,\n\tE,\n\tR,\n>(tagId: TAG, createClient: Effect.Effect<TClient, E, R>) {\n\ttype Client = TClient | TxClient<TClient>;\n\ttype Tx = TxClient<TClient>;\n\n\tclass Drizzle extends Context.Tag(tagId)<Drizzle, Client>() {}\n\tconst txTag = `${tagId}.tx` as const;\n\tclass DrizzleTx extends Context.Tag(txTag)<DrizzleTx, Tx>() {}\n\n\tconst db = <T>(fn: (client: Client) => Promise<T>) =>\n\t\tEffect.gen(function* () {\n\t\t\tconst client = yield* Drizzle;\n\t\t\treturn yield* Effect.tryPromise({\n\t\t\t\ttry: () => fn(client),\n\t\t\t\tcatch: (cause) =>\n\t\t\t\t\tnew DrizzleError({ message: 'Database operation failed', cause }),\n\t\t\t});\n\t\t});\n\n\t/** Requires being inside withTransaction - enforces transaction at compile time */\n\tconst tx = <T>(fn: (client: Tx) => Promise<T>) =>\n\t\tEffect.gen(function* () {\n\t\t\tconst client = yield* DrizzleTx;\n\t\t\treturn yield* Effect.tryPromise({\n\t\t\t\ttry: () => fn(client),\n\t\t\t\tcatch: (cause) =>\n\t\t\t\t\tnew DrizzleError({ message: 'Database operation failed', cause }),\n\t\t\t});\n\t\t});\n\n\tconst withTransaction = <A, E, R>(effect: Effect.Effect<A, E, R>) =>\n\t\tEffect.gen(function* () {\n\t\t\tconst client = yield* Drizzle;\n\t\t\tconst runFork =\n\t\t\t\tyield* FiberSet.makeRuntimePromise<Exclude<R, DrizzleTx>>();\n\n\t\t\treturn yield* Effect.tryPromise<A, E | DrizzleError>({\n\t\t\t\ttry: () =>\n\t\t\t\t\t(client as TClient).transaction((txClient) =>\n\t\t\t\t\t\trunFork(\n\t\t\t\t\t\t\teffect.pipe(\n\t\t\t\t\t\t\t\tEffect.provideService(Drizzle, txClient as Client),\n\t\t\t\t\t\t\t\tEffect.provideService(DrizzleTx, txClient as Tx),\n\t\t\t\t\t\t\t\tEffect.mapError((e) => new WrappedTxError('', { cause: e })),\n\t\t\t\t\t\t\t) as Effect.Effect<A, WrappedTxError, Exclude<R, DrizzleTx>>,\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\tcatch: (error) => {\n\t\t\t\t\tif (error instanceof WrappedTxError) return error.cause as E;\n\t\t\t\t\treturn new DrizzleError({\n\t\t\t\t\t\tmessage: 'Transaction failed',\n\t\t\t\t\t\tcause: error,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t});\n\t\t}).pipe(Effect.scoped);\n\n\treturn {\n\t\tdb,\n\t\ttx,\n\t\twithTransaction,\n\t\tlayer: Layer.effect(Drizzle, createClient),\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAuD;AAMhD,IAAM,YAAY,sBAAQ;AAE1B,IAAM,eAAN,cAA2B,mBAAK,YAAY,wBAAwB,EAGxE;AAAC;AAUJ,IAAM,iBAAN,cAA6B,MAAM;AAAC;AAE7B,SAAS,eAKd,OAAY,cAA4C;AAAA,EAIzD,MAAM,gBAAgB,sBAAQ,IAAI,KAAK,EAAmB,EAAE;AAAA,EAAC;AAC7D,QAAM,QAAQ,GAAG,KAAK;AAAA,EACtB,MAAM,kBAAkB,sBAAQ,IAAI,KAAK,EAAiB,EAAE;AAAA,EAAC;AAE7D,QAAM,KAAK,CAAI,OACd,qBAAO,IAAI,aAAa;AACvB,UAAM,SAAS,OAAO;AACtB,WAAO,OAAO,qBAAO,WAAW;AAAA,MAC/B,KAAK,MAAM,GAAG,MAAM;AAAA,MACpB,OAAO,CAAC,UACP,IAAI,aAAa,EAAE,SAAS,6BAA6B,MAAM,CAAC;AAAA,IAClE,CAAC;AAAA,EACF,CAAC;AAGF,QAAM,KAAK,CAAI,OACd,qBAAO,IAAI,aAAa;AACvB,UAAM,SAAS,OAAO;AACtB,WAAO,OAAO,qBAAO,WAAW;AAAA,MAC/B,KAAK,MAAM,GAAG,MAAM;AAAA,MACpB,OAAO,CAAC,UACP,IAAI,aAAa,EAAE,SAAS,6BAA6B,MAAM,CAAC;AAAA,IAClE,CAAC;AAAA,EACF,CAAC;AAEF,QAAM,kBAAkB,CAAU,WACjC,qBAAO,IAAI,aAAa;AACvB,UAAM,SAAS,OAAO;AACtB,UAAM,UACL,OAAO,uBAAS,mBAA0C;AAE3D,WAAO,OAAO,qBAAO,WAAgC;AAAA,MACpD,KAAK,MACH,OAAmB;AAAA,QAAY,CAAC,aAChC;AAAA,UACC,OAAO;AAAA,YACN,qBAAO,eAAe,SAAS,QAAkB;AAAA,YACjD,qBAAO,eAAe,WAAW,QAAc;AAAA,YAC/C,qBAAO,SAAS,CAAC,MAAM,IAAI,eAAe,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;AAAA,UAC5D;AAAA,QACD;AAAA,MACD;AAAA,MACD,OAAO,CAAC,UAAU;AACjB,YAAI,iBAAiB,eAAgB,QAAO,MAAM;AAClD,eAAO,IAAI,aAAa;AAAA,UACvB,SAAS;AAAA,UACT,OAAO;AAAA,QACR,CAAC;AAAA,MACF;AAAA,IACD,CAAC;AAAA,EACF,CAAC,EAAE,KAAK,qBAAO,MAAM;AAEtB,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA,OAAO,oBAAM,OAAO,SAAS,YAAY;AAAA,EAC1C;AACD;","names":[]}
@@ -0,0 +1,46 @@
1
+ import * as effect_Scope from 'effect/Scope';
2
+ import * as effect_Cause from 'effect/Cause';
3
+ import * as effect_Types from 'effect/Types';
4
+ import { Effect, Context, Layer } from 'effect';
5
+
6
+ declare const TagTypeId: symbol;
7
+ declare const DrizzleError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
8
+ readonly _tag: "ff-effect/DrizzleError";
9
+ } & Readonly<A>;
10
+ declare class DrizzleError extends DrizzleError_base<{
11
+ message: string;
12
+ cause?: unknown;
13
+ }> {
14
+ }
15
+ type AnyDrizzleClient = {
16
+ transaction: (fn: (tx: any) => Promise<any>) => Promise<any>;
17
+ };
18
+ type TxClient<TClient extends AnyDrizzleClient> = Parameters<Parameters<TClient['transaction']>[0]>[0];
19
+ declare function createDatabase<TAG extends string, TClient extends AnyDrizzleClient, E, R>(tagId: TAG, createClient: Effect.Effect<TClient, E, R>): {
20
+ db: <T>(fn: (client: TClient | TxClient<TClient>) => Promise<T>) => Effect.Effect<T, DrizzleError, {
21
+ readonly Id: TAG;
22
+ readonly Type: TClient | TxClient<TClient>;
23
+ readonly [TagTypeId]: Context.TagTypeId;
24
+ }>;
25
+ tx: <T>(fn: (client: TxClient<TClient>) => Promise<T>) => Effect.Effect<T, DrizzleError, {
26
+ readonly Id: `${TAG}.tx`;
27
+ readonly Type: TxClient<TClient>;
28
+ readonly [TagTypeId]: Context.TagTypeId;
29
+ }>;
30
+ withTransaction: <A, E_1, R_1>(effect: Effect.Effect<A, E_1, R_1>) => Effect.Effect<A, DrizzleError | E_1, {
31
+ readonly Id: TAG;
32
+ readonly Type: TClient | TxClient<TClient>;
33
+ readonly [TagTypeId]: Context.TagTypeId;
34
+ } | Exclude<Exclude<R_1, {
35
+ readonly Id: `${TAG}.tx`;
36
+ readonly Type: TxClient<TClient>;
37
+ readonly [TagTypeId]: Context.TagTypeId;
38
+ }>, effect_Scope.Scope>>;
39
+ layer: Layer.Layer<{
40
+ readonly Id: TAG;
41
+ readonly Type: TClient | TxClient<TClient>;
42
+ readonly [TagTypeId]: Context.TagTypeId;
43
+ }, E, R>;
44
+ };
45
+
46
+ export { DrizzleError, TagTypeId, createDatabase };
@@ -0,0 +1,46 @@
1
+ import * as effect_Scope from 'effect/Scope';
2
+ import * as effect_Cause from 'effect/Cause';
3
+ import * as effect_Types from 'effect/Types';
4
+ import { Effect, Context, Layer } from 'effect';
5
+
6
+ declare const TagTypeId: symbol;
7
+ declare const DrizzleError_base: new <A extends Record<string, any> = {}>(args: effect_Types.Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => effect_Cause.YieldableError & {
8
+ readonly _tag: "ff-effect/DrizzleError";
9
+ } & Readonly<A>;
10
+ declare class DrizzleError extends DrizzleError_base<{
11
+ message: string;
12
+ cause?: unknown;
13
+ }> {
14
+ }
15
+ type AnyDrizzleClient = {
16
+ transaction: (fn: (tx: any) => Promise<any>) => Promise<any>;
17
+ };
18
+ type TxClient<TClient extends AnyDrizzleClient> = Parameters<Parameters<TClient['transaction']>[0]>[0];
19
+ declare function createDatabase<TAG extends string, TClient extends AnyDrizzleClient, E, R>(tagId: TAG, createClient: Effect.Effect<TClient, E, R>): {
20
+ db: <T>(fn: (client: TClient | TxClient<TClient>) => Promise<T>) => Effect.Effect<T, DrizzleError, {
21
+ readonly Id: TAG;
22
+ readonly Type: TClient | TxClient<TClient>;
23
+ readonly [TagTypeId]: Context.TagTypeId;
24
+ }>;
25
+ tx: <T>(fn: (client: TxClient<TClient>) => Promise<T>) => Effect.Effect<T, DrizzleError, {
26
+ readonly Id: `${TAG}.tx`;
27
+ readonly Type: TxClient<TClient>;
28
+ readonly [TagTypeId]: Context.TagTypeId;
29
+ }>;
30
+ withTransaction: <A, E_1, R_1>(effect: Effect.Effect<A, E_1, R_1>) => Effect.Effect<A, DrizzleError | E_1, {
31
+ readonly Id: TAG;
32
+ readonly Type: TClient | TxClient<TClient>;
33
+ readonly [TagTypeId]: Context.TagTypeId;
34
+ } | Exclude<Exclude<R_1, {
35
+ readonly Id: `${TAG}.tx`;
36
+ readonly Type: TxClient<TClient>;
37
+ readonly [TagTypeId]: Context.TagTypeId;
38
+ }>, effect_Scope.Scope>>;
39
+ layer: Layer.Layer<{
40
+ readonly Id: TAG;
41
+ readonly Type: TClient | TxClient<TClient>;
42
+ readonly [TagTypeId]: Context.TagTypeId;
43
+ }, E, R>;
44
+ };
45
+
46
+ export { DrizzleError, TagTypeId, createDatabase };
@@ -0,0 +1,62 @@
1
+ // src/for/drizzle/index.ts
2
+ import { Context, Data, Effect, FiberSet, Layer } from "effect";
3
+ var TagTypeId = Context.TagTypeId;
4
+ var DrizzleError = class extends Data.TaggedError("ff-effect/DrizzleError") {
5
+ };
6
+ var WrappedTxError = class extends Error {
7
+ };
8
+ function createDatabase(tagId, createClient) {
9
+ class Drizzle extends Context.Tag(tagId)() {
10
+ }
11
+ const txTag = `${tagId}.tx`;
12
+ class DrizzleTx extends Context.Tag(txTag)() {
13
+ }
14
+ const db = (fn) => Effect.gen(function* () {
15
+ const client = yield* Drizzle;
16
+ return yield* Effect.tryPromise({
17
+ try: () => fn(client),
18
+ catch: (cause) => new DrizzleError({ message: "Database operation failed", cause })
19
+ });
20
+ });
21
+ const tx = (fn) => Effect.gen(function* () {
22
+ const client = yield* DrizzleTx;
23
+ return yield* Effect.tryPromise({
24
+ try: () => fn(client),
25
+ catch: (cause) => new DrizzleError({ message: "Database operation failed", cause })
26
+ });
27
+ });
28
+ const withTransaction = (effect) => Effect.gen(function* () {
29
+ const client = yield* Drizzle;
30
+ const runFork = yield* FiberSet.makeRuntimePromise();
31
+ return yield* Effect.tryPromise({
32
+ try: () => client.transaction(
33
+ (txClient) => runFork(
34
+ effect.pipe(
35
+ Effect.provideService(Drizzle, txClient),
36
+ Effect.provideService(DrizzleTx, txClient),
37
+ Effect.mapError((e) => new WrappedTxError("", { cause: e }))
38
+ )
39
+ )
40
+ ),
41
+ catch: (error) => {
42
+ if (error instanceof WrappedTxError) return error.cause;
43
+ return new DrizzleError({
44
+ message: "Transaction failed",
45
+ cause: error
46
+ });
47
+ }
48
+ });
49
+ }).pipe(Effect.scoped);
50
+ return {
51
+ db,
52
+ tx,
53
+ withTransaction,
54
+ layer: Layer.effect(Drizzle, createClient)
55
+ };
56
+ }
57
+ export {
58
+ DrizzleError,
59
+ TagTypeId,
60
+ createDatabase
61
+ };
62
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/for/drizzle/index.ts"],"sourcesContent":["import { Context, Data, Effect, FiberSet, Layer } from 'effect';\n\n// TypeScript issue where the return type of createDatabase\n// contains internal Effect types (TagTypeId) that aren't exported\n// from this module,\n// so TypeScript can't \"name\" them in the declaration file.\nexport const TagTypeId = Context.TagTypeId;\n\nexport class DrizzleError extends Data.TaggedError('ff-effect/DrizzleError')<{\n\tmessage: string;\n\tcause?: unknown;\n}> {}\n\ntype AnyDrizzleClient = {\n\ttransaction: (fn: (tx: any) => Promise<any>) => Promise<any>;\n};\n\ntype TxClient<TClient extends AnyDrizzleClient> = Parameters<\n\tParameters<TClient['transaction']>[0]\n>[0];\n\nclass WrappedTxError extends Error {}\n\nexport function createDatabase<\n\tTAG extends string,\n\tTClient extends AnyDrizzleClient,\n\tE,\n\tR,\n>(tagId: TAG, createClient: Effect.Effect<TClient, E, R>) {\n\ttype Client = TClient | TxClient<TClient>;\n\ttype Tx = TxClient<TClient>;\n\n\tclass Drizzle extends Context.Tag(tagId)<Drizzle, Client>() {}\n\tconst txTag = `${tagId}.tx` as const;\n\tclass DrizzleTx extends Context.Tag(txTag)<DrizzleTx, Tx>() {}\n\n\tconst db = <T>(fn: (client: Client) => Promise<T>) =>\n\t\tEffect.gen(function* () {\n\t\t\tconst client = yield* Drizzle;\n\t\t\treturn yield* Effect.tryPromise({\n\t\t\t\ttry: () => fn(client),\n\t\t\t\tcatch: (cause) =>\n\t\t\t\t\tnew DrizzleError({ message: 'Database operation failed', cause }),\n\t\t\t});\n\t\t});\n\n\t/** Requires being inside withTransaction - enforces transaction at compile time */\n\tconst tx = <T>(fn: (client: Tx) => Promise<T>) =>\n\t\tEffect.gen(function* () {\n\t\t\tconst client = yield* DrizzleTx;\n\t\t\treturn yield* Effect.tryPromise({\n\t\t\t\ttry: () => fn(client),\n\t\t\t\tcatch: (cause) =>\n\t\t\t\t\tnew DrizzleError({ message: 'Database operation failed', cause }),\n\t\t\t});\n\t\t});\n\n\tconst withTransaction = <A, E, R>(effect: Effect.Effect<A, E, R>) =>\n\t\tEffect.gen(function* () {\n\t\t\tconst client = yield* Drizzle;\n\t\t\tconst runFork =\n\t\t\t\tyield* FiberSet.makeRuntimePromise<Exclude<R, DrizzleTx>>();\n\n\t\t\treturn yield* Effect.tryPromise<A, E | DrizzleError>({\n\t\t\t\ttry: () =>\n\t\t\t\t\t(client as TClient).transaction((txClient) =>\n\t\t\t\t\t\trunFork(\n\t\t\t\t\t\t\teffect.pipe(\n\t\t\t\t\t\t\t\tEffect.provideService(Drizzle, txClient as Client),\n\t\t\t\t\t\t\t\tEffect.provideService(DrizzleTx, txClient as Tx),\n\t\t\t\t\t\t\t\tEffect.mapError((e) => new WrappedTxError('', { cause: e })),\n\t\t\t\t\t\t\t) as Effect.Effect<A, WrappedTxError, Exclude<R, DrizzleTx>>,\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\tcatch: (error) => {\n\t\t\t\t\tif (error instanceof WrappedTxError) return error.cause as E;\n\t\t\t\t\treturn new DrizzleError({\n\t\t\t\t\t\tmessage: 'Transaction failed',\n\t\t\t\t\t\tcause: error,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t});\n\t\t}).pipe(Effect.scoped);\n\n\treturn {\n\t\tdb,\n\t\ttx,\n\t\twithTransaction,\n\t\tlayer: Layer.effect(Drizzle, createClient),\n\t};\n}\n"],"mappings":";AAAA,SAAS,SAAS,MAAM,QAAQ,UAAU,aAAa;AAMhD,IAAM,YAAY,QAAQ;AAE1B,IAAM,eAAN,cAA2B,KAAK,YAAY,wBAAwB,EAGxE;AAAC;AAUJ,IAAM,iBAAN,cAA6B,MAAM;AAAC;AAE7B,SAAS,eAKd,OAAY,cAA4C;AAAA,EAIzD,MAAM,gBAAgB,QAAQ,IAAI,KAAK,EAAmB,EAAE;AAAA,EAAC;AAC7D,QAAM,QAAQ,GAAG,KAAK;AAAA,EACtB,MAAM,kBAAkB,QAAQ,IAAI,KAAK,EAAiB,EAAE;AAAA,EAAC;AAE7D,QAAM,KAAK,CAAI,OACd,OAAO,IAAI,aAAa;AACvB,UAAM,SAAS,OAAO;AACtB,WAAO,OAAO,OAAO,WAAW;AAAA,MAC/B,KAAK,MAAM,GAAG,MAAM;AAAA,MACpB,OAAO,CAAC,UACP,IAAI,aAAa,EAAE,SAAS,6BAA6B,MAAM,CAAC;AAAA,IAClE,CAAC;AAAA,EACF,CAAC;AAGF,QAAM,KAAK,CAAI,OACd,OAAO,IAAI,aAAa;AACvB,UAAM,SAAS,OAAO;AACtB,WAAO,OAAO,OAAO,WAAW;AAAA,MAC/B,KAAK,MAAM,GAAG,MAAM;AAAA,MACpB,OAAO,CAAC,UACP,IAAI,aAAa,EAAE,SAAS,6BAA6B,MAAM,CAAC;AAAA,IAClE,CAAC;AAAA,EACF,CAAC;AAEF,QAAM,kBAAkB,CAAU,WACjC,OAAO,IAAI,aAAa;AACvB,UAAM,SAAS,OAAO;AACtB,UAAM,UACL,OAAO,SAAS,mBAA0C;AAE3D,WAAO,OAAO,OAAO,WAAgC;AAAA,MACpD,KAAK,MACH,OAAmB;AAAA,QAAY,CAAC,aAChC;AAAA,UACC,OAAO;AAAA,YACN,OAAO,eAAe,SAAS,QAAkB;AAAA,YACjD,OAAO,eAAe,WAAW,QAAc;AAAA,YAC/C,OAAO,SAAS,CAAC,MAAM,IAAI,eAAe,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;AAAA,UAC5D;AAAA,QACD;AAAA,MACD;AAAA,MACD,OAAO,CAAC,UAAU;AACjB,YAAI,iBAAiB,eAAgB,QAAO,MAAM;AAClD,eAAO,IAAI,aAAa;AAAA,UACvB,SAAS;AAAA,UACT,OAAO;AAAA,QACR,CAAC;AAAA,MACF;AAAA,IACD,CAAC;AAAA,EACF,CAAC,EAAE,KAAK,OAAO,MAAM;AAEtB,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA,OAAO,MAAM,OAAO,SAAS,YAAY;AAAA,EAC1C;AACD;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ff-effect",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -20,23 +20,34 @@
20
20
  ],
21
21
  "scripts": {
22
22
  "build": "tsup",
23
- "test": "vitest run",
23
+ "test": "bun -b vitest run",
24
24
  "dev": "tsup --watch"
25
25
  },
26
26
  "devDependencies": {
27
+ "@effect/platform": "^0.94.2",
28
+ "@effect/platform-bun": "^0.87.1",
29
+ "@effect/platform-node": "^0.104.1",
27
30
  "@effect/vitest": "^0.27.0",
31
+ "@libsql/client": "^0.17.0",
28
32
  "@orpc/contract": "^1.11.3",
29
33
  "@orpc/server": "^1.11.3",
30
34
  "@total-typescript/tsconfig": "^1.0.4",
31
35
  "@types/bun": "^1.3.2",
36
+ "@typescript/native-preview": "^7.0.0-dev.20260122.4",
32
37
  "tsup": "^8.5.0",
33
38
  "typescript": "^5.9.3",
34
39
  "valibot": "^1.1.0",
35
40
  "vitest": "^4.0.10"
36
41
  },
37
42
  "peerDependencies": {
43
+ "drizzle-orm": "^0.44.7",
38
44
  "effect": "^3.19.3"
39
45
  },
46
+ "peerDependenciesMeta": {
47
+ "drizzle-orm": {
48
+ "optional": true
49
+ }
50
+ },
40
51
  "publishConfig": {
41
52
  "access": "public"
42
53
  },
@@ -0,0 +1,150 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { FileSystem, Path } from '@effect/platform';
3
+ import * as BunContext from '@effect/platform-bun/BunContext';
4
+ import { describe, expect, layer } from '@effect/vitest';
5
+ import { createClient } from '@libsql/client';
6
+ import { sql } from 'drizzle-orm';
7
+ import { drizzle } from 'drizzle-orm/libsql';
8
+ import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
9
+ import { Effect } from 'effect';
10
+ import { expectTypeOf } from 'vitest';
11
+ import { createDatabase, DrizzleError } from './index.js';
12
+
13
+ const users = sqliteTable('users', {
14
+ id: text('id').primaryKey(),
15
+ name: text('name').notNull(),
16
+ });
17
+
18
+ const schema = { users };
19
+
20
+ const createTestDb = Effect.gen(function* () {
21
+ const fs = yield* FileSystem.FileSystem;
22
+ const path = yield* Path.Path;
23
+
24
+ const tmpDir = yield* fs.makeTempDirectoryScoped();
25
+ const dbPath = path.join(tmpDir, `test-${randomUUID()}.db`);
26
+
27
+ const client = createClient({ url: `file:${dbPath}` });
28
+ return drizzle(client, { schema });
29
+ });
30
+
31
+ const setupTable = (testDb: ReturnType<typeof drizzle>) =>
32
+ Effect.gen(function* () {
33
+ yield* Effect.promise(() => testDb.run(sql`DROP TABLE IF EXISTS users`));
34
+ yield* Effect.promise(() =>
35
+ testDb.run(
36
+ sql`CREATE TABLE users (id TEXT PRIMARY KEY, name TEXT NOT NULL)`,
37
+ ),
38
+ );
39
+ });
40
+
41
+ layer(BunContext.layer)((it) => {
42
+ describe('db', () => {
43
+ it.scoped('wraps promises and returns correct data', () =>
44
+ Effect.gen(function* () {
45
+ const testDb = yield* createTestDb;
46
+ yield* setupTable(testDb);
47
+ yield* Effect.promise(() =>
48
+ testDb.insert(users).values({ id: '1', name: 'Alice' }),
49
+ );
50
+
51
+ const database = createDatabase('test/db', Effect.succeed(testDb));
52
+
53
+ const result = yield* database
54
+ .db((d) => d.select().from(users))
55
+ .pipe(Effect.provide(database.layer));
56
+
57
+ expect(result).toEqual([{ id: '1', name: 'Alice' }]);
58
+ }),
59
+ );
60
+
61
+ it.scoped('surfaces errors as DrizzleError', () =>
62
+ Effect.gen(function* () {
63
+ const testDb = yield* createTestDb;
64
+
65
+ const database = createDatabase(
66
+ 'test/db-error',
67
+ Effect.succeed(testDb),
68
+ );
69
+
70
+ const result = yield* database
71
+ .db(() => Promise.reject(new Error('DB failure')))
72
+ .pipe(Effect.provide(database.layer), Effect.flip);
73
+
74
+ expect(result).toBeInstanceOf(DrizzleError);
75
+ expect(result.message).toBe('Database operation failed');
76
+ expect(result.cause).toBeInstanceOf(Error);
77
+ expect((result.cause as Error).message).toBe('DB failure');
78
+ }),
79
+ );
80
+ });
81
+
82
+ describe('withTransaction', () => {
83
+ it.scoped('provides transaction context to nested db calls', () =>
84
+ Effect.gen(function* () {
85
+ const testDb = yield* createTestDb;
86
+ yield* setupTable(testDb);
87
+
88
+ const database = createDatabase('test/db-tx', Effect.succeed(testDb));
89
+
90
+ yield* Effect.gen(function* () {
91
+ const ok = 'ok' as const;
92
+ const txResult = yield* database.withTransaction(
93
+ Effect.gen(function* () {
94
+ yield* database.db((d) =>
95
+ d.insert(users).values({ id: '1', name: 'Bob' }),
96
+ );
97
+ yield* database.db((d) =>
98
+ d.insert(users).values({ id: '2', name: 'Carol' }),
99
+ );
100
+ return ok;
101
+ }),
102
+ );
103
+ expectTypeOf(txResult).toEqualTypeOf<typeof ok>();
104
+
105
+ const result = yield* database.db((d) => d.select().from(users));
106
+ expect(result).toHaveLength(2);
107
+ expect(result).toEqual([
108
+ { id: '1', name: 'Bob' },
109
+ { id: '2', name: 'Carol' },
110
+ ]);
111
+ }).pipe(Effect.provide(database.layer));
112
+ }),
113
+ );
114
+
115
+ it.scoped('rolls back on error', () =>
116
+ Effect.gen(function* () {
117
+ const testDb = yield* createTestDb;
118
+ yield* setupTable(testDb);
119
+
120
+ const database = createDatabase(
121
+ 'test/db-rollback',
122
+ Effect.succeed(testDb),
123
+ );
124
+
125
+ const result = yield* Effect.gen(function* () {
126
+ const txResult = yield* database
127
+ .withTransaction(
128
+ Effect.gen(function* () {
129
+ yield* database.db((d) =>
130
+ d.insert(users).values({ id: '1', name: 'Dave' }),
131
+ );
132
+
133
+ yield* database.tx((d) =>
134
+ d.insert(users).values({ id: '2', name: 'Bob' }),
135
+ );
136
+ return yield* Effect.fail(new Error('Intentional failure'));
137
+ }),
138
+ )
139
+ .pipe(Effect.either);
140
+
141
+ expect(txResult._tag).toBe('Left');
142
+
143
+ return yield* database.db((d) => d.select().from(users));
144
+ }).pipe(Effect.provide(database.layer));
145
+
146
+ expect(result).toEqual([]);
147
+ }),
148
+ );
149
+ });
150
+ });
@@ -0,0 +1,91 @@
1
+ import { Context, Data, Effect, FiberSet, Layer } from 'effect';
2
+
3
+ // TypeScript issue where the return type of createDatabase
4
+ // contains internal Effect types (TagTypeId) that aren't exported
5
+ // from this module,
6
+ // so TypeScript can't "name" them in the declaration file.
7
+ export const TagTypeId = Context.TagTypeId;
8
+
9
+ export class DrizzleError extends Data.TaggedError('ff-effect/DrizzleError')<{
10
+ message: string;
11
+ cause?: unknown;
12
+ }> {}
13
+
14
+ type AnyDrizzleClient = {
15
+ transaction: (fn: (tx: any) => Promise<any>) => Promise<any>;
16
+ };
17
+
18
+ type TxClient<TClient extends AnyDrizzleClient> = Parameters<
19
+ Parameters<TClient['transaction']>[0]
20
+ >[0];
21
+
22
+ class WrappedTxError extends Error {}
23
+
24
+ export function createDatabase<
25
+ TAG extends string,
26
+ TClient extends AnyDrizzleClient,
27
+ E,
28
+ R,
29
+ >(tagId: TAG, createClient: Effect.Effect<TClient, E, R>) {
30
+ type Client = TClient | TxClient<TClient>;
31
+ type Tx = TxClient<TClient>;
32
+
33
+ class Drizzle extends Context.Tag(tagId)<Drizzle, Client>() {}
34
+ const txTag = `${tagId}.tx` as const;
35
+ class DrizzleTx extends Context.Tag(txTag)<DrizzleTx, Tx>() {}
36
+
37
+ const db = <T>(fn: (client: Client) => Promise<T>) =>
38
+ Effect.gen(function* () {
39
+ const client = yield* Drizzle;
40
+ return yield* Effect.tryPromise({
41
+ try: () => fn(client),
42
+ catch: (cause) =>
43
+ new DrizzleError({ message: 'Database operation failed', cause }),
44
+ });
45
+ });
46
+
47
+ /** Requires being inside withTransaction - enforces transaction at compile time */
48
+ const tx = <T>(fn: (client: Tx) => Promise<T>) =>
49
+ Effect.gen(function* () {
50
+ const client = yield* DrizzleTx;
51
+ return yield* Effect.tryPromise({
52
+ try: () => fn(client),
53
+ catch: (cause) =>
54
+ new DrizzleError({ message: 'Database operation failed', cause }),
55
+ });
56
+ });
57
+
58
+ const withTransaction = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
59
+ Effect.gen(function* () {
60
+ const client = yield* Drizzle;
61
+ const runFork =
62
+ yield* FiberSet.makeRuntimePromise<Exclude<R, DrizzleTx>>();
63
+
64
+ return yield* Effect.tryPromise<A, E | DrizzleError>({
65
+ try: () =>
66
+ (client as TClient).transaction((txClient) =>
67
+ runFork(
68
+ effect.pipe(
69
+ Effect.provideService(Drizzle, txClient as Client),
70
+ Effect.provideService(DrizzleTx, txClient as Tx),
71
+ Effect.mapError((e) => new WrappedTxError('', { cause: e })),
72
+ ) as Effect.Effect<A, WrappedTxError, Exclude<R, DrizzleTx>>,
73
+ ),
74
+ ),
75
+ catch: (error) => {
76
+ if (error instanceof WrappedTxError) return error.cause as E;
77
+ return new DrizzleError({
78
+ message: 'Transaction failed',
79
+ cause: error,
80
+ });
81
+ },
82
+ });
83
+ }).pipe(Effect.scoped);
84
+
85
+ return {
86
+ db,
87
+ tx,
88
+ withTransaction,
89
+ layer: Layer.effect(Drizzle, createClient),
90
+ };
91
+ }