Skip to content

resources, relations and collections #77

@rafinskipg

Description

@rafinskipg

Discussion for improving the resources API:

Return usable entities that have some methods:

//Resource API
var buildingsCollection = driver.collection('buildings');

var flatHouse = buildingsCollection.get('myFlatHouse');

flatHouse.set('members', 5); //Setteable entities

flatHouse.save(); // Pass Callback or expect promise ? 

console.log(flatHouse); //=> Doesn't show the methods that it has, only the enumerable properties

flatHouse.fetch(); // Resets state to the one that is in the database

flatHouse.remove(); //Removes item from database

Collection:

In collections we can inherit from Array.prototype and add some alias like add -> push, taking advantage from the native functions like Array.prototype.filter, Array.protype.splice, etc.

var myCollection = new driver.collection('fishes');
//or new driver.collection('fishes', resourceSchema);

console.log(myCollection); //=> [ ]

myCollection.add({ name : 'fesh'});

console.log(myCollection); //=> [{ name : 'fesh'}]

myCollection.save(); // Sends data to the backend, updates the ID of the item
console.log(myCollection); //=> [{ name : 'fesh', id : '12312-123asdas-1231-' }]

myCollection.pop(); // Removes one element

myCollection.fetch();  //Syncs with database

myCollection.removeAt(5); //or deleteAt , removes element at certain position, based on 0 index.

myCollection.removeLast(4); //Delete last 4

myCollection.reset(); //Resets state

myCollection.on(); //Event handlers

Storing changed values
The "Resource" and "Collection" will inherit from one superobject. Resource will have a mechanism for detecting changes in its attributes, storing the things that have diverged.
Calling Resource.prototype.reset will restore that changes. Calling Collection.prototype.reset will look each one of the resources in the collection, and reset them. If any of the elements has not an ID will remove it from the collection. And if some element was removed manually it will add it again.

Save
When doing a save a resource will be able to make a PUT call with only the changed attributes, if it is a new resource it will make a POST call.
In a collection it will loop for each of the resources and it will the same behaviour from the resource.

Auto Save
Users must be able to to set a config value on the resource (if they want) to make auto-save changes to models. Each set, delete, add, etc will trigger the save command.

  • Resource/Collection config options:
    • autoSave : false by default
    • autoSaveInterval: 0 by default (ms). If a change is made it will delay the save until the time of the interval has been reached, if another change has been made before the interval has been reach, it will delay the save again autoSaveInterval ms.

Set Collection Schemas

var schema = {
    "title": "Example Schema",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        }
    },
    "required": ["firstName", "lastName"]
}

var myCollection = new driver.collection(name, schema);

var myResource = myCollection.create(); //Returns a new resource with the schema
//var myPreviouslyCreatedResource = myCollection.get(ID);

myResource.set('age', -1);
myResource.isValid(); //false

Resources schemas

var MyPersonClass = new driver.resource(options, schema);

var persona = new MyPersonClass({ firstName : 'Rafa'});

persona; // { 'firstName': 'Rafa', 'lastName': '', 'age' : 0 }

persona.save(); //cb err missing parameter lastName

Aggregation

Aggregation uses the aggregation query syntax and asks the backend for aggregation values (unsaved items will not be used)

collection.sum();
collection.average();
collection.min();
collection.max();
collection.combine();
collection.histogram();

Pagination

collection.get(ID); //Returns a single resource
collection.get(); //Gets the 10 last elements 
collection.get(query); //Gets the 10 last elements that match the query
collection.next(); //Inflates the collection with 10 more elements with the previous query or without if none used on the last get call

Note that collection.fetch() syncs the current items, it does not pick more items from the database

Events

collection.on() 
/*
- change, data
- add 
- remove
- etc
*/

To look at

  • Loki JS
  • Falkor JS

Continue with:

  • Relations API

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions