https://github.com/codemeasandwich/mongoose-model-restore
should be able to remove and restore a document
https://github.com/codemeasandwich/mongoose-model-restore
Last synced: over 1 year ago
JSON representation
should be able to remove and restore a document
- Host: GitHub
- URL: https://github.com/codemeasandwich/mongoose-model-restore
- Owner: codemeasandwich
- License: apache-2.0
- Created: 2022-03-07T11:58:42.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-07T12:03:32.000Z (over 4 years ago)
- Last Synced: 2025-02-04T14:21:56.620Z (over 1 year ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-model-restore
a mongoose model generator with a change stream
**info:**
[](https://badge.fury.io/js/graphql-mongoose-model-restore)
[](https://www.apache.org/licenses/LICENSE-2.0)
[](https://github.com/codemeasandwich/mongoose-model-restore/pulls)
### If this was helpful, [★ it on github](https://github.com/codemeasandwich/mongoose-model-restore)
## [Demo / Sandbox](https://tonicdev.com/codemeasandwich/57a0727c80254315001cb366) :thumbsup:
# Install
`npm install mongoose-model-restore`
# Api
``` js
const modelPlus = require('mongoose-model-restore');
```
### constructor
query/mutator you wish to use, and an alias or filter arguments.
| Argument (**one** to **two**) | Description
|--- |---
| String | modelNameS: name of model
| Object | schema: model definition
| * Bool | (**optional**) enableDownStream = TRUE : Should start listening for changes and __ADD__ stream$ PROP
"mongoose-model-stream" will always push changes, but the '**enableDownStream**' options is wheat you want to also receive changes.
**e.g.** a "**log**"! you would want to push changes but an a large site. This this would be a lot of traffic, that only an Admin interface would want to listen for.
``` js
const ChatSchema = new mongoose.schema({
text: String
});
const Chat = modelPlus('Chat', ChatSchema);
```
### stream$ (think of it like "find", but tailing the collection)
An [RxJs](http://reactivex.io/rxjs/) [Observable](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html) for change events like ('create', 'update', 'remove' & ['restore'](https://npmjs.com/codemeasandwich/mongoose-model-restore))
> the event type is a string 'create', 'update', 'remove' or 'restore'
> **That is set as the object's toSring/valueOf**
``` js
Chat.stream$.map(function (doc) {
return doc + ' ' + (doc == "update") + ' ' + JSON.stringify(doc) + ' ';
})
.subscribe(console.log, // data
console.error, // error
console.info); // close
// "update true { "text":"bar", "id":"57ff5d867fb7794f9c52a21f"}"
```
## run samples
``` bash
node example/sample.js
```
# Example
``` js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const modulePlus = require('mongoose-model-stream');
//=============================================== Logic
const ChatSchema = new Schema({ text: String }, { timestamps: true });
//=========================================== generator
const ChatDB = modulePlus('Chat', ChatSchema);
//===================================== start listening
ChatDB.stream$.subscribe(console.log, console.error, console.info);
//======================================= start example
setTimeout(function () {
//+++++++++++++++++++++++++++++++++++++++++++++ create
ChatDB.create({ text: "foo" })
.then(function (chatMessage) {
console.log(" >> FROM ", 'text = "' + chatMessage.text + '"');
chatMessage.text = "bar";
console.log(" >> TO ", 'text = "' + chatMessage.text + '"');
//+++++++++++++++++++++++++++++++++++++++++++++ update
return chatMessage.save();
}).then(function (chatMessage) {
setTimeout(function () {
console.log(" >>> FROM ", 'text = "' + chatMessage.text + '"');
chatMessage.text = "baz";
console.log(" >>> TO ", 'text = "' + chatMessage.text + '"');
//+++++++++++++++++++++++++++++++++++++++++++++ update
chatMessage.save();
}, 1000);
});
}, 1000);
```