Package Exports
- objection-express-crud
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (objection-express-crud) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
objection-express-crud
Installation
npm i --save objection-express-crud
Example
import bodyParser from 'body-parser';
import express from 'express';
import Knex from 'knex';
import { Model } from 'objection';
import { buildTable, Column, Table } from 'objection-annotations';
import objectionCrud from 'objection-express-crud';
@Table('todo_list')
class Todo extends Model {
@Column('increments')
id: number;
@Column('string', { required: true, schema: { maxLength: 20 } })
content: string;
@Column('integer', { required: true })
priority: number;
}
(async () => {
await buildTable(Knex({
client: 'sqlite3',
connection: {
filename: process.env['DB_FILE'],
},
useNullAsDefault: true,
}), Todo);
const app = express();
app.use(bodyParser.json());
// Register CRUD route
app.use('/todo', objectionCrud(Todo, {
async resultWrap(result, req, { routeName, getTotalPage, actualPage, getCount }) {
if (routeName === 'list') {
return {
items: result, paging: {
current: actualPage,
totalItem: await getCount(),
total: await getTotalPage(),
},
};
}
return result;
},
}));
app.listen(8080);
})();
Options
insertGraphOptions
routes:
Register express routing for objection, default option is truepreRoutes:
Register handler(s) before objection handlerpostRoutes:
Register handler(s) after objection handlerlistFn:
Function to modify query builder for listing routedetailFn:
Function to modify query builder for detail routeresultWrap:
Wrap function of query resulthandleResponse
handleForbidden
handleNotFound
handleError
canList
canDetail
canInsert
canUpdate
canDelete