5.0.1 • Published 1 year ago

class-mongo v5.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
1 year ago

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: '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


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

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

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

5.0.1

1 year ago

0.3.17

4 years ago

0.3.15

4 years ago

0.3.14

4 years ago

0.3.19

4 years ago

0.3.18

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.11

4 years ago

0.3.10

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.4

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.99

5 years ago

0.2.98

5 years ago

0.2.97

5 years ago

0.2.95

5 years ago

0.2.94

5 years ago

0.2.93

5 years ago

0.2.92

5 years ago

0.2.91

5 years ago

0.2.90

5 years ago

0.2.89

5 years ago

0.2.88

5 years ago

0.2.87

5 years ago

0.2.86

5 years ago

0.2.85

5 years ago

0.2.84

5 years ago

0.2.83

5 years ago

0.2.81

5 years ago

0.2.80

5 years ago

0.2.78

5 years ago

0.2.77

5 years ago

0.2.76

5 years ago

0.2.73

5 years ago

0.2.72

5 years ago

0.2.71

5 years ago

0.2.69

5 years ago

0.2.68

5 years ago

0.2.67

5 years ago