mongoose-repository
A mongoose Repository based Include Plugin:
- mongoose-delete (default options: { deletedAt: true, indexFields: true, overrideMethods: true })
- mongoose-timestamps
- mongoose-paginate
install
npm install @spksoft/mongoose-repository
Usage
user.repository.js file
import RepositoryBuilder from '@spksoft/mongoose-repository'
const schemaDefinition = {
name: {
type: String,
require: true
},
foo: {
type: [Number],
require: true
}
}
export default RepositoryBuilder('Bar', schemaDefinition)
// {
// Model,
// Schema,
// Repository,
// schemaDefinition,
// default: Repository
// }
Example
find with repository
import UserRepository from './user.repository.js'
export default async function getUser() {
var filter = {
name: 'Yana'
}
var options = {
limit: 10, // limit data 10 rows
page: 1, // start 1
sort: 'username -password', // sort by "username" ascending and "password" descending
populate: 'userType'
}
return UserRepository.find(filter, options)
}
find with model
import UserRepository from './user.repository.js'
export default async function getUser() {
var filter = {
name: 'Yana'
}
var options = {
limit: 10, // limit data 10 rows
page: 1, // start 1
sort: 'username -password', // sort by "username" ascending and "password" descending
populate: 'userType'
}
return UserRepository.Model.find(filter, options)
}
get schema definition
import UserRepository from './user.repository.js'
export default async function getUserSchemaDefinition() {
return UserRepository.schemaDefinition
}
create
import UserRepository from './user.repository'
UserRepository.create({
name: 'eiei',
foo: [12, 69]
})