import { Effect, Context, Data } from 'effect';
import { Surreal, type ConnectOptions } from 'surrealdb.js'; // Or your specific import
// Error Type
export class SurrealDBError extends Data.TaggedError('SurrealDBError')<{
cause?: unknown;
message?: string;
}> {}
// Service Interface (Intended)
interface SurrealDBImpl {
use: <T>(fn: (client: Surreal) => T) => Effect.Effect<Awaited<T>, SurrealDBError, never>;
}
// Service Tag
export class SurrealDB extends Context.Tag('SurrealDB')<SurrealDB, SurrealDBImpl>() {} // <-- Is this Tag definition correct?
// Function to manage connection lifecycle
export const makeConnectionEffect = (url: string | URL, options: ConnectOptions) =>
Effect.acquireRelease(
Effect.tryPromise({
try: async () => {
const _client = new Surreal();
await _client.connect(url, options);
// Add signin/use if needed
return _client;
},
catch: (e) => new SurrealDBError({ cause: e, message: 'Error connecting' })
}),
(client) => Effect.promise(() => client.close()) // Or Effect.sync
);
import { Effect, Context, Data } from 'effect';
import { Surreal, type ConnectOptions } from 'surrealdb.js'; // Or your specific import
// Error Type
export class SurrealDBError extends Data.TaggedError('SurrealDBError')<{
cause?: unknown;
message?: string;
}> {}
// Service Interface (Intended)
interface SurrealDBImpl {
use: <T>(fn: (client: Surreal) => T) => Effect.Effect<Awaited<T>, SurrealDBError, never>;
}
// Service Tag
export class SurrealDB extends Context.Tag('SurrealDB')<SurrealDB, SurrealDBImpl>() {} // <-- Is this Tag definition correct?
// Function to manage connection lifecycle
export const makeConnectionEffect = (url: string | URL, options: ConnectOptions) =>
Effect.acquireRelease(
Effect.tryPromise({
try: async () => {
const _client = new Surreal();
await _client.connect(url, options);
// Add signin/use if needed
return _client;
},
catch: (e) => new SurrealDBError({ cause: e, message: 'Error connecting' })
}),
(client) => Effect.promise(() => client.close()) // Or Effect.sync
);