-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53958ce
commit e761fcf
Showing
6 changed files
with
278 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
node_modules | ||
npm-debug.log | ||
.idea/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016-2017 Manuel Di Iorio | ||
|
||
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. | ||
The MIT License (MIT) | ||
Copyright (c) 2016-2019 Manuel Di Iorio | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,76 @@ | ||
# Mongoose FindOrCreate() | ||
## Extend the mongoose schemas with a findOrCreate() plugin. Essentially, if a document is not found, will be atomically created or (if specified) updated | ||
|
||
### Install it with: | ||
|
||
npm i find-or-create | ||
or | ||
git clone https://github.com/manuel-di-iorio/find-or-create.git | ||
|
||
### **Examples**: | ||
|
||
```javascript | ||
yourSchema.statics.findOrCreate = require("find-or-create"); | ||
YourModel = mongoose.model("yourSchema", yourSchema); | ||
|
||
YourModel.findOrCreate({_id: myID}, {apples: 2}, (err, result) => { | ||
if (err) return console.error(err); | ||
console.log(result.doc); // the document itself | ||
console.log(result.isNew); // if the document has just been created | ||
}); | ||
``` | ||
|
||
Example upserting the document and using the promise return: | ||
|
||
```javascript | ||
Model.findOrCreate({_id: myID, apples: 2}, {apples: 5}, {upsert: true}) | ||
.then((result) => { | ||
console.log(result.doc); | ||
console.log(result.isNew); | ||
}) | ||
.catch(console.error); | ||
``` | ||
|
||
|
||
--- | ||
## API: | ||
|
||
```javascript | ||
MongooseModel.findOrCreate(query, doc, [options, callback]); | ||
``` | ||
If you don't specify a callback, it will be returned a promise. | ||
|
||
--- | ||
|
||
- **doc** is the document that will be inserted if the document based on your **query** is not found, otherwise the record will be updated with the new document (if upsert is enabled). | ||
|
||
- **options** is an optional object that will be passed to the underlying mongoose 'findOrCreate' method. | ||
|
||
You can find the possible options here: http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate | ||
|
||
Set `{upsert: true}` to update the document when it already exists, otherwhise it will be only inserted when not found | ||
|
||
|
||
--- | ||
## Test with | ||
|
||
```bash | ||
npm test | ||
``` | ||
|
||
--- | ||
## License | ||
|
||
MIT | ||
# FindOrCreate v2.0 for Mongoose | ||
### Extend the mongoose schemas with a findOrCreate() plugin. Essentially, if a document is not found, will be atomically created or (if specified) updated | ||
|
||
### Install it with: | ||
|
||
npm i find-or-create | ||
|
||
### **Examples**: | ||
|
||
```javascript | ||
yourSchema.statics.findOrCreate = require("find-or-create"); | ||
YourModel = mongoose.model("yourSchema", yourSchema); | ||
|
||
YourModel.findOrCreate({_id: myID}, {apples: 2}, (err, result) => { | ||
if (err) return console.error(err); | ||
console.log(result.doc); // the document itself | ||
console.log(result.isNew); // if the document has just been created | ||
}); | ||
``` | ||
|
||
Example upserting the document and using the promise return: | ||
|
||
```javascript | ||
Model.findOrCreate({_id: myID, apples: 2}, {apples: 5}, {upsert: true}) | ||
.then((result) => { | ||
console.log(result.doc); | ||
console.log(result.isNew); | ||
}) | ||
.catch(console.error); | ||
``` | ||
|
||
## Note: | ||
|
||
As of Mongoose v5, to use this module you need to set the global option `useFindAndModify` to _false_, otherwise a warning will be logged. | ||
|
||
Example: | ||
``` | ||
mongoose.connect(uri, { useFindAndModify: false }); | ||
``` | ||
|
||
## Upgrading to Mongoose 5: | ||
|
||
The latest v2.0 of this plugin is compatible with Mongoose 5.x. | ||
If you need retro-compatibility with Mongoose 4.x, please install the version 1.1 of this module. | ||
|
||
|
||
--- | ||
## API: | ||
|
||
```javascript | ||
MongooseModel.findOrCreate(query, doc, [options, callback]); | ||
``` | ||
If you don't specify a callback, it will be returned a promise. | ||
|
||
--- | ||
|
||
- **doc** is the document that will be inserted if the document based on your **query** is not found, otherwise the record will be updated with the new document (if upsert is enabled). | ||
|
||
- **options** is an optional object that will be passed to the underlying mongoose 'findOrCreate' method. | ||
|
||
You can find the possible options here: http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate | ||
|
||
Set `{upsert: true}` to update the document when it already exists, otherwhise it will be only inserted when not found | ||
|
||
|
||
--- | ||
## Test with | ||
|
||
```bash | ||
npm test | ||
``` | ||
|
||
--- | ||
## License | ||
|
||
MIT |
Oops, something went wrong.