Skip to content

Commit

Permalink
add ObjectId
Browse files Browse the repository at this point in the history
add ObjectId
  • Loading branch information
manyuanrong authored Mar 7, 2020
2 parents 1185ac4 + b511025 commit 4c61aa2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
## Examples

```ts
import { init, MongoClient } from "https://deno.land/x/[email protected].1/mod.ts";
import { init, MongoClient } from "https://deno.land/x/[email protected].2/mod.ts";

// Initialize the plugin
await init();
Expand Down
3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
6 changes: 4 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -37,15 +39,15 @@ 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"
});

assertEquals(Object.keys(insertId), ["$oid"]);

const user1 = await users.findOne({
_id: insertId
_id: ObjectId(insertId.$oid)
});

assertEquals(user1, {
Expand Down
11 changes: 11 additions & 0 deletions ts/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";

export enum CommandType {
ConnectWithUri = "ConnectWithUri",
ConnectWithOptions = "ConnectWithOptions",
Expand All @@ -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 };
}

0 comments on commit 4c61aa2

Please sign in to comment.