Giter VIP home page Giter VIP logo

mongoose-repository's Introduction

mongoose-repository

A mongoose Repository based Include Plugin:

  • mongoose
  • mongoose-delete (default options: { deletedAt: true, indexFields: true, overrideMethods: true })
  • mongoose-history
  • mongoose-paginate
  • mongoose-timestamp
  • mongoose-aggregate-paginate
  • rascal

install

npm install sendit-mongoose-repository --save

Create repo Example

bar.repository.js file

import mongoose from 'mongoose'
import RepositoryBuilder from 'sendit-mongoose-repository'

const schemaDefinition = {
  name: {
    type: String,
    require: true
  },
  foos: {
    type: [Number],
    require: true
  },
  company: { type: Mongoose.Schema.Types.ObjectId, ref: 'Company' },
}

export const builder = RepositoryBuilder('Bar', schemaDefinition)
export default builder.Repository
// builder provides:
//  {
//      Model,
//      Schema,
//      Repository,
//      schemaDefinition
//  }

BaseRepostory provides functions

.findOne(query: any, options: any)
.find(query: any = {}, options: any = {})
.create(data: any)
.update(query: any, data: any)
.upsert(query: any, data: any)
  (default options: {upsert: true, new: true})
.delete(data: any)
.aggregate(data: any)
.aggregatePaginate(query: any, options)

Usage Example

Find one

import BarRepository from './bar.repository.js'
export default async function list() {
  var filter = {
    name: 'default'
  }
  var options = {
    populate: 'company' //optional
  }
  return BarRepository.findOne(filter, options)
}

Find all

import BarRepository from './bar.repository.js'

export default async function list() {
  var filter = {
    name: 'default'
  }
  var options = {
    populate: 'company' //optional
  }
  return BarRepository.find(filter, options)
}

Find with Paginate (required options.limit and options.page)

var filter = {
  name: 'default'
}
var options = {
  limit: 10, // required
  page: 1, // required, start 1
  sort: {name: -1}, // optional, default: {_id: 1}, (ex. sort descending name)
  populate: 'company', // optional
  select: '-_id -__v -password' // optional omit _id, __v, password  
}
return BarRepository.find(filter, options)

Create

await BarRepository.create({ name: 'default' })

Update

await BarRepository.update({ name: 'default' }, { foos: [12, 69] })

Delete

await BarRepository.delete({ name: 'default' })

Aggregate

import BarRepository from './bar.repository.js'

export default async function list() {
  var filter = {
    name: 'default'
  }
  var options = {
    populate: 'company' //optional
  }
  return BarRepository.find(filter, options)
}

Aggregate Paginate

var aggregateQuery = [
  { $match : { name: 'default' } },
  { $project: { foos: 1 } }
]
var options = {
  limit: 10, // required
  page: 1, // required, start 1
  sort: {name: -1}
}
return BarRepository.aggregatePaginate(filter, options)

AMQP

Publish queue after create or update

Pattern queue name

node_env.serviceName.create.model
node_env.serviceName.update.model

example

local.fleet.create.vehicles
import { init } from 'sendit-mongoose-repository'

init({
  exchange: 'exchange-name',
  models: ['model'],
  ttl: 50000000000000, //millisecond
  service: 'serviceName',
  vhosts: 'local',
  connection: {
    slashes: true,
    protocol: 'amqp',
    hostname: '127.0.0.1',
    user: 'guest',
    password: 'guest',
    vhost: `//local`,
    port: 5672,
    options: {
      heartbeat: 5,
    },
    socketOptions: {
      timeout: 1000,
    },
  },
})

Example For cluster connections

connections: [
        "amqp://guest:[email protected]:5672/v1?heartbeat=10",
        "amqp://guest:[email protected]:5672/v1?heartbeat=10",
        "amqp://guest:[email protected]:5672/v1?heartbeat=10"
      ]

mongoose-repository's People

Contributors

bossbossk20 avatar doctoraod avatar nantaphop avatar nv4re avatar preferalias avatar spksoft avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.