class-mongo
v5.0.1
Published
Mongo storage for class entities
Downloads
298
Readme
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
andValidation
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: '[email protected]'
});
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.01
static fetch1.02
static fetchPartial1.03
static fetchMany1.07
static count1.08
static upsert1.09
static upsertBy1.10
static upsertMany1.11
static upsertManyBy1.12
static patch1.13
static del1.14
static delMany1.15
static getCollection1.16
static getDb1.17
.upsert1.18
.patch1.19
.del
2.1
define2.2
interface IMongoSettings2.2.1
connection2.2.2
db2.2.3
ip2.2.4
port2.2.5
name
3.1
ensureAll
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
- Parameters: #countDocuments
- Returns:
number
1.05
static upsert
- Parameters: #updateOne / #insertOne
- Returns: class entity (with
_id
field set in case ofinsert
action)
If _id
is not present the model will be inserted, otherwise updated.
let user = await UserEntity.upsert({ username: 'foo', email: '[email protected]' });
1.05
static upsertBy
- Parameters: #updateOne / #insertOne
- Returns: class entity (with
_id
field set in case ofinsert
action)
Inserts or updates the document based on the field value
let user = await UserEntity.upsertBy('username', { username: 'foo', email: '[email protected]' }, {});
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