|  | 
| 1 | 1 | /** | 
| 2 | 2 |  * @since 0.0.1 | 
| 3 | 3 |  */ | 
| 4 |  | -import type * as Brand from "effect/Brand" | 
| 5 |  | -import * as Context from "effect/Context" | 
| 6 | 4 | import * as Effect from "effect/Effect" | 
| 7 | 5 | import * as F from "effect/Function" | 
| 8 |  | -import * as Layer from "effect/Layer" | 
| 9 | 6 | import type * as Schema from "effect/Schema" | 
| 10 | 7 | import type { Document, DropCollectionOptions, ListCollectionsOptions } from "mongodb" | 
| 11 | 8 | import { Db } from "mongodb" | 
| 12 | 9 | import type * as Collection from "./Collection.js" | 
| 13 | 10 | import * as DocumentCollection from "./DocumentCollection.js" | 
| 14 | 11 | import * as ListCollectionsCursor from "./ListCollectionsCursor.js" | 
| 15 |  | -import * as MongoClient from "./MongoClient.js" | 
| 16 | 12 | import * as MongoError from "./MongoError.js" | 
| 17 | 13 | 
 | 
| 18 | 14 | export const documentCollection: { | 
| @@ -89,35 +85,3 @@ export const dropCollection: { | 
| 89 | 85 | ) | 
| 90 | 86 | 
 | 
| 91 | 87 | const isDb = (x: unknown) => x instanceof Db | 
| 92 |  | - | 
| 93 |  | -export type DbService<K extends string> = { | 
| 94 |  | -  db: Effect.Effect<Db, MongoError.MongoError> | 
| 95 |  | -} & Brand.Brand<K> | 
| 96 |  | - | 
| 97 |  | -export const Tag = <K extends string>(key: K) => Context.GenericTag<DbService<K>>(key) | 
| 98 |  | -export type TagType<K extends string> = ReturnType<typeof Tag<K>> | 
| 99 |  | - | 
| 100 |  | -export const fromEffect = <DbK extends string, MongoClientK extends string, E = never, R = never>( | 
| 101 |  | -  dbTag: TagType<DbK>, | 
| 102 |  | -  clientTag: MongoClient.TagType<MongoClientK>, | 
| 103 |  | -  dbName: Effect.Effect<string, E, R> | 
| 104 |  | -) => | 
| 105 |  | -  Layer.effect( | 
| 106 |  | -    dbTag, | 
| 107 |  | -    Effect.gen(function*(_) { | 
| 108 |  | -      const { client } = yield* _(clientTag) | 
| 109 |  | -      const dbName_ = yield* _(dbName) | 
| 110 |  | -      const db = yield* _( | 
| 111 |  | -        client, | 
| 112 |  | -        Effect.map((client) => MongoClient.db(client, dbName_)), | 
| 113 |  | -        Effect.cached | 
| 114 |  | -      ) | 
| 115 |  | -      return dbTag.of({ db } as DbService<DbK>) // TODO fix cast using branded ctor | 
| 116 |  | -    }) | 
| 117 |  | -  ) | 
| 118 |  | - | 
| 119 |  | -export const fromConst = <DbK extends string, MongoClientK extends string>( | 
| 120 |  | -  dbTag: TagType<DbK>, | 
| 121 |  | -  clientTag: MongoClient.TagType<MongoClientK>, | 
| 122 |  | -  dbName: string | 
| 123 |  | -) => fromEffect(dbTag, clientTag, Effect.succeed(dbName)) | 
0 commit comments