Skip to content

Commit da31577

Browse files
committed
use json() method and update readme
1 parent 007ef6a commit da31577

File tree

3 files changed

+66
-69
lines changed

3 files changed

+66
-69
lines changed

README.md

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
1-
# Deno MongoDB Atlas Data API Wrapper
1+
# Deno MongoDB Atlas Data API
22

3-
This is a simple library that turns the long and repeating code to access the MongoDB Data API into more simple, readable, and maintainable code.
3+
This is a simple library that provides an interface to the MongoDB Atlas Data API. The library can be used to perform basic CRUD (Create, Read, Update, and Delete) operations on a MongoDB database without the need for a dedicated driver.
4+
5+
The library is written in TypeScript, and it provides TypeScript definitions for all of its methods.
6+
7+
## Usage
8+
To use the DataAPI library, you first need to initialize it with your MongoDB Atlas API key and the URL of your Data API endpoint. You can then use the insertOne, insertMany, findOne, find, updateOne, and deleteOne methods to perform CRUD operations on your database.
9+
10+
Here's an example of how you can use the library to insert a document into a collection:
411

5-
This library can simplify your code from this:
612
```ts
7-
const URL = BASE_URL + '/insertOne';
8-
const response = await fetch(URL, {
9-
method: 'POST',
10-
headers: {
11-
'Content-Type': 'application/json',
12-
'api-key': API_KEY,
13-
},
14-
body: JSON.stringify({
15-
collection: 'collectionName',
16-
database: 'databaseName',
17-
dataSource: 'the-data-source',
18-
document: {
19-
name: 'test',
20-
age: 17
21-
},
22-
}),
13+
import { DataAPI } from "https://deno.land/x/mongodbdataapi/mod.ts";
14+
15+
const api = DataAPI.init(
16+
"https://data.mongodb-api.com/app/data-abcde/endpoint/data/v1/action", // remember to add "/action"!
17+
"my-cluster",
18+
"my-database",
19+
"my-api-key",
20+
);
21+
22+
const result = await api.insertOne("my-collection", {
23+
name: "John Doe",
24+
age: 42,
2325
});
24-
```
25-
to this:
26-
```ts
27-
const instance = DataAPI.get();
28-
instance.insertOne('collectionName', {name: 'test', age: 17})
29-
```
30-
But make sure to initialize the DataAPI instance with this code first:
31-
```ts
32-
import { DataAPI } from 'https://deno.land/x/mongodbdataapi/mod.ts';
3326

34-
const instance = DataAPI.init(BASE_URL + '/action', DATA_SOURCE, DATABASE, API_KEY) // returns the DataAPI instance
27+
console.log(result);
3528
```
3629

30+
The `init` method initializes the library with the URL of the Data API endpoint, the name of the cluster, the name of the database, and the API key. The insertOne method is then used to insert a document into the my-collection collection. The result of the operation is logged to the console.
31+
3732
## When to Use the Data API
3833

3934
*taken straight from this [documentation page](https://www.mongodb.com/docs/atlas/api/data-api/)*

dataApi.ts

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ export class DataAPI {
2929
}
3030
/**
3131
* @param url for example: " https://data.mongodb-api.com/app/data-abcde/endpoint/data/v1/action "
32+
* @param dataSource your cluster
33+
* @param databaseName database name inside your cluster
3234
* @param apiKey
33-
* @returns
35+
*
36+
* @see https://www.mongodb.com/docs/atlas/api/data-api/
3437
*/
3538
static init(
3639
url: string,
@@ -52,6 +55,12 @@ export class DataAPI {
5255
}
5356
return DataAPI.instance!;
5457
}
58+
/**
59+
* @param collection
60+
* @param document
61+
*
62+
* @see https://www.mongodb.com/docs/atlas/api/data-api/
63+
*/
5564
// deno-lint-ignore no-explicit-any
5665
async insertOne(collection: string, document: any) {
5766
const URI = this.url + "/insertOne";
@@ -67,12 +76,16 @@ export class DataAPI {
6776
document,
6877
}),
6978
});
70-
const json = (await readJSON(response.body!)) as Inserted;
71-
return json;
79+
return await response.json() as Inserted;
7280
}
7381

