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

class-mongo

v5.0.1

Published

Mongo storage for class entities

Downloads

298

Readme

Lightweight but powerful MongoDB ORM on top of class entities. Backed by mongodb native driver.

with TypeScript support

  • class-json is used as a Serialization and Validation library.

this is loosely coupled and can be replaced with any other

  • Can be decoupled from base classes:

you may want to share same models in nodejs and browser environments

Short example to get the feeling.

import { Serializable, Json, Rule } from 'class-json'

export class User extends Serializable<User> {
    _id: string

    @Rule.required()
    name: string

    @Rule.pattern(/@/)
    email: string

    @Json.type(Date)
    createdAt = new Date()

    @Json.type(BigInt)
    amount: bigint
}
import { User } from './User'
import { MongoEntityFor, table, index, dbType } from 'class-mongo'


@table('users')
export class UserDb extends MongoEntityFor(User) {

    @index({ unique: true })
    email: string

    /*(MongoType, JsType)*/
    @dbType('decimal', BigInt)
    amount: bigint
}


// e.g
let user = new UserDb({
    name: 'Smith',
    email: '[email protected]'
});
await user.upsert();
console.log(user._id);

Same as a single class:

import { Serializable, Json, Rule } from 'class-json'
import { MongoEntity, table, index } from 'class-mongo'

@table('users')
export class User extends MongoEntity<User> {
    _id: string

    @index({ unique: true })
    @Rule.required()
    name: string

    @Rule.pattern(/@/)
    email: string

    @Json.type(Date)
    createdAt: Date
}

☰ API


1 MongoEntity

1.01 static fetch

  • Parameters: #findOne
  • Returns: class instance
let user = await UserEntity.fetch({ username: 'foo' });

1.02 static fetchPartial

Similar to fetch but has strongly typed Input projection and strongly typed Output partial entity model.

  • Parameters: #findOne
  • Returns: class instance with omitted fields not included in projection
let user = await UserEntity.fetchPartial({ username: 'foo' }, {
    projection: {
        email: 1
    }
});

// User is of type Pick<UserEntity, 'email'>
console.log(user.email); // OK
console.log(user.username); // TypeScript Error

1.03 static fetchMany

  • Parameters: #find
  • Returns: array of class instances
let users = await UserEntity.fetchMany({ visits: { $gte: 100 }});

1.03 static fetchManyPartial

  • Parameters: #find
  • Returns: array of class instances with omitted fields not included in projection
let users = await UserEntity.fetchMany({
    visits: { $gte: 100 }
}, {
    projection: {  username: 1 }
});

1.03 static fetchManyPaged

Similar to fetchMany but requires limit and skip, returns also total amount of documents.

  • Parameters: #find
  • Returns: { collection: InstanceType<T>[], total: number }
let users = await UserEntity.fetchMany({
    visits: { $gte: 100 }
}, {
    sort: { username: 1 },
    limit: 50,
    skip: 0
});

1.03 static fetchManyPagedPartial

Similar to fetchManyPaged but also requires projection field and returns strongly types models.

  • Parameters: #find
  • Returns: { collection: InstanceType<Partial<T>>[], total: number }
let users = await UserEntity.fetchMany({
    visits: { $gte: 100 }
}, {
    sort: { username: 1 },
    limit: 50,
    skip: 0
});

1.03 static count

1.05 static upsert

  • Parameters: #updateOne / #insertOne
  • Returns: class entity (with _id field set in case of insert action)

If _id is not present the model will be inserted, otherwise updated.

let user = await UserEntity.upsert({ username: 'foo', email: '[email protected]' });

1.05 static upsertBy

  • Parameters: #updateOne / #insertOne
  • Returns: class entity (with _id field set in case of insert action)

Inserts or updates the document based on the field value

let user = await UserEntity.upsertBy('username', { username: 'foo', email: '[email protected]' }, {});

1.05 static upsertMany

1.05 static upsertManyBy

2 MongoSettings

2.1 define

Configurate mongo connection before making any mongodb requests

import { MongoSettings } from 'class-mongo';

MongoSettings.define({
    db: 'fooTables'
    connection: 'connection_string';
    params: {
        // additional parameters if needed
    }
});

:copyright: MIT - Atma.js