t3chnoboy / koa-mongo-rest Goto Github PK
View Code? Open in Web Editor NEW:leaves: Rest API generation for Koa
Home Page: https://www.npmjs.org/package/koa-mongo-rest
:leaves: Rest API generation for Koa
Home Page: https://www.npmjs.org/package/koa-mongo-rest
I ran the below code:
var createModel = require('koa-mongo-rest');
var schema = {
schema: {
email: String,
name: String,
password: String,
address: String,
zipcode: Number,
lists: Array
},
collectionName: 'user'
};
var koa = require('koa');
var router = require('koa-router');
var mongoUrl = '127.0.0.1:27017'
var app = koa();
app.use(router(app));
var model = app.model = createModel(schema, mongoUrl);
model.generateApi(app);
app.listen(process.env.PORT || 5000);
and got error:
return pluralize(name.toLowerCase());
^
TypeError: Cannot call method 'toLowerCase' of undefined
at exports.toCollectionName (/Users/XXXX/node_modules/mongoose/lib/utils.js:28:25)
at Mongoose.model (/Users/XXXX/node_modules/mongoose/lib/index.js:361:46)
at model (/Users/XXXX/node_modules/koa-mongo-rest/lib/model.js:11:19)
at api (/Users/XXXX/node_modules/koa-mongo-rest/lib/index.js:10:11)
at Object.<anonymous> (/Users/XXXX/Downloads/koa-mongo-rest-master/example/app.js:22:9)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:349:32)
at Function.Module._load (module.js:305:12)
at Function.Module.runMain (module.js:490:10)
I also ran the example in koa-mongo-rest/example, it did lanuch successfully, but when I changed createModel = require('../lib/index');
to createModel = require('koa-mongo-rest');
, the same error occurred.
I just stole the code of https://github.com/adamjacobbecker/mongoose-api-query and used it to enhance findAll
. Is it appropriate for integrating mongoose-api-query
to koa-mongo-rest
? If you thinks so, I may convert the code to coffeescript, then submit to your repo.
It's using app.get/app.post from koa.router and those don't work anymore, or have I missed something?
mongoose-timestamp
https://www.npmjs.org/package/mongoose-stamp is used as below:
model = createModel(schema, mongoUrl);
var timestamps = require('mongoose-timestamp');
model.schema.plugin(timestamps);
but when I post a new record, the time stamp is not added to the record.
Am I using it right?
Hi,
At fist thans for this repo.
I have problems with the actions functions, when I call routes ressouces/:id i have in the related action function
You may only yield a function, promise, generator, array, or object, but the following object was passed: "55af8ca1baedabac1b10b075"
For exemple https://github.com/t3chnoboy/koa-mongo-rest/blob/master/lib/actions.js#L26.
I dont understand this line by the way.
I do something wrong ?
I am running the example, with a simple modification in require koa-mongo-rest
koa = require('koa');
router = require('koa-router');
generateApi = require('koa-mongo-rest');
mongoUrl = '127.0.0.1:27017';
mongoose = require('mongoose');
mongoose.connect(mongoUrl);
schema = new mongoose.Schema({
email: String,
name: String,
password: String,
address: String,
zipcode: Number,
lists: Array
});
app = koa();
app.use(router(app));
model = mongoose.model('user', schema);
generateApi(app, model, '/api');
app.listen(process.env.PORT || 5000);
It gives me this error:
C:\del\mongotest>npm start
[email protected] start C:\del\mongotest
node --harmony app.js
assert.js:86
throw new assert.AssertionError({
^
AssertionError: app.use() requires a generator function
at Application.app.use (C:\del\mongotest\node_modules\koa\lib\application.js:100:5)
at Object. (C:\del\mongotest\app.js:19:5)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "start"
npm ERR! node v0.12.5
npm ERR! npm v2.11.2
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: node --harmony app.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script 'node --harmony app.js'.
npm ERR! This is most likely a problem with the mongotest package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node --harmony app.js
npm ERR! You can get their info via:
npm ERR! npm owner ls mongotest
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\del\mongotest\npm-debug.log
I have some idea about how to add access control with koa and koa-mongo-rest. It would be nice to see it documented though since that may be a main point of concern before adopting this system.
Maybe something like:
apiAuthorizationMap = {
'/api/users/:id': function* () {
// this.session......
yield true;
}
};
app.use(function* (next) {
// this.route doesn't quite work, but let's hand wave for now.
if (this.method === 'GET' || apiAuthorizationMap[this.route].apply(this)) {
yield next;
} else {
this.throw('Unauthorized', 401);
}
});
Do you guys have a better way?
Other requests to the application are logged, but the once handled by this module do not.
Great work by the way :)
when I add app.use(bodyParser) all the methods in koa-mongo-rest which look for the body start failing silently and the request is left hanging.
I'm not sure how to deal with that yet, so I'm opening for discussion.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.