mongoose-url-slugs
Advanced tools
Comparing version 0.0.1 to 0.0.2
44
index.js
@@ -1,2 +0,3 @@ | ||
var extend = require('extend'); | ||
var extend = require('extend'), | ||
inspect = require('util').inspect; | ||
@@ -25,2 +26,3 @@ function defaultURLSlugGeneration(text) { | ||
} | ||
return (function (schema) { | ||
@@ -31,5 +33,5 @@ var schemaField = {}; | ||
schema.methods.ensureUniqueSlug = function (slug, cb) { | ||
schema.methods.ensureUniqueSlug = function (doc, slug, cb) { | ||
if (!options.index.unique) return cb(null, true); | ||
var model = this.model(this.constructor.modelName); | ||
var model = doc.constructor; | ||
var q = {}; | ||
@@ -44,30 +46,38 @@ q[options.key] = slug; | ||
schema.pre('save', function (next) { | ||
if (this.get([options.key])) next(); | ||
var self = this; | ||
schema.pre('validate', function (next) { | ||
var doc = this; | ||
if (doc.get(options.key)) return next(); | ||
var toSlugify = ''; | ||
if (slugProperty instanceof Array) { | ||
for (var i = 0; i < slugProperty.length; i++) { | ||
toSlugify += this.get(slugProperty[i]) + ' '; | ||
toSlugify += doc.get(slugProperty[i]) + ' '; | ||
} | ||
toSlugify = toSlugify.substr(0, toSlugify.length-1); | ||
} else { | ||
toSlugify = this.get(slugProperty); | ||
toSlugify = doc.get(slugProperty); | ||
} | ||
function uniqueSlugGeneration(slugCount) { | ||
function uniqueSlugGeneration(slugCount, cb) { | ||
if (typeof slugCount == 'function') { | ||
cb = slugCount; | ||
slugCount = undefined; | ||
} | ||
slugCount = slugCount || 1; | ||
var tmpSlug = options.generator(toSlugify + ((slugCount > 1)? ' ' + slugCount : '')); | ||
schema.methods.ensureUniqueSlug(tmpSlug, function (e, unique) { | ||
if (e) next(e); | ||
if (!unique) return uniqueSlugGeneration(++slugCount); | ||
else return tmpSlug; | ||
schema.methods.ensureUniqueSlug(doc, tmpSlug, function (e, unique) { | ||
if (e) cb(e); | ||
if (!unique) return uniqueSlugGeneration(++slugCount, cb); | ||
else cb(null, tmpSlug); | ||
}); | ||
} | ||
this.set(options.key, uniqueSlugGeneration()); | ||
next(); | ||
uniqueSlugGeneration(function (e, finalSlug) { | ||
if (e) return next(e); | ||
doc.set(options.key, finalSlug); | ||
next(); | ||
}); | ||
}); | ||
}); | ||
}; |
@@ -9,3 +9,3 @@ { | ||
"description": "Create URL compatiable slugs on mongoose models, ensuring uniqueness.", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"keywords": [ | ||
@@ -28,5 +28,4 @@ "mongoose slugs", | ||
"dependencies": { | ||
"async": "0.9.0", | ||
"extend": "1.2.1" | ||
} | ||
} |
123
Readme.md
@@ -19,17 +19,13 @@ # Mongoose URL Slugs | ||
```js | ||
var Streaming-S3 = require('streaming-s3'), | ||
fs = require('fs'); | ||
var mongoose = require('mongoose'), | ||
Schema = mongoose.Schema, | ||
URLSlugs = require('mongoose-url-slugs'); | ||
var fStream = fs.CreateReadStream(__dirname + '/video.mp4'); | ||
var uploader = new Streaming-S3(fStream, 'accessKey', 'secretKey', | ||
{ | ||
Bucket: 'example.streaming-s3.com', | ||
Key: 'video.mp4', | ||
ContentType: 'video/mp4' | ||
}, function (err, resp, stats) { | ||
if (err) return console.log('Upload error: ', e); | ||
console.log('Upload stats: ', stats); | ||
console.log('Upload successful: ', resp); | ||
} | ||
); | ||
var testSchema = new Schema({ | ||
first_name: {type: String, default: '', trim: true}, | ||
last_name: {type: String, default: '', trim: true}, | ||
rev: {type: String, default: '', trim: true} | ||
}); | ||
testSchema.plugin(URLSlugs('first_name last_name')); | ||
``` | ||
@@ -40,94 +36,31 @@ | ||
```js | ||
var Streaming-S3 = require('streaming-s3'), | ||
fs = require('fs'); | ||
var mongoose = require('mongoose'), | ||
Schema = mongoose.Schema, | ||
URLSlugs = require('mongoose-url-slugs'); | ||
var fStream = fs.CreateReadStream(__dirname + '/video.mp4'); | ||
var uploader = new Streaming-S3(fStream, 'accessKey', 'secretKey', | ||
{ | ||
Bucket: 'example.streaming-s3.com', | ||
Key: 'video.mp4', | ||
ContentType: 'video/mp4' | ||
} | ||
); | ||
uploader.begin(); // important if callback not provided. | ||
uploader.on('data', function (bytesRead) { | ||
console.log(bytesRead, ' bytes read.'); | ||
var testSchema = new Schema({ | ||
first_name: {type: String, default: '', trim: true}, | ||
last_name: {type: String, default: '', trim: true}, | ||
rev: {type: String, default: '', trim: true} | ||
}); | ||
uploader.on('part', function (number) { | ||
console.log('Part ', number, ' uploaded.'); | ||
}); | ||
// All parts uploaded, but upload not yet acknowledged. | ||
uploader.on('uploaded', function (stats) { | ||
console.log('Upload stats: ', stats); | ||
}); | ||
uploader.on('finished', function (resp, stats) { | ||
console.log('Upload finished: ', resp); | ||
}); | ||
uploader.on('error', function (e) { | ||
console.log('Upload error: ', e); | ||
}); | ||
// Save slugs to 'myslug' field. | ||
testSchema.plugin(URLSlugs('first_name last_name', {key: 'myslug'})); | ||
``` | ||
### Example 2: Uploading remote file without callback and options | ||
```js | ||
var Streaming-S3 = require('streaming-s3'), | ||
request = require('request'); | ||
var rStream = request.get('http://www.google.com'); | ||
var uploader = new Streaming-S3(rStream, 'accessKey', 'secretKey', | ||
{ | ||
Bucket: 'example.streaming-s3.com', | ||
Key: 'google.html', | ||
ContentType: 'text/html' | ||
}, | ||
{ | ||
concurrentParts: 2, | ||
waitTime: 10000, | ||
retries: 1, | ||
maxPartSize: 10*1024*1024, | ||
} | ||
); | ||
uploader.begin(); // important if callback not provided. | ||
uploader.on('data', function (bytesRead) { | ||
console.log(bytesRead, ' bytes read.'); | ||
}); | ||
uploader.on('part', function (number) { | ||
console.log('Part ', number, ' uploaded.'); | ||
}); | ||
// All parts uploaded, but upload not yet acknowledged. | ||
uploader.on('uploaded', function (stats) { | ||
console.log('Upload stats: ', stats); | ||
}); | ||
uploader.on('finished', function (resp, stats) { | ||
console.log('Upload finished: ', resp); | ||
}); | ||
uploader.on('error', function (e) { | ||
console.log('Upload error: ', e); | ||
}); | ||
``` | ||
## Defaults and Configurables | ||
* **concurrentParts** (Default: 5) - Parts that are uploaded simultaneously. | ||
* **waitTime** (Default: 1 min (60000 ms)) - Time to wait for verification from S3 after uploading parts. | ||
* **retries** (Default: 5) - Number of times to retry uploading a part, before failing. | ||
* **maxPartSize** (Default: 5 MB) - Maximum size of each part. | ||
* **key** (Default: 'slug') - Parts that are uploaded simultaneously. | ||
* **generator(text)** (Default: lowercases and then replaces all alphanumeric characters to '-') - Function to generate slug. | ||
* **index** - key schema settings. (see below) | ||
* - **index.type** (Default: String) - Mongoose schema property type. | ||
* - **index.trim** (Default: true) - Mongoose schema property trim. | ||
* - **index.index** (Default: true) - Mongoose schema property index. | ||
* - **index.unique** (Default: true) - Mongoose schema property unique. | ||
* - **index.required** (Default: true) - Mongoose schema property required. | ||
## History | ||
* v0.0.1 (2014-06-09) -- Initial release. | ||
* v0.0.2 (2014-06-09) -- Initial release. | ||
@@ -134,0 +67,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
68
7013
88
- Removedasync@0.9.0
- Removedasync@0.9.0(transitive)