GithubHelp home page GithubHelp logo

psanetra / entity-cache-js Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 20 KB

entity-cache.js manages your JavaScript data in caches.

License: MIT License

JavaScript 6.03% TypeScript 92.92% Shell 1.05%

entity-cache-js's Introduction

entity-cache.js

entity-cache.js manages your JavaScript objects in caches.

Features

  • Makes sure that there is only one instance of every unique entity (identified by an id property)
  • Recursively updates existing entities with new data
    • Properties, which do not exist in the updated data object, will not be updated
  • Automatically resolves entity dependencies by foreign keys using other entity-caches
    • If entity dependencies do not exists while an entity is added to the cache, the dependencies will be resolved as soon as these become available
  • Provides events if new entities are added to a cache or entities are removed from a cache
  • Supports and coded in TypeScript

Installation

npm install entity-cache-js --save

Examples

Initialize an entity-cache

var entityCacheJs = require('entity-cache-js');

function Person(personId, motherId, fatherId, firstName, lastName, address)
{
  this.personId = personId;

  this.motherId = motherId;
  this.fatherId = fatherId;

  this.firstName = firstName;
  this.lastName = lastName;
  this.address = address;
}

var personCache = new entityCacheJs.EntityCache('person', Person, 'personId');

personCache.addEntityCacheDependency(personCache, 'motherId', 'mother');
personCache.addEntityCacheDependency(personCache, 'fatherId', 'father');

Add an entity instance to the cache

personCache.updateOrInsert(new Person(1, 2, 3, 'John', 'Doe', { zip : '12345' }));
personCache.updateOrInsert(new Person(2, null, null, 'Julia', 'Joe'));
personCache.updateOrInsert(new Person(3, null, null, 'Johnson', 'Doe'));

Use entities

var johnDoe = personCache.get(1);

//use mother reference
console.log(johnDoe.mother.firstName); //prints Julia

//iterate over all entities
for(var i in personCache.entities)
{
  console.log(personCache.entities[i].firstName);
}

Update existing entity

console.log(johnDoe.address.zip); //prints 12345

personCache.updateOrInsert({
  personId: 1,
  address : {
    zip : '98765'
  }
});

console.log(johnDoe.address.zip); //prints 98765

Bugs

There may be still a few bugs. Please report them!

License

MIT

entity-cache-js's People

Contributors

psanetra 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.