Skip to content

Commit

Permalink
Db: documentCollection/collection functions are not an effectful oper…
Browse files Browse the repository at this point in the history
…ation like mongodb driver
  • Loading branch information
devmatteini committed Oct 11, 2024
1 parent 18137ba commit b6a1dec
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 33 deletions.
4 changes: 2 additions & 2 deletions examples/copy-between-dbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type MyType = Schema.Schema.Type<typeof MyType>
const program = Effect.gen(function*(_) {
const sourceInstance = yield* _(MongoClient.connect("mongodb://localhost:27017"))
const sourceDb = MongoClient.db(sourceInstance, "source")
const sourceCollection = yield* _(Db.documentCollection(sourceDb, "records"))
const sourceCollection = Db.documentCollection(sourceDb, "records")
const sourceItems = yield* _(
DocumentCollection.find(sourceCollection),
DocumentFindCursor.typed(MyType),
Expand All @@ -26,7 +26,7 @@ const program = Effect.gen(function*(_) {

const destinationInstance = yield* _(MongoClient.connect("mongodb://localhost:27017"))
const destinationDb = MongoClient.db(destinationInstance, "destination")
const destinationCollection = yield* _(Db.collection(destinationDb, "records", MyType))
const destinationCollection = Db.collection(destinationDb, "records", MyType)

yield* _(Collection.insertMany(destinationCollection, sourceItems))
})
Expand Down
2 changes: 1 addition & 1 deletion examples/elaborate-stream-with-partitioned-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type MyType = Schema.Schema.Type<typeof MyType>
const program = Effect.gen(function*(_) {
const sourceInstance = yield* _(MongoClient.connect("mongodb://localhost:27017"))
const sourceDb = MongoClient.db(sourceInstance, "source")
const sourceCollection = yield* _(Db.documentCollection(sourceDb, "records"))
const sourceCollection = Db.documentCollection(sourceDb, "records")

yield* _(
DocumentCollection.find(sourceCollection),
Expand Down
24 changes: 9 additions & 15 deletions src/Db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,33 @@ import * as MongoClient from "./MongoClient.js"
import type * as MongoError from "./MongoError.js"

export const documentCollection: {
(name: string): (db: Db) => Effect.Effect<DocumentCollection.DocumentCollection>
(db: Db, name: string): Effect.Effect<DocumentCollection.DocumentCollection>
(name: string): (db: Db) => DocumentCollection.DocumentCollection
(db: Db, name: string): DocumentCollection.DocumentCollection
} = F.dual(
(args) => isDb(args[0]),
(db: Db, name: string) =>
Effect.sync(() =>
new DocumentCollection.DocumentCollection({
collection: db.collection(name)
})
)
(db: Db, name: string): DocumentCollection.DocumentCollection =>
new DocumentCollection.DocumentCollection({
collection: db.collection(name)
})
)

export const collection: {
<A extends Document, I extends Document = A, R = never>(
name: string,
schema: Schema.Schema<A, I, R>
): (db: Db) => Effect.Effect<Collection.Collection<A, I, R>>
): (db: Db) => Collection.Collection<A, I, R>
<A extends Document, I extends Document = A, R = never>(
db: Db,
name: string,
schema: Schema.Schema<A, I, R>
): Effect.Effect<Collection.Collection<A, I, R>>
): Collection.Collection<A, I, R>
} = F.dual(
(args) => isDb(args[0]),
<A extends Document, I extends Document = A, R = never>(
db: Db,
name: string,
schema: Schema.Schema<A, I, R>
): Effect.Effect<Collection.Collection<A, I, R>> =>
F.pipe(
documentCollection(db, name),
Effect.map(DocumentCollection.typed(schema))
)
): Collection.Collection<A, I, R> => DocumentCollection.typed(documentCollection(db, name), schema)
)

const isDb = (x: unknown) => x instanceof Db
Expand Down
2 changes: 1 addition & 1 deletion test/Collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describeMongo("Collection", (ctx) => {

const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.collection(db, "find-one", User))
const collection = Db.collection(db, "find-one", User)

yield* _(
Collection.insertOne(collection, user)
Expand Down
8 changes: 4 additions & 4 deletions test/DocumentCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describeMongo("DocumentCollection", (ctx) => {
test("insert and find", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.documentCollection(db, "insert-and-find"))
const collection = Db.documentCollection(db, "insert-and-find")

yield* _(DocumentCollection.insertOne(collection, { name: "John" }))

Expand All @@ -26,7 +26,7 @@ describeMongo("DocumentCollection", (ctx) => {
test("insert many and find", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.documentCollection(db, "insert-many-and-find"))
const collection = Db.documentCollection(db, "insert-many-and-find")

yield* _(
DocumentCollection.insertMany(collection, [{ name: "NAME_1" }, { name: "NAME_2" }, { name: "NAME_3" }])
Expand All @@ -47,7 +47,7 @@ describeMongo("DocumentCollection", (ctx) => {
test("find one", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.documentCollection(db, "find-one"))
const collection = Db.documentCollection(db, "find-one")

yield* _(
DocumentCollection.insertMany(collection, [{ name: "ANY_NAME_1" }, { name: "john" }, { name: "ANY_NAME_2" }])
Expand All @@ -64,7 +64,7 @@ describeMongo("DocumentCollection", (ctx) => {
test("find one - no result", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.documentCollection(db, "find-one-no-result"))
const collection = Db.documentCollection(db, "find-one-no-result")

yield* _(
DocumentCollection.insertMany(collection, [{ name: "ANY_NAME_1" }, { name: "ANY_NAME_2" }, {
Expand Down
12 changes: 6 additions & 6 deletions test/DocumentFindCursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describeMongo("DocumentFindCursor", (ctx) => {
test("filter", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.documentCollection(db, "filter"))
const collection = Db.documentCollection(db, "filter")

yield* _(
DocumentCollection.insertMany(collection, [
Expand Down Expand Up @@ -42,7 +42,7 @@ describeMongo("DocumentFindCursor", (ctx) => {

const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.documentCollection(db, "project"))
const collection = Db.documentCollection(db, "project")

yield* _(
DocumentCollection.insertMany(collection, [
Expand Down Expand Up @@ -71,7 +71,7 @@ describeMongo("DocumentFindCursor", (ctx) => {
test("sort", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.documentCollection(db, "sort"))
const collection = Db.documentCollection(db, "sort")

yield* _(
DocumentCollection.insertMany(collection, [
Expand Down Expand Up @@ -104,7 +104,7 @@ describeMongo("DocumentFindCursor", (ctx) => {
test("limit", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.documentCollection(db, "limit"))
const collection = Db.documentCollection(db, "limit")

yield* _(
DocumentCollection.insertMany(collection, [
Expand Down Expand Up @@ -137,7 +137,7 @@ describeMongo("DocumentFindCursor", (ctx) => {

const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.documentCollection(db, "many-operations"))
const collection = Db.documentCollection(db, "many-operations")

yield* _(
DocumentCollection.insertMany(collection, [
Expand Down Expand Up @@ -177,7 +177,7 @@ describeMongo("DocumentFindCursor", (ctx) => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)

const collection = yield* _(Db.documentCollection(db, "stream"))
const collection = Db.documentCollection(db, "stream")
yield* _(DocumentCollection.insertMany(collection, users))

return yield* _(
Expand Down
8 changes: 4 additions & 4 deletions test/FindCursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describeMongo("FindCursor", (ctx) => {

const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.collection(db, "decode-documents-with-schema", User))
const collection = Db.collection(db, "decode-documents-with-schema", User)

yield* _(Collection.insertMany(collection, anyTestEntities))

Expand All @@ -36,7 +36,7 @@ describeMongo("FindCursor", (ctx) => {

const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.collection(db, "project", User))
const collection = Db.collection(db, "project", User)

yield* _(Collection.insertMany(collection, anyUsers))

Expand All @@ -58,7 +58,7 @@ describeMongo("FindCursor", (ctx) => {

const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.collection(db, "stream", User))
const collection = Db.collection(db, "stream", User)

yield* _(Collection.insertMany(collection, anyUsers))

Expand All @@ -78,7 +78,7 @@ describeMongo("FindCursor", (ctx) => {
test("stream with partitioned errors", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const documentCollection = yield* _(Db.documentCollection(db, "stream-with-partitioned-errors"))
const documentCollection = Db.documentCollection(db, "stream-with-partitioned-errors")
const collection = DocumentCollection.typed(documentCollection, User)

yield* _(Collection.insertMany(collection, FastCheck.sample(UserArbitrary, 6)))
Expand Down

0 comments on commit b6a1dec

Please sign in to comment.