mongoose-base-repository
Promised based Mongoose Base Repository.
Installation
npm install mongoose-base-repository --save
Usage
Sample Schema:
'use strict'; const mongoose = ;const Schema = mongooseSchema; const UserSchema = id: type: Number required: true unique: true name: type: String required: true trim: true age: type: Number active: type: Boolean default: true ; mongoose;
Sample 1:
'use strict'; const MongooseRepository = ;const userRepository = 'User'; // Can be passed as mongoose.model('User') instance userRepository;
Sample 2:
// UserRepository.js 'use strict'; const mongoose = ;const MongooseRepository = ; const User = mongoose; { superUser foreignKeys; } moduleexports = ;
// Test.js 'use strict'; const userRepository = ; userRepository;
FindOne
userRepository;
FindOne (with sort)
userRepository;
FindAll
userRepository;
FindAll (with sort)
userRepository;
FindAll (with sort & limit)
userRepository;
findSome
userRepository;
findManyById
userRepository;
Aggregate
userRepository;
create
userRepository;
update
const dbUser = /* .. */ ;userRepository;
remove
userRepository
removeMany
userRepository;
removeManyById
userRepository;
removeAll
userRepository;
count
userRepository;
findOneAndUpdate
userRepository;
Foreign Keys
Schema:
'use strict'; const mongoose = ;const Schema = mongooseSchema; const PostSchema = id: type: Number required: true unique: true title: type: String required: true trim: true content: type: String required: true trim: true author: type: SchemaTypesObjectId ref: 'User' ; mongoose;
Sample:
'use strict'; const MongooseRepository = ;const postRepository = 'Post' 'author'; // 'author' field here is a foreign key of User Schema postRepository;
Tests
npm test
License
The MIT License (MIT)
Copyright (c) 2017 iiWebi
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.