Giter Site home page Giter Site logo

mongo-rest-client's Introduction

Mongo-Rest-Client

npm version NPM Downloads npm bundle size npm package minimized gzipped size NPM License

A MongoDB connector that uses fetch to communicate with DB. Fully type safe rest api based connector. Runs everywhere from NodeJS to Edge runtime.

Features

  • Fully Type-Safe Schema based connector
    • Instead of calling db.collection("users").insertOne(...), you call db.users.insertOne(UserObject)
  • No Crypto Module requried (Can run in both NodeJS and Edge Network (like Vercel))
  • Framework agnostic - Run anywhere that runs JS
  • Uses fetch api under-the-hood. So take advantage of caching in framework like NextJS

Supported Functions

  • Find one document (findOne)
  • Find many documents (findMany)
  • Insert One Document (insertOne)
  • Insert Documents (insertMany)
  • Update One Document (updateOne)
  • Update Documents (updateMany)
  • Delete One Document (deleteOne)
  • Delete Documents (deleteMany)
  • Aggregate Documents (todo)

Usage

Create a db by passing baseUrl (of data api), apiKey, dbName, and a schemaBuilder (a function that takes db instance and return an object of db.collection (add type to this object to take full advantage of it))

// db.ts file
import { MongoDB } from "mongo-rest-client";
export const db = MongoDB.connect({
  baseUrl: process.env.mongoDBUrl,
  apiKey: process.env.mongoDBApiKey,
  dbName: process.env.mongoDBName,
  schemaBuilder: (db) => ({
    users: db.collection<User>("users"),
    posts: db.collection<Post>("posts"),
  }),
});

// use it anywhere
await db.users.insertOne(...) // typesafe
await db.users.updateOne(...) // typesafe filter and data
const user = db.users.findOne({
    postID: "something..." // Compile-time ERROR: postID does not exists in User
}) // typesafe Filter
const user = db.users.findOne({
  _id: {
    $oid: "valid mongo db id"
  } // MongoDB requires an ObjectID. Dont worry its typesafe
}) // typesafe Filter

Limitations

mongo-rest-client uses the Data API from MongoDB. Hence, it has all the limitations of DataAPI like transactions. Read more here: https://www.mongodb.com/docs/atlas/app-services/data-api/openapi/

mongo-rest-client's People

Contributors

fahidsarker avatar

Stargazers

BJ choi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.