From b51102576c37010656ce6fb2760635aa0ba6cab7 Mon Sep 17 00:00:00 2001 From: Enok <416828041@qq.com> Date: Sat, 7 Mar 2020 23:17:39 +0800 Subject: [PATCH] add ObjectId --- README.md | 2 +- mod.ts | 3 ++- test.ts | 6 ++++-- ts/types.ts | 11 +++++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 98b0dd46..8fc3d4b6 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ ## Examples ```ts -import { init, MongoClient } from "https://deno.land/x/mongo@v0.3.1/mod.ts"; +import { init, MongoClient } from "https://deno.land/x/mongo@v0.3.2/mod.ts"; // Initialize the plugin await init(); diff --git a/mod.ts b/mod.ts index fca94280..6b71cc7c 100644 --- a/mod.ts +++ b/mod.ts @@ -2,5 +2,6 @@ export * from "./ts/client.ts"; export * from "./ts/collection.ts"; export * from "./ts/database.ts"; export * from "./ts/result.ts"; +export { ObjectId } from "./ts/types.ts"; export * from "./ts/util.ts"; -export const VERSION = "v0.3.1"; +export const VERSION = "v0.3.2"; diff --git a/test.ts b/test.ts index 91270c1e..6fe1362a 100644 --- a/test.ts +++ b/test.ts @@ -1,6 +1,8 @@ import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts"; import { cargoBuild } from "./build.ts"; import { init, MongoClient } from "./mod.ts"; +import "./ts/tests/types-check.test.ts"; +import { ObjectId } from "./ts/types.ts"; const { test, runTests } = Deno; @@ -37,7 +39,7 @@ test(async function testListCollectionNames() { test(async function testInsertOne() { const db = getClient().database("test"); const users = db.collection("mongo_test_users"); - const insertId = await users.insertOne({ + const insertId: ObjectId = await users.insertOne({ username: "user1", password: "pass1" }); @@ -45,7 +47,7 @@ test(async function testInsertOne() { assertEquals(Object.keys(insertId), ["$oid"]); const user1 = await users.findOne({ - _id: insertId + _id: ObjectId(insertId.$oid) }); assertEquals(user1, { diff --git a/ts/types.ts b/ts/types.ts index be160b56..6f1c2954 100644 --- a/ts/types.ts +++ b/ts/types.ts @@ -1,3 +1,5 @@ +import { assert } from "https://deno.land/std@v0.35.0/testing/asserts.ts"; + export enum CommandType { ConnectWithUri = "ConnectWithUri", ConnectWithOptions = "ConnectWithOptions", @@ -9,3 +11,12 @@ export enum CommandType { Delete = "Delete", Update = "Update" } + +export interface ObjectId { + $oid: string; +} +export function ObjectId($oid: string) { + const isLegal = /[0-9a-fA-F]{24}/.test($oid); + assert(isLegal, `ObjectId("${$oid}") is not legal.`); + return { $oid }; +}