data-prism
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -11,4 +11,2 @@ import { mapValues } from "lodash-es"; | ||
const sampleRes = Object.values(graph[resType])[0]; | ||
if (!sampleRes) | ||
return; | ||
Object.entries(resSchema.relationships).forEach(([relName, relSchema]) => { | ||
@@ -15,0 +13,0 @@ const { cardinality, type: foreignType, inverse } = relSchema; |
@@ -101,2 +101,7 @@ import { defaultExpressionEngine } from "@data-prism/expressions"; | ||
} | ||
if (result[propName] === undefined) { | ||
throw new Error(`A related resource was not found on resource ${query.type}.${query.id}. ${propName}: ${JSON.stringify(result[RAW].relationships[propName])}. Check that all of the refs in ${query.type}.${query.id} are valid.`); | ||
} | ||
if (result[propName] === null) | ||
return null; | ||
return go({ | ||
@@ -103,0 +108,0 @@ ...propQuery, |
export { linkInverses, emptyGraph, mergeGraphs } from "./graph.js"; | ||
export { | ||
flattenResource, | ||
normalizeResource, | ||
createGraphFromTrees, | ||
} from "./mappers.js"; | ||
export { flattenResource, normalizeResource, createGraphFromTrees, } from "./mappers.js"; | ||
export { createQueryGraph, queryGraph } from "./graph/query.js"; | ||
export { | ||
forEachQuery, | ||
mapQuery, | ||
reduceQuery, | ||
forEachSchemalessQuery, | ||
mapSchemalessQuery, | ||
reduceSchemalessQuery, | ||
} from "./query.js"; | ||
export { forEachQuery, mapQuery, reduceQuery, forEachSchemalessQuery, mapSchemalessQuery, reduceSchemalessQuery, } from "./query.js"; | ||
export { createStore } from "./store.js"; |
@@ -6,3 +6,2 @@ import { Graph, NormalResource } from "./graph.js"; | ||
[k: string]: Mapper; | ||
id?: string; | ||
}; | ||
@@ -15,3 +14,3 @@ type GraphMappers = { | ||
[k: string]: unknown; | ||
}, schema: Schema, graphMappers?: GraphMappers): NormalResource; | ||
}, schema: Schema, resourceMappers?: ResourceMappers): NormalResource; | ||
export declare function createGraphFromTrees(rootResourceType: string, rootResources: { | ||
@@ -18,0 +17,0 @@ [k: string]: unknown; |
@@ -11,5 +11,4 @@ import { mapValues } from "lodash-es"; | ||
} | ||
export function normalizeResource(resourceType, resource, schema, graphMappers = {}) { | ||
export function normalizeResource(resourceType, resource, schema, resourceMappers = {}) { | ||
const resSchema = schema.resources[resourceType]; | ||
const resourceMappers = graphMappers[resourceType] ?? {}; | ||
const attributes = mapValues(resSchema.attributes, (_, attr) => { | ||
@@ -24,7 +23,5 @@ const mapper = resourceMappers[attr]; | ||
const relationships = mapValues(resSchema.relationships, (relSchema, rel) => { | ||
const relMapper = graphMappers[relSchema.type] ?? {}; | ||
const relResSchema = schema.resources[relSchema.type]; | ||
const mapper = resourceMappers[rel]; | ||
const emptyRel = relSchema.cardinality === "many" ? [] : null; | ||
const relIdField = relMapper.id ?? relResSchema.idField ?? "id"; | ||
const relIdField = schema.resources[relSchema.type].idField ?? "id"; | ||
const relVal = typeof mapper === "function" | ||
@@ -35,4 +32,2 @@ ? mapper(resource) | ||
: resource[rel]; | ||
if (relVal === undefined) | ||
return undefined; | ||
return applyOrMap(relVal ?? emptyRel, (relRes) => typeof relRes === "object" | ||
@@ -54,3 +49,3 @@ ? { type: relSchema.type, id: relRes[relIdField] } | ||
const resourceId = resource[idField]; | ||
output[resourceType][resourceId] = normalizeResource(resourceType, resource, schema, graphMappers); | ||
output[resourceType][resourceId] = normalizeResource(resourceType, resource, schema, resourceMappers); | ||
Object.entries(resourceSchema.relationships).forEach(([relName, relSchema]) => { | ||
@@ -57,0 +52,0 @@ const mapper = resourceMappers[relName]; |
{ | ||
"name": "data-prism", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -33,3 +33,2 @@ import { mapValues } from "lodash-es"; | ||
const sampleRes = Object.values(graph[resType])[0]; | ||
if (!sampleRes) return; | ||
@@ -36,0 +35,0 @@ Object.entries(resSchema.relationships).forEach(([relName, relSchema]) => { |
@@ -149,2 +149,16 @@ import { defaultExpressionEngine } from "@data-prism/expressions"; | ||
if (result[propName] === undefined) { | ||
throw new Error( | ||
`A related resource was not found on resource ${query.type}.${ | ||
query.id | ||
}. ${propName}: ${JSON.stringify( | ||
result[RAW].relationships[propName], | ||
)}. Check that all of the refs in ${query.type}.${ | ||
query.id | ||
} are valid.`, | ||
); | ||
} | ||
if (result[propName] === null) return null; | ||
return go({ | ||
@@ -151,0 +165,0 @@ ...propQuery, |
@@ -6,4 +6,4 @@ import { mapValues } from "lodash-es"; | ||
type Mapper = string | ((res: any) => unknown); // eslint-disable-line | ||
type ResourceMappers = { [k: string]: Mapper; id?: string }; | ||
type Mapper = string | ((res: any) => unknown); // eslint-disable-line | ||
type ResourceMappers = { [k: string]: Mapper }; | ||
type GraphMappers = { [k: string]: ResourceMappers }; | ||
@@ -27,6 +27,5 @@ | ||
schema: Schema, | ||
graphMappers: GraphMappers = {}, | ||
resourceMappers: ResourceMappers = {}, | ||
): NormalResource { | ||
const resSchema = schema.resources[resourceType]; | ||
const resourceMappers = graphMappers[resourceType] ?? {}; | ||
@@ -44,7 +43,5 @@ const attributes = mapValues(resSchema.attributes, (_, attr) => { | ||
const relationships = mapValues(resSchema.relationships, (relSchema, rel) => { | ||
const relMapper = graphMappers[relSchema.type] ?? {}; | ||
const relResSchema = schema.resources[relSchema.type]; | ||
const mapper = resourceMappers[rel]; | ||
const emptyRel = relSchema.cardinality === "many" ? [] : null; | ||
const relIdField = relMapper.id ?? relResSchema.idField ?? "id"; | ||
const relIdField = schema.resources[relSchema.type].idField ?? "id"; | ||
@@ -58,4 +55,2 @@ const relVal = | ||
if (relVal === undefined) return undefined; | ||
return applyOrMap(relVal ?? emptyRel, (relRes) => | ||
@@ -87,3 +82,3 @@ typeof relRes === "object" | ||
const idField = resourceMappers.id ?? resourceSchema.idField ?? "id"; | ||
const resourceId = resource[idField]; | ||
const resourceId = resource[idField as string]; | ||
@@ -94,3 +89,3 @@ output[resourceType][resourceId] = normalizeResource( | ||
schema, | ||
graphMappers, | ||
resourceMappers, | ||
); | ||
@@ -97,0 +92,0 @@ |
@@ -67,3 +67,7 @@ import { expect, it, describe } from "vitest"; | ||
...careBearData.bears[1], | ||
relationships: {}, | ||
relationships: { | ||
home: null, | ||
bestFriend: null, | ||
powers: [], | ||
}, | ||
}); | ||
@@ -82,2 +86,4 @@ }); | ||
home: { type: "homes", id: "1" }, | ||
bestFriend: null, | ||
powers: [], | ||
}, | ||
@@ -96,2 +102,4 @@ }); | ||
relationships: { | ||
home: null, | ||
bestFriend: null, | ||
powers: [{ type: "powers", id: "careBearStare" }], | ||
@@ -110,5 +118,3 @@ }, | ||
const resource = normalizeResource("bears", base, careBearSchema, { | ||
bears: { | ||
name: "nombre", | ||
}, | ||
name: "nombre", | ||
}); | ||
@@ -129,5 +135,3 @@ | ||
const resource = normalizeResource("bears", base, careBearSchema, { | ||
bears: { | ||
home: "casa", | ||
}, | ||
home: "casa", | ||
}); | ||
@@ -144,35 +148,2 @@ | ||
it("formats with a relationship renaming and a nonstandard id", () => { | ||
const powaz = { | ||
...flatCareBearData.powers.map((p) => ({ | ||
...p, | ||
powerId: undefined, | ||
powaId: p.powerId, | ||
})), | ||
}; | ||
const base = { | ||
...flatCareBearData.bears[0], | ||
powers: undefined, | ||
powaz: [powaz[0]], | ||
}; | ||
const resource = normalizeResource("bears", base, careBearSchema, { | ||
bears: { | ||
powers: "powaz", | ||
}, | ||
powers: { | ||
id: "powaId", | ||
}, | ||
}); | ||
expect(resource).toEqual({ | ||
...careBearData.bears[1], | ||
relationships: { | ||
...careBearData.bears[1].relationships, | ||
powers: [{ type: "powers", id: "careBearStare" }], | ||
}, | ||
}); | ||
}); | ||
it("formats with a function", () => { | ||
@@ -185,5 +156,3 @@ const base = { | ||
const resource = normalizeResource("bears", base, careBearSchema, { | ||
bears: { | ||
name: (res) => `${res.name} a.k.a. ${res.nombre}`, | ||
}, | ||
name: (res) => `${res.name} a.k.a. ${res.nombre}`, | ||
}); | ||
@@ -210,17 +179,2 @@ | ||
}); | ||
it("keeps undefined relationships undefined", () => { | ||
const base = omit(flatCareBearData.bears[0], ["home", "powers"]); | ||
const resource = normalizeResource("bears", base, careBearSchema); | ||
expect(resource).toEqual({ | ||
...careBearData.bears[1], | ||
relationships: { | ||
bestFriend: null, | ||
home: undefined, | ||
powers: undefined, | ||
}, | ||
}); | ||
}); | ||
}); | ||
@@ -227,0 +181,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
241594
3049