pspublisher
v1.0.1
Published
A module for automatic, asynchronous CRUD operations on a Mongo database.
Downloads
5
Maintainers
Readme
Pretty Simple Publisher
pspublisher is a package for automatic, asynchronous CRUD operations on a Mongo database.
Installation
You can clone this repo with git clone https://github.com/vlmlee/pspublisher.git
and then run npm install
from inside the cloned directory to install the dependencies.
or you can install this package via npm:
npm install pspublisher
Overview
pspublisher/
├── index.js
├── lib/
│ └── trackedFiles.json
├── logs/
│ ├── all-logs.log
│ └── exceptions.log
└── test/
├── test-directory
├── loading-directory
├── example.js
└── test.js
Motivation
This module was created for personal use on a blog. I thought it was unneccessary to have to write forms for your own website when we had all these powerful tools that could handle those file operations for us. A web application is "just a wrapper around a database" afterall.
So why not automate the process? And so we did.
Usage
pspublisher is built on top of chokidar and mongoose. Both are mature and robust modules that made this project possible. Both are built with asynchronicity in mind.
To use it, require it like:
const publisher = require('pspublisher').pspublisher;
and then define a path or directory that you want to watch. We also tell it what database to send our documents.
publisher('/path/to/directory');
publisher.connect('mongodb://localhost/myapp');
// or chained:
publisher('/path/to/directory').connect('mongodb://localhost/myapp');
After doing so, we can define a model to structure our documents. With this module, we want to create a model by using an array with three fields: name, schema, and collection. You can define the schema to be anything you like, as long as your documents match that schema. pspublisher currently has no support for subdocuments.
// Example
publisher.models([{
name: 'Posts',
schema: {
name: String,
body: String,
url: String
},
collection: 'name-of-collection' // comments, for instance
}]);
Then you run the script with:
publisher.start();
Then, just set up your mongo database, move your files into that directory, and voila! It shows up in your database.
Recommendations
For the time being, only json files are supported. Make sure your schemas that have important requirements uses required. Also, as of right now, you can only use one schema for one instance of pspublisher.
To have the script automatically exit when an exception is thrown, you will want to change exitOnError
on the logger to true:
publisher.logger({exitOnError: true});
Protect trackedFiles.json
Don't modify this file.
Further Work
A lot of further work needs to be done. Validating schemas, more support for multiple schemas and other files are in consideration. Some slight optimizations could also be done.
Author: Michael Lee http://www.corollari.com
License: MIT License
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.