class-mongo
/ MongoDB persistence layerLightweight 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
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.01
static fetch
1.02
static fetchMany
1.03
static count
1.04
static upsert
1.05
static upsertMany
1.06
static upsertManyBy
1.07
static patch
1.08
static del
1.09
static delMany
1.10
static getCollection
1.11
static getDb
1.12
.upsert
1.13
.patch
1.14
.del
2.1
define2.2
interface IMongoSettings2.2.1
connection2.2.2
db2.2.3
ip2.2.4
port2.2.5
name3.1
ensureAll:copyright: MIT - Atma.js
Generated using TypeDoc