Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
erfanium committed May 25, 2022
1 parent 4ed8a6e commit 47a7bbf
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_.ts
8 changes: 4 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true
}
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true
}
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -15,8 +17,8 @@ Replace `LATEST_VERSION` with

```ts
import {
Bson,
MongoClient,
ObjectId,
} from "https://deno.land/x/atlas_sdk@LATEST_VERSION/mod.ts";
```

Expand All @@ -35,7 +37,7 @@ const client = new MongoClient({
```ts
// Defining schema interface
interface UserSchema {
_id: ObjectId | string;
_id: ObjectId;
username: string;
password: string;
}
Expand Down Expand Up @@ -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" });
```
```
52 changes: 28 additions & 24 deletions client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bson } from "./deps.ts";
import { Document, EJSON } from "./deps.ts";

export interface MongoClientConstructorOptions {
appId: string;
Expand Down Expand Up @@ -45,7 +45,7 @@ class Database {
this.client = client;
}

collection<T = Bson.Document>(name: string) {
collection<T = Document>(name: string) {
return new Collection<T>(name, this);
}
}
Expand All @@ -70,8 +70,8 @@ class Collection<T> {
}

async findOne(
filter: Bson.Document,
{ projection }: { projection?: Bson.Document } = {},
filter: Document,
{ projection }: { projection?: Document } = {},
): Promise<T> {
const result = await this.callApi("findOne", {
filter,
Expand All @@ -81,10 +81,10 @@ class Collection<T> {
}

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;
} = {},
Expand All @@ -100,8 +100,8 @@ class Collection<T> {
}

updateOne(
filter: Bson.Document,
update: Bson.Document,
filter: Document,
update: Document,
{ upsert }: { upsert?: boolean },
): Promise<
{ matchedCount: number; modifiedCount: number; upsertedId?: string }
Expand All @@ -114,8 +114,8 @@ class Collection<T> {
}

updateMany(
filter: Bson.Document,
update: Bson.Document,
filter: Document,
update: Document,
{ upsert }: { upsert?: boolean },
): Promise<
{ matchedCount: number; modifiedCount: number; upsertedId?: string }
Expand All @@ -128,8 +128,8 @@ class Collection<T> {
}

replaceOne(
filter: Bson.Document,
replacement: Bson.Document,
filter: Document,
replacement: Document,
{ upsert }: { upsert?: boolean },
): Promise<
{ matchedCount: number; modifiedCount: number; upsertedId?: string }
Expand All @@ -141,24 +141,24 @@ class Collection<T> {
});
}

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<T = Bson.Document>(pipeline: Bson.Document[]): Promise<T[]> {
async aggregate<T = Document>(pipeline: Document[]): Promise<T[]> {
const result = await this.callApi("aggregate", { pipeline });
return result.documents;
}

async countDocuments(
filter?: Bson.Document,
filter?: Document,
options?: { limit?: number; skip?: number },
): Promise<number> {
const pipeline: Bson.Document[] = [];
const pipeline: Document[] = [];
if (filter) {
pipeline.push({ $match: filter });
}
Expand Down Expand Up @@ -189,28 +189,32 @@ class Collection<T> {
return 0;
}

async callApi(method: string, extra: Bson.Document) {
// deno-lint-ignore no-explicit-any
async callApi(method: string, extra: Document): Promise<any> {
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,
...extra,
}),
});

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);
}
}
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * as Bson from "https://deno.land/x/[email protected].0/mod.ts";
export * from "https://deno.land/x/[email protected].2/mod.ts";
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Bson } from "./deps.ts";
export * from "./deps.ts";
export { MongoClient } from "./client.ts";
export type { MongoClientConstructorOptions } from "./client.ts";
12 changes: 6 additions & 6 deletions test.ts
Original file line number Diff line number Diff line change
@@ -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 }>();
Expand All @@ -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",
Expand All @@ -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(
Expand Down

0 comments on commit 47a7bbf

Please sign in to comment.