Commit 20fbf5d1 authored by Rich Gerdes's avatar Rich Gerdes

Add source classes fro definitions and resources.

parent 533d35c7
'use strict';
import Definition from './../schema/Definition';
class Resource {
/**
* Create a new Resource.
*/
constructor(definition, client) {
this.definition = definition;
this.client = client;
this._relationship_promises = [];
}
/**
* Intercept a property.
*/
get(target, propKey, receiver) {
if (propKey === 'save') {
// Update this resource.
return () => {
return this.definition.save(target);
}
}
else if (propKey === 'delete') {
// Delete this resource.
return () => {
return this.definition.delete(target);
}
}
else if (propKey in target.relationships) {
// Get Relations.
if (!(propKey in this._relationship_promises)) {
var definition_id = this.definition.relationshipDefinition(propKey);
var prop_definition = this.client.on(definition_id);
if (!prop_definition) {
console.log(definition_id);
return Promise.reject(new Error('Unable to load definition for property `' + propKey + '`'));
}
if (Array.isArray(target.relationships[propKey].data)) {
var uuids = target.relationships[propKey].data.map(stub => stub.id);
this._relationship_promises[propKey] = prop_definition.getMultiple(uuids);
} else {
this._relationship_promises[propKey] = prop_definition.get(data.id);
}
}
return this._relationship_promises[propKey];
}
else if (propKey in target.attributes) {
// Return property if it exists.
return new Promise((resolve, reject) => {
resolve(target.attributes[propKey]);
});
}
}
}
export default Resource;
'use strict';
import Path from './Path';
import Route from './../client/Route';
import Resource from './../resource/Resource';
class Definition {
/**
* Create a new OpenJAC client.
*/
constructor(client, schema) {
this.client = client;
this.schema = schema;
this.paths = {};
}
/**
* Set paths list.
*/
setPaths(paths) {
this.paths = paths;
}
/**
* Retrieve a resource by id.
*/
get(id) {
if (this.paths.individual !== undefined) {
return this.paths.individual.execute('GET', id).then(response => {
return this.constructResource(response.data);
});
}
else {
throw 'No individual route for definition.';
}
}
/**
* Load multiple resources by uuid.
*/
getMultiple(ids) {
var or_filter = {
uuid: ids.join(',')
};
return this.getAll(or_filter);
}
save(object) {
console.log("save");
}
new(object) {
}
/**
* Retrieve a resource by id.
*/
getAll(filters = {}) {
if (this.paths.collection !== undefined) {
return this.paths.collection.execute('GET', filters).then(response => {
var resources = [];
for (let item of response.data) {
resources.push(this.constructResource(response.data));
}
return resources;
});
}
else {
throw 'No collection route for definition.';
}
}
/**
* Fetch the set of related resources using the related url.
*/
related(parent, related_url) {
return this.client.fetch(related_url).then(results => {
console.log(related);
});
}
/**
* Determine the definition type of an relationship property.
*/
relationshipDefinition(property) {
if (property in this.schema.properties.relationships.properties) {
return this.schema.properties.relationships.properties[property].properties.data.properties.type.enum[0];
}
return undefined;
}
/**
* Given a data blob, construct a resource.
*/
constructResource(data) {
return new Proxy(data, new Resource(this, this.client));
}
}
export default Definition;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment