Skip to content

Commit

Permalink
Refactored resource to use decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
karelklima committed Nov 16, 2021
1 parent e8b7c7b commit e3e28bc
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 419 deletions.
6 changes: 6 additions & 0 deletions library/decoder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class Decoder {
}
}

if (this.graph.size > 0 && output.length < 1) {
throw new Error(
`Unable to decode graph - no resources with type <${ldkit.Resource}> found`
);
}

return output;
}

Expand Down
1 change: 1 addition & 0 deletions library/resource/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"peerDependencies": {
"@ldkit/context": "*",
"@ldkit/decoder": "*",
"@ldkit/engine": "*",
"@ldkit/namespaces": "*",
"@ldkit/rdf": "*",
Expand Down
1 change: 0 additions & 1 deletion library/resource/src/iri.ts

This file was deleted.

109 changes: 0 additions & 109 deletions library/resource/src/proxy.ts

This file was deleted.

30 changes: 20 additions & 10 deletions library/resource/src/query-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Iri } from "./iri";
import type {
Property,
Schema,
Expand All @@ -7,8 +6,8 @@ import type {
} from "@ldkit/schema";
import { getSchemaProperties } from "@ldkit/schema";
import { $, CONSTRUCT, SELECT, INSERT, DELETE } from "@ldkit/sparql";
import { Quad, variable, namedNode, quad, toRdf } from "@ldkit/rdf";
import { rdf, xsd } from "@ldkit/namespaces";
import { Quad, variable, namedNode, quad, toRdf, Iri } from "@ldkit/rdf";
import { rdf, xsd, ldkit } from "@ldkit/namespaces";

type Entity = SchemaInterfaceIdentity &
Partial<SchemaInterfaceType> &
Expand All @@ -23,6 +22,14 @@ export class QueryBuilder {
this.schemaProperties = getSchemaProperties(this.schema);
}

private getResourceSignature() {
return quad(
variable("iri"),
namedNode(rdf.type),
namedNode(ldkit.Resource)
);
}

private getProperty(key: string) {
return this.schemaProperties[key];
}
Expand All @@ -34,7 +41,7 @@ export class QueryBuilder {
}

private entityToRdf(entity: Entity) {
const { "@id": iri, "@type": extraTypes, ...properties } = entity;
const { $id: iri, $type: extraTypes, ...properties } = entity;
const result = new Array<Quad>();
const types = [...this.schema["@type"], ...(extraTypes || [])];
types.forEach((type) => {
Expand Down Expand Up @@ -66,7 +73,7 @@ export class QueryBuilder {

Object.keys(properties).forEach((prop, index) => {
const property = properties[prop];
const isOptional = property["@meta"].includes("@optional");
const isOptional = property["@optional"];
if (!includeOptional && isOptional) {
return;
}
Expand Down Expand Up @@ -103,8 +110,11 @@ export class QueryBuilder {
}

getByIrisQuery = (iris: Iri[]) => {
const query = CONSTRUCT`${this.getShape("iri", true, false)}`
.WHERE`${this.getShape("iri", true, true)} VALUES ?iri { ${iris.map(
const query = CONSTRUCT`${this.getResourceSignature()} ${this.getShape(
"iri",
true,
false
)}`.WHERE`${this.getShape("iri", true, true)} VALUES ?iri { ${iris.map(
namedNode
)} }`.build();

Expand Down Expand Up @@ -149,7 +159,7 @@ export class QueryBuilder {

Object.keys(relations).forEach((key, index) => {
const property = this.getProperty(key);
if (property["@meta"].includes("@array")) {
if (property["@array"]) {
throw "Array properties are not supported for update";
}
const predicate = this.getProperty(key)["@id"];
Expand Down Expand Up @@ -193,7 +203,7 @@ const getConditionsFromSchema = (

Object.keys(properties).forEach((prop, index) => {
const property = properties[prop];
if (wrapOptional && property["@meta"].includes("@optional")) {
if (wrapOptional && property["@optional"]) {
conditions.push($`\nOPTIONAL {`);
}
conditions.push(
Expand All @@ -210,7 +220,7 @@ const getConditionsFromSchema = (
`${varPrefix}_${index}`
);
}
if (wrapOptional && property["@meta"].includes("@optional")) {
if (wrapOptional && property["@optional"]) {
conditions.push($`\n}\n`);
}
});
Expand Down
71 changes: 13 additions & 58 deletions library/resource/src/resource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { map, switchMap, tap, share } from "rxjs/operators";
import { BehaviorSubject } from "rxjs";

import type { Graph } from "@ldkit/rdf";
import type { Graph, Iri } from "@ldkit/rdf";
import { bindingsQuery, quadsQuery, updateQuery } from "@ldkit/engine";
import type { Context } from "@ldkit/context";
import { resolveContext } from "@ldkit/context";
Expand All @@ -11,23 +11,11 @@ import type {
SchemaInterface,
SchemaInterfaceType,
SchemaInterfaceIdentity,
//SchemaInterfaceIdentity,
} from "@ldkit/schema";
import { expandSchema } from "@ldkit/schema";
import { decode } from "@ldkit/decoder";

import type { Iri } from "./iri";
import {
deleteQuery,
findIrisQuery,
findQuery,
getObjectByIrisQuery,
insertQuery,
QueryBuilder,
} from "./query-builder";
import { createProxy } from "./proxy";
import { entityToRdf } from "./utils";

//type Identity = SchemaInterfaceIdentity | Iri;
import { QueryBuilder } from "./query-builder";

export class Resource<S extends SchemaPrototype, I = SchemaInterface<S>> {
private readonly schema: Schema;
Expand All @@ -41,13 +29,8 @@ export class Resource<S extends SchemaPrototype, I = SchemaInterface<S>> {
this.queryBuilder = new QueryBuilder(this.schema);
}

private createProxy(graph: Graph, pointer: Iri) {
return createProxy(
this.schema,
graph,
pointer,
this.context
) as unknown as I;
private decode(graph: Graph) {
return decode(graph, this.schema, this.context) as unknown as I[];
}

count() {
Expand All @@ -67,11 +50,7 @@ export class Resource<S extends SchemaPrototype, I = SchemaInterface<S>> {
console.log(sparqlConstructQuery);
return quadsQuery(sparqlConstructQuery, this.context).pipe(
map((graph) => {
const iris = Object.keys(graph);
return iris.reduce((result, iri) => {
result.push(this.createProxy(graph, iri));
return result;
}, new Array<I>());
return this.decode(graph);
})
);
}
Expand Down Expand Up @@ -99,45 +78,21 @@ export class Resource<S extends SchemaPrototype, I = SchemaInterface<S>> {

findByIris(iris: Iri[]) {
const q = this.queryBuilder.getByIrisQuery(iris);
console.log(q);
return quadsQuery(q, this.context).pipe(
map((graph) => {
return iris.reduce((result, iri) => {
// Ignore objects that are not matched
if (graph[iri]) {
result.push(this.createProxy(graph, iri));
}
return result;
}, new Array<I>());
console.warn(graph);
return this.decode(graph);
})
);
}

findAll() {
const q = findIrisQuery(this.schema);
const q2 = findQuery(this.schema);
return bindingsQuery(q, this.context).pipe(
map((bindings) => {
return bindings.reduce((acc, binding) => {
acc.push(binding.get("?iri").value);
return acc;
}, new Array<Iri>());
}),
switchMap((iris) =>
quadsQuery(q2).pipe(
map((graph) => {
return iris.map((iri) => this.createProxy(graph, iri)) as I[];
})
)
)
);
}

insert(
entity: Omit<I, "@type" | "@id"> &
entity: Omit<I, "$type" | "$id"> &
Partial<SchemaInterfaceType> &
SchemaInterfaceIdentity
) {
console.log(`Inserting ${entity["@id"]} data`);
console.log(`Inserting ${entity.$id} data`);

const q = this.queryBuilder.insertQuery(entity);

Expand All @@ -152,7 +107,7 @@ export class Resource<S extends SchemaPrototype, I = SchemaInterface<S>> {
}

update(entity: Partial<Omit<I, "@id">> & SchemaInterfaceIdentity) {
console.log(`Updating ${entity["@id"]} data`);
console.log(`Updating ${entity.$id} data`);

const q = this.queryBuilder.updateQuery(entity);

Expand All @@ -167,7 +122,7 @@ export class Resource<S extends SchemaPrototype, I = SchemaInterface<S>> {
}

delete(identity: SchemaInterfaceIdentity) {
const iri = identity["@id"];
const iri = identity.$id;
console.log(`Deleting ${iri} data`);

const q = this.queryBuilder.deleteQuery(iri);
Expand Down
4 changes: 1 addition & 3 deletions library/resource/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { SchemaInterface } from "@ldkit/schema";
import type { Schema, Property } from "@ldkit/schema";
import { namedNode, quad, toRdf, Quad } from "@ldkit/rdf";
import type { Iri } from "./iri";
import { namedNode, quad, toRdf, Quad, Iri } from "@ldkit/rdf";
import { rdf, xsd } from "@ldkit/namespaces";

const convertProperty = (value: any, property: Property) => {
Expand Down
3 changes: 3 additions & 0 deletions library/resource/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{
"path": "../context"
},
{
"path": "../decoder"
},
{
"path": "../engine"
},
Expand Down
2 changes: 0 additions & 2 deletions specs/src/data/index.ts

This file was deleted.

15 changes: 0 additions & 15 deletions specs/src/data/models.ts

This file was deleted.

Loading

0 comments on commit e3e28bc

Please sign in to comment.