|
| 1 | +# A few examples of using upsert_authors / upsert_document / upsert_content / upsert_concept etc. |
| 2 | + |
| 3 | +In general, for external references such as `author_id`, you can either: |
| 4 | + |
| 5 | +1. use the database id as is: `author_id` |
| 6 | +2. Use the platform id, with the name transformed such as `author_local_id` |
| 7 | +3. Put the data inline as a subobject, with the name transformed such as `author_inline` |
| 8 | + |
| 9 | +Note that embeddings are always inline. |
| 10 | + |
| 11 | +Here are complete examples: |
| 12 | + |
| 13 | +```typescript |
| 14 | +import type { |
| 15 | + LocalAccountDataInput, |
| 16 | + LocalDocumentDataInput, |
| 17 | + LocalContentDataInput, |
| 18 | + LocalConceptDataInput, |
| 19 | +} from "@repo/database/inputTypes"; |
| 20 | +import type { Json } from "@repo/database/dbTypes"; |
| 21 | +import type { DGSupabaseClient } from "@repo/database/lib/client"; |
| 22 | + |
| 23 | +const demoCode = async (client: DGSupabaseClient) => { |
| 24 | + const accounts: LocalAccountDataInput[] = [ |
| 25 | + { |
| 26 | + account_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 27 | + name: "maparent", |
| 28 | + }, |
| 29 | + ]; |
| 30 | + |
| 31 | + const docs: LocalDocumentDataInput[] = [ |
| 32 | + { |
| 33 | + source_local_id: "page1_uid", |
| 34 | + created: "2000/01/01", |
| 35 | + last_modified: "2001/01/02", |
| 36 | + author_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 37 | + }, |
| 38 | + ]; |
| 39 | + |
| 40 | + const contents: LocalContentDataInput[] = [ |
| 41 | + { |
| 42 | + author_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 43 | + author_inline: { |
| 44 | + account_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 45 | + name: "maparent", |
| 46 | + }, |
| 47 | + document_inline: { |
| 48 | + source_local_id: "page1_uid", |
| 49 | + created: "2000/01/01", |
| 50 | + last_modified: "2001/01/02", |
| 51 | + author_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 52 | + }, |
| 53 | + source_local_id: "a_roam_uid", |
| 54 | + scale: "document", |
| 55 | + created: "2000/01/01", |
| 56 | + last_modified: "2001/01/02", |
| 57 | + text: "[[CLM]] a claim", |
| 58 | + }, |
| 59 | + { |
| 60 | + author_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 61 | + document_local_id: "page1_uid", |
| 62 | + source_local_id: "a_roam_uid2", |
| 63 | + scale: "section", |
| 64 | + created: "2000/01/02", |
| 65 | + last_modified: "2001/01/03", |
| 66 | + part_of_local_id: "a_roam_uid", |
| 67 | + text: "Some subsection", |
| 68 | + }, |
| 69 | + { |
| 70 | + author_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 71 | + document_inline: docs[0], |
| 72 | + source_local_id: "a_roam_uid3", |
| 73 | + scale: "paragraph", |
| 74 | + created: "2000/01/02", |
| 75 | + last_modified: "2001/01/03", |
| 76 | + part_of_local_id: "a_roam_uid2", |
| 77 | + text: "Some paragraph", |
| 78 | + embedding_inline: { |
| 79 | + model: "openai_text_embedding_3_small_1536", |
| 80 | + vector: [0], // assume that the vector has the requisite length |
| 81 | + }, |
| 82 | + }, |
| 83 | + ]; |
| 84 | + |
| 85 | + const concepts: LocalConceptDataInput[] = [ |
| 86 | + { |
| 87 | + author_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 88 | + source_local_id: "a_roam_uid3", |
| 89 | + created: "2000/01/01", |
| 90 | + last_modified: "2001/01/02", |
| 91 | + name: "Some subsubtext", |
| 92 | + schema_represented_by_local_id: "known_claim_schema_local_id", |
| 93 | + }, |
| 94 | + ]; |
| 95 | + |
| 96 | + // Base scenario: insert references in order of dependencies |
| 97 | + // 1. upsert accounts. |
| 98 | + { |
| 99 | + const { data, error } = await client.rpc("upsert_accounts_in_space", { |
| 100 | + space_id_: 12, |
| 101 | + accounts, |
| 102 | + }); |
| 103 | + if (error) console.error(error); |
| 104 | + console.log(data); |
| 105 | + } |
| 106 | + |
| 107 | + // 2. upsert documents. |
| 108 | + { |
| 109 | + const { data, error } = await client.rpc("upsert_documents", { |
| 110 | + v_space_id: 12, |
| 111 | + data: docs as Json, |
| 112 | + }); |
| 113 | + if (error) console.error(error); |
| 114 | + console.log(data); |
| 115 | + } |
| 116 | + |
| 117 | + // 3. content |
| 118 | + { |
| 119 | + const { data, error } = await client.rpc("upsert_content", { |
| 120 | + v_space_id: 12, |
| 121 | + data: contents as Json, |
| 122 | + v_creator_id: 63, |
| 123 | + }); |
| 124 | + if (error) console.error(error); |
| 125 | + console.log(data); |
| 126 | + } |
| 127 | + |
| 128 | + // 4. upsert concept |
| 129 | + { |
| 130 | + const { data, error } = await client.rpc("upsert_concepts", { |
| 131 | + v_space_id: 12, |
| 132 | + data: concepts as Json, |
| 133 | + }); |
| 134 | + if (error) console.error(error); |
| 135 | + console.log(data); |
| 136 | + } |
| 137 | + |
| 138 | + // Variant: if all content is known to also be a document, you can upsert both as if it were a single object |
| 139 | + const page_contents: LocalContentDataInput[] = [ |
| 140 | + { |
| 141 | + author_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 142 | + source_local_id: "a_page_uid2", |
| 143 | + created: "2000/01/02", |
| 144 | + last_modified: "2001/01/03", |
| 145 | + text: "Some other page", |
| 146 | + }, |
| 147 | + ]; |
| 148 | + |
| 149 | + { |
| 150 | + const { data, error } = await client.rpc("upsert_content", { |
| 151 | + v_space_id: 12, |
| 152 | + data: page_contents as Json, |
| 153 | + v_creator_id: 63, |
| 154 | + content_as_document: true, |
| 155 | + }); |
| 156 | + if (error) console.error(error); |
| 157 | + console.log(data); |
| 158 | + } |
| 159 | + |
| 160 | + // Nesting: you can nest information. Nested types can inherit information from the type above. |
| 161 | + // Eg 1: upserting content with nested documents and authors |
| 162 | + const contentWithNestedDoc: LocalContentDataInput[] = [ |
| 163 | + { |
| 164 | + author_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 165 | + source_local_id: "a_page_uid3", |
| 166 | + created: "2000/01/02", |
| 167 | + scale: "document", |
| 168 | + last_modified: "2001/01/03", |
| 169 | + text: "Yet another page", |
| 170 | + author_inline: { |
| 171 | + account_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 172 | + name: "maparent", |
| 173 | + }, |
| 174 | + document_inline: { |
| 175 | + source_local_id: "a_page_uid3", |
| 176 | + // note that created, last_modified and author_inline will be inherited |
| 177 | + }, |
| 178 | + }, |
| 179 | + ]; |
| 180 | + { |
| 181 | + const { data, error } = await client.rpc("upsert_content", { |
| 182 | + v_space_id: 12, |
| 183 | + data: contentWithNestedDoc as Json, |
| 184 | + v_creator_id: 63, |
| 185 | + }); |
| 186 | + if (error) console.error(error); |
| 187 | + console.log(data); |
| 188 | + } |
| 189 | + |
| 190 | + // eg 2: Nesting multiple contents in a concept |
| 191 | + const conceptsWithNestedContent: LocalConceptDataInput[] = [ |
| 192 | + { |
| 193 | + author_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 194 | + source_local_id: "a_roam_uid4", |
| 195 | + created: "2000/01/01", |
| 196 | + last_modified: "2001/01/02", |
| 197 | + name: "[[CLM]] another claim", |
| 198 | + schema_represented_by_local_id: "known_claim_schema_local_id", |
| 199 | + author_inline: { |
| 200 | + account_local_id: "sR22zZ470dNPkIf9PpjQXXdTBjG2", |
| 201 | + name: "maparent", |
| 202 | + }, |
| 203 | + document_inline: { |
| 204 | + source_local_id: "a_page_uid3", |
| 205 | + // note that created, last_modified and author_inline will be inherited |
| 206 | + }, |
| 207 | + contents_inline: [ |
| 208 | + { |
| 209 | + text: "The claim page contents", |
| 210 | + variant: "full", |
| 211 | + // here, source_local_id, document_inline, author_inline, created, last_modified are inherited |
| 212 | + }, |
| 213 | + { |
| 214 | + text: "[[CLM]] another claim", |
| 215 | + variant: "direct", |
| 216 | + embedding_inline: { |
| 217 | + model: "openai_text_embedding_3_small_1536", |
| 218 | + vector: [0], // assume that the vector has the requisite length |
| 219 | + // again source_local_id etc. are inherited |
| 220 | + }, |
| 221 | + // same variables are intherited |
| 222 | + }, |
| 223 | + ], |
| 224 | + }, |
| 225 | + ]; |
| 226 | + |
| 227 | + { |
| 228 | + const { data, error } = await client.rpc("upsert_concepts", { |
| 229 | + v_space_id: 12, |
| 230 | + data: conceptsWithNestedContent as Json, |
| 231 | + }); |
| 232 | + if (error) console.error(error); |
| 233 | + console.log(data); |
| 234 | + } |
| 235 | +}; |
| 236 | +``` |
0 commit comments