mongoose-db-shortcuts

mongoose database shortcuts.

Usage no npm install needed!

<script type="module">
  import mongooseDbShortcuts from 'https://cdn.skypack.dev/mongoose-db-shortcuts';
</script>

README

MONGOOSE-DB-SHORTCUTS

version 0.0.4

loading the library.

var db = require('mongoose-db-shortcuts');

db.schema

db.schema

var schema=db.schema(model_name);

db.model

db.model

var model=db.model(model_name);

db.one

db.one

db.one({
  model: 'ModelName',
  find: { _id: _id }
},function(err,res){
  // do something
});

db.count

db.count where

db.count({
  model: 'ModelName',
  where: { ... }
},function(err,res){
  // do something
});

db.count find

db.count({
  model: 'ModelName',
  find: { _id: _id }
},function(err,res){
  // do something
});

db.exists

db.exists where

db.exists({
  model: 'ModelName',
  where: { _id: _id }
},function(err,res){
  // do something
});

db.exists find

db.exists({
  model: 'ModelName',
  find: { _id: _id }
},function(err,res){
  // do something
});

db.get

complex db.get example

var _q={
  model: 'Papers',
  where: {
    $or: [
      {_id: { $in: [0,1,2,3,4,5] }},
      {numbers: { $in: [0,1,2,3,4,5]}}
    ]
  },
  populate: 'cover',
  select: 'cover name cont',
  limit: 10
};
db.get(_q,function(err,papers){
  if(err) done(true,err);
  else{
    done(null,papers);
  }
});

db.get as find

find all models.

var query = {
  model:'modelName'
};
db.get(query, callback(err, res){
  // do something
});

db.get as findById

modelID String

var query = {
  model:'modelName',
  id: modelID
};
db.get(query, callback(err, res){
  // do something
});

db.get - Find by multiple ids.

id accepts an Array of values.

modelID Array or String

var query = {
  model:'modelName',
  where: { _id : { $in : [...] } }
};
db.get(query, callback(err, res){
  // do something
});

db.get as find (passing a where option.)

var query = {
  model:'modelName',
  where: { ... } // list of columns
};
db.get(query, callback(err, res){
  // do something
});

db.create

var _q = {
  model:'Papers',
  body: { 
    name:'Important paper.',
    cont:'...'
  }
};
db.get(_q, callback(err, res){
  // do something
});

db.update (it's like using get.)

var _q={
  model: 'Papers',
  where: { name: 'Unnamed paper.' },
  body:{
    name: 'Important paper.'
  },
  limit: 10
};
db.update(_q,function(err,affected){
  done(err,affected);
});

db.update

passing a function

var _q={
  model: 'Papers',
  where: { _id: _id },
  body:function(model,opts,cb){
    // model - the model.
    // opts - this same _q object.
    // cb - callback for complete.
    // this - model schema.
    model.update({
      // update 1
    },{
      // update 2
    });
  }
};
db.update(_q,function(err,affected){
  done(err,affected);
});

db.destroy (it's like using get.)

var query = {
  model: 'modelName',
  where: { _id: _id }
};
db.destroy(query, callback(err, res){
  // do something
});

License (MIT)

Copyright (c) 2014 Victor P.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.