Giter VIP home page Giter VIP logo

js-data / js-data Goto Github PK

View Code? Open in Web Editor NEW
1.6K 1.6K 139.0 19.71 MB

Give your data the treatment it deserves with a framework-agnostic, datastore-agnostic JavaScript ORM built for ease of use and peace of mind. Works in Node.js and in the Browser. Main Site: http://js-data.io, API Reference Docs: http://api.js-data.io/js-data

License: MIT License

JavaScript 79.45% CSS 20.55%
data-mapper datastore javascript nodejs odm orm

js-data's Introduction

js-data logo

Browser tests Node.js tests
Tests Tests Tests Tests Tests Tests Tests

Community & Maintainers always welcome - message in #jsdata-core-discuss Slack channel if you want to help with issues, improvements, tests, documentation, tutorials, adapters, etc.. There are several adapters that could use minor maintenance as well as new adapters to be written.

JSData is a framework-agnostic, datastore-agnostic ORM for Node.js and the Browser.

Adapters allow JSData to connect to various data sources such as Firebase, MySql, RethinkDB, MongoDB, localStorage, Redis, a REST API, etc. With JSData you can re-use your data modeling code between environments, keep your data layer intact when transitioning between app frameworks, and work with a unified data API on the server and the client. JSData employs conventions for rapid development, but allows for endless customization in order to meet your particular needs.

Just getting started?

Start with the JSData Getting Started Tutorial or explore the API Reference Documentation.

Need help?

Please post a question on Stack Overflow. This is the preferred method.

You can also chat with folks on the Slack Channel. If you end up getting your question answered, please still consider consider posting your question to Stack Overflow (then possibly answering it yourself). Thanks!

Want to contribute?

Awesome! You can get started over at the Contributing guide.

And thank you!

License

The MIT License (MIT)

Copyright (c) 2014-2017 js-data project authors

js-data's People

Contributors

antoinebrault avatar crobinson42 avatar davincho avatar frans-k avatar internalfx avatar ivanvoznyakovsky avatar jmdobry avatar kenjiqq avatar kvandelden avatar luisvt avatar lumimies avatar matthewoverall avatar mattlewis92 avatar mhwinkler avatar mightyguava avatar mikeu avatar mitranim avatar nickescallon avatar nvahalik avatar ozzieorca avatar pencroff avatar pik avatar ready-research avatar stalniy avatar systemparadox avatar tfoxy avatar treyenelson avatar tronix117 avatar wilgert avatar zuzusik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-data's Issues

Client side relations

I am in the process of selecting the model layer provider for our new angular app. I have BookshelfJS ORM and PostgreSQL on the Node server. What I am trying to understand is whether there any benefits in setting up relations/associations on the client side if I already have them set up in Bookshelf ORM? I see value in the client side model validations to avoid unnecessary requests, but client side associations on the other hand... Any advantages?

DS#defineResource docs missing deserialize and serialize options

The docs at http://www.js-data.io/v1.3.0/docs/dsdefineresource seem to be missing the deserialize and serialize options.

I am using the deserialize option like this:

DS.defineResource({
    name: 'info',
    endpoint: 'info',
    basePath: '/api',
    relations: {
        belongsTo: {
            document: {
                parent: true,
                localField: 'document',
                localKey: 'documentId'
            }
        }
    },
    deserialize: function (resource, data) {
        return [data.data];
    }
});

(I have an endpoint that returns a single object instead of an array and am using the deserialize function to place the object in an array so that js-data can use it.)

feat: Load relations based on local field name

It would be nice if one could load relations based on the local field name instead of the resource name. This will come in handy when having multiple relationships of the same type and you only want to load one of them.

Example:

