|
2 | 2 | <img src="/logo.png" height="120px" /> |
3 | 3 | </p> |
4 | 4 |
|
5 | | -# mongodb-data-api |
| 5 | +# mongodb-data-api-fetch |
6 | 6 |
|
7 | | -[](https://github.com/surmon-china/mongodb-data-api/stargazers) |
| 7 | +[](https://github.com/trophologist/mongodb-data-api-fetch/stargazers) |
8 | 8 | |
9 | | -[](https://www.npmjs.com/package/mongodb-data-api) |
| 9 | +[](https://www.npmjs.com/package/mongodb-data-api-fetch) |
10 | 10 | |
11 | | -[](https://codecov.io/gh/surmon-china/mongodb-data-api) |
12 | | - |
13 | | -[](/LICENSE) |
| 11 | +[](/LICENSE) |
14 | 12 |
|
15 | | -MongoDB Atlas [Data API](https://www.mongodb.com/docs/atlas/api/data-api/) SDK for Node.js. |
| 13 | +MongoDB Atlas [Data API](https://www.mongodb.com/docs/atlas/api/data-api/) SDK for Cloudflare Workers. |
16 | 14 |
|
17 | | ---- |
| 15 | +Fully compatible with https://github.com/surmon-china/mongodb-data-api/ |
18 | 16 |
|
19 | 17 | ### Installation |
20 | 18 |
|
21 | 19 | ```bash |
22 | | -npm install mongodb-data-api --save |
| 20 | +npm install mongodb-data-api-fetch --save |
23 | 21 | ``` |
24 | 22 |
|
25 | 23 | or |
26 | 24 |
|
27 | 25 | ```bash |
28 | | -yarn add mongodb-data-api |
| 26 | +yarn add mongodb-data-api-fetch |
29 | 27 | ``` |
30 | 28 |
|
31 | 29 | ### Usage |
32 | 30 |
|
33 | 31 | #### Init |
34 | 32 |
|
35 | 33 | ```ts |
36 | | -import { createMongoDBDataAPI } from 'mongodb-data-api' |
| 34 | +import { createMongoDBDataAPI } from 'mongodb-data-api-fetch' |
37 | 35 |
|
38 | 36 | // init by URL Endpoint |
39 | 37 | const api = createMongoDBDataAPI({ |
40 | 38 | apiKey: '<your_mongodb_api_key>', |
41 | | - urlEndpoint: 'https://data.mongodb-api.com/app/<your_mongodb_app_id>/endpoint/data/v1' |
| 39 | + urlEndpoint: |
| 40 | + 'https://data.mongodb-api.com/app/<your_mongodb_app_id>/endpoint/data/v1' |
42 | 41 | }) |
43 | 42 |
|
44 | 43 | // or init by app ID |
@@ -76,52 +75,40 @@ See [MongoDB Data API Resources](https://www.mongodb.com/docs/atlas/api/data-api |
76 | 75 | 1. find a single document |
77 | 76 |
|
78 | 77 | ```ts |
79 | | -api |
80 | | - .findOne({ |
81 | | - dataSource: '<target_cluster_name>', |
82 | | - database: '<target_database_name>', |
83 | | - collection: '<target_collection_name>', |
84 | | - filter: { name: 'Surmon' } |
85 | | - }) |
86 | | - .then((result) => { |
87 | | - console.log(result.document) |
88 | | - }) |
| 78 | +const { document } = await api.findOne({ |
| 79 | + dataSource: '<target_cluster_name>', |
| 80 | + database: '<target_database_name>', |
| 81 | + collection: '<target_collection_name>', |
| 82 | + filter: { name: 'Surmon' } |
| 83 | +}) |
89 | 84 | ``` |
90 | 85 |
|
91 | 86 | 2. insert a single document |
92 | 87 |
|
93 | 88 | ```ts |
94 | | -api |
95 | | - .insertOne({ |
96 | | - dataSource: '<target_cluster_name>', |
97 | | - database: '<target_database_name>', |
98 | | - collection: '<target_collection_name>', |
99 | | - document: { |
100 | | - name: 'Surmon', |
101 | | - age: 19 |
102 | | - } |
103 | | - }) |
104 | | - .then((result) => { |
105 | | - console.log(result.insertedId) |
106 | | - }) |
| 89 | +const { insertedId } = await api.insertOne({ |
| 90 | + dataSource: '<target_cluster_name>', |
| 91 | + database: '<target_database_name>', |
| 92 | + collection: '<target_collection_name>', |
| 93 | + document: { |
| 94 | + name: 'Surmon', |
| 95 | + age: 19 |
| 96 | + } |
| 97 | +}) |
107 | 98 | ``` |
108 | 99 |
|
109 | 100 | 3. run an aggregation pipeline |
110 | 101 |
|
111 | 102 | ```ts |
112 | | -api |
113 | | - .aggregate({ |
114 | | - dataSource: '<target_cluster_name>', |
115 | | - database: '<target_database_name>', |
116 | | - collection: '<target_collection_name>', |
117 | | - pipeline: [ |
118 | | - { $match: { status: 'urgent' } }, |
119 | | - { $group: { _id: '$productName', sumQuantity: { $sum: '$quantity' } } } |
120 | | - ] |
121 | | - }) |
122 | | - .then((result) => { |
123 | | - console.log(result.documents) |
124 | | - }) |
| 103 | +const { document } = await api.aggregate({ |
| 104 | + dataSource: '<target_cluster_name>', |
| 105 | + database: '<target_database_name>', |
| 106 | + collection: '<target_collection_name>', |
| 107 | + pipeline: [ |
| 108 | + { $match: { status: 'urgent' } }, |
| 109 | + { $group: { _id: '$productName', sumQuantity: { $sum: '$quantity' } } } |
| 110 | + ] |
| 111 | +}) |
125 | 112 | ``` |
126 | 113 |
|
127 | 114 | #### Method chaining |
@@ -193,25 +180,9 @@ const customerCollection = new MongoDBDataAPI<CustomerDocument>( |
193 | 180 | const customer = await customerCollection.findOne({ ... }) |
194 | 181 | ``` |
195 | 182 |
|
196 | | -### Development |
197 | | - |
198 | | -```bash |
199 | | -# install dependencies |
200 | | -yarn |
201 | | - |
202 | | -# lint |
203 | | -yarn lint |
204 | | - |
205 | | -# test |
206 | | -yarn test |
207 | | - |
208 | | -# build |
209 | | -yarn build |
210 | | -``` |
211 | | - |
212 | 183 | ### Changelog |
213 | 184 |
|
214 | | -Detailed changes for each release are documented in the [release notes](/CHANGELOG.md). |
| 185 | +Please refer to https://github.com/surmon-china/mongodb-data-api/ |
215 | 186 |
|
216 | 187 | ### License |
217 | 188 |
|
|
0 commit comments