GithubHelp home page GithubHelp logo

joserfjuniorllms / redis-crud-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from syaau/redis-crud

0.0 1.0 0.0 8 KB

Simple library for implementing CRUD in REDIS

JavaScript 85.36% Shell 14.64%

redis-crud-1's Introduction

redis-crud

A very simple library for generating models that uses REDIS as the database and provides the basic CRUD functionality.

Usage

const redis = require('then-redis');
const db = redis.createClient();
const generateModel = require('redis-crud').generateModel;

const UserModel = generateModel('User');

// Insert a new record and get the serial id using promise
UserModel.insert({username: 'admin', password: 'pwd'}).then(id => {
  console.log('New record id is ', id);
});

// Retrieve the record
UserModel.get(1).then(obj => {
  console.log('The record at 1 is ', obj);
});

// Update the record
UserModel.update(1, { name: 'John Doe' }).then(() => {
  // The record is updated
});

// Delete the record
UserModel.delete(1).then(res => {
  // The record is deleted
});

// Iterate through all the records, providing the number of records to
// retrieve on each iteration
const iterator = UserModel.iterate(10);
iterator.next().then(records => {
  // The list of records are available here
});

Using hooks

const UserModel = require('redis-crud').generatorModel(db, 'User');

// The hook invoked before a new record is inserted, with the object instance
// that is being inserted. Throw an error here to avoid the record from
// being inserted
UserModel.beforeInsert = function(obj) {

};

// The hook invoked after the record is inserted. The hook is provided with
// the new id of the record. Do the post record insertion operations here,
// like create secondary records for easy data retrieval based on certain
// properties
UserModel.afterInsert = function(obj, id) {

};

// The hook invoked before the record is updated. The obj provides the
// properties that are going to be updated (either new or old). Throwing an
// exception here will stop the record from being updated
UserModel.beforeUpdate = function(id, obj) {

};

// The hook invoked after the record is updated. The parameters are same as
// the ones provided in beforeUpdate
UserModel.afterUpdate = function(id, obj) {

};

// The hook invoked before the record is deleted. The obj provides the object
// in the database that is going to be removed. Throwing an exception here will
// stop the record from being deleted
UserModel.beforeDelete = function(id, obj) {

};

// The hook invoked after the record is deleted.
UserModel.afterDelete = function(id, obj) {

};

redis-crud-1's People

Contributors

syaau avatar

Watchers

James Cloos 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.