Options
All
  • Public
  • Public/Protected
  • All
Menu

class-mongo

class-mongo / MongoDB persistence layer

Lightweight but powerful MongoDB ORM on top of class entities.

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: 'foo@bar.fake'
});
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


:copyright: MIT - Atma.js

Generated using TypeDoc