Skip to content

Commit 06d882f

Browse files
authored
fix: fix some type related bugs (#361)
1 parent 42b96d4 commit 06d882f

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

src/collection/collection.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { Document, ObjectId } from "../../deps.ts";
2-
import { MongoDriverError, MongoInvalidArgumentError } from "../error.ts";
2+
import {
3+
MongoDriverError,
4+
MongoInvalidArgumentError,
5+
MongoServerError,
6+
} from "../error.ts";
37
import { WireProtocol } from "../protocol/mod.ts";
48
import {
59
AggregateOptions,
@@ -185,7 +189,7 @@ export class Collection<T> {
185189
const { writeErrors } = res;
186190
if (writeErrors) {
187191
const [{ errmsg }] = writeErrors;
188-
throw new Error(errmsg);
192+
throw new MongoServerError(errmsg);
189193
}
190194
return {
191195
insertedIds,
@@ -199,7 +203,7 @@ export class Collection<T> {
199203
options?: UpdateOptions,
200204
) {
201205
const {
202-
upsertedIds = [],
206+
upsertedIds,
203207
upsertedCount,
204208
matchedCount,
205209
modifiedCount,
@@ -243,7 +247,7 @@ export class Collection<T> {
243247
);
244248
}
245249

246-
const { upsertedIds = [], upsertedCount, matchedCount, modifiedCount } =
250+
const { upsertedIds, upsertedCount, matchedCount, modifiedCount } =
247251
await update(
248252
this.#protocol,
249253
this.#dbName,

tests/cases/03_curd.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ObjectId } from "../../mod.ts";
12
import {
23
MongoInvalidArgumentError,
34
MongoServerError,
@@ -6,10 +7,10 @@ import { CreateCollectionOptions } from "../../src/types.ts";
67
import { testWithClient, testWithTestDBClient } from "../common.ts";
78
import { assert, assertEquals, assertRejects, semver } from "../test.deps.ts";
89

9-
interface IUser {
10+
interface User {
11+
_id: string | ObjectId;
1012
username?: string;
1113
password?: string;
12-
_id: string;
1314
uid?: number;
1415
date?: Date;
1516
}
@@ -23,7 +24,7 @@ testWithClient("testListCollectionNames", async (client) => {
2324
});
2425

2526
testWithTestDBClient("testInsertOne", async (db) => {
26-
const users = db.collection<IUser>("mongo_test_users");
27+
const users = db.collection<User>("mongo_test_users");
2728
const insertId = await users.insertOne({
2829
username: "user1",
2930
password: "pass1",
@@ -45,7 +46,7 @@ testWithTestDBClient("testInsertOne", async (db) => {
4546
});
4647

4748
testWithTestDBClient("testUpsertOne", async (db) => {
48-
const users = db.collection<IUser>("mongo_test_users");
49+
const users = db.collection<User>("mongo_test_users");
4950
await users.insertOne({
5051
username: "user1",
5152
password: "pass1",
@@ -78,7 +79,7 @@ testWithTestDBClient("testUpsertOne", async (db) => {
7879
});
7980

8081
testWithTestDBClient("testInsertOneTwice", async (db) => {
81-
const users = db.collection<IUser>("mongo_test_users");
82+
const users = db.collection<User>("mongo_test_users");
8283
await users.insertOne({
8384
_id: "aaaaaaaaaaaaaaaaaaaaaaaa",
8485
username: "user1",
@@ -89,15 +90,14 @@ testWithTestDBClient("testInsertOneTwice", async (db) => {
8990
users.insertOne({
9091
_id: "aaaaaaaaaaaaaaaaaaaaaaaa",
9192
username: "user1",
92-
// deno-lint-ignore no-explicit-any
93-
}) as any,
94-
undefined,
93+
}),
94+
MongoServerError,
9595
"E11000",
9696
);
9797
});
9898

9999
testWithTestDBClient("testFindOne", async (db) => {
100-
const users = db.collection<IUser>("mongo_test_users");
100+
const users = db.collection<User>("mongo_test_users");
101101
await users.insertOne({
102102
username: "user1",
103103
password: "pass1",
@@ -122,7 +122,7 @@ testWithTestDBClient("testFindOne", async (db) => {
122122
});
123123

124124
testWithTestDBClient("testInsertMany", async (db) => {
125-
const users = db.collection<IUser>("mongo_test_users");
125+
const users = db.collection<User>("mongo_test_users");
126126
const { insertedCount, insertedIds } = await users.insertMany([
127127
{
128128
username: "many",
@@ -171,7 +171,7 @@ testWithTestDBClient("testFindAndModify-delete", async (db) => {
171171
});
172172

173173
testWithTestDBClient("test chain call for Find", async (db) => {
174-
const users = db.collection<IUser>("mongo_test_users");
174+
const users = db.collection<User>("mongo_test_users");
175175
await users.insertMany([
176176
{
177177
username: "user1",
@@ -191,7 +191,7 @@ testWithTestDBClient("test chain call for Find", async (db) => {
191191
});
192192

193193
testWithTestDBClient("testUpdateOne", async (db) => {
194-
const users = db.collection<IUser>("mongo_test_users");
194+
const users = db.collection<User>("mongo_test_users");
195195
await users.insertOne({
196196
username: "user1",
197197
password: "pass1",
@@ -206,7 +206,7 @@ testWithTestDBClient("testUpdateOne", async (db) => {
206206
});
207207

208208
testWithTestDBClient("testUpdateOne Error", async (db) => { // TODO: move tesr errors to a new file
209-
const users = db.collection<IUser>("mongo_test_users");
209+
const users = db.collection<User>("mongo_test_users");
210210
await users.insertOne({
211211
username: "user1",
212212
password: "pass1",
@@ -220,7 +220,7 @@ testWithTestDBClient("testUpdateOne Error", async (db) => { // TODO: move tesr e
220220
});
221221

222222
testWithTestDBClient("testUpdateOneWithUpsert", async (db) => {
223-
const users = db.collection<IUser>("mongo_test_users");
223+
const users = db.collection<User>("mongo_test_users");
224224
await users.insertOne({
225225
username: "user1",
226226
password: "pass1",
@@ -236,7 +236,7 @@ testWithTestDBClient("testUpdateOneWithUpsert", async (db) => {
236236
});
237237

238238
testWithTestDBClient("testReplaceOne", async (db) => {
239-
const users = db.collection<IUser>("mongo_test_users");
239+
const users = db.collection<User>("mongo_test_users");
240240
await users.insertOne({
241241
username: "user1",
242242
password: "pass1",
@@ -254,7 +254,7 @@ testWithTestDBClient("testReplaceOne", async (db) => {
254254
});
255255

256256
testWithTestDBClient("testDeleteOne", async (db) => {
257-
const users = db.collection<IUser>("mongo_test_users");
257+
const users = db.collection<User>("mongo_test_users");
258258
await users.insertOne({
259259
username: "user1",
260260
password: "pass1",
@@ -264,7 +264,7 @@ testWithTestDBClient("testDeleteOne", async (db) => {
264264
});
265265

266266
testWithTestDBClient("testFindOr", async (db) => {
267-
const users = db.collection<IUser>("mongo_test_users");
267+
const users = db.collection<User>("mongo_test_users");
268268
await users.insertMany([
269269
{
270270
username: "user1",
@@ -289,7 +289,7 @@ testWithTestDBClient("testFindOr", async (db) => {
289289
});
290290

291291
testWithTestDBClient("testFind", async (db) => {
292-
const users = db.collection<IUser>("mongo_test_users");
292+
const users = db.collection<User>("mongo_test_users");
293293
await users.insertMany([
294294
{
295295
username: "user",
@@ -315,7 +315,7 @@ testWithTestDBClient("testFind", async (db) => {
315315
});
316316

317317
testWithTestDBClient("test multiple queries at the same time", async (db) => {
318-
const users = db.collection<IUser>("mongo_test_users");
318+
const users = db.collection<User>("mongo_test_users");
319319
await users.insertOne({
320320
_id: "aaaaaaaaaaaaaaaaaaaaaaaa",
321321
username: "user1",
@@ -335,7 +335,7 @@ testWithTestDBClient("test multiple queries at the same time", async (db) => {
335335
});
336336

337337
testWithTestDBClient("testCount", async (db) => {
338-
const users = db.collection<IUser>("mongo_test_users");
338+
const users = db.collection<User>("mongo_test_users");
339339
await users.insertMany([
340340
{
341341
username: "user",
@@ -355,7 +355,7 @@ testWithTestDBClient("testCount", async (db) => {
355355
});
356356

357357
testWithTestDBClient("testCountDocuments", async (db) => {
358-
const users = db.collection<IUser>("mongo_test_users");
358+
const users = db.collection<User>("mongo_test_users");
359359
await users.insertMany([
360360
{
361361
username: "user1",
@@ -382,7 +382,7 @@ testWithTestDBClient("testCountDocuments", async (db) => {
382382
});
383383

384384
testWithTestDBClient("testEstimatedDocumentCount", async (db) => {
385-
const users = db.collection<IUser>("mongo_test_users");
385+
const users = db.collection<User>("mongo_test_users");
386386
await users.insertMany([
387387
{
388388
username: "user1",
@@ -406,7 +406,7 @@ testWithTestDBClient("testEstimatedDocumentCount", async (db) => {
406406
});
407407

408408
testWithTestDBClient("testAggregation", async (db) => {
409-
const users = db.collection<IUser>("mongo_test_users");
409+
const users = db.collection<User>("mongo_test_users");
410410
await users.insertMany([
411411
{
412412
username: "user1",
@@ -426,7 +426,7 @@ testWithTestDBClient("testAggregation", async (db) => {
426426
},
427427
]);
428428
const docs = await users
429-
.aggregate([
429+
.aggregate<{ _id: string; total: number }>([
430430
{ $match: { username: "many" } },
431431
{ $group: { _id: "$username", total: { $sum: 1 } } },
432432
])
@@ -435,7 +435,7 @@ testWithTestDBClient("testAggregation", async (db) => {
435435
});
436436

437437
testWithTestDBClient("testUpdateMany", async (db) => {
438-
const users = db.collection<IUser>("mongo_test_users");
438+
const users = db.collection<User>("mongo_test_users");
439439
await users.insertMany([
440440
{
441441
username: "user1",
@@ -467,7 +467,7 @@ testWithTestDBClient("testUpdateMany", async (db) => {
467467
});
468468

469469
testWithTestDBClient("testDeleteMany", async (db) => {
470-
const users = db.collection<IUser>("mongo_test_users");
470+
const users = db.collection<User>("mongo_test_users");
471471
await users.insertMany([
472472
{
473473
username: "user1",
@@ -491,7 +491,7 @@ testWithTestDBClient("testDeleteMany", async (db) => {
491491
});
492492

493493
testWithTestDBClient("testDistinct", async (db) => {
494-
const users = db.collection<IUser>("mongo_test_users");
494+
const users = db.collection<User>("mongo_test_users");
495495
await users.insertMany([
496496
{
497497
username: "user1",
@@ -515,7 +515,7 @@ testWithTestDBClient("testDistinct", async (db) => {
515515
});
516516

517517
testWithTestDBClient("testDropConnection", async (db) => {
518-
const users = db.collection<IUser>("mongo_test_users");
518+
const users = db.collection<User>("mongo_test_users");
519519
await users.insertOne({
520520
_id: "aaaaaaaaaaaaaaaaaaaaaaaa",
521521
username: "user1",
@@ -526,7 +526,7 @@ testWithTestDBClient("testDropConnection", async (db) => {
526526
});
527527

528528
testWithTestDBClient("testFindWithSort", async (db) => {
529-
const users = db.collection<IUser>("mongo_test_users");
529+
const users = db.collection<User>("mongo_test_users");
530530

531531
const condition = { uid: { $exists: true } };
532532

@@ -565,7 +565,7 @@ testWithTestDBClient("testFindWithSort", async (db) => {
565565
});
566566

567567
testWithTestDBClient("testFindEmptyAsyncIteration", async (db) => {
568-
const users = db.collection<IUser>("mongo_test_users");
568+
const users = db.collection<User>("mongo_test_users");
569569
for (let i = 0; i < 10; i++) {
570570
await users.insertOne({
571571
username: "testFindWithSort",
@@ -591,7 +591,7 @@ testWithClient("testFindWithMaxTimeMS", async (client) => {
591591
"4.2.0",
592592
);
593593

594-
const users = db.collection<IUser>("mongo_test_users");
594+
const users = db.collection<User>("mongo_test_users");
595595
for (let i = 0; i < 10; i++) {
596596
await users.insertOne({
597597
username: "testFindWithMaxTimeMS",

0 commit comments

Comments
 (0)