Giter VIP home page Giter VIP logo

mongoose-named-scopes's Introduction

mongoose-named-scopes

๐Ÿ€ Define custom, chainable Mongoose queries

// Definition (placed in Scheme)
ProductSchema.scope('available').where('available').equals(true);
ProductSchema.scope('mostRecent', function(count) {
  return this.sort('-updatedAt').limit(count);
});
ProductSchema.scope('category', function(cat) {
  return this.where('category').equals(cat);
};
// etc

// Usage (called from anywhere)
Product.category('men').available().mostRecent(10);
User.male().olderThan(18).sortByAge().populateProfile();
Task.assignedTo(john).highPriority().project('mongoose').limit(5);

Usage

Install it with npm:

npm install --save mongoose-named-scopes

First, you need to register the plugin into the schemas that you want to use it:

var namedScopesPlugin = require('mongoose-named-scopes');

// For one Schema
UserSchema.plugin(namedScopesPlugin);

// For all Schemas at once
mongoose.plugin(namedScopesPlugin);

Then, use schema.scope (or schema.namedScope) to define your scopes:

// You can define scopes by chaining operator calls
UserSchema.scope('male').where('gender').equals('male');

// Or you can pass a function, for when you want to have parameters
// or need to use other statements
UserSchema.scope('olderThan', function (age) {
  // Be sure to return `this`!
  return this.where('age').gt(age);
});

UserSchema.scope('youngerThan', function (age) {
  return this.where('age').lt(age);
});

// Scopes can make use of other scopes!
UserSchema.scope('twenties').olderThan(19).youngerThan(30);

// Heads up! We need to implement this by passing a function so that the
// date parameter gets evaluated when you actually call the scope
UserSchema.scope('active', function () {
  const yesterday = +new Date() - 24*60*60*1000;
  return this.where('lastLogin').gte(yesterday);
});

Now, use the named scopes as if they were query functions:

// You can specify more operators
User.populate('children').olderThan(50).sort('age'); // ...

// Returning array results
User.olderThan(20).exec().then((users) => {}).catch(err);

// Returning single results
User.olderThan(100).findOne().exec().then((users) => {}).catch(err);

Enjoy!

Contributing

Contributions are greatly appreciated!

This is a very new library that extends Mongoose in some unusual ways. Please report bugs in the Issues.

Feel free to develop additional features or fix bugs and send them over as Pull Requests.

mongoose-named-scopes's People

Contributors

gberger avatar world avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mongoose-named-scopes's Issues

always returns array of results

I love this package. So simple and brilliant. Question: all results of scopes return array even if I set limit(1). Is there anyway to use this package and have it findOne and return a single result instead of [resutls]?

Cant't use findById after use scope function.

When i execute a query in node, using a scope function and findById, like this:

ActionModel.byTenant(tenantId).findById(id, function (err, action) {
...
});

It throws this error:

{
    "message": "ActionModel.byTenant(...).findById is not a function",
    "error": {}
}

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.