npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

objection-datatables

v2.1.0

Published

connecting objection.js with jquery datatables

Downloads

4

Readme

Datatables Query for Objection.js

npm node

Description

  • This package serves good projects that use server side Jquery DataTables plugin on their frontend.
  • Pagination using limit and offset.
  • Limit is used for number of rows returned.
  • Can fetch graph too with query.
  • Supports column ordering
  • Supports search by any column

Installation

NPM

npm i objection-datatables --save

Usage (simple)

Mixin and usage of the plugin

const { Model } = require('objection');

// Import the dataTable plugin.
const dataTable = require('objection-datatables');

// Mixin
class Cat extends dataTable(Model) {
    static get tableName() {
        return 'cats';
    }
}

Express.js route example using the plugin

// express.js route example

route.get('/cats', async (req, res) => {
    /**
     * Use req.query since datatables plugin sends
     * all info as url query string parameters
     */
    const cats = await Cat.query().dataTable(req.query).where('cute', true);

    res.json({
        draw: req.query.draw,
        data: cats,
        recordsTotal: cats.total,
    });
});

Simple example on the frontend of a datatable

<table id="my-table"></table>
<script>
    $('#my-table').DataTable({
        serverSide: true,
        ajax: {
            url: '/cats',
            dataSrc: 'data.results',
        },
    });
</script>

Usage (complex)

  • As mentioned, this package also supports graph fetching if a graph name was mentioned in the columns of a datatable options, for example, consider the following models
const { Model } = require('objection');

// Import the dataTable plugin.
const dataTable = require('objection-datatables');

// class person (owner)
class Person extends Model {
    static get tableName() {
        return 'persons';
    }

    static get jsonSchema() {
        return {
            type: 'object',
            required: ['name', 'stock'],
            properties: {
                id: { type: 'integer' },
                name: { type: 'string' },
                company: { type: 'string' },
                loves_cats: { type: 'boolean' },
            },
            additionalProperties: false,
        };
    }
}

// Mixin Cat class which uses the datatable plugin
class Cat extends dataTable(Model) {
    static get tableName() {
        return 'cats';
    }

    static get jsonSchema() {
        return {
            type: 'object',
            required: ['name', 'stock'],
            properties: {
                id: { type: 'integer' },
                name: { type: 'string' },
                color: { type: 'string' },
                cute: { type: 'boolean' },
                owner_id: { type: 'integer' },
            },
            additionalProperties: false,
        };
    }

    static get relationMappings() {
        const { BelongsToOneRelation } = Model;
        return {
            owner: {
                relation: BelongsToOneRelation,
                modelClass: Person,
                join: {
                    from: 'cats.owner_id',
                    to: 'persons.id',
                },
            },
        };
    }
}

complex Express.js route example using the plugin

// express.js route example

route.get('/cats', async (req, res) => {
    /**
     * here, .dataTable query handles all graph fetching
     * which is called by the datatable columns,
     * if any more graph fetching or conditional queries exist,
     * it can be written before it or after it too.
     * 
     * @see https://vincit.github.io/objection.js/recipes/custom-query-builder.html#custom-query-builder-extending-the-query-builder
     */
    const cats = await Cat.query()
        .dataTable(req.query)
        .where('cute', true)
        .modifyGraph('owner', (builder) => builder.select('name', 'company'));

    res.json({
        draw: req.query.draw,
        data: cats,
        recordsTotal: cats.total,
    });
});

complex example on the frontend of a datatable

<table id="my-table"></table>
<script>
    $('#my-table').DataTable({
        serverSide: true,
        ajax: {
            url: '/cats',
            dataSrc: 'data.results',
        },
        // server side orders by first column (which is id) descending
        // graph fetched columns like owner columns are unorderable (can't be ordered)
        order [[0, 'desc']]
        columns:[
            // query only selects columns that are declared here
            { title:"ID", data:'id' },
            { title:"Name", data:'name' },
            /**
             * 1) fetches "owner" graph only if it's a relation mapping to the Cat model
             * 2) if the next two lines were commented, no graph would be fetched
             * 3) query selects all columns for a graph
             * 4) to resolve number 3, use modifyGraph from objection as in the example
             * 5) if you want all columns from graph, do not use modifyGraph
             * 6) you can fetch multiple graphs as well
             */
            { title:"Owner name", data: 'owner', render:'name' },
            { title: "Owner company", data: 'owner', render:'company'}
        ]
    });
</script>