Skip to content

Commit

Permalink
fix: fix some type related bugs (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
erfanium authored Jun 17, 2022
1 parent 42b96d4 commit 06d882f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
12 changes: 8 additions & 4 deletions src/collection/collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Document, ObjectId } from "../../deps.ts";
import { MongoDriverError, MongoInvalidArgumentError } from "../error.ts";
import {
MongoDriverError,
MongoInvalidArgumentError,
MongoServerError,
} from "../error.ts";
import { WireProtocol } from "../protocol/mod.ts";
import {
AggregateOptions,
Expand Down Expand Up @@ -185,7 +189,7 @@ export class Collection<T> {
const { writeErrors } = res;
if (writeErrors) {
const [{ errmsg }] = writeErrors;
throw new Error(errmsg);
throw new MongoServerError(errmsg);
}
return {
insertedIds,
Expand All @@ -199,7 +203,7 @@ export class Collection<T> {
options?: UpdateOptions,
) {
const {
upsertedIds = [],
upsertedIds,
upsertedCount,
matchedCount,
modifiedCount,
Expand Down Expand Up @@ -243,7 +247,7 @@ export class Collection<T> {
);
}

const { upsertedIds = [], upsertedCount, matchedCount, modifiedCount } =
const { upsertedIds, upsertedCount, matchedCount, modifiedCount } =
await update(
this.#protocol,
this.#dbName,
Expand Down
62 changes: 31 additions & 31 deletions tests/cases/03_curd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ObjectId } from "../../mod.ts";
import {
MongoInvalidArgumentError,
MongoServerError,
Expand All @@ -6,10 +7,10 @@ import { CreateCollectionOptions } from "../../src/types.ts";
import { testWithClient, testWithTestDBClient } from "../common.ts";
import { assert, assertEquals, assertRejects, semver } from "../test.deps.ts";

interface IUser {
interface User {
_id: string | ObjectId;
username?: string;
password?: string;
_id: string;
uid?: number;
date?: Date;
}
Expand All @@ -23,7 +24,7 @@ testWithClient("testListCollectionNames", async (client) => {
});

testWithTestDBClient("testInsertOne", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
const insertId = await users.insertOne({
username: "user1",
password: "pass1",
Expand All @@ -45,7 +46,7 @@ testWithTestDBClient("testInsertOne", async (db) => {
});

testWithTestDBClient("testUpsertOne", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertOne({
username: "user1",
password: "pass1",
Expand Down Expand Up @@ -78,7 +79,7 @@ testWithTestDBClient("testUpsertOne", async (db) => {
});

testWithTestDBClient("testInsertOneTwice", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertOne({
_id: "aaaaaaaaaaaaaaaaaaaaaaaa",
username: "user1",
Expand All @@ -89,15 +90,14 @@ testWithTestDBClient("testInsertOneTwice", async (db) => {
users.insertOne({
_id: "aaaaaaaaaaaaaaaaaaaaaaaa",
username: "user1",
// deno-lint-ignore no-explicit-any
}) as any,
undefined,
}),
MongoServerError,
"E11000",
);
});

testWithTestDBClient("testFindOne", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertOne({
username: "user1",
password: "pass1",
Expand All @@ -122,7 +122,7 @@ testWithTestDBClient("testFindOne", async (db) => {
});

testWithTestDBClient("testInsertMany", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
const { insertedCount, insertedIds } = await users.insertMany([
{
username: "many",
Expand Down Expand Up @@ -171,7 +171,7 @@ testWithTestDBClient("testFindAndModify-delete", async (db) => {
});

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

testWithTestDBClient("testUpdateOne", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertOne({
username: "user1",
password: "pass1",
Expand All @@ -206,7 +206,7 @@ testWithTestDBClient("testUpdateOne", async (db) => {
});

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

testWithTestDBClient("testUpdateOneWithUpsert", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertOne({
username: "user1",
password: "pass1",
Expand All @@ -236,7 +236,7 @@ testWithTestDBClient("testUpdateOneWithUpsert", async (db) => {
});

testWithTestDBClient("testReplaceOne", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertOne({
username: "user1",
password: "pass1",
Expand All @@ -254,7 +254,7 @@ testWithTestDBClient("testReplaceOne", async (db) => {
});

testWithTestDBClient("testDeleteOne", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertOne({
username: "user1",
password: "pass1",
Expand All @@ -264,7 +264,7 @@ testWithTestDBClient("testDeleteOne", async (db) => {
});

testWithTestDBClient("testFindOr", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertMany([
{
username: "user1",
Expand All @@ -289,7 +289,7 @@ testWithTestDBClient("testFindOr", async (db) => {
});

testWithTestDBClient("testFind", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertMany([
{
username: "user",
Expand All @@ -315,7 +315,7 @@ testWithTestDBClient("testFind", async (db) => {
});

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

testWithTestDBClient("testCount", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertMany([
{
username: "user",
Expand All @@ -355,7 +355,7 @@ testWithTestDBClient("testCount", async (db) => {
});

testWithTestDBClient("testCountDocuments", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertMany([
{
username: "user1",
Expand All @@ -382,7 +382,7 @@ testWithTestDBClient("testCountDocuments", async (db) => {
});

testWithTestDBClient("testEstimatedDocumentCount", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertMany([
{
username: "user1",
Expand All @@ -406,7 +406,7 @@ testWithTestDBClient("testEstimatedDocumentCount", async (db) => {
});

testWithTestDBClient("testAggregation", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertMany([
{
username: "user1",
Expand All @@ -426,7 +426,7 @@ testWithTestDBClient("testAggregation", async (db) => {
},
]);
const docs = await users
.aggregate([
.aggregate<{ _id: string; total: number }>([
{ $match: { username: "many" } },
{ $group: { _id: "$username", total: { $sum: 1 } } },
])
Expand All @@ -435,7 +435,7 @@ testWithTestDBClient("testAggregation", async (db) => {
});

testWithTestDBClient("testUpdateMany", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertMany([
{
username: "user1",
Expand Down Expand Up @@ -467,7 +467,7 @@ testWithTestDBClient("testUpdateMany", async (db) => {
});

testWithTestDBClient("testDeleteMany", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertMany([
{
username: "user1",
Expand All @@ -491,7 +491,7 @@ testWithTestDBClient("testDeleteMany", async (db) => {
});

testWithTestDBClient("testDistinct", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertMany([
{
username: "user1",
Expand All @@ -515,7 +515,7 @@ testWithTestDBClient("testDistinct", async (db) => {
});

testWithTestDBClient("testDropConnection", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
await users.insertOne({
_id: "aaaaaaaaaaaaaaaaaaaaaaaa",
username: "user1",
Expand All @@ -526,7 +526,7 @@ testWithTestDBClient("testDropConnection", async (db) => {
});

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

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

Expand Down Expand Up @@ -565,7 +565,7 @@ testWithTestDBClient("testFindWithSort", async (db) => {
});

testWithTestDBClient("testFindEmptyAsyncIteration", async (db) => {
const users = db.collection<IUser>("mongo_test_users");
const users = db.collection<User>("mongo_test_users");
for (let i = 0; i < 10; i++) {
await users.insertOne({
username: "testFindWithSort",
Expand All @@ -591,7 +591,7 @@ testWithClient("testFindWithMaxTimeMS", async (client) => {
"4.2.0",
);

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

0 comments on commit 06d882f

Please sign in to comment.