cs-mongo-data-api
v1.2.7
Published
client for data API MongoDB atlas
Downloads
85
Maintainers
Readme
mongo-data-api
a wrapper client for REST Data API MongoDB atlas.
Features
- it work on cloudflare worker and edge server
- easy to run and test on local development
- it can switch to run on NodeJS and connect direct to mongodb by node driver
- automatic transfrom _id to id on input and output
- support bulkfind (group multiple find query to one request)
- support bulkWrite (group multiple write query to one request)
read mongodb offical page documentation for more information mongo-data-api
npm install cs-mongo-data-api
Usage
import { MongoDataApi } from 'cs-mongo-data-api';
export class MyService extends MongoDataApi {
// define your collection
get user(){ return this.collection<User>('User'); }
}
const api = new MyService({
apiUrl: process.env.MONGO_API_URL!,
database: process.env.MONGO_DATABASE!,
apiKey: process.env.MONGO_API_KEY!,
dataSource: process.env.MONGO_DATA_SOURCE!,
})
api.user.findOne({ id:'123'})
api.user.insertOne({...})
api.collection<Photo>('Photos').findOne({ id:'123'})
//... bulkFind query
const result = await api.box.bulkFind([
{
collection: 'Box',
key: 'box',
filter: { id: '123' },
},
{
collection: 'User',
key: 'user',
filter: { name: 'lala' },
},
]);
result.getOne<User>('user');
result.getMany<Box>('box');
switch to mongodb node driver
By default it use fetch function to call data api but you can switch to mongodb
node driver by set api.queryApdater
when you create api change apiUrl to mongodb node driver connect url
import { mongoDbAdapter } from 'cs-mongo-data-api/lib/mongoDbAdapter';
api.queryApdater = mongoDbAdapter
after that all query is same
bulkWrite
You need to deploy a custom function to run bulkWrite
Tutorial
function code
deploy above code with route /bulkAction
Usage
import { MongoDataApi } from 'cs-mongo-data-api';
api.user.bulkWrite([
{
insertOne: {
document: { id: '123', name: 'test' },
},
},{
updateOne: {
filter: { id: '123' },
update: { $set: { name: 'test' } },
},
},
])
Alternatives
realm-web it can't run on local mongodb