express-crudify-mongoose

Mongoose to CRUD endpoints

Showing:

Popularity

Downloads/wk

5

GitHub Stars

3

Maintenance

Last Commit

5yrs ago

Contributors

3

Package

Dependencies

9

Size (min+gzip)

252.4KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Express CRUDify Mongoose

NPM version Build Status Coverage Status Dependency Status devDependency Status

Like express-restify-mongoose but with way less ambition.

Produces a simple CRUD interface.

Dependencies / tools

  • Babel
  • ..

Usage

npm install express-crudify-mongoose --save
const UserSchema = new Schema({
    name : {type: String, required: true},
    email: {type: String, required: true},
    admin: {type: Boolean, default: false, required: true},
});

const Users = db.model('user', UserSchema);
const readonly = ['admin'];

const crud = crudify({
    readonly,
    Model: Users,
});

server.use('/users', crud);

Endpoints

  • GET /users
  • POST /users
  • GET /users:_id
  • PATCH /users/:_id
  • DELETE /users/:_id

Operations

Filter

GET /users?name=Alex

Select all users with where name Alex.

The query gets forwarded straight to MongoDB, so it's possible to write queries like /posts?date[$gt]=2016-01-01T00:00:00.000Z to get all posts dated after 2016.

⚠️ If you're making a public-facing API, you might want to use preBuildQuery middleware to validate the params used.

Selecting partial outputs

GET /users?$select=name,email
GET /users/:id?$select=name,email

Only output name & email.

Pagination

GET /users?$limit=10
GET /users?$limit=10&skip=10

Middlewares

Middlewares can be async by being ES7 async functions or functions returning promises.

If you pass in an array of functions, they'll be executed sequentially.

preSave

Useful for custom validation, that you'd only want to run when change is done through API request and can therefore simply not be done by Mongoose's .pre('save', fn).

const readonly = ['admin'];
const Users = db.model('user', UserSchema);

const preSave = async (user) => {
    if (user.admin) {
        let err = new Error("Can't change admin users through API");
        err.statusCode = 400;

        throw err;
    }
    let err = new Error('Validation failed');
    
    
    const newData = data;
    
    // return the newData that you want in the output
    return newData;
}

const crud = crudify({
    Model: Users,
    readonly,
    preSave, // preSave: [preSave, ..] is also supported
});

preOutput

preOutput functions will receive an object with properties data and req.

  • req is the current
  • data is the data that is currently set to be output to client.

Note that preOutput is run the same for all endpoints, so sometimes data is an array and sometimes it's a singular item.

const Users = db.model('user', UserSchema);

const preOutput = async (data) => {
    const newData = data;
    
    // return the newData that you want in the output
    return newData;
}

const crud = crudify({
    Model: Users,
    preOutput, // preOutput: [preOutput, ..] is also supported
});

Development

  1. Clone project
  • make setup
  • make run

You can run example project with make example-babel

Git Commit Messages

Based on atom & dannyfritz/commit-message-emoji.

  • Use the present tense ("Add feature" not "Added feature")

  • Use the imperative mood ("Move cursor to..." not "Moves cursor to...")

  • Limit the first line to 72 characters or less

  • Reference issues and pull requests liberally

  • Start the commit message with an applicable emoji:

    Commit TypeEmoji
    Initial Commit🎉 :tada:
    Version Tag🔖 :bookmark:
    New Feature:sparkles:
    Bugfix🐛 :bug:
    Metadata📇 :card_index:
    Documentation📚 :books:
    Performance🐎 :racehorse:
    Cosmetic/refactor🎨 :art:
    Lint fixes👕 :shirt:
    Tests:white_check_mark:
    Removing files🔥 :fire:
    Security🔒 :lock:
    Upgrade deps⬆️ :arrow_up:
    Downgrade deps⬇️ :arrow_down:
    General Update:zap:
    Adding to debt😷 :mask:
    OtherBe creative

Code

TBC

Run project:

make run

Deploy

TBC

Documentation

Team

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100