Skip to content

Commit f249b0c

Browse files
authored
Update README.md
1 parent 8021e20 commit f249b0c

File tree

1 file changed

+182
-1
lines changed

1 file changed

+182
-1
lines changed

README.md

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,184 @@
1+
<p align="center">
2+
<img src="/logo.svg" height="120px" />
3+
</p>
4+
15
# mongodb-data-api
26

3-
MongoDB atlas [data API](https://docs.atlas.mongodb.com/api/data-api/) SDK for @nodejs
7+
MongoDB Atlas [Data API](https://docs.atlas.mongodb.com/api/data-api/) SDK for Node.js.
8+
9+
---
10+
11+
### Installation
12+
13+
```bash
14+
npm install mongodb-data-api --save
15+
```
16+
17+
or
18+
19+
```bash
20+
yarn add mongodb-data-api
21+
```
22+
23+
### Usage
24+
25+
#### Init
26+
27+
```ts
28+
import { MongoDBDataAPI, Region } from 'mongodb-data-api'
29+
30+
// init by URL Endpoint
31+
const api = new MongoDBDataAPI({
32+
apiKey: '<your_mongodb_api_key>',
33+
urlEndpoint: 'https://data.mongodb-api.com/app/<your_mongodb_app_id>/endpoint/data/beta'
34+
})
35+
36+
// or init by app ID
37+
const api = new MongoDBDataAPI({
38+
apiKey: '<your_mongodb_api_key>',
39+
appId: '<your_mongodb_app_id>'
40+
})
41+
42+
// specific region of app
43+
const api = new MongoDBDataAPI({
44+
apiKey: '<your_mongodb_api_key>',
45+
appId: '<your_mongodb_app_id>',
46+
region: Region.Virginia
47+
})
48+
```
49+
50+
#### Actions
51+
52+
See [MongoDB Data API Resources](https://docs.atlas.mongodb.com/api/data-api-resources/).
53+
54+
- [`api.findOne`](https://docs.atlas.mongodb.com/api/data-api-resources/#find-a-single-document)
55+
- [`api.find`](https://docs.atlas.mongodb.com/api/data-api-resources/#find-multiple-documents)
56+
- [`api.insertOne`](https://docs.atlas.mongodb.com/api/data-api-resources/#insert-a-single-document)
57+
- [`api.insertMany`](https://docs.atlas.mongodb.com/api/data-api-resources/#insert-multiple-documents)
58+
- [`api.updateOne`](https://docs.atlas.mongodb.com/api/data-api-resources/#update-a-single-document)
59+
- [`api.updateMany`](https://docs.atlas.mongodb.com/api/data-api-resources/#update-multiple-documents)
60+
- [`api.replaceOne`](https://docs.atlas.mongodb.com/api/data-api-resources/#replace-a-single-document)
61+
- [`api.deleteOne`](https://docs.atlas.mongodb.com/api/data-api-resources/#delete-a-single-document)
62+
- [`api.deleteMany`](https://docs.atlas.mongodb.com/api/data-api-resources/#delete-multiple-documents)
63+
- [`api.aggregate`](https://docs.atlas.mongodb.com/api/data-api-resources/#run-an-aggregation-pipeline)
64+
65+
#### Action examples
66+
67+
1. find a single document
68+
69+
```ts
70+
api
71+
.findOne({
72+
dataSource: '<target_cluster_name>',
73+
database: '<target_database_name>',
74+
collection: '<target_collection_name>',
75+
filter: { name: 'Surmon' }
76+
})
77+
.then((result) => {
78+
console.log(result.document)
79+
})
80+
```
81+
82+
2. insert a single document
83+
84+
```ts
85+
api
86+
.insertOne({
87+
dataSource: '<target_cluster_name>',
88+
database: '<target_database_name>',
89+
collection: '<target_collection_name>',
90+
document: {
91+
name: 'Surmon',
92+
age: 19
93+
}
94+
})
95+
.then((result) => {
96+
console.log(result.insertedId)
97+
})
98+
```
99+
100+
3. run an aggregation pipeline
101+
102+
```ts
103+
api
104+
.aggregate({
105+
dataSource: '<target_cluster_name>',
106+
database: '<target_database_name>',
107+
collection: '<target_collection_name>',
108+
pipeline: [
109+
{ $match: { status: 'urgent' } },
110+
{ $group: { _id: '$productName', sumQuantity: { $sum: '$quantity' } } }
111+
]
112+
})
113+
.then((result) => {
114+
console.log(result.documents)
115+
})
116+
```
117+
118+
#### Method chaining
119+
120+
```ts
121+
// api.$cluster
122+
const clusterA = api.$cluster('a')
123+
124+
// api.$cluster.$database
125+
const databaseB = clusterA.$database('b')
126+
const databaseC = clusterA.$database('c')
127+
128+
// api.$cluster.$database.$collection
129+
const bItemCollection = databaseB.$collection('item')
130+
const cItemCollection = databaseC.$collection('item')
131+
132+
// actions
133+
bItemCollection.findOne({ filter: {/*...*/} })
134+
cItemCollection.insertOne({ document: {/*...*/} })
135+
136+
// -------------
137+
138+
// chaining is equivalent to the api call
139+
api.$cluster('a').$database('b').$collection('c').findOne({ filter: {} })
140+
// the same as
141+
api.findOne({
142+
dataSource: 'a',
143+
database: 'b',
144+
collection: 'c',
145+
filter: {}
146+
})
147+
```
148+
149+
#### Specific Action
150+
151+
You can specific the action request to prevent this package from lagging relative to the official one.
152+
153+
```ts
154+
api.$$action('findOne', {
155+
dataSource: '...',
156+
database: '...',
157+
collection: '...',
158+
filter: {}
159+
})
160+
```
161+
162+
### Development
163+
164+
```bash
165+
# install dependencies
166+
yarn
167+
168+
# lint
169+
yarn lint
170+
171+
# test
172+
yarn test
173+
174+
# build
175+
yarn build
176+
```
177+
178+
### Changelog
179+
180+
Detailed changes for each release are documented in the [release notes](https://github.com/surmon-china/mongodb-data-api/blob/master/CHANGELOG.md).
181+
182+
### License
183+
184+
[MIT](https://github.com/surmon-china/mongodb-data-api/blob/master/LICENSE)

0 commit comments

Comments
 (0)