0.1.1 • Published 4 years ago
ormon_crudable v0.1.1
Ormon_Crudable
Generate crud on qpi for an Ormon Model
Quick Start
// import ormon
const { OrmonEngine, MongoDriver } = require("ormon")
// import ormon_crudable
const OrmonCrudable = require("ormon_crudable")
// Import Qpi
const Server = require("qpi")
// connect to the database
OrmonEngine.init(new MongoDriver("mongodb://localhost:27017/db"))
// Create QPI server
const server = new Server({
body: true, // enables body parser
query: true, // enables query parser
})
// Create a model
class Post extends OrmonCrudable {}
// Bind the model to a collection
OrmonEngine.bind("posts", Post, {
author: {type: "string", required: true },
published: { type: "date", default: () => new Date(), format: d => d.toISOString() },
content: { type: "string" }
})
// Create crud routes
Post.crud(server, "/articles")
server.listen(80)
create an article
> curl localhost/articles -X POST -H "content-type: application/json"
400
{
"code": "BadRequest",
"message": "Some fields are missing or invalid."
}
> curl localhost/articles -X POST -H "content-type: application/json" -d '
{
"author": "me"
}
'
200 {
"id": "230498092384",
"author": "me",
"published": "2010-11-12T13:14:15.016Z"
}
list all articles
> curl localhost/articles -X GET
{
"size": 50, // 50 articles per page
"length": 1, // number of page (2 pages = page 0 and page 1)
"current": 0, // current page index
"total": 1, // total number of articles,
"post": [ // the `Post` model is bound to the `post` collection
{
"id": "230498092384",
"author": "me",
"published": "2010-11-12T13:14:15.016Z"
}
]
}
Update an article
> curl localhost/articles/230498092384?mask=content -X PATCH -H "application/json" -d '
{
"content": "Patch just replace the content present in the mask",
"author": "you ?"
}
200 {
"id": "230498092384",
"author": "me",
"published": "2010-11-12T13:14:15.016Z",
"content": "Patch just replace the content present in the mask"
}
Replace an article
> curl localhost/articles/230498092384 -X PUT -H "application/json" -d '
{
"content": "Put overrides everything",
"author": "you ?",
}
200 {
"id": "230498092384",
"author": "you ?",
"published": "2010-11-12T13:14:15.016Z",
"content": "Put overrides everything"
}
Delete an article
> curl localhost/articles/230498092384 -X DELETE
200 {
"id": "230498092384",
"author": "you ?",
"published": "2010-11-12T13:14:15.016Z",
"content": "Put overrides everything"
}
> curl localhost/articles/230498092384 -X DELETE
404 {
"code": "NotFound",
"message": "This resource cannot be found."
}
Ovrerriding default behavior
const { DateTime } = require("luxon")
Post.crud(server, "/articles", {
list: [
async Q => {
// default start time is current month
let start = DateTime.local().startOf("month")
// if a month is specified as query parameters
if (Q.query.month) {
const filterByMonth = DateTime.fromISO(Q.query.month)
if (!filterByMonth.isValid)
return Q.badRequest() // if the month query parameter is invalid => 400
start = filterByMonth.startOf("month")
}
const end = start.endOf("month")
const [first, articles] = await Promise.all([
Post.findOne({}, { sort: [[ "published", 1 ]]}), // get the first article ever written
Post.findMany({
date: { // filter by date
$gte: start.toJSDate(),
$lte: end.toJSDate()
}
}, [], p => p.format()), // format all found articles
])
return Q.send({
first: first && first.$state.date || "",
start: start.toJSDate(),
end: end.toJSDate(),
articles
})
}
]
})