objection-express-crud
TypeScript icon, indicating that this package has built-in type declarations

2.2.1 • Public • Published

objection-express-crud

version npm

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 true
  • preRoutes: Register handler(s) before objection handler
  • postRoutes: Register handler(s) after objection handler
  • listFn: Function to modify query builder for listing route
  • detailFn: Function to modify query builder for detail route
  • resultWrap: Wrap function of query result
  • handleResponse
  • handleForbidden
  • handleNotFound
  • handleError
  • canList
  • canDetail
  • canInsert
  • canUpdate
  • canDelete

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.2.11latest

Version History

VersionDownloads (Last 7 Days)Published
2.2.11
2.2.01
2.1.11
2.1.01
2.0.01
1.3.11
1.3.01
1.2.01
1.1.01
1.0.02

Package Sidebar

Install

npm i objection-express-crud

Weekly Downloads

11

Version

2.2.1

License

MIT

Unpacked Size

38 kB

Total Files

17

Last publish

Collaborators

  • dkhang97