DS.defineResource({
  name: 'document',
  relations: {
    belongsTo: {
      user: {
        localField: 'created_by'
        localKey: 'userId',

      },
      user: {
        localField: 'modified_by'
        localKey: 'userId'
      }
    }
  }
}
DS.loadRelations('document', document, ['created_by']);

`Relation is defined but the resource is not` Error

I have these two resources defined in Angular:

.factory('Classified', function(DS) {
  return DS.defineResource({
    name: 'classified',
    endpoint: '/classifieds',
    relations: {
      hasMany: {
        ad: {
          foreignKey: 'adId',
          localField: 'ad'
        }
      }
    }
  });
});

.factory('Ad', function(DS) {
  return resource = DS.defineResource({
    name: 'ad',
    endpoint: '/ads',
    relations: {
      belongsTo: {
        classified: {
          localKey: 'classifiedId',
          localField: 'classified'
        }
      }
    }
  });

Then when I try to resolve Classified in my top level state:

.config(function($stateProvider) {
  $stateProvider
    .state('app.classified', {
      url: '/classifieds/:classifiedId',
      resolve: {
        classified: function($stateParams, Classified) {
          return Classified.find($stateParams.classifiedId);
        }
      },
     ... ... ...

it throws this error:

RuntimeError: classified relation is defined but the resource is not!

Does it mean that if I define a relation, I need to inject the related model factories upon first use?

Multiple parents for nested urls (re-issued from https://github.com/js-data/js-data-angular/issues/250)

Hi,
I have a special use-case, where multiple belongsTo are needed.
In our data-model, where we don't know the hierarchy-depth of nested elements in advance, so we use a 'general' entity, that is nested in 'itself'. We have an additional 'type' entity, defining the nodes special attributes, like this:

name: 'Entity',
endpoint: 'entities.json',

relations:
{
    belongsTo:
    {
        Entity:
        {
            localField: 'parent',
            localKey: 'parentId',
            parent: false
        },
        EntityType:
        {
            localField: 'type',
            localKey: 'typeId'
        }
    },
    hasMany:
    {
        Entity:
        {
            localField: 'children',
            foreignKey: 'parentId'
        }
}

I didn't find a clue in the docs this wouldn't work, but unfortunately it doesn't seem to do so.
It produces an error:

Entity RuntimeError Object
  37.defaultsPrototype.error    
  37.defaultsPrototype.errorFn  
  _inject   
  _inject   
  inject    
  (anonymous function)  
  processResults    
  (anonymous function)  
  deferred.promise.then.wrappedCallback 
  (anonymous function)  
  $get.Scope.$eval  
  $get.Scope.$digest    
  $get.Scope.$apply 
  done  
  completeRequest   
  xhr.onreadystatechange    
Entity RuntimeError Object
  37.defaultsPrototype.error    
  37.defaultsPrototype.errorFn  
  _inject   
  _inject   
  inject    
  (anonymous function)  
  processResults    
  (anonymous function)  
  deferred.promise.then.wrappedCallback 
  (anonymous function)  
  $get.Scope.$eval  
  $get.Scope.$digest    
  $get.Scope.$apply 
  done  
  completeRequest   
  xhr.onreadystatechange    
Entity RuntimeError Object
  37.defaultsPrototype.error    
  37.defaultsPrototype.errorFn  
  _inject   
  _inject   
  inject    
  (anonymous function)  
  processResults    
  (anonymous function)  
  deferred.promise.then.wrappedCallback 
  (anonymous function)  
  $get.Scope.$eval  
  $get.Scope.$digest    
  $get.Scope.$apply 
  done  
  completeRequest   
  xhr.onreadystatechange    
TypeError: Cannot read property 'id' of undefined

Adapters

  • - http
  • - localStorage
  • - localforage
  • - firebase
  • - mysql
  • - mariadb
  • - mongo
  • - postgres
  • - nedb
  • - sqlite3
  • - rethinkdb
  • - redis

Sync between localKey and localField

Hi,

I'm evaluating this project and ran into an issue. For example, having this relations:

var Status = store.defineResource({
  name: 'status',
  relations: {
    belongsTo: {
      document: {
        localKey: 'document_id',
        localField: 'document'
      }
    }
  }
});

if status.document_id equals 2 and I do:

Document.find(3).then(function(doc) {
    status.document = doc;
  });

The status record gets in a invalid state regarding status.document_id (2) and status.document(#3).

Am I doing something wrong? Because it seems that it should be the library's responsibility to sync those two related values.

Anyway, great project and I'll probably going to use it.

Thanks.

Webpack: js-data-schema module not found

When using js-data with webpack, I get this warning

WARNING in .//js-data/src/datastore/index.js
Module not found: Error: Cannot resolve module 'js-data-schema' in /Users/kentcdodds/Developer/alianza/atac5/node_modules/js-data/src/datastore
@ ./
/js-data/src/datastore/index.js 310:16-41

this amounts to webpack resolving this:

try {
  Schemator = require('js-data-schema');
} catch (e) {
}

Into this:

try {
  Schemator = __webpack_require__(!(function webpackMissingModule() { var e = new Error("Cannot find module \"js-data-schema\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()));
} catch (e) {
}

which actually works out, but I would love to avoid the warning/hack. Any ideas on how to accomplish this would be awesome ๐Ÿ‘

Find ocassionally returns an undefined document

Hello,

Sometimes, especially on mobile Safari, find promise is resolved with an undefined document even though it exists - right before and right after it returns the expected document. No other operations are made meanwhile, just reading.

The used library versions are:

  • js-data: 1.4.0
  • js-data-schema: 1.0.0
  • js-data-localforage: 1.0.0
  • localforage: 1.2.2

I am using the localforage adapter with default configuration.

Code snippet:

AppState
  .find(userID)
  .then(function(state) {
    console.log("This is sometimes undefined:", state);
  })
  .catch(function(err) {
    console.log("I would expect to receive an error:", err);
  })
;

It worths mentioning that the AppState resource does not have a defined schema, only methods.

Any ideas?

Thanks,
Jรณzsi

custom instance methods using es6 classes

Hi there,

I'm using es6 in my current project and would like to have my DS models extend some existing base classes. I've been googling but I can't think of a nice way to give js-data the methods: { ... } and use those in my classes (again, not a JS expert). It might be possible to mix methods: {..} into my class using Object.assign(..) but that feels kinda wrong.

Do have any thoughts on this?

Thanks & cheers

r.js optimze does not seem to work

Hi,

data-js does not seem to optimze correctly with any version of r.js. The error is "Error: js-data must be loaded!". Have you tested with r.js?

Cheers,
Jose

Extensions in endpoints?

Noticed that there isn't a way to add extensions to endpoints, such that the same resource could hit both GET /posts.json and GET /posts/1.json. My backend uses extension parsing which is why this poses a problem for me.

Tried some workarounds but ultimately would need to set the endpoint for each call to find() or findAll() etc, which defeats the DRY mentality.

If there were a way to (a) add extensions via config or (b) modify endpoints for each type of CRUD operation, the issue could be resolved. Thoughts?

How to access response headers?

The documentation for DS#findAll show this example:

User.findAll({
  age: {
    '>': 30
  }
}).then(function (users) {
  // users[0].age; 55 // etc., etc.
});

What's the correct way to get a hold of the response headers? Would be great if docs showed this as well.

how good is that ? trying to understand filter performance

is there a faster query mecanism to select records within a range of attributes ?

        var params = {
            where: {
                age: {
                     '>': 10,
                    '<': 30 
                    }
                }
            };

results

null 'new data store created' {} null null
48 records/seconds [1000] records created >> in 20.679 seconds
18 records/second [382] records out of 1000 >> in 20.689 seconds

null 'new data store created' {} null null
8 records/seconds [5000] records created >> in 556.671 seconds
3 records/second [1902] records out of 5000 >> in 556.697 seconds

Add js-data-schema integration

If js-data-schema is loaded the default validate lifecycle hook will change from a noop to a function that validates an item against a resource's schema if the developer defines one. Example:

var User = store.defineResource({
  name: 'user',
  schema: {
    name: {
      type: 'string',
      nullable: false
    },
    id: {
      type: 'string',
      nullable: true
    }
  }
});

beforeInject not called on relationships (1.0.0-beta.1)

I have hasMany relationship on my Profile object, when Profile.find returns and automatically injects the Profile it also injects the hasMany relationship for Suggestions. Unfortunately it doesn't call beforeInject as it used to before I switched over to js-data. When I step through the js-data code I noticed something odd. definition.beforeInject is defined correctly, but right after DSUtils._ is called, options.beforeInject is set to default method rather than my override.

  var name = definition.name;
  options = DSUtils._(definition, options);


  if (options.notify) {
    options.beforeInject(options, attrs);

You can see the definition for both below:
Profile:

var definition = {
    name: 'profile',
    defaultAdapter: 'profileAdapter',
    relations: {
        hasOne: {
            user: {
                localField: 'owner',
                foreignKey: 'id'
            }
        },
        hasMany: {
            suggestion: {
                localField: 'suggestions',
                foreignKey: 'ownerId'
            }
        }
    },
    computed: {
        id: ['owner', _updateId]
    },
    methods: {
        // Instance methods
        updateLinks: _updateLinks
    }
}

Suggestion:

var definition = {
    name: 'suggestion',
    defaultAdapter: 'suggestionAdapter',
    relations: {
        belongsTo: {
            user: {
                localField: 'owner',
                localKey: 'ownerId'
            }
        }
    },
    computed: {
        ownerId: ['owner', _updateOwnerId],
        votes: ['totalVotes', ParseData.defaultValueHandler(0)],
        skips: ['skipped', ParseData.defaultValueHandler(0)],
        views: ['votes', 'skips', _updateViews],
        slug: ['text', _updateSlug],
        link: ['slug', 'id', _updateLink],
        kdr: ['votes', 'skips', _updateKdr],
        typeClass: ['type', _updateTypeClass],
        typeDisplay: ['type', _updateTypeDisplay],
        url: ['url', _updateUrl],
        imageUrl: ['type', _updateImageUrl]
    },
    methods: {
        // Instance methods
        updateLinks: _updateLinks
    },
    beforeInject: _beforeInject,
    afterInject: _afterInject
}

I'll see if I can put together a plnkr to illustrate this.

filtering error

hi, i am using a simple data structure as shown below.

var profile = {  id : i,
                userId: i,
                age: a,
                sex: sexes[n],
                color: colors[p]    

                }

the error is trying to filter some data.

console.log("Query results"+JSON.stringify(Profile.filter('profile',{
  where: {
      age: {
        '>': 10,
        '<': 20
      }
  }
})));

the error is

js-data/node_modules/js-data/src/datastore/sync_methods/filter.js:12
    throw new DSErrors.IA('"params" must be an object!');
          ^
Error: "params" must be an object!
    at DS.filter

Defining non-entity endpoints

What would be the right approach to define a non-entity end point in js-data?

Something like this:

GET /projects/:projectId/search?q=some+query&facet1=value1&face2=value2...

Do I still need to define search as a resource (e.g. var Search = store.defineResource('search'));? Or is there a better way?

resource.filter for substring

angular-data copy issue. Added here as requested.

DEFAULT_RECORDS = [{ id: 1, name: 'John Evans', number: '01928 356115' },{ id: 16, name: 'Murbinator', number: '053180 080000' }];

lookup: function (name) {
            var retrievedRecords = {};
            var _this = this;
            Record.findAll().then(function () {
                var params = {
                    where: {
                        name: {
                            'in': name
                        }
                    }
                };
                retrievedRecords = Record.filter(params);
                debugger;
                _this.lookupSuccess(retrievedRecords);
            }).catch(function (error) {
                console.log(error);
            });
        },

Where 'in': name name is "John" no record is found
Where 'in': name name is "John Evans" one record is found.
Looking at issue 238 that appears to be the way I should be filtering but is not the value I expect to be return for sub-string filtering.

angular-data : 1.5.3
angular : 1.3.8

[Docs] Multiple adapters

It's not immediately apparent if js-data supports saving to multiple adaters and syncing between them.

Use-case: Firebase with local storage for offline use.

API seems to register them into an array of adapters, so seems like it should at a glance of the source, however until I install and attempt it, there is no information in the docs to inform me of these capabilities.

[Question] How to use WebSocket to update data in DataStore

How would one proceed if data is received via a WebSocket connection and then should be injected/updated/ejected in the DataStore? The inject/eject seams to be clear sailing because the API is there. But how should an existing item in the DataStore be updated via the synchronous API?

My first intention was to retrieve the item via get(), update it with the data received from the server and then save() it with some Noop-Adapter to get the data updated in the DataStore. Or am I overcomplicating things because just by editing the item it will be updated in the DataStore? Sry for this noob question but I'm just starting out with angular-data/js-data and have setup a small project to test a few things.

Are there any plans to support some kind of plugin/addon for js-data that goes in this direction of using WebSockets to get updates "automagically"?

Add version to JSData export

I'm creating egghead.io lessons on JSData. I like to show what version I'm using. I don't believe there's any way to get the version from JSData. Suggested API:

var version = JSData.version;
console.log(version);

Outputs:

{
  "full": "v1.0.0-alpha.5-8",
  "major": 1,
  "minor": 0,
  "dot": 0,
  "alpha": "5-8",
  "beta": false
}

Or something like that. :-)

Implement ImmutableStore (and create js-data-immutable)

I am considering this lib for storing my data in a React app. One of my requirement is to use immutable data for fast comparison. But I don't know if it is possible to do so with a custom adapter since adapters are plugged to the central memory store which cannot manage immutable date itself.
Am I wrong? Should I try do hack into it or do you think the built-in "identity mapping" should be enough?

instanceof for class instances

I see that model instanceof Resource is not returning true, and the only way is to use DS.definitions as in

user instanceof DS.definitions['user']['User']

Is there another recommended way? (Otherwise, feature request ๐Ÿ˜„ )

Example apps

js-data-examples

  • - js-data + js-data-http
  • - js-data + js-data-angular
  • - js-data + js-data-react (if one gets created) + js-data-http
  • - js-data + js-data-localStorage
  • - js-data + (server) js-data-sql
  • - js-data + (server) + js-data-nedb (RequelPro)
  • - js-data + (server) + js-data-rethinkdb

A generic frontend js-data + js-data-http example app should be built that can be re-used for the many example server apps. The frontend would be the same for all of them, but the backends would be different.

A generic js-data + mongo (or something) backend should be created which could be re-used as the backend for the many example client apps. The backend would be the same for all of them, but the frontends would be different.

Plural resources in URL

I have the following end point /classifieds/:id that returns data for a given classified. I've defined the resource as:

angular.module('components.classified')
.factory('Classified', function(DS) {
  return DS.defineResource('classified');

Finding the classified with id 1 issues a GET request with singular classified word in the URL:

Classified.find(1); // -> /classified/1

I need it to be plural instead, etc. /classifieds/1. Do I need to define resource as plural to achieve this? Is this the right way:

  return DS.defineResource('classifieds');

feat: Call the inject and eject lifecycle hooks regardless of if the notify option is enabled

My use case is that I have the notify option disabled as it causes significant performance issues when it is enabled and injecting several thousand entities at once, however I still want to hook into the before and after inject / eject methods. If you have a default inject / eject hook set then you could override it on a per model basis to a blank method if you required the same behaviour.

Update bower

Installing from bower gives me 0.4.2 version. The latest stable appears to be 1.0.0-beta.1. Is it time to update bower, so it's in sync with latest stable as npm?

Issue with offset

There is a problem with offset:
Suppose that resource has no injected items, then by calling

DS.findAll('resource', {offset: 10, limit: 10})

we will get requested items injected to data store and our query cached. But if we call the same code again, the DS.filter function kicks in and will offset these 10 items in data store, thus returning empty array.

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.