MongoDB atlas data API SDK for CF Workers
npm install @embedvr/mongodb-data-api





MongoDB Atlas Data API SDK for Node.js and V8 Isolate environments like Cloudflare Workers.
Fixes a bug with aggregation.
---
``bash`
npm i @embedvr/mongodb-data-api
or
`bash`
yarn add @embedvr/mongodb-data-api
#### Init
`ts
import { createMongoDBDataAPI, Region } from '@embedvr/mongodb-data-api'
// init by URL Endpoint
const api = createMongoDBDataAPI({
apiKey: '
urlEndpoint: 'https://data.mongodb-api.com/app/
})
// or init by app ID
const api = createMongoDBDataAPI({
apiKey: '
appId: '
})
// specific region of app
const api = createMongoDBDataAPI({
apiKey: '
appId: '
region: Region.Virginia
})
`
#### Actions
See MongoDB Data API Resources.
- api.findOne
- api.find
- api.insertOne
- api.insertMany
- api.updateOne
- api.updateMany
- api.replaceOne
- api.deleteOne
- api.deleteMany
- api.aggregate
#### Action examples
1. find a single document
`ts`
api
.findOne({
dataSource: '
database: '
collection: '
filter: { name: 'Surmon' }
})
.then((result) => {
console.log(result.document)
})
2. insert a single document
`ts`
api
.insertOne({
dataSource: '
database: '
collection: '
document: {
name: 'Surmon',
age: 19
}
})
.then((result) => {
console.log(result.insertedId)
})
3. run an aggregation pipeline
`ts`
api
.aggregate({
dataSource: '
database: '
collection: '
pipeline: [
{ $match: { status: 'urgent' } },
{ $group: { _id: '$productName', sumQuantity: { $sum: '$quantity' } } }
]
})
.then((result) => {
console.log(result.documents)
})
#### Method chaining
`ts
// select cluster
const clusterA = api.$cluster('a')
// select database
const databaseB = clusterA.$database('b')
// select collection
const collectionC = databaseB.$collection
// data actions
const data = await collectionC.findOne({
filter: {
/.../
}
})
const result = await collectionC.insertOne({
document: {
/.../
}
})
// -------------
// chaining is equivalent to the api call
api.$cluster('a').$database('b').$collection
// the same as
api.findOne
dataSource: 'a',
database: 'b',
collection: 'c',
filter: {}
})
`
#### Specific Action
You can specific the action request to prevent this package from lagging relative to the official one.
`ts`
api.$$action('findOne', {
dataSource: '...',
database: '...',
collection: '...',
filter: {}
})
#### Original Class
You can use the original Class to implement some special requirements.
`ts
import { MongoDBDataAPI } from 'mongodb-data-api'
const customerCollection = new MongoDBDataAPI
{
apiKey: '
appId: '
},
{
dataSource: '
database: '
collection: '
}
)
const customer = await customerCollection.findOne({ ... })
`
`bashinstall dependencies
yarn
Detailed changes for each release are documented in the release notes.