|
1 | 1 | import * as dotenv from "dotenv"; |
2 | 2 | import neo4j, { Driver } from "neo4j"; |
3 | | -import type { Lantern, Beacon, Ash } from "types/beaconTypes.ts"; |
4 | | -import { creds as c } from "utils/creds/neo4j.ts"; |
| 3 | +import type { Lantern, Ash, Ember, DBError } from "types/beaconTypes.ts"; |
| 4 | +import type { Attempt } from "types/serverTypes.ts"; |
| 5 | +import { creds as c } from "utils/auth/neo4jCred.ts"; |
5 | 6 |
|
6 | 7 | dotenv.load({ export: true }); |
7 | 8 |
|
8 | | -export async function writeBeacons(entries:Lantern[]) { |
9 | | - console.groupCollapsed(`====== FUNCTION writeBeacons(${entries.length} entries) ======`); |
10 | | - let i = 0; |
11 | | - for (const entry of entries) { |
12 | | - console.log(`Passing Beacon ${i}: "${entry.input}"`); |
13 | | - await writeBeacon(entry); |
14 | | - i++; |
15 | | - }; |
16 | | - console.groupEnd(); |
17 | | -} |
18 | | - |
19 | | -export async function writeBeacon(entry:Lantern):Promise<Beacon | Ash> { |
| 9 | +export async function writeBeacon(entry:Lantern):Promise<Attempt> { |
20 | 10 | console.groupCollapsed(`====== FUNCTION writeBeacon(${entry.input}) ======`); |
21 | | - let driver: Driver; |
22 | | - let newEntry: Beacon | Ash; |
23 | | - |
| 11 | + |
| 12 | + let driver: Driver | undefined; |
| 13 | + let attempt: Attempt; |
| 14 | + let attemptError: DBError | undefined; |
| 15 | + |
24 | 16 | try { |
25 | | - console.groupCollapsed(`--- Neo4j ---`); |
26 | 17 | console.info("Initialising Driver..."); |
27 | 18 | driver = neo4j.driver(c.URI, neo4j.auth.basic(c.USER, c.PASSWORD)); |
28 | | - console.info("Connecting to Aura..."); |
29 | 19 | await driver.getServerInfo(); |
30 | | - |
31 | | - console.info("Building Query Params..."); |
32 | | - const verbProps = { |
33 | | - dbId: entry.id ?? "no id", |
34 | | - presetId: entry.presetId ?? "no preset id", |
35 | | - input: entry.input, |
36 | | - atoms: { |
37 | | - client: { |
38 | | - subject: entry.atoms.client.subject, |
39 | | - verb: entry.atoms.client.verb, |
40 | | - object: entry.atoms.client.object, |
41 | | - adverbial: entry.atoms.client.adverbial |
42 | | - }, |
43 | | - server: { |
44 | | - subject: entry.atoms.server.subject, |
45 | | - verb: entry.atoms.server.verb, |
46 | | - object: entry.atoms.server.object, |
47 | | - adverbial: entry.atoms.server.adverbial |
48 | | - } |
49 | | - }, |
50 | | - isPublic: entry.isPublic ?? false, |
51 | | - isArchived: entry.isArchived ?? false, |
52 | | - isSnoozed: entry.isSnoozed ?? false, |
53 | | - category: entry.category ?? "", |
54 | | - actions: entry.actions ?? [] |
55 | | - }; |
| 20 | + |
| 21 | + const userId = entry.authId ? entry.authId : "test-auth-id"; |
56 | 22 |
|
57 | 23 | console.info("Executing Cypher Query..."); |
58 | 24 | const result = await driver.executeQuery( |
59 | | - `MERGE (s:User {name:$sName})-[v:VERB {input: $vInput}]->(o:Concept {name:$oName}) |
60 | | - SET v = $vProps |
| 25 | + `MERGE (s:User {name:$sProps.name})-[v:VERB]->(o:Concept {name:$oProps.name}) |
| 26 | + SET v.dbId = randomUUID(), |
| 27 | + v += $vProps |
61 | 28 | RETURN s, v, o`, |
62 | 29 | { |
63 | | - sName: entry.atoms.server.subject.head, |
64 | | - vInput: verbProps.input, |
| 30 | + sProps: { name: entry.shards.subject.head }, |
| 31 | + oProps: { name: entry.shards.object.head }, |
65 | 32 | vProps: { |
66 | | - id: verbProps.id, |
67 | | - presetId: verbProps.presetId, |
68 | | - input: verbProps.input, |
69 | | - name: verbProps.atoms.server.verb.head, |
70 | | - atomsClientSubject: verbProps.atoms.client.subject, |
71 | | - atomsClientVerb: verbProps.atoms.client.verb, |
72 | | - atomsClientObject: verbProps.atoms.client.object, |
73 | | - atomsClientAdverbial: verbProps.atoms.client.adverbial, |
74 | | - atomsServerSubjectHead: verbProps.atoms.server.subject.head, |
75 | | - atomsServerSubjectArticle: verbProps.atoms.server.subject.article, |
76 | | - atomsServerSubjectQuantity: verbProps.atoms.server.subject.quantity, |
77 | | - atomsServerSubjectDescriptors: verbProps.atoms.server.subject.descriptors, |
78 | | - atomsServerSubjectPhrase: verbProps.atoms.server.subject.phrase, |
79 | | - atomsServerVerbHead: verbProps.atoms.server.verb.head, |
80 | | - atomsServerVerbPhrase: verbProps.atoms.server.verb.phrase, |
81 | | - atomsServerVerbDescriptors: verbProps.atoms.server.verb.descriptors, |
82 | | - atomsServerObjectHead: verbProps.atoms.server.object.head, |
83 | | - atomsServerObjectArticle: verbProps.atoms.server.object.article, |
84 | | - atomsServerObjectQuantity: verbProps.atoms.server.object.quantity, |
85 | | - atomsServerObjectDescriptors: verbProps.atoms.server.object.descriptors, |
86 | | - atomsServerObjectPhrase: verbProps.atoms.server.object.phrase, |
87 | | - isPublic: verbProps.isPublic, |
88 | | - isArchived: verbProps.isArchived, |
89 | | - isSnoozed: verbProps.isSnoozed, |
90 | | - category: verbProps.category, |
91 | | - actions: verbProps.actions |
92 | | - }, |
93 | | - oName: entry.atoms.server.object.head |
94 | | - }, { database: "neo4j" } |
| 33 | + authId: userId, |
| 34 | + presetId: entry.presetId ?? "", |
| 35 | + input: entry.input, |
| 36 | + name: entry.shards.verb.head, |
| 37 | + atoms_subject: entry.atoms.subject, |
| 38 | + atoms_verb: entry.atoms.verb, |
| 39 | + atoms_object: entry.atoms.object, |
| 40 | + atoms_adverbial: entry.atoms.adverbial, |
| 41 | + shards_subject_head: entry.shards.subject.head, |
| 42 | + shards_subject_phrase: entry.shards.subject.phrase, |
| 43 | + shards_subject_article: entry.shards.subject.article, |
| 44 | + shards_subject_quantity: entry.shards.subject.quantity, |
| 45 | + shards_subject_descriptors: entry.shards.subject.descriptors, |
| 46 | + shards_verb_head: entry.shards.verb.head, |
| 47 | + shards_verb_phrase: entry.shards.verb.phrase, |
| 48 | + shards_verb_descriptors: entry.shards.verb.descriptors, |
| 49 | + shards_object_head: entry.shards.object.head, |
| 50 | + shards_object_phrase: entry.shards.object.phrase, |
| 51 | + shards_object_article: entry.shards.object.article, |
| 52 | + shards_object_quantity: entry.shards.object.quantity, |
| 53 | + shards_object_descriptors: entry.shards.object.descriptors, |
| 54 | + shards_adverbial: entry.shards.adverbial, |
| 55 | + isPublic: entry.isPublic ?? false, |
| 56 | + isArchived: entry.isArchived ?? false, |
| 57 | + isSnoozed: entry.isSnoozed ?? false, |
| 58 | + category: entry.category ?? "", |
| 59 | + actions: [], |
| 60 | + errorLogs: [] |
| 61 | + } |
| 62 | + }, |
| 63 | + { database: "neo4j" } |
95 | 64 | ); |
96 | 65 |
|
97 | | - console.info(`Assembling Return Object...`); |
98 | | - newEntry = { |
99 | | - // ...entry, |
100 | | - // id: result.records[0].get("id") |
101 | | - subject: result.records[0].get("s"), |
102 | | - verb: result.records[0].get("v"), |
103 | | - object: result.records[0].get("o") |
| 66 | + const record = result.records[0].get("v").properties; |
| 67 | + |
| 68 | + console.groupCollapsed(`Ember`); |
| 69 | + const ember: Ember = { |
| 70 | + authId: record.authId, |
| 71 | + dbId: record.dbId, |
| 72 | + presetId: record.presetId, |
| 73 | + input: record.input, |
| 74 | + atoms: { |
| 75 | + subject: record.atoms_subject, |
| 76 | + verb: record.atoms_verb, |
| 77 | + object: record.atoms_object, |
| 78 | + adverbial: record.atoms_adverbial |
| 79 | + }, |
| 80 | + category: record.category, |
| 81 | + isPublic: record.isPublic, |
| 82 | + isArchived: record.isArchived, |
| 83 | + isSnoozed: record.isSnoozed, |
| 84 | + actions: record.actions, |
| 85 | + errorLogs: record.errorLogs |
104 | 86 | }; |
| 87 | + console.info(`Ember assembled`); |
| 88 | + console.log(ember); |
| 89 | + console.groupEnd(); |
| 90 | + |
| 91 | + attempt = { record: ember }; |
105 | 92 | } catch (err) { |
106 | | - console.warn(`Connection error`); |
107 | | - // console.warn(err); |
| 93 | + console.groupCollapsed(`--- Error ---`); |
| 94 | + |
| 95 | + console.groupCollapsed(`Details`); |
| 96 | + console.warn(err); |
108 | 97 | const cause = err instanceof Error ? err.cause : "Cause Unknown"; |
| 98 | + console.info(`Error Cause:`); |
109 | 99 | console.warn(cause); |
| 100 | + console.groupEnd(); |
110 | 101 |
|
111 | | - newEntry = { |
112 | | - ...entry, |
113 | | - errorLogs: [{ isError: true, errorCause: cause }] |
114 | | - }; |
115 | | - |
116 | | - return newEntry as Ash; |
| 102 | + console.groupCollapsed(`Ash`); |
| 103 | + attemptError = { isError: true, errorCause: cause }; |
| 104 | + attempt = { |
| 105 | + record: { ...entry, errorLogs: [attemptError] }, |
| 106 | + error: attemptError |
| 107 | + } |
| 108 | + console.log(attempt); |
| 109 | + console.groupEnd(); |
| 110 | + |
| 111 | + console.groupEnd(); |
117 | 112 | } finally { |
118 | 113 | console.info("Closing Driver..."); |
119 | 114 | driver?.close(); |
120 | | - console.groupEnd(); |
121 | 115 | } |
122 | 116 |
|
123 | | - console.groupCollapsed(`--- Return ---`); |
124 | | - console.log(newEntry.shards.subject); |
125 | | - console.log(newEntry.shards.verb); |
126 | | - console.log(newEntry.shards.object); |
127 | 117 | console.groupEnd(); |
128 | | - |
129 | | - return newEntry; |
| 118 | + console.info(`==================================================`); |
| 119 | + return attempt; |
| 120 | +} |
| 121 | + |
| 122 | +export async function writeBeacons(entries:Lantern[]) { |
| 123 | + console.groupCollapsed(`====== FUNCTION writeBeacons(${entries.length} entries) ======`); |
| 124 | + let i = 0; |
| 125 | + for (const entry of entries) { |
| 126 | + console.log(`Passing Beacon ${i}: "${entry.input}"`); |
| 127 | + await writeBeacon(entry); |
| 128 | + i++; |
| 129 | + }; |
| 130 | + console.groupEnd(); |
130 | 131 | } |
0 commit comments