93
|
1 // Load requested view and provide access to it
|
95
|
2 import { Store, NamedNode, DataFactory } from "n3";
|
|
3 import { fetchAndParse } from "./fetchAndParse";
|
|
4 import { RDF, EX } from "./namespaces";
|
|
5 import { labelOrTail, uriValue } from "./rdf_value";
|
94
|
6 const Uri = DataFactory.namedNode;
|
93
|
7
|
94
|
8 function firstElem<E>(seq: Iterable<E>): E {
|
|
9 for (let e of seq) {
|
|
10 return e;
|
|
11 }
|
|
12 throw new Error("no elems");
|
|
13 }
|
|
14
|
97
|
15 export interface TableDesc {
|
|
16 uri: NamedNode;
|
|
17 primary: NamedNode;
|
|
18 joins: NamedNode[];
|
|
19 }
|
|
20
|
|
21
|
93
|
22 export class View {
|
|
23 graph: Store;
|
|
24 ready: Promise<null>;
|
94
|
25 viewRoot!: NamedNode;
|
95
|
26
|
93
|
27 constructor(public url: string | "") {
|
94
|
28 (window as any).v = this; //debug
|
93
|
29 this.graph = new Store();
|
|
30 this.ready = new Promise((res, rej) => {
|
|
31 if (url) {
|
|
32 fetchAndParse(url).then((s2) => {
|
|
33 this.graph = s2;
|
|
34 res(null);
|
|
35 });
|
|
36 } else {
|
|
37 res(null);
|
|
38 }
|
|
39 });
|
94
|
40 this.ready.then(() => {
|
|
41 this.viewRoot = firstElem(
|
|
42 this.graph.getSubjects(RDF("type"), EX("View"), null)
|
|
43 ) as NamedNode;
|
|
44 });
|
93
|
45 }
|
95
|
46
|
93
|
47 label() {
|
94
|
48 return labelOrTail(this.graph, Uri(this.url));
|
|
49 }
|
95
|
50
|
94
|
51 // filtered+ordered list of types to show at the top level
|
97
|
52 toplevelTables(typesPresent: NamedNode[]): TableDesc[] {
|
|
53 const ret: TableDesc[] = [];
|
94
|
54 for (let table of this.graph.getObjects(this.viewRoot, EX("table"), null)) {
|
97
|
55 const tableType = uriValue(this.graph, table, EX("primaryType"));
|
|
56 const joins: NamedNode[] = [];
|
|
57 for (let joinType of this.graph.getObjects(table, EX("joinType"), null)) {
|
|
58 joins.push(joinType as NamedNode);
|
|
59 }
|
|
60 joins.sort();
|
|
61 ret.push({ uri: table as NamedNode, primary: tableType, joins: joins });
|
94
|
62 }
|
|
63 ret.sort();
|
|
64 return ret;
|
93
|
65 }
|
|
66 }
|