npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bridge-mongo

v1.0.27

Published

A mongodb ORM on top of mongoose that match perfectly with the Bridge framework

Downloads

146

Readme

Bridge Mongo

Bridge-mongo is a typed framework built on top of Mongoose, the popular MongoDB object-document mapping (ODM) library. It provides a type-safe approach to querying MongoDB databases that makes it almost impossible to write poorly constructed queries, all while using the same syntax as Mongoose.

Try it live

👉 See more informations on mongo.bridge.codes 👈

Documentation

Full documentation for bridge-mongo can be found here.

Installation

# npm
npm install bridge-mongo
# Yarn
yarn add bridge-mongo
# pnpm
pnpm add bridge-mongo

Quickstart

Define your Schemas

Defining your schemas in bridge-mongo is just as easy as it is with Mongoose. You can define your schemas using Mongoose's schema syntax, and then use the createDB function to create your models.

When you use createDB, bridge-mongo will automatically create and register each model with Mongoose using the keys from your schema object as the model names. This means you can define all of your schemas in one place and have them automatically created and registered as models for you.

import { createDB, Schema, mongoose } from 'bridge-mongo';

// Defining a User Schema
const userSchema = new Schema({
  name: { type: String, required: true },
  email: String,
  age: { type: Number, default: 18 },
  job: { type: String, enum: ['developer', 'designer'] },
  settings: {
    isActive: Boolean,
  },
});

// Defining a Post Schema
const postSchema = new Schema(
  {
    text: { type: String, required: true },
    userId: { type: mongoose.Types.ObjectId, req: true },
    likes: Number,
  },
  { timestamps: true },
);

// The keys correspond to the model Name
const DB = createDB({
  User: userSchema,
  Post: postSchema,
});

Connect to MongoDB

Connecting to your MongoDB database using bridge-mongo is just as easy as it is with Mongoose. In fact, you can import mongoose directly from bridge-mongo and use its connect function to connect to your database.

import { mongoose } from 'bridge-mongo';

const launch = async () => {
    await mongoose.connect('Your MongoDB URL here');

    console.log('Connected!')
}

launch();

Start enjoying type safety

You can enjoy the benefits of total type safety and guidance through TypeScript. The fully typed query results and error handling provided by bridge-mongo make it easy to write correct, efficient queries with confidence.

Read the documentation to get started or get a look to the examples below.

Some Queries examples:

import { isError } from 'bridge-mongo';

async () => {
  const user = await DB.user.create({ name: 'Nab' });

  const post = await DB.post.findOne({ userId: user._id }, {text: 1});

  if (!isError(post)) console.log(post)


  const posts = await DB.post.find({ likes: { $gt: 10 }});

  const res = await DB.user.findByIdAndUpdate(user._id, { name: 'Neo' }, { projection: { name: 1 } })
}

Some Aggregate examples:

async () => {
  
  // Fetching all users that have created post with their posts
  const blogers = await DB.user
    .aggregate()
    .project({ name: 1 })
    .lookup({ from: 'posts', localField: '_id', foreignField: 'userId' })
    .match({ 'posts.0': { $exists: true } })
    .exec();

  // Fetching all posts from the last 24 hours with their author only if he's >= 21 years old

  const yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);

  const posts = await DB.post
    .aggregate()
    .project({ text: 1, createdAt: 1, userId: 1 })
    .match({ createdAt: { $gt: yesterday } })
    .lookup({ from: 'users', let: { userId: '$userId' }, as: 'user' }, (user, { userId }) =>
      user
        .match({ $expr: { $eq: ['$_id', userId] }, age: { $gte: 21 } })
        .project({ name: 1 })
        .limit(1),
    )
    .unwind('$user')
    .unset('userId')
    .exec();
}

Bridge-mongo is constantly evolving and adding new features to provide a fully typed, robust, and easy-to-use interface for interacting with MongoDB. While many essential functions are already implemented, there are still many more features that can be added to meet specific use cases. If you are interested in contributing or discussing features that you'd like to see implemented, you can join the Bridge-mongo Discord server and be a part of the community.