Comparing version 0.3.2 to 0.3.3-1
36
index.js
@@ -12,9 +12,15 @@ // Dependencies | ||
// Set a field or array off fields to be populated | ||
function populateQuery (query, populate) { | ||
if (!query) throw new Error('Query was undefined'); | ||
if (!populate) return; | ||
populate = JSON.parse(populate); | ||
if (!Array.isArray(populate)) populate = [ populate ]; | ||
populate.forEach(function (field) { query.populate(field) }); | ||
// Apply various options based on request query parameters | ||
function applyQueryParams (query, request) { | ||
var populate; | ||
if (!query) throw new Error('Query was falsy'); | ||
if (request.query.skip) query.skip(request.query.skip); | ||
if (request.query.limit) query.limit(request.query.limit); | ||
if (request.query.populate) { | ||
populate = JSON.parse(populate); | ||
if (!Array.isArray(populate)) populate = [ populate ]; | ||
populate.forEach(function (field) { query.populate(field) }); | ||
} | ||
} | ||
@@ -47,3 +53,3 @@ | ||
if (options.restrict) options.restrict(query, request); | ||
populateQuery(query, request.query.populate); | ||
applyQueryParams(query, request); | ||
@@ -67,3 +73,3 @@ query.count(function (error, count) { | ||
if (options.restrict) options.restrict(query, request); | ||
populateQuery(query, request.query.populate); | ||
applyQueryParams(query, request); | ||
@@ -101,3 +107,2 @@ query.exec(function (error, doc) { | ||
if (options.restrict) options.restrict(query, request); | ||
populateQuery(query, request.query.populate); | ||
@@ -126,3 +131,2 @@ query.exec(function (error, doc) { | ||
if (options.restrict) options.restrict(query, request); | ||
populateQuery(query, request.query.populate); | ||
@@ -151,3 +155,3 @@ query.exec(function (error, count) { | ||
if (options.restrict) options.restrict(query, request); | ||
populateQuery(query, request.query.populate); | ||
applyQueryParams(query, request); | ||
@@ -176,3 +180,3 @@ query.count(function (error, count) { | ||
if (options.restrict) options.restrict(query, request); | ||
populateQuery(query, request.query.populate); | ||
applyQueryParams(query, request); | ||
@@ -245,3 +249,3 @@ // Stream the array to the client | ||
location = options.basePath + '?query={ id: { $in: [' + ids.join(',') + '] } }'; | ||
location = options.basePath + '?conditions={ _id: { $in: [' + ids.join() + '] } }'; | ||
response.set('Location', location); | ||
@@ -278,3 +282,3 @@ | ||
if (options.restrict) options.restrict(query, request); | ||
populateQuery(query, request.query.populate); | ||
applyQueryParams(query, request); | ||
@@ -333,3 +337,3 @@ query.exec(function (error, count) { | ||
response.set('Allow', allowed.join(', ')); | ||
response.set('Allow', allowed.join()); | ||
@@ -336,0 +340,0 @@ next(); |
{ | ||
"name": "baucis", | ||
"version": "0.3.2", | ||
"version": "0.3.3-1", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -1,3 +0,3 @@ | ||
baucis v0.3.2 | ||
============= | ||
baucis v0.3.3-1 | ||
=============== | ||
@@ -27,3 +27,3 @@ Baucis is Express middleware that creates configurable REST APIs using Mongoose schemata. | ||
// Define a Mongoose schema | ||
// Define a couple Mongoose schemata | ||
var Vegetable = new mongoose.Schema({ | ||
@@ -33,9 +33,21 @@ name: String | ||
var Fruit = new mongoose.Schema({ | ||
name: String | ||
}); | ||
// Also note that Mongoose middleware will be executed as usual. | ||
Vegetable.pre('save', function () { ... }); | ||
// Register the schema | ||
mongoose.model('vegetable', Vegetable); | ||
// Create routes for the schema | ||
// Create routes for the schemata | ||
baucis.rest({ | ||
singular: 'vegetable' | ||
singular: 'vegetable', | ||
}); | ||
baucis.rest({ | ||
singular: 'fruit' | ||
}); | ||
// Create the app and listen for API requests | ||
@@ -57,6 +69,2 @@ var app = express(); | ||
Also note that Mongoose middleware will be executed as usual. | ||
Vegetable.pre('save', function () { ... }); | ||
Examples | ||
@@ -104,15 +112,12 @@ -------- | ||
url: '/vegetables', | ||
// This method stringifies baucis' options into fetch's `data` option, | ||
// while leaving regular fetch options as they are. | ||
baucis: function (baucisOptions, fetchOptions) { | ||
fetchOptions = _.clone(fetchOptions || {}); | ||
fetchOptions.data = {}; | ||
baucis: function (options) { | ||
if (!options) return this.fetch(); | ||
if (baucisOptions) { | ||
Object.keys(baucisOptions).forEach(function (key) { | ||
fetchOptions.data[key] = JSON.stringify(baucisOptions[key]) | ||
}); | ||
} | ||
var data = {}; | ||
return this.fetch(fetchOptions); | ||
Object.keys(options).forEach(function (key) { | ||
data[key] = JSON.stringify(options[key]) | ||
}); | ||
return this.fetch({ data: data }); | ||
} | ||
@@ -131,10 +136,10 @@ }); | ||
var promise = vegetables.baucis({ | ||
vegetables.baucis({ | ||
conditions: { color: red }, | ||
populate: 'child' | ||
}}); | ||
}); | ||
// or | ||
populate: ['child1i', 'child2' ] | ||
populate: ['child1', 'child2' ] | ||
@@ -154,2 +159,9 @@ // or | ||
`skip` and `limit` are available as well, typically used for paging: | ||
vegetables.baucis({ | ||
skip: 20, | ||
limit: 10 | ||
}); | ||
`bacuis.rest` | ||
@@ -185,3 +197,3 @@ ------------- | ||
* The `Location` HTTP header is set for PUT and POST responses. | ||
* If `relations: true` is passed to `baucis.rest`, HTTP link headers will be set for all responses. | ||
* If `relations: true` is passed to `baucis.rest`, the HTTP `Link` header will be set with various links for all responses. | ||
@@ -188,0 +200,0 @@ Controllers |
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
271462
23
1043
246