Skip to content

Commit 5560cd8

Browse files
committed
examples: add typescript example
1 parent e892d2d commit 5560cd8

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

examples/bookstore.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import hx from "helix";
2+
3+
const schema = hx.schema();
4+
5+
const Chapter = schema.defineNode({
6+
index: I64,
7+
});
8+
9+
schema.index(Chapter.index, { unique: true });
10+
11+
const SubChapter = schema.defineNode({
12+
title: hx.String,
13+
content: hx.String,
14+
15+
embedding: SubChapterEmbedding,
16+
});
17+
18+
const SubChapterEmbedding = schema.defineVector({
19+
dimensions: 1536,
20+
hnsw: hx.cosine,
21+
});
22+
23+
const Contains = schema.defineEdge({ from: Chapter, to: SubChapter });
24+
25+
const ArgChapter = hx.Struct({
26+
id: hx.I64,
27+
subchapters: hx.List(ArgSubchapter),
28+
});
29+
30+
const ArgSubChapter = hx.Struct({
31+
title: hx.String,
32+
content: hx.String,
33+
chunk: hx.Vector,
34+
});
35+
36+
const loadDocsRag = schema.query({
37+
name: "loaddocs_rag",
38+
arguments: [hx.List(ArgChapter)],
39+
returns: hx.String,
40+
}, (db, [chapters]) => {
41+
chapters.forEach((c) => {
42+
const cNode = db.addNode(Chapter({ index: c.id }));
43+
44+
c.subchapters.forEach((sc) => {
45+
const scNode = db.addNode(SubChapter({
46+
title: sc.title,
47+
content: sc.content,
48+
embedding: SubChapterEmbedding(sc.chunk),
49+
}));
50+
51+
db.addEdge(Contains({ from: cNode, to: scNode }));
52+
});
53+
});
54+
55+
return "Success";
56+
});
57+
58+
const edgeNode = schema.query({
59+
name: "edge_node",
60+
arguments: [],
61+
returns: hx.Iterator(Contains),
62+
}, (db, []) => {
63+
return db.nodes[Chapter].outgoingEdges[Contains];
64+
});
65+
66+
const edgeNodeId = schema.query({
67+
name: "edge_node_id",
68+
arguments: [hx.Id(Chapter)],
69+
returns: hx.Iterator(Contains),
70+
}, (db, [id]) => {
71+
return db.nodes[Chapter]({ id }).outgoingEdges[Contains];
72+
});

0 commit comments

Comments
 (0)