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

@sugo/mongodb

v1.0.3

Published

Lightweight ORM for MongoDB. This package's goal is to easily standarize CRUD operations for MongoDB and to add some common functionalities.

Downloads

6

Readme

@sugo/mongodb

Lightweight ORM for MongoDB. This package's goal is to easily standarize CRUD operations for MongoDB and to add some common functionalities.

Requirements

node version >= 8

How to install

npm install --save @sugo/mongodb

Defining Schemas/Collections

We define collections by creating instances of the Collection class. The constructor for this class needs the collections name in the database and can receive field definitions and options.

Connection

Connection to the database is made using the connect and disconnect methods. THe connect methods receives an MongoDB URI and MongoClient options.

import { connect } from '@sugo/mongodb';
const Cats = new Collection(COL_NAME);
connect(
  URI,
  { useNewUrlParser: true },
).then(() => Cats.list().then((cats: any[]) => console.log(cats)));

By default, this package uses an internal MongoClient instance. The developer can pass it's own MongoClient instance.

const client = new MongoClient(URI, { useNewUrlParser: true });
import { connect } from '@sugo/mongodb';
const Cats = new Collection(COL_NAME, {}, { client });
connect(URI).then(() => Cats.list().then((cats: any[]) => console.log(cats)));

Field definitions

Field definitions help set the behavior for each field. They include validation, default values and more. Field definitions do not limit the fields that will be stored (unlink Mongoose default behavior), they apply their options on the field if needed. Field definitions use dot-notation (using the dot-object). In case of arrays, use the * wildcard.

const SpecifiedCol = new Collection<ICat>(
  COL_NAME,
  {
    name: {
      defaultValue: 'default',
    },
  },
  { client },
);

Default values

const SpecifiedCol = new Collection<ICat>(
  COL_NAME,
  {
    name: {
      defaultValue: 'default',
    },
  },
  { client },
);

OR

const SpecifiedCol = new Collection<ICat>(COL_NAME, {
  name: {
    defaultValue: (doc: IDynamicObject) => 'hello world',
  },
});

Validation

This packages supports sanitization. This allows us to make validations to a field before storing it. To add a validation, we add the validations attribute with an object with async methods. This methods will receive the value of the field and the document and must return a boolean.

const SpecifiedCol = new Collection(
  COL_NAME,
  {
    name: {
      validations: {
        TRIM: async (value: any, doc: IDynamicObject) => value !== undefined;
      },
    },
  },
);

Included

  • IS_NOT_NULL
  • MIN: Recieves a number.
  • MAX: Recieves a number.
  • RANGE: Recieves two numbers.
  • MIN_LENGTH: Recieves a number.
  • MAX_LENGTH: Recieves a number.
  • HAS_DATE_FORMAT: Recieves a momentjs format string

Sanitization

This packages supports sanitization. This allows us to make modifications to a field before storing it. To add sanitization, we add the sanitizations attribute with an object with async methods. This methods will receive the document and must return the sanitized value.

const SpecifiedCol = new Collection(
  COL_NAME,
  {
    name: {
      sanitizations: {
        TRIM: (value: string, doc: IDynamicObject) => value.trim();
      },
    },
  },
);

Included

  • TRIM
  • HASH: Receives an options Object { algorith: string; salt: string }
  • TO_LOWERCASE
  • TO_UPPERCASE

Hidden fields

Some times we want to hide a confidential field like a password. The hidden specification removes the field when transforming the Document into json.

const SpecifiedCol = new Collection<ICat>(COL_NAME, {
  name: {
    hidden: true,
  },
});