1.2.7 • Published 10 months ago

cs-mongo-data-api v1.2.7

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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

Links

function

1.2.7

10 months ago

1.2.6

10 months ago

1.2.5

10 months ago

1.2.4

10 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.2.0

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.11

10 months ago

1.0.10

10 months ago

0.8.5

11 months ago

0.8.4

11 months ago

0.11.0

11 months ago

0.10.1

11 months ago

0.11.1

11 months ago

0.11.2

11 months ago

0.10.0

11 months ago

0.9.0

11 months ago

0.8.1

11 months ago

0.7.2

11 months ago

0.8.0

11 months ago

0.7.1

11 months ago

0.6.2

11 months ago

0.5.3

11 months ago

0.9.2

11 months ago

0.8.3

11 months ago

0.9.1

11 months ago

0.8.2

11 months ago

0.5.0

11 months ago

0.7.0

11 months ago

0.6.1

11 months ago

0.5.2

11 months ago

0.6.0

11 months ago

0.5.1

11 months ago

0.4.2

11 months ago

0.4.1

11 months ago

0.4.0

11 months ago

0.3.1

11 months ago

0.3.0

11 months ago

0.2.5

11 months ago

0.2.4

11 months ago

0.2.3

11 months ago

0.2.2

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.0

11 months ago

0.0.10

11 months ago

0.0.9

12 months ago

0.0.8

12 months ago

0.0.7

12 months ago

0.0.6

12 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago