Last active
December 25, 2018 22:01
-
-
Save developit/ec2f438efc5feea4fd3a to your computer and use it in GitHub Desktop.
mongoose-resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mongooseResource from '../lib/mongoose-resource'; | |
import Foo from '../models/foo'; // a mongoose Model | |
export default mongooseResource('foo', Foo); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import resource from 'resource-router-middleware'; | |
import { toRes } from './util'; | |
export default function(name, model) { | |
return resource({ | |
id : name, | |
list({ params }, res) { | |
var limit = Math.max(1, Math.min(50, params.limit|0 || 10)); | |
if (params.search) { | |
return model.textSearch(params.search, { | |
limit : limit, | |
language : 'en', | |
lean : true | |
}, toRes(res)); | |
} | |
model.find({}).skip(params.start|0 || 0).limit(limit).exec(toRes(res)); | |
}, | |
create({ body }, res) { | |
model.create(body, toRes(res)); | |
}, | |
read({ params }, res) { | |
model.findById(params[name], toRes(res)); | |
}, | |
update({ body, params }, res) { | |
delete body._id; | |
model.findByIdAndUpdate(params[name], { $set:body }, toRes(res)); | |
}, | |
delete(req, res) { | |
model.findByIdAndRemove(params[name], toRes(res)); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the
read()
,update()
anddelete()
functions are usingthis.id
but sinceid
is in the same object, I don't believe you can reference it in that manner. I think it works better if you usename
instead.