Giter Site home page Giter Site logo

node-crud-generator's Introduction

CRUD Generator

CRUD generator is a Node.js helper for generating Create, Read, Update & Destroy (CRUD) actions for your methods and, optionally, attaching them to your controllers.

Features

  • Pagination
  • Filtering (When attached to controllers via query params)

How to use

Install CRUD Generator npm install crud-generator

An example controller might look like:

var CrudGenerator = require('crud-generator');
var model = require('../model');
var crud = new CrudGenerator(model);

var controller = {
	customAction: function() {
		// code
	}
};

// Attach all CRUD actions to your controller
crud.all(controller);

// Attach some CRUD actions to your controller
crud.only('create show', controller);

// Attach all but index
crud.without('index', controller);

// Just get crud show method for model
var show = crud.only('show');

// Get all methods for model
var crudMethods = crud.all();

module.exports = controller;

Overriding generated CRUD actions

Sometimes you need custom logic for CRUD actions. You can achieve this by manually writing a custom show action for your controller which will override anything attached by the Crud Generator.

var CrudGenerator = require('crud-generator');
var model = require('../model');
var crud = new CrudGenerator(model);

var controller = {
	// This method will always take precedence over the Crud Generator
	show: function() {}
};

// Attach all CRUD actions to your controller
crud.all().extend(controller);

// Controller show method will still reference custom method above

module.exports = controller;

Pagination

You can control pagination of data returned from the CRUD index method by passing options as query params. The 2 options available are:

  • perPage: Controls how many documents are returned for each page
  • page: Controls which page to show in conjuntion with the perPage option

Sorting

A query string, given a sort key, will set the sort order.

A - prefix can be used to specify decending order.

Sort fields can be chained using comma seperation.

Example

/?sort=-name: Sorts by name in decending order.

/?sort=name,-date: Sorts by name in ascending order and date in decending order.

Filtering

TODO

===================

node-crud-generator's People

Contributors

rolandnsharp avatar tonymilne avatar willmcclellan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.