|
1 | 1 | import * as DocumentCollection from "effect-mongodb/DocumentCollection" |
| 2 | +import * as DocumentFindCursor from "effect-mongodb/DocumentFindCursor" |
| 3 | +import * as F from "effect/Function" |
2 | 4 | import * as Schema from "effect/Schema" |
| 5 | +import type { Document } from "mongodb" |
3 | 6 |
|
4 | 7 | const MyType = Schema.Struct({ |
5 | 8 | birthday: Schema.Date |
6 | 9 | }) |
| 10 | +type MyType = typeof MyType.Type |
7 | 11 |
|
8 | | -declare const documentCollection: DocumentCollection.DocumentCollection |
| 12 | +declare const anyDocument: Document |
| 13 | + |
| 14 | +declare const collection: DocumentCollection.DocumentCollection |
| 15 | + |
| 16 | +// ------------------------------------------------------------------------------------- |
| 17 | +// find |
| 18 | +// ------------------------------------------------------------------------------------- |
| 19 | + |
| 20 | +// $ExpectType Effect<Document[], MongoError, never> |
| 21 | +DocumentFindCursor.toArray(DocumentCollection.find(collection, { birthday: "2024-11-28" })) |
| 22 | + |
| 23 | +// $ExpectType Effect<Document[], MongoError, never> |
| 24 | +F.pipe(collection, DocumentCollection.find({ birthday: "2024-11-28" }), DocumentFindCursor.toArray) |
| 25 | + |
| 26 | +// ------------------------------------------------------------------------------------- |
| 27 | +// findOne |
| 28 | +// ------------------------------------------------------------------------------------- |
| 29 | + |
| 30 | +// $ExpectType Effect<Option<Document>, MongoError, never> |
| 31 | +DocumentCollection.findOne(collection, { birthday: "2024-11-28" }) |
| 32 | + |
| 33 | +// $ExpectType Effect<Option<Document>, MongoError, never> |
| 34 | +F.pipe(collection, DocumentCollection.findOne({ birthday: "2024-11-28" })) |
| 35 | + |
| 36 | +// ------------------------------------------------------------------------------------- |
| 37 | +// insertOne |
| 38 | +// ------------------------------------------------------------------------------------- |
| 39 | + |
| 40 | +// $ExpectType Effect<InsertOneResult<Document>, MongoError, never> |
| 41 | +DocumentCollection.insertOne(collection, anyDocument) |
| 42 | + |
| 43 | +// $ExpectType Effect<InsertOneResult<Document>, MongoError, never> |
| 44 | +DocumentCollection.insertOne(collection, anyDocument, { comment: "any" }) |
| 45 | + |
| 46 | +// $ExpectType Effect<InsertOneResult<Document>, MongoError, never> |
| 47 | +F.pipe(collection, DocumentCollection.insertOne(anyDocument)) |
| 48 | + |
| 49 | +// ------------------------------------------------------------------------------------- |
| 50 | +// insertMany |
| 51 | +// ------------------------------------------------------------------------------------- |
| 52 | + |
| 53 | +// $ExpectType Effect<InsertManyResult<Document>, MongoError, never> |
| 54 | +DocumentCollection.insertMany(collection, [anyDocument]) |
| 55 | + |
| 56 | +// $ExpectType Effect<InsertManyResult<Document>, MongoError, never> |
| 57 | +F.pipe(collection, DocumentCollection.insertMany([anyDocument])) |
| 58 | + |
| 59 | +// ------------------------------------------------------------------------------------- |
| 60 | +// deleteOne |
| 61 | +// ------------------------------------------------------------------------------------- |
| 62 | + |
| 63 | +// $ExpectType Effect<DeleteResult, MongoError, never> |
| 64 | +DocumentCollection.deleteOne(collection, { birthday: "2024-11-28" }) |
| 65 | + |
| 66 | +// $ExpectType Effect<DeleteResult, MongoError, never> |
| 67 | +F.pipe(collection, DocumentCollection.deleteOne({ birthday: "2024-11-28" })) |
| 68 | + |
| 69 | +// ------------------------------------------------------------------------------------- |
| 70 | +// deleteMany |
| 71 | +// ------------------------------------------------------------------------------------- |
| 72 | + |
| 73 | +// $ExpectType Effect<DeleteResult, MongoError, never> |
| 74 | +DocumentCollection.deleteMany(collection, { birthday: "2024-11-28" }) |
| 75 | + |
| 76 | +// $ExpectType Effect<DeleteResult, MongoError, never> |
| 77 | +F.pipe(collection, DocumentCollection.deleteMany({ birthday: "2024-11-28" })) |
| 78 | + |
| 79 | +// ------------------------------------------------------------------------------------- |
| 80 | +// updateMany |
| 81 | +// ------------------------------------------------------------------------------------- |
| 82 | + |
| 83 | +// $ExpectType Effect<UpdateResult<Document>, MongoError, never> |
| 84 | +DocumentCollection.updateMany(collection, { birthday: "2024-11-28" }, { $set: { birthday: "2024-11-29" } }) |
| 85 | + |
| 86 | +// $ExpectType Effect<UpdateResult<Document>, MongoError, never> |
| 87 | +F.pipe(collection, DocumentCollection.updateMany({ birthday: "2024-11-28" }, { $set: { birthday: "2024-11-29" } })) |
| 88 | + |
| 89 | +// ------------------------------------------------------------------------------------- |
| 90 | +// replaceOne |
| 91 | +// ------------------------------------------------------------------------------------- |
| 92 | + |
| 93 | +// $ExpectType Effect<Document | UpdateResult<Document>, MongoError, never> |
| 94 | +DocumentCollection.replaceOne(collection, { birthday: "2024-11-28" }, anyDocument) |
| 95 | + |
| 96 | +// $ExpectType Effect<Document | UpdateResult<Document>, MongoError, never> |
| 97 | +F.pipe(collection, DocumentCollection.replaceOne({ birthday: "2024-11-28" }, anyDocument)) |
| 98 | + |
| 99 | +// ------------------------------------------------------------------------------------- |
| 100 | +// findOneAndReplace |
| 101 | +// ------------------------------------------------------------------------------------- |
| 102 | + |
| 103 | +// $ExpectType Effect<ModifyResult<Document>, MongoError, never> |
| 104 | +DocumentCollection.findOneAndReplace(collection, { birthday: "2024-11-28" }, anyDocument, { |
| 105 | + includeResultMetadata: true |
| 106 | +}) |
| 107 | + |
| 108 | +// $ExpectType Effect<ModifyResult<Document>, MongoError, never> |
| 109 | +F.pipe( |
| 110 | + collection, |
| 111 | + DocumentCollection.findOneAndReplace({ birthday: "2024-11-28" }, anyDocument, { includeResultMetadata: true }) |
| 112 | +) |
| 113 | + |
| 114 | +// $ExpectType Effect<Option<Document>, MongoError, never> |
| 115 | +DocumentCollection.findOneAndReplace(collection, { birthday: "2024-11-28" }, anyDocument, { |
| 116 | + includeResultMetadata: false |
| 117 | +}) |
| 118 | + |
| 119 | +// $ExpectType Effect<Option<Document>, MongoError, never> |
| 120 | +F.pipe( |
| 121 | + collection, |
| 122 | + DocumentCollection.findOneAndReplace({ birthday: "2024-11-28" }, anyDocument, { includeResultMetadata: false }) |
| 123 | +) |
| 124 | + |
| 125 | +// $ExpectType Effect<Option<Document>, MongoError, never> |
| 126 | +DocumentCollection.findOneAndReplace(collection, { birthday: "2024-11-28" }, anyDocument, { comment: "any" }) |
| 127 | + |
| 128 | +// $ExpectType Effect<Option<Document>, MongoError, never> |
| 129 | +F.pipe(collection, DocumentCollection.findOneAndReplace({ birthday: "2024-11-28" }, anyDocument, { comment: "any" })) |
| 130 | + |
| 131 | +// $ExpectType Effect<Option<Document>, MongoError, never> |
| 132 | +DocumentCollection.findOneAndReplace(collection, { birthday: "2024-11-28" }, anyDocument) |
| 133 | + |
| 134 | +// $ExpectType Effect<Option<Document>, MongoError, never> |
| 135 | +F.pipe(collection, DocumentCollection.findOneAndReplace({ birthday: "2024-11-28" }, anyDocument)) |
| 136 | + |
| 137 | +// ------------------------------------------------------------------------------------- |
| 138 | +// rename |
| 139 | +// ------------------------------------------------------------------------------------- |
| 140 | + |
| 141 | +// $ExpectType Effect<DocumentCollection, MongoError, never> |
| 142 | +DocumentCollection.rename(collection, "new-collection") |
| 143 | + |
| 144 | +// $ExpectType Effect<DocumentCollection, MongoError, never> |
| 145 | +F.pipe(collection, DocumentCollection.rename("new-collection")) |
| 146 | + |
| 147 | +// ------------------------------------------------------------------------------------- |
| 148 | +// drop |
| 149 | +// ------------------------------------------------------------------------------------- |
| 150 | + |
| 151 | +// $ExpectType Effect<boolean, MongoError, never> |
| 152 | +DocumentCollection.drop(collection) |
| 153 | + |
| 154 | +// $ExpectType Effect<boolean, MongoError, never> |
| 155 | +F.pipe(collection, DocumentCollection.drop()) |
| 156 | + |
| 157 | +// ------------------------------------------------------------------------------------- |
| 158 | +// createIndexes |
| 159 | +// ------------------------------------------------------------------------------------- |
| 160 | + |
| 161 | +// $ExpectType Effect<string[], MongoError, never> |
| 162 | +DocumentCollection.createIndexes(collection, [{ key: { birthday: 1 } }]) |
| 163 | + |
| 164 | +// $ExpectType Effect<string[], MongoError, never> |
| 165 | +F.pipe(collection, DocumentCollection.createIndexes([{ key: { birthday: 1 } }])) |
| 166 | + |
| 167 | +// ------------------------------------------------------------------------------------- |
| 168 | +// createIndex |
| 169 | +// ------------------------------------------------------------------------------------- |
| 170 | + |
| 171 | +// $ExpectType Effect<string, MongoError, never> |
| 172 | +DocumentCollection.createIndex(collection, { birthday: 1 }) |
| 173 | + |
| 174 | +// $ExpectType Effect<string, MongoError, never> |
| 175 | +F.pipe(collection, DocumentCollection.createIndex({ birthday: 1 })) |
| 176 | + |
| 177 | +// ------------------------------------------------------------------------------------- |
| 178 | +// dropIndex |
| 179 | +// ------------------------------------------------------------------------------------- |
| 180 | + |
| 181 | +// $ExpectType Effect<void, MongoError, never> |
| 182 | +DocumentCollection.dropIndex(collection, "birthday_1") |
| 183 | + |
| 184 | +// $ExpectType Effect<void, MongoError, never> |
| 185 | +F.pipe(collection, DocumentCollection.dropIndex("birthday_1")) |
| 186 | + |
| 187 | +// ------------------------------------------------------------------------------------- |
| 188 | +// aggregate |
| 189 | +// ------------------------------------------------------------------------------------- |
| 190 | + |
| 191 | +const groupByBirthday = [{ $group: { _id: "$birthday", birthdays: { $sum: 1 } } }] |
| 192 | + |
| 193 | +// $ExpectType DocumentAggregationCursor |
| 194 | +DocumentCollection.aggregate(collection, groupByBirthday) |
| 195 | + |
| 196 | +// $ExpectType DocumentAggregationCursor |
| 197 | +F.pipe(collection, DocumentCollection.aggregate(groupByBirthday)) |
| 198 | + |
| 199 | +// ------------------------------------------------------------------------------------- |
| 200 | +// estimatedDocumentCount |
| 201 | +// ------------------------------------------------------------------------------------- |
| 202 | + |
| 203 | +// $ExpectType Effect<number, MongoError, never> |
| 204 | +DocumentCollection.estimatedDocumentCount(collection) |
| 205 | + |
| 206 | +// $ExpectType Effect<number, MongoError, never> |
| 207 | +F.pipe(collection, DocumentCollection.estimatedDocumentCount()) |
| 208 | + |
| 209 | +// ------------------------------------------------------------------------------------- |
| 210 | +// countDocuments |
| 211 | +// ------------------------------------------------------------------------------------- |
| 212 | + |
| 213 | +// $ExpectType Effect<number, MongoError, never> |
| 214 | +DocumentCollection.countDocuments(collection, { birthday: "2024-11-28" }) |
| 215 | + |
| 216 | +// $ExpectType Effect<number, MongoError, never> |
| 217 | +F.pipe(collection, DocumentCollection.countDocuments({ birthday: "2024-11-28" })) |
| 218 | + |
| 219 | +// ------------------------------------------------------------------------------------- |
| 220 | +// typed |
| 221 | +// ----------------------------------------------------------------- |
| 222 | + |
| 223 | +// $ExpectType Collection<{ readonly birthday: Date; }, { readonly birthday: string; }, never> |
| 224 | +DocumentCollection.typed(collection, MyType) |
9 | 225 |
|
10 | 226 | // $ExpectType Collection<{ readonly birthday: Date; }, { readonly birthday: string; }, never> |
11 | | -DocumentCollection.typed(documentCollection, MyType) |
| 227 | +F.pipe(collection, DocumentCollection.typed(MyType)) |
12 | 228 |
|
13 | 229 | // @ts-expect-error |
14 | | -DocumentCollection.typed(documentCollection, Schema.Date) |
| 230 | +DocumentCollection.typed(collection, Schema.Date) |
15 | 231 |
|
16 | 232 | // TODO the following test should work, i.e. array are not acceptable as a collection type |
17 | 233 | // // @ts-expect-error |
18 | | -// DocumentCollection.typed(documentCollection, Schema.Array(MyType)) |
| 234 | +// DocumentCollection.typed(collection, Schema.Array(MyType)) |
0 commit comments