Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

baucis

Package Overview
Dependencies
Maintainers
1
Versions
202
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

baucis - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

144

index.js
var express = require('express');
var mongoose = require('mongoose');
var lingo = require('lingo');

@@ -7,7 +8,7 @@ var BASE_URI = '/api/'; // TODO config

var model = function(schema) {
return mongoose.models[schema.metadata('singular')];
return mongoose.model(schema.metadata('singular'));
};
var get = function(schema) {
// return the spcefied resource
// retrieve the addressed document
var r = function(request, response, next) {

@@ -26,6 +27,5 @@ var id = request.params.id;

var post = function(schema) {
// treat the given resource as a collection, and push the given object to it
// treat the addressed document as a collection, and push the addressed object to it (?)
var r = function (request, response, next) {
// TODO should check if certain metadata is set and perform accordingly
response.send(405); // method not allowed (as of yet unimplemented) ???
response.send(405); // method not allowed (as of yet unimplemented) ??? //TODO or 501?
};

@@ -37,3 +37,3 @@

var put = function (schema) {
// replace given object, or create it if nonexistant
// replace the addressed document, or create it if nonexistant
var r = function(request, response, next) {

@@ -44,3 +44,3 @@ var id = request.params.id || null;

if (err) return next(err);
response.send(200);
response.send(200); // TODO send 201 when creating or 200/204 when updating
});

@@ -66,6 +66,7 @@ };

var pluralGet = function (schema) {
// TODO take range params, etc.
// retrieve documents matching conditions
var r = function (request, response, next) {
var query = request.query || {}; // TODO validate? // get from JSON or queryStr
model(schema).find(query, function(err, docs) {
var conditions = request.body || {};
var query = model(schema).find(conditions);
query.exec(function(err, docs) {
if (err) return next(err);

@@ -80,9 +81,25 @@ response.json(docs);

var pluralPost = function (schema) {
// create a new object and return its ID
// create a new document and return its ID
var r = function(request, response, next) {
var o = new (model(schema))(request.body);
o.save(function (err, doc) {
if (err) return next(err);
response.json(doc._id);
if (!request.body || request.body.length === 0) {
return next(new Error('Must supply a document or array to POST'));
}
var ids = [];
var given = request.body;
if (!Array.isArray(given)) given = [given];
var docs = given.map( function(e) {
return new (model(schema))(e);
});
response.statusCode = 201;
docs.forEach( function (doc) {
doc.save(function (err, doc) {
if (err) return next(err);
ids.push(doc._id);
if (ids.length === docs.length) response.json(ids);
});
});
};

@@ -94,23 +111,5 @@

var pluralPut = function (schema) {
// replace entire collection with given new collection and return IDs
var r = function(request, response, next) {
model(schema).remove({}, function (err, foo) {
if (err) return next(err);
// TODO make sure is array (or marshal) and respond accordingly
var docs = request.body
var ids = [];
var numSaved = 0;
docs.forEach( function (data, i) { // TODO use some async lib here
var doc = new (model(schema))(data);
doc.save( function (err, doc) {
if (err) return next(err);
ids[i] = doc._id;
numSaved++;
if(numSaved === docs.length) response.json(ids);
});
});
});
// repalce all docs with given docs ...
var r = function (request, response, next) {
response.send(405); // method not allowed (as of yet unimplemented) ??? // TODO 501?
};

@@ -122,5 +121,7 @@

var pluralDel = function (schema) {
// delete entire collection
// delete all documents matching conditions
var r = function(request, response, next) {
model(schema).remove({}, function (err, count) {
var conditions = request.body || {};
var query = model(schema).remove(conditions);
query.exec(function (err, count) {
if (err) return next(err);

@@ -134,40 +135,44 @@ response.json(count);

express.HTTPServer.prototype.rest =
express.HTTPSServer.prototype.rest = function (schemata) {
var t;
module.exports = {};
if (schemata === 'Object') {
t = [];
for (key in schemata) {
if (!schemata.hasOwnProperty(key)) continue;
t.push(schemata[key]);
// TODO maybe check if express.HTTPServer exists and hook that up for backward compat.
module.exports.rest = function (app, schemata) {
if (!Array.isArray(schemata)) {
// if array leave alone, otherwise
if (schemata.paths) {
// single schema -> array
schemata = [schemata];
}
schemata = t;
else {
// hash -> array
schemata = Object.keys(schemata).map( function (key) {
return schemata[key];
});
}
}
else if (schemata !== 'Array') {
schemata = [schemata];
}
var that = this;
schemata.forEach( function (schema) {
var middleware = schema.metadata('middleware') || [];
var singular = BASE_URI + schema.metadata('singular');
var plural = BASE_URI + schema.metadata('plural');
var singular = schema.metadata('singular');
var plural = schema.metadata('plural') || lingo.pluralize(singular);
var singularUrl = BASE_URI + singular + '/:id';
var pluralUrl = BASE_URI + plural + '/';
var middleware = schema.metadata('middleware') || [];
// add if not already present
if (!model(schema)) {
mongoose.model(schema.metadata('singular'), schema, schema.metadata('plural'));
mongoose.model(singular, schema, plural);
}
that.get(singular + '/:id', middleware, get(schema));
that.post(singular, middleware, post(schema));
that.put(singular + '/:id', middleware, put(schema));
that.del(singular + '/:id', middleware, del(schema));
that.get(plural, middleware, pluralGet(schema));
that.post(plural, middleware, pluralPost(schema));
that.put(plural, middleware, pluralPut(schema));
that.del(plural, middleware, pluralDel(schema));
// app.head(singularUrl, middleware, head(schema)); // TODO
app.get(singularUrl, middleware, get(schema));
app.post(singularUrl, middleware, post(schema));
app.put(singularUrl, middleware, put(schema));
app.del(singularUrl, middleware, del(schema));
// app.head(pluralUrl, middleware, pluralHead(schema)); // TODO
app.get(pluralUrl, middleware, pluralGet(schema));
app.post(pluralUrl, middleware, pluralPost(schema));
app.put(pluralUrl, middleware, pluralPut(schema));
app.del(pluralUrl, middleware, pluralDel(schema));
});

@@ -181,3 +186,3 @@ };

if(data && typeof(data) === 'object') {
if (this._metadata) throw new Error('Metadata was already set'); // ecma5? TODO
if (this._metadata) throw new Error('Metadata was already set');
return this._metadata = data;

@@ -188,2 +193,1 @@ }

};
{
"name": "baucis",
"version": "0.0.0",
"version": "0.0.1",
"main": "index.js",

@@ -5,0 +5,0 @@ "scripts": {

@@ -6,5 +6,5 @@ baucis

David Rijckaert - Philemon and Baucis Giving Hospitality to Jupiter and Mercury
*David Rijckaert - Philemon and Baucis Giving Hospitality to Jupiter and Mercury*
Like Baucis and Philemon of old, this library provides REST to the weary traveler. Automatically creates REST services from Mongoose models:
Like Baucis and Philemon of old, this library provides REST to the weary traveler. Automatically creates REST services from Mongoose schemata:

@@ -16,4 +16,3 @@ var Vegetable = new Schema({

Vegetable.metadata({
singular: 'vegetable',
plural: 'vegetables'
singular: 'vegetable'
});

@@ -34,8 +33,7 @@

* GET /vegetable/:id — get the addressed document
* POST /vegetable/:id — currently unimplemented (in the future will push the data into a field specified for the addressed object)
* PUT /vegetable/:id — create or update the given document
* PUT /vegetable/:id — create or update the addressed document
* DEL /vegetable/:id — delete the addressed object
* GET /vegetables/ — get all documents (in the future will accept query args to pass to the mongo server)
* POST /vegetables/ — creates a new object and sends back its ID
* POST /vegetables/ — creates a new document and sends back its ID
* PUT /vegetables/ — replace all documents with given new documents

@@ -69,3 +67,2 @@ * DEL /vegetables/ — delete all documents (also will accept query args in future)

singular: 'vegetable',
plural: 'vegetables',
middleware: function(request, response, next) {

@@ -72,0 +69,0 @@ if (request.isAuthenticated()) return next();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc