@spksoft/mongoose-repository
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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]
})

Package Sidebar

Install

npm i @spksoft/mongoose-repository

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

77.8 kB

Total Files

17

Last publish

Collaborators

  • spksoft