class-mongo v5.0.1
Lightweight but powerful MongoDB ORM on top of class entities. Backed by mongodb native driver.
with TypeScript support
class-json is used as a
SerializationandValidationlibrary.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
1class MongoEntity -1.01static fetch1.02static fetchPartial -1.03static fetchMany1.04static fetchManyPartial1.05static fetchManyPaged1.06static fetchManyPagedPartial1.07static count1.08static upsert1.09static upsertBy1.10static upsertMany1.11static upsertManyBy1.12static patch1.13static del1.14static delMany1.15static getCollection1.16static getDb1.17.upsert1.18.patch1.19.del
2.1define2.2interface IMongoSettings2.2.1connection2.2.2db2.2.3ip2.2.4port2.2.5name
3.1ensureAll
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 Error1.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
- Parameters: #countDocuments
- Returns:
number
1.05 static upsert
- Parameters: #updateOne / #insertOne
- Returns: class entity (with
_idfield set in case ofinsertaction)
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
_idfield set in case ofinsertaction)
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
- Parameters: #updateOne / #insertOne
- Returns: array of class entities
1.05 static upsertManyBy
- Parameters: #updateOne / #insertOne
- Returns: array of class entities
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
1 year ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago