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
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
}-
-
1.01static fetch -
1.02static fetchPartial -
1.03static fetchMany -
1.07static count -
1.08static upsert -
1.09static upsertBy -
1.10static upsertMany -
1.11static upsertManyBy -
1.12static patch -
1.13static del -
1.14static delMany -
1.15static getCollection -
1.16static getDb -
1.17.upsert -
1.18.patch -
1.19.del
-
-
2.1define2.2interface IMongoSettings2.2.1connection2.2.2db2.2.3ip2.2.4port2.2.5name
-
3.1ensureAll
- Parameters: #findOne
- Returns:
class instance
let user = await UserEntity.fetch({ username: 'foo' });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- Parameters: #find
- Returns:
array of class instances
let users = await UserEntity.fetchMany({ visits: { $gte: 100 }});- 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 }
});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
});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
});- Parameters: #countDocuments
- Returns:
number
- 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' });- 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' }, {});- Parameters: #updateOne / #insertOne
- Returns: array of class entities
- Parameters: #updateOne / #insertOne
- Returns: array of class entities
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
}
});©️ MIT - Atma.js
