@polygraph/jsonapi-store
Advanced tools
Comparing version
@@ -15,2 +15,5 @@ "use strict"; | ||
function JsonApiStore(schema, transport) { | ||
// these lines are due to a flaw in axios that requires setting headers here | ||
transport.defaults.headers['Accept'] = 'application/vnd.api+json'; | ||
transport.defaults.headers['Content-Type'] = 'application/vnd.api+json'; | ||
function getOne(query) { | ||
@@ -22,3 +25,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
params, | ||
headers: { 'Content-Type': 'application/vnd.api+json' }, | ||
headers: { 'Content-Type': 'application/vnd.api+json', Accept: 'application/vnd.api+json' }, | ||
}); | ||
@@ -40,3 +43,3 @@ if (response.status === 404) | ||
const response = yield transport.get(`/${query.type}`, { | ||
headers: { 'Content-Type': 'application/vnd.api+json' }, | ||
headers: { 'Content-Type': 'application/vnd.api+json', Accept: 'application/vnd.api+json' }, | ||
}); | ||
@@ -79,4 +82,103 @@ const data = response.data.data; | ||
}, | ||
// TODO | ||
merge: function (rawGraph) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// it ain't pretty, but it's what you get when demanding create and | ||
// update seperately; caching could mitigate significantly, but this is a | ||
// function worth avoiding on this adapter | ||
// throw referenceRelationships(rawGraph); | ||
const resourceRefs = flattenGraph(rawGraph); | ||
const existingResourceGraphs = yield Promise.all(resourceRefs.map(ref => getOne({ id: ref.id, type: ref.type }))); | ||
const existingResources = existingResourceGraphs.filter(x => x !== null); | ||
const existingIndex = utils_1.indexOn(existingResources, ['type', 'id']); | ||
// TODO: Filter dup updates | ||
const modifications = resourceRefs.map(resource => utils_1.pathOr(existingIndex, [resource.type, resource.id], false) | ||
? this.update(resource) | ||
: this.create(resource)); | ||
return Promise.all(modifications); | ||
}); | ||
}, | ||
create: function (resource) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const rDefs = schema.resources[resource.type].relationships; | ||
const data = { | ||
type: resource.type, | ||
id: resource.id, | ||
attributes: resource.attributes, | ||
relationships: utils_1.mapObj(resource.relationships || {}, (rel, relName) => ({ | ||
data: { type: rDefs[relName].type, id: rel }, | ||
})), | ||
}; | ||
return transport.post(`/${resource.type}`, { data }); | ||
}); | ||
}, | ||
update: function (resource) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const rDefs = schema.resources[resource.type].relationships; | ||
const data = { | ||
type: resource.type, | ||
id: resource.id, | ||
attributes: resource.attributes || {}, | ||
relationships: utils_1.mapObj(resource.relationships || {}, (rel, relName) => ({ | ||
data: utils_1.applyOrMap(rel, id => ({ type: rDefs[relName].type, id })), | ||
})), | ||
}; | ||
return transport.patch(`/${resource.type}/${resource.id}`, { data }); | ||
}); | ||
}, | ||
delete: function (resource) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return transport.delete(`/${resource.type}/${resource.id}`); | ||
}); | ||
}, | ||
replaceRelationship: function (replacement) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { type, id, relationship, foreignId } = replacement; | ||
const rDef = schema.resources[replacement.type].relationships[relationship]; | ||
const data = utils_1.applyOrMap(foreignId, id => ({ type: rDef.type, id })); | ||
return transport.patch(`/${type}/${id}/relationships/${relationship}`, { data }); | ||
}); | ||
}, | ||
replaceRelationships: function (replacement) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.replaceRelationship(Object.assign(Object.assign({}, replacement), { foreignId: replacement.foreignIds })); | ||
}); | ||
}, | ||
appendRelationships: function (extraRelationships) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { type, id, relationship, foreignIds } = extraRelationships; | ||
const rDef = schema.resources[extraRelationships.type].relationships[relationship]; | ||
const data = foreignIds.map(id => ({ type: rDef.type, id })); | ||
return transport.post(`/${type}/${id}/relationships/${relationship}`, { data }); | ||
}); | ||
}, | ||
deleteRelationship: function ({ type, id, relationship }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return transport.patch(`/${type}/${id}/relationships/${relationship}`, { data: null }); | ||
}); | ||
}, | ||
deleteRelationships: function ({ type, id, relationship, foreignIds }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const rDef = schema.resources[type].relationships[relationship]; | ||
const data = foreignIds.map(id => ({ type: rDef.type, id })); | ||
// the double wrapped data is on axios... :( | ||
return transport.delete(`/${type}/${id}/relationships/${relationship}`, { data: { data } }); | ||
}); | ||
}, | ||
}; | ||
function flattenGraph(fullGraph) { | ||
const rDefs = schema.resources[fullGraph.type].relationships; | ||
let out = [fullGraph]; | ||
utils_1.forEachObj(fullGraph.relationships || {}, (relResources, relName) => { | ||
const ary = Array.isArray(relResources) ? relResources : [relResources]; | ||
ary | ||
.map(id => ({ type: rDefs[relName].type, id })) | ||
.map(flattenGraph) | ||
.forEach(d => { | ||
out = [...out, ...d]; | ||
}); | ||
}); | ||
return out; | ||
} | ||
} | ||
exports.JsonApiStore = JsonApiStore; |
@@ -0,1 +1,2 @@ | ||
import { Resource } from '@polygraph/data-graph/dist/types'; | ||
export interface Database { | ||
@@ -91,2 +92,4 @@ run: (query: string, ...args: any) => Promise<any>; | ||
get: (query: Query) => Promise<Result>; | ||
create: (resource: Resource) => Promise<any>; | ||
update: (resource: Resource) => Promise<any>; | ||
merge?: (resourceGraph: ResourceGraph) => Promise<any>; | ||
@@ -93,0 +96,0 @@ delete?: (resource: ResourceGraph) => Promise<any>; |
{ | ||
"name": "@polygraph/jsonapi-store", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "dist/index.js", | ||
@@ -16,4 +16,4 @@ "scripts": { | ||
"dependencies": { | ||
"@polygraph/data-graph": "^0.0.2", | ||
"@polygraph/schema-utils": "^0.0.5", | ||
"@polygraph/data-graph": "^0.0.3", | ||
"@polygraph/schema-utils": "^0.0.6", | ||
"@polygraph/utils": "^0.0.6" | ||
@@ -52,3 +52,3 @@ }, | ||
}, | ||
"gitHead": "fc9e12a5c974fe83ce295fa4269b48699aba6ee2" | ||
"gitHead": "8c782fc9eacd6623f6fbbdfd849a9cb541420282" | ||
} |
55371
19.9%19
11.76%1608
13.32%+ Added
+ Added
- Removed
- Removed
Updated