Code Monkey home page Code Monkey logo

amygdala's Introduction

Amygdala

Amygdala is a RESTful HTTP client for JavaScript powered web applications. Simply configure it once with your API schema, and easily do GET, POST, PUT and DELETE requests with minimal effort and a consistent API. Amygdala also handles session-based caching on the browser for fast access to your data, and aims to have offline through indexedDB and localStorage.

browser support

The overview

Amygdala requires you to define a schema of how your API and data relations look like. With the schema defined, Amygdala gives you some very simple methods to access your API, and stores the data locally in the browser session (and eventually in localStorage) for easy and fast data access.

NOTE: Amygdala is under heavy development, and schema support is limited.

How it works

1. Install Amygdala

If you're using npm, you can just npm install amygdala. Otherwise, download the browser version of from the dist directory.

2. Create your instance and define your schema

  var store = new Amygdala({
    'config': {
      'apiUrl': 'http://localhost:8000',
      'idAttribute': 'url',
      'headers': {
        'X-CSRFToken': getCookie('csrftoken')
      },
      'localStorage': true
    },
    'schema': {
      'users': {
        'url': '/api/v2/user/'
      },
      'teams': {
        'url': '/api/v2/team/',
        'orderBy': 'name',
        'oneToMany': {
          'members': 'members'
        }
      },
      'members': {
        'foreignKey': {
          'user': 'users'
        }
      }
    }
  });

3. Use it

  // Get a list of users from the remote server
  var users = store.get('users');

  // Get the list of active users from the local cache
  var users = store.findAll('users', {'active': true});

  // Get a single user from the local cache
  var user = store.find('users', {'username': 'amy82'});

  // Add a new user
  store.add('users', {'username': 'gdala67', 'active': false});

  // update an existing user
  // NOTE: At the moment we rely on the url attribute containing
  // the API endpoint for the user.
  store.update('users', {'url': '/api/v2/user/32/', 'active': true});

  // delete a user
  store.remove('users', {'url': '/api/v2/user/32/'});

Configuration

Here are the few configuration settings you can set:

  • apiUrl // your base API url (protocol, domain name, port).
  • idAttribute // if you want to use a custom "primary key"
  • headers // holds a key/value list of headers to be sent on every request
  • localStorage // If set to true, all your data will be persist across sessions (read-only).

Events

Amygdala uses [https://www.npmjs.org/package/event-emitter](Event Emitter) under the hood to trigger some very basic events. Right now it only triggers two different events:

  • change
  • change:type

To listen to these events, you can use any of [https://www.npmjs.org/package/event-emitter](Event Emitter)'s binding methods, the most common being on:

  // Listen to any change in the store
  store.on('change', function() { ... });

  // Listen to any change of a specific type
  store.on('change:users', function() { ... });

amygdala's People

Contributors

mlouro avatar bkonkle avatar

Watchers

Ismail BOUDAOUD 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.