From 47a7bbfcbad58215f0a18dc71a7414cebe195ab7 Mon Sep 17 00:00:00 2001 From: ERFANIUM Date: Thu, 26 May 2022 01:40:43 +0430 Subject: [PATCH] v0.2.0 --- .gitignore | 1 + .vscode/settings.json | 8 +++---- README.md | 10 +++++---- client.ts | 52 +++++++++++++++++++++++-------------------- deps.ts | 2 +- mod.ts | 2 +- test.ts | 12 +++++----- 7 files changed, 47 insertions(+), 40 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a47fc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_.ts \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 3002255..e40716f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { - "deno.enable": true, - "deno.lint": true, - "deno.unstable": true -} \ No newline at end of file + "deno.enable": true, + "deno.lint": true, + "deno.unstable": true +} diff --git a/README.md b/README.md index fbd0d0b..f131db8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Atlas SDK -> **atlas_sdk** is a TypeSafe [MongoDB Atlas Data API](https://www.mongodb.com/docs/atlas/api/data-api/#introduction) SDK for Deno & Deno Deploy +> **atlas_sdk** is a TypeSafe +> [MongoDB Atlas Data API](https://www.mongodb.com/docs/atlas/api/data-api/#introduction) +> SDK for Deno & Deno Deploy [![Discord server](https://img.shields.io/discord/768918486575480863?color=blue&label=Ask%20for%20help%20here&logo=discord&style=flat-square)](https://discord.gg/HEdTCvZUSf) @@ -15,8 +17,8 @@ Replace `LATEST_VERSION` with ```ts import { - Bson, MongoClient, + ObjectId, } from "https://deno.land/x/atlas_sdk@LATEST_VERSION/mod.ts"; ``` @@ -35,7 +37,7 @@ const client = new MongoClient({ ```ts // Defining schema interface interface UserSchema { - _id: ObjectId | string; + _id: ObjectId; username: string; password: string; } @@ -128,4 +130,4 @@ const { matchedCount, modifiedCount, upsertedId } = await users.replaceOne( const deleteCount = await users.deleteOne({ _id: insertId }); const deleteCount2 = await users.deleteMany({ username: "test" }); -``` \ No newline at end of file +``` diff --git a/client.ts b/client.ts index 79acef4..07080f2 100644 --- a/client.ts +++ b/client.ts @@ -1,4 +1,4 @@ -import { Bson } from "./deps.ts"; +import { Document, EJSON } from "./deps.ts"; export interface MongoClientConstructorOptions { appId: string; @@ -45,7 +45,7 @@ class Database { this.client = client; } - collection(name: string) { + collection(name: string) { return new Collection(name, this); } } @@ -70,8 +70,8 @@ class Collection { } async findOne( - filter: Bson.Document, - { projection }: { projection?: Bson.Document } = {}, + filter: Document, + { projection }: { projection?: Document } = {}, ): Promise { const result = await this.callApi("findOne", { filter, @@ -81,10 +81,10 @@ class Collection { } async find( - filter?: Bson.Document, + filter?: Document, { projection, sort, limit, skip }: { - projection?: Bson.Document; - sort?: Bson.Document; + projection?: Document; + sort?: Document; limit?: number; skip?: number; } = {}, @@ -100,8 +100,8 @@ class Collection { } updateOne( - filter: Bson.Document, - update: Bson.Document, + filter: Document, + update: Document, { upsert }: { upsert?: boolean }, ): Promise< { matchedCount: number; modifiedCount: number; upsertedId?: string } @@ -114,8 +114,8 @@ class Collection { } updateMany( - filter: Bson.Document, - update: Bson.Document, + filter: Document, + update: Document, { upsert }: { upsert?: boolean }, ): Promise< { matchedCount: number; modifiedCount: number; upsertedId?: string } @@ -128,8 +128,8 @@ class Collection { } replaceOne( - filter: Bson.Document, - replacement: Bson.Document, + filter: Document, + replacement: Document, { upsert }: { upsert?: boolean }, ): Promise< { matchedCount: number; modifiedCount: number; upsertedId?: string } @@ -141,24 +141,24 @@ class Collection { }); } - deleteOne(filter: Bson.Document): Promise<{ deletedCount: number }> { + deleteOne(filter: Document): Promise<{ deletedCount: number }> { return this.callApi("deleteOne", { filter }); } - deleteMany(filter: Bson.Document): Promise<{ deletedCount: number }> { + deleteMany(filter: Document): Promise<{ deletedCount: number }> { return this.callApi("deleteMany", { filter }); } - async aggregate(pipeline: Bson.Document[]): Promise { + async aggregate(pipeline: Document[]): Promise { const result = await this.callApi("aggregate", { pipeline }); return result.documents; } async countDocuments( - filter?: Bson.Document, + filter?: Document, options?: { limit?: number; skip?: number }, ): Promise { - const pipeline: Bson.Document[] = []; + const pipeline: Document[] = []; if (filter) { pipeline.push({ $match: filter }); } @@ -189,17 +189,19 @@ class Collection { return 0; } - async callApi(method: string, extra: Bson.Document) { + // deno-lint-ignore no-explicit-any + async callApi(method: string, extra: Document): Promise { const { endpoint, appId, apiKey, dataSource } = this.client; const url = `${endpoint}/app/${appId}/endpoint/data/beta/action/${method}`; const response = await this.client.fetch(url, { method: "POST", headers: { - "Content-Type": "application/json", + "Content-Type": "application/ejson", + "Accept": "application/ejson", "api-key": apiKey, }, - body: Bson.EJSON.stringify({ + body: EJSON.stringify({ collection: this.name, database: this.database.name, dataSource: dataSource, @@ -207,10 +209,12 @@ class Collection { }), }); - if (response.ok) { - return response.json(); + const body = await response.text(); + + if (!response.ok) { + throw new Error(`${response.statusText}: ${body}`); } - throw new Error(`${response.statusText}: ${await response.text()}`); + return EJSON.parse(body); } } diff --git a/deps.ts b/deps.ts index aac36e6..83137a6 100644 --- a/deps.ts +++ b/deps.ts @@ -1 +1 @@ -export * as Bson from "https://deno.land/x/web_bson@v0.2.0/mod.ts"; +export * from "https://deno.land/x/web_bson@v0.2.2/mod.ts"; diff --git a/mod.ts b/mod.ts index 5e50366..58e7f24 100644 --- a/mod.ts +++ b/mod.ts @@ -1,3 +1,3 @@ -export { Bson } from "./deps.ts"; +export * from "./deps.ts"; export { MongoClient } from "./client.ts"; export type { MongoClientConstructorOptions } from "./client.ts"; diff --git a/test.ts b/test.ts index a2783fd..aaf4bc6 100644 --- a/test.ts +++ b/test.ts @@ -1,7 +1,7 @@ // deno-lint-ignore-file require-await -import { Bson, MongoClient } from "./mod.ts"; -import { deferred } from "https://deno.land/std@0.135.0/async/deferred.ts"; -import { assertEquals } from "https://deno.land/std@0.135.0/testing/asserts.ts"; +import { MongoClient, ObjectId } from "./mod.ts"; +import { deferred } from "https://deno.land/std@0.140.0/async/deferred.ts"; +import { assertEquals } from "https://deno.land/std@0.140.0/testing/asserts.ts"; Deno.test("Sample Test", async () => { const fetchMock = deferred<{ url: string; init: RequestInit }>(); @@ -14,12 +14,12 @@ Deno.test("Sample Test", async () => { fetchMock.resolve({ url, init }); return { ok: true, - json: async () => ({ ok: true }), + text: async () => (JSON.stringify({ ok: true })), }; }) as typeof fetch, }); - const _id = new Bson.ObjectId(); + const _id = new ObjectId(); client.database("db-name").collection("c-name").insertOne({ _id, foo: "bar", @@ -33,7 +33,7 @@ Deno.test("Sample Test", async () => { assertEquals(init.method, "POST"); assertEquals( new Headers(init.headers).get("Content-Type"), - "application/json", + "application/ejson", ); assertEquals(new Headers(init.headers).get("api-key"), "API_KEY"); assertEquals(