74-
// deno-lint-ignore no-explicit-any
75-
async insertMany(collection: string, documents: any[]) {
82+
/**
83+
* @param collection
84+
* @param documents
85+
*
86+
* @see https://www.mongodb.com/docs/atlas/api/data-api/
87+
*/
88+
async insertMany<T>(collection: string, documents: T[]) {
7689
const URI = this.url + "/insertMany";
7790
const response = await fetch(URI, {
7891
method: "POST",
@@ -86,11 +99,16 @@ export class DataAPI {
8699
documents,
87100
}),
88101
});
89-
const json = (await readJSON(response.body!)) as InsertedMany;
90-
return json;
102+
return await response.json() as InsertedMany;
91103
}
104+
/**
105+
* @param collection
106+
* @param filter
107+
*
108+
* @see https://www.mongodb.com/docs/atlas/api/data-api/
109+
*/
92110
// deno-lint-ignore no-explicit-any
93-
async findOne(collection: string, filter: any) {
111+
async findOne<T>(collection: string, filter: any) {
94112
const URI = this.url + "/findOne";
95113
const response = await fetch(URI, {
96114
method: "POST",
@@ -104,17 +122,17 @@ export class DataAPI {
104122
filter,
105123
}),
106124
});
107-
const json = (await readJSON(response.body!)) as FindOne;
108-
return json;
125+
return await response.json() as FindOne<T>;
109126
}
110127
/**
111128
* @param collection
112129
* @param filter example: { "breed": "Labrador", "age": { "$gt" : 2} }
113130
* @param sort example: { "age" : 1 }
114-
* @returns
131+
*
132+
* @see https://www.mongodb.com/docs/atlas/api/data-api/
115133
*/
116134
// deno-lint-ignore no-explicit-any
117-
async find(collection: string, filter: any, sort?: any) {
135+
async find<T>(collection: string, filter: any, sort?: any) {
118136
const URI = this.url + "/find";
119137
const response = await fetch(URI, {
120138
method: "POST",
@@ -129,8 +147,7 @@ export class DataAPI {
129147
sort,
130148
}),
131149
});
132-
const json = (await readJSON(response.body!)) as Find;
133-
return json;
150+
return await response.json() as Find<T>;
134151
}
135152
/**
136153
* @param collection
@@ -154,8 +171,7 @@ export class DataAPI {
154171
update,
155172
}),
156173
});
157-
const json = (await readJSON(response.body!)) as UpdateOne;
158-
return json;
174+
return await response.json() as UpdateOne;
159175
}
160176
// deno-lint-ignore no-explicit-any
161177
async deleteOne(collection: string, filter: any) {
@@ -172,8 +188,7 @@ export class DataAPI {
172188
filter,
173189
}),
174190
});
175-
const json = (await readJSON(response.body!)) as DeleteOne;
176-
return json;
191+
return await response.json() as DeleteOne;
177192
}
178193
// deno-lint-ignore no-explicit-any
179194
async aggregate(collection: string, pipeline: any) {
@@ -190,8 +205,8 @@ export class DataAPI {
190205
pipeline,
191206
}),
192207
});
193-
const json = await readJSON(response.body!);
194-
return json;
208+
209+
return await response.json();
195210
}
196211
/**
197212
* ```ts
@@ -209,7 +224,8 @@ export class DataAPI {
209224
}),
210225
});
211226
* ```
212-
* for more information, please [read](https://www.mongodb.com/developer/products/atlas/atlas-data-api-introduction/)
227+
*
228+
* @see https://www.mongodb.com/docs/atlas/api/data-api/
213229
*/
214230
// deno-lint-ignore no-explicit-any
215231
async custom(collection: string, action: string, query: any) {
@@ -226,18 +242,6 @@ export class DataAPI {
226242
...query,
227243
}),
228244
});
229-
const json = await readJSON(response.body!);
230-
return json;
245+
return await response.json();
231246
}
232247
}
233-
234-
async function readText(rs: ReadableStream<Uint8Array>) {
235-
const reader = rs.getReader();
236-
const value = (await reader.read()).value;
237-
reader.cancel();
238-
return new TextDecoder().decode(value);
239-
}
240-
241-
async function readJSON(rs: ReadableStream<Uint8Array>) {
242-
return JSON.parse(await readText(rs));
243-
}

types.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ export type InsertedMany = {
66
insertedIds: string[];
77
};
88

9-
export type FindOne = {
10-
// deno-lint-ignore no-explicit-any
11-
document: any;
9+
export type FindOne<T> = {
10+
document: T;
1211
};
1312

14-
export type Find = {
15-
// deno-lint-ignore no-explicit-any
16-
documents: any[];
13+
export type Find<T> = {
14+
documents: T[];
1715
};
1816

1917
export type UpdateOne = {

0 commit comments

Comments
 (0)