Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/surmon-china/mongodb-data-api

MongoDB Atlas Data API SDK for @nodejs
https://github.com/surmon-china/mongodb-data-api

mongodb mongodb-api mongodb-atlas mongodb-atlas-api mongodb-atlas-cloud mongodb-atlas-data-api mongodb-cloud-api mongodb-data-api mongodb-driver mongodb-serverless

Last synced: 12 days ago
JSON representation

MongoDB Atlas Data API SDK for @nodejs

Awesome Lists containing this project

README

        



# mongodb-data-api

[![GitHub stars](https://img.shields.io/github/stars/surmon-china/mongodb-data-api.svg?style=for-the-badge)](https://github.com/surmon-china/mongodb-data-api/stargazers)
 
[![npm](https://img.shields.io/npm/v/mongodb-data-api?color=c7343a&label=npm&style=for-the-badge)](https://www.npmjs.com/package/mongodb-data-api)
 
[![Test Codecov](https://img.shields.io/codecov/c/github/surmon-china/mongodb-data-api?style=for-the-badge)](https://codecov.io/gh/surmon-china/mongodb-data-api)
 
[![GitHub license](https://img.shields.io/github/license/surmon-china/mongodb-data-api.svg?style=for-the-badge)](/LICENSE)

MongoDB Atlas [Data API](https://www.mongodb.com/docs/atlas/api/data-api/) SDK for Node.js.

---

### Installation

```bash
npm install mongodb-data-api --save
```

or

```bash
yarn add mongodb-data-api
```

### Usage

#### Init

```ts
import { createMongoDBDataAPI } from 'mongodb-data-api'

// init by URL Endpoint
const api = createMongoDBDataAPI({
apiKey: '',
urlEndpoint: 'https://data.mongodb-api.com/app//endpoint/data/v1'
})

// or init by app ID
const api = createMongoDBDataAPI({
apiKey: '',
appId: ''
})

// specific region and cloud of app
const api = createMongoDBDataAPI({
apiKey: '',
appId: '',
region: '', // e.g. us-east-1
cloud: '' // e.g. aws
})
```

#### Actions

See [MongoDB Data API Resources](https://www.mongodb.com/docs/atlas/api/data-api-resources/).

- [`API.findOne`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#find-a-single-document)
- [`API.find`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#find-multiple-documents)
- [`API.insertOne`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#insert-a-single-document)
- [`API.insertMany`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#insert-multiple-documents)
- [`API.updateOne`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#update-a-single-document)
- [`API.updateMany`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#update-multiple-documents)
- [`API.replaceOne`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#replace-a-single-document)
- [`API.deleteOne`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#delete-a-single-document)
- [`API.deleteMany`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#delete-multiple-documents)
- [`API.aggregate`](https://www.mongodb.com/docs/atlas/api/data-api-resources/#run-an-aggregation-pipeline)

#### 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('c')
// 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('c').findOne({ filter: {} })
// the same as
api.findOne({
dataSource: 'a',
database: 'b',
collection: 'c',
filter: {}
})
```

#### Specific Action

You can specify 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({ ... })
```

### Development

```bash
# install dependencies
yarn

# lint
yarn lint

# test
yarn test

# build
yarn build
```

### Changelog

Detailed changes for each release are documented in the [release notes](/CHANGELOG.md).

### License

[MIT](/LICENSE)