Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 979f1a3e0b909f7ad5af420241bbb0980078d211 0 parents
@cayasso authored
2  .gitignore
@@ -0,0 +1,2 @@
+.DS_Store
+node_modules
4 Makefile
@@ -0,0 +1,4 @@
+TESTS = $(shell find test -name "test.js")
+
+test:
+ mocha $(TESTS) -R spec
82 README.md
@@ -0,0 +1,82 @@
+## About mongoose-allocate
+
+Plugin for add padding to [mongoose][] documents, main use case is for capped collections.
+
+## How it works
+
+It uses [mongoose][] `save` hook event to add a padding field `__p` (with a large string)
+the first time a document is created in a collection.
+
+## Install
+
+npm install mongoose-allocate
+
+## Usage
+
+```javascript
+var mongoose = require('mongoose');
+var allocatePlugin = require('mongoose-allocate');
+
+...
+
+var SampleSchema = new Schema({
+ ...
+}, { capped: { size: 1000, max: 5, autoIndexId: true } }));
+
+SampleSchema.plugin(allocatePlugin, {len: 512, char: 'x'});
+
+var SampleModel = db.model("SampleModel", SampleSchema);
+```
+
+## Options
+
+ // Default options
+ {
+ // padding length
+ len: 256,
+
+ // padding character
+ char: '0',
+
+ // padding field name
+ fieldName: '__p'
+ }
+
+## Test
+
+ // Default options
+ mocha
+
+[Mocha][] is needed for runing the test
+
+
+## Bugs and pull requests
+
+Please use the github [repository][] to notify bugs and make pull requests.
+
+## License
+
+Copyright (c) 2013 Jonathan brumley <cayasso@gmail.com>
+
+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.
+
+[mongoose]: http://mongoosejs.com
+[Mocha]: http://visionmedia.github.com/mocha/
+[repository]: http://github.com/cayasso/mongoose-allocate
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/padding');
72 lib/allocate.js
@@ -0,0 +1,72 @@
+module.exports = function setup (schema, options) {
+
+ options = options || {};
+
+ var o = {
+ len: options.len || 256,
+ char: options.char || '0',
+ fieldName: options.fieldName || '__p'
+ };
+
+ var isPadded = false;
+ var pad = getString();
+
+ /**
+ * Create the padding field
+ */
+
+ function createField () {
+ var field = {};
+ field[o.fieldName] = {'type': String, 'default': ''};
+ return field;
+ }
+
+ /**
+ * Add padding
+ */
+
+ function addPadding (next) {
+ if (this.isNew) {
+ this[o.fieldName] = pad;
+ }
+ next();
+ }
+
+ /**
+ * Remove padding
+ */
+
+ function removePadding (doc) {
+ if (doc[o.fieldName] === pad) {
+ var d = {}, q = { _id: doc._id };
+ d[o.fieldName] = '';
+ doc.constructor.findOneAndUpdate(q, d, function (err, doc) {
+ if (err) console.log(err);
+ });
+ }
+ }
+
+ /**
+ * Get current settings
+ */
+
+ function getSettings () {
+ return o;
+ }
+
+ /**
+ * Create pad string
+ */
+
+ function getString () {
+ var s = '', c = o.char.toString();
+ for (var i = 0; i < o.len; i++) s += c;
+ return s;
+ }
+
+ schema.add(createField());
+ schema.pre('save', addPadding);
+ schema.post('save', removePadding);
+ schema.statics.getPaddingSettings = getSettings;
+ schema.methods.getPaddingSettings = getSettings;
+};
38 package.json
@@ -0,0 +1,38 @@
+{
+ "name": "mongoose-allocate",
+ "description": "Plugin for adding padding to Mongoose fields, good for capped collections.",
+ "version": "0.0.1",
+ "engines": {
+ "node": ">= 0.8.0"
+ },
+ "keywords": [
+ "mongoose",
+ "capped collections",
+ "plugin"
+ ],
+ "author": {
+ "name": "Jonathan Brumley",
+ "email": "cayasso@gmail.com"
+ },
+ "homepage": "https://github.com/cayasso/mongoose-allocate",
+ "main": "./index",
+ "scripts": {
+ "test": "make test"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/cayasso/mongoose-allocate.git"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "*",
+ "mongoose": "*",
+ "expect.js"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://www.opensource.org/licenses/mit-license.php"
+ }
+ ]
+}
2  test/mocha.opts
@@ -0,0 +1,2 @@
+-R spec
+--ui bdd
112 test/test.js
@@ -0,0 +1,112 @@
+var expect = require('expect.js');
+var mongoose = require('mongoose');
+var Schema = mongoose.Schema;
+var padding = require('../lib/allocate');
+
+/**
+ * connect to MongoDB with Mongoose
+ **/
+
+var MongoDB = process.env.MONGO_DB || 'mongodb://localhost/mongoose-test-allocate';
+mongoose.connect(MongoDB);
+
+describe('mongoose-padding', function () {
+ describe('capped collections', function () {
+ it ('should fail without the plugin applied', function (done) {
+ var s = new Schema({
+ title1: String,
+ title2: String,
+ title3: String
+ }, { capped: { size: 1000, max: 5, autoIndexId: true } });
+ mongoose.model('M1', s).create({
+ title1: 'T'
+ }, function (err, doc) {
+ if (err) return done(err);
+ doc.title1 = 'TTT';
+ doc.title2 = 'xxxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxx',
+ doc.save(function (_err, _doc) {
+ expect(_err).to.be.ok();
+ done();
+ });
+ });
+ });
+ it ('should not fail with the plugin applied', function (done) {
+ var s = new Schema({
+ title1: String,
+ title2: String,
+ title3: String
+ }, { capped: { size: 1000, max: 5, autoIndexId: true } });
+
+ // Applying plugin
+ s.plugin(padding);
+
+ mongoose.model('M2', s).create({
+ title1: 'T'
+ }, function (err, doc) {
+ if (err) return done(err);
+ doc.title1 = 'TTTTTTTTT';
+ doc.title2 = 'xxxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxx',
+ doc.save(function (_err, _doc) {
+ if (err) return done(_err);
+ expect(_doc).to.be.ok();
+ expect(_doc.title2).to.be.ok('xxxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxx');
+ done();
+ });
+ });
+ });
+ it ('should allow getting plugin settings', function (done) {
+ var s = new Schema({
+ title1: String,
+ title2: String,
+ title3: String
+ }, { capped: { size: 1000, max: 5, autoIndexId: true } });
+
+ // Applying plugin
+ s.plugin(padding, { fieldName: 'padding', len: 100, char: 'x'});
+ var Model = mongoose.model('M3', s);
+
+ Model.create({
+ title1: 'T'
+ }, function (err, doc) {
+ if (err) return done(err);
+ expect(Model.getPaddingSettings()).to.be.eql({ len: 100, char: 'x', fieldName: 'padding'});
+ done();
+ });
+ });
+ it ('should use default settings if no options are passed', function (done) {
+ var s = new Schema({
+ title1: String,
+ title2: String,
+ title3: String
+ }, { capped: { size: 1000, max: 5, autoIndexId: true } });
+
+ // Applying plugin
+ s.plugin(padding);
+ var Model = mongoose.model('M4', s);
+
+ Model.create({
+ title1: 'T'
+ }, function (err, doc) {
+ if (err) return done(err);
+ expect(Model.getPaddingSettings()).to.be.eql({ len: 256, char: '0', fieldName: '__p'});
+ done();
+ });
+ });
+ it ('should get settings with both static and instance "getPaddingSettings" method', function (done) {
+ var s = new Schema({
+ title1: String
+ }, { capped: { size: 1000, max: 5, autoIndexId: true } });
+
+ // Applying plugin
+ s.plugin(padding);
+ var Model = mongoose.model('M5', s);
+ Model.create({
+ title1: 'T'
+ }, function (err, doc) {
+ if (err) return done(err);
+ expect(Model.getPaddingSettings()).to.be.eql(doc.getPaddingSettings());
+ done();
+ });
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.