{"id":13555426,"url":"https://github.com/yahoo/elide-js","last_synced_at":"2025-04-03T08:31:15.830Z","repository":{"id":57222391,"uuid":"44630883","full_name":"yahoo/elide-js","owner":"yahoo","description":"Elide is a library that makes it easy to talk to a JSON API compliant backend.","archived":true,"fork":false,"pushed_at":"2018-09-20T22:06:01.000Z","size":124,"stargazers_count":18,"open_issues_count":8,"forks_count":3,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-03-20T22:22:56.940Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yahoo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-20T19:45:47.000Z","updated_at":"2024-04-02T17:42:19.000Z","dependencies_parsed_at":"2022-08-30T12:12:13.819Z","dependency_job_id":null,"html_url":"https://github.com/yahoo/elide-js","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yahoo%2Felide-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yahoo%2Felide-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yahoo%2Felide-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yahoo%2Felide-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yahoo","download_url":"https://codeload.github.com/yahoo/elide-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246965689,"owners_count":20861910,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T12:03:12.322Z","updated_at":"2025-04-03T08:31:13.302Z","avatar_url":"https://github.com/yahoo.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","others"],"sub_categories":[],"readme":"**Please Note:** This repository has known security vulnerabilities. Use at your own risk!\n\n[![Stories in Ready](https://badge.waffle.io/yahoo/elide-js.png?label=ready\u0026title=Ready)](https://waffle.io/yahoo/elide-js)\n[![Build Status](https://travis-ci.org/yahoo/elide-js.svg?branch=master)](https://travis-ci.org/yahoo/elide-js) [![npm version](https://badge.fury.io/js/elide-js.svg)](https://badge.fury.io/js/elide-js) [![Code Climate](https://codeclimate.com/github/yahoo/elide-js/badges/gpa.svg)](https://codeclimate.com/github/yahoo/elide-js)\n\nElide\n===============\nElide is a library that makes it easy to talk to a [JSON API](http://jsonapi.org/format) compliant backend.\nWhile it was specifically designed to talk to the [Elide server](https://github.com/yahoo/elide) it should\nwork with any server that is JSON API compliant.\n\nWhile the examples make use of ES6, it is by no means required to write your code in ES6. Elide is developed\nin ES6, but the distributed code is ES5 so you can run it without any other support libraries.\n\n## Getting Started\n```\nnpm install elide-js\n```\n\nElide is tested on:\n  * node 4.1.x\n  * node 4.0.x\n  * node 0.12.x\n  * node 0.10.x\n\n## Usage\n\n  * [Schema](#Schema)\n  * [API](#API)\n    - [*constructor*](#constructor)\n    - [find](#find)\n    - [create](#create)\n    - [update](#update)\n    - [delete](#delete)\n    - [commit](#commit)\n\n### Schema\nThe schema is a javascript object composed of two sections: `stores` and `models`.\n\nExample schema\n```javascript\nvar SCHEMA = {\n  stores: {\n    memory: {\n      type: 'memory', // memory stores cache data in the browser\n      upstream: 'jsonapi', // upstream stores are where queries fall back to  \n      ttl: 60000\n    },\n    jsonapi: {\n      type: 'jsonapi', // jsonapi stores talk to remote servers to fetch data\n      baseURL: 'https://stg6-large.flurry.gq1.yahoo.com/pulse/v1'\n    }\n  },\n  models: {\n    // all models have an implicit id property\n    // only the properties that are listed directly in the object or in the links object\n    // are parsed out of the response from server or sent to the server\n    company: { // the property names in models are the name of the model (JSON API type)\n      meta: {\n        store: 'jsonapi', // where we should start looking for instances of this model\n        isRootObject: true // if we can query for this model directly (defaults to false)\n      },\n      name: 'string',\n      publisherLevel: 'string|number', // the values of the properties are meant as documentation,\n                                       // in reality they could be any valid javascript value\n      publisherDiscount: 'number',\n      links: {\n        project: {  // this key will be the property name in the parsed object\n          model: 'project',  // what model type the property links to\n          type: 'hasMany',   // hasOne|hasMany\n          inverse: 'company' // what property on the other object will hold the inverse relationship\n        }\n      }\n    },\n    project: {\n      meta: {\n        store: 'memory'\n      }\n      name: 'string'\n    }\n  }\n};\n\nexport default SCHEMA; // or module.exports = SCHEMA; if you prefer\n```\n\n### API\n#### *constructor*(`schema`, `options`)\n`schema` - an object as described in [schema](#Schema)\n\n`options` - an object\n`options.promise` - Your favorite promise implementation\n```javascript\nvar schema = require('./schema'); // import schema from './schema';\nvar Promise = require('es6-promise').Promise // import {Promise} from 'es6-promise';\n\nvar options = {\n  promise: Promise\n};\n\nvar elide = new Elide(schema, options);\n```\n#### find(`model`, `id`, `opts`) → Promise(`result`)\n`model` - the name of the model (or property for nested queries) to search\n\n`id` - (optional) the id to find (leave blank for collections)\n\n`opts` - (optional) additional options for querying sparse fields, filtering and includes (see below)\n\n`result` - the object returned by the query. Will have the format: \n```\n{\n  data: object|array, \n  included: {\n    model: [], \n    model2: []\n  }\n}```\n\n```javascript\nelide.find('company', 1)\n  .then((results) =\u003e {\n    // do something with company 1\n  }).catch((error) =\u003e {\n    // inspect error to see what went wrong\n  });\n\nelide.find('company', 1)\n  .find('projects')\n  .then(function(results) {\n    // do something with company 1's projects\n  }).catch(function(error) {\n    // inspect error to see what went wrong\n  });\n  \nelide.find('company', 1, {fields: {projects: ['name', 'companyid']}})\n  .find('projects')\n    .then(function(results) {\n      // do something with company 1's projects's name and company id\n    }).catch(function(error) {\n      // inspect error to see what went wrong\n    });\n\nelide.find('company', 1, {filters: {project: [ {attribute: 'name', operator: \"in\", value: \"myapp\" }]}})\n  .find('projects')\n    .then(function(results) {\n      // do something with company 1's only myapp projects\n    }).catch(function(error) {\n      // inspect error to see what went wrong\n    });\n```\n\n##### Options\n\n`include` - an array of additional resource objects to include in the results that are \nrelated to the primary data. The contents of the array are the property names of the \nrelationships. For example `['authors', 'authors.spouse', 'publisher.bankAccounts']` \nwould include the authors for the requested books, the spouses for the included authors, \nand the bank accounts for the publisher of the requested books.\n\nFor instance, you might query for books and include the related author resources as follows:\n\n```javascript\nelide.find('book', {include: ['authors']})\n  .then((results) =\u003e {\n    console.log(results.data); // the books\n    console.log(results.included); // the included resources (authors)\n  });\n```\n\n`fields` - an object that specifies which set of fields to return for each model.\nBy default, all attributes and relationships described in the model will be fetched.\n\nFor instance, query for the title and authors of books as follows:\n\n```javascript\nelide.find('book', {fields: {book: ['title', 'authors']}})\n  .then((results) =\u003e {\n    console.log(results.data); // only book information will be available\n  });\n```\n\n**Note:** If you specify a fields option, it overrides the fields for **all models**.\nWhat this means is that if you query for books, include authors and ask for title and \nauthors of books, you will not get any fields back for authors. In addition to\ntitle and authors of books, you will also have to include name of authors, as follows:\n\n```javascript\nelide.find('book', {include: ['authors'], fields: {book: ['title', 'authors'], author: ['name']}})\n  .then((results) =\u003e {\n    console.log(results.data); // only book.title and book.authors will be available\n    console.log(results.included); // only author.name will be available\n  });\n```\n\n`filters` - an object that specifies criteria that result types must match.\n\nFor instance, query for all books that start with Harry Potter as follows:\n\n```javascript\nelide.find('book', {filters: {book: [ {attribute: 'title', operator: \"prefix\", value: \"Harry Potter\"} ]})\n  .then((results) =\u003e {\n    console.log(results.data); // returns books with title Harry Potter\n  });\n```\n\n#### create(`model`, `state`) → Promise(`result`)\n`model` - The name of the model to be created\n\n`state` - The initial state (without `id`) of the new object\n\n`result` - The object created by `create`\n```javascript\nlet company = {\n  name: 'Flurry',\n  publisherLevel: 'Advanced',\n  publisherDiscount: 0.5\n};\nelide.create('company', company)\n  .then((createdCompany) =\u003e {\n    // company now has an id\n  }).catch((error) =\u003e {\n    // inspect error to see what went wrong\n  });\n```\n\n#### update(`model`, `newState`) → Promise(`result`)\n`model` - The name of the model to be updated\n\n`state` - The new state of the object\n\n`result` - The object updated by `update`\n```javascript\nlet company = {\n  id: 1,\n  name: 'Flurry by Yahoo!'\n};\n\nelide.update('company', company)\n  .then(function(updatedCompany) {\n    // company.name now == 'Flurry by Yahoo!'\n  }).catch(function(error) {\n    // inspect error to see what went wrong\n  });\n```\n\n#### delete(`model`, `state`) → Promise()\n`model` - The name of the model to be deleted\n\n`state` - An object that contains at least `id` with the id of the object to be deleted\n\nDelete receives no value, but returns a Promise so errors can be caught.\n```javascript\nelide.delete('company', {id: 1})\n  .then(function() {\n    // there is no value received\n  }).catch((error) =\u003e {\n    // inspect error to see what went wrong\n  });\n```\n\n#### commit() → Promise()\nCommit receives no value but returns a Promise so errors can be caught\n```javascript\nelide.commit()\n  .then(() =\u003e {\n    // there is no value received\n  }).catch((error) =\u003e {\n    // inspect error to see what went wrong\n  });\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyahoo%2Felide-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyahoo%2Felide-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyahoo%2Felide-js/lists"}