Skip to content

Commit

Permalink
feat(import,csv): use StringRecordId if an id matches a viable stri…
Browse files Browse the repository at this point in the history
…ng recordId
  • Loading branch information
Odonno committed Feb 6, 2025
1 parent b0ffa31 commit b8db085
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/components/App/modals/data-import.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
useRef,
useState,
} from "react";
import { RecordId, Table } from "surrealdb";
import { RecordId, StringRecordId, Table } from "surrealdb";
import { adapter } from "~/adapter";
import type { OpenedTextFile } from "~/adapter/base";
import { Icon } from "~/components/Icon";
Expand Down Expand Up @@ -155,6 +155,20 @@ const CsvImportForm = ({
return o;
};

const getWhat = (content: any) => {
if ("id" in content) {
if (
typeof content.id === "string" &&
(content.id as string).startsWith(`${table}:`)
) {
return new StringRecordId(content.id);
}
return new RecordId(table, content.id);
}

return new Table(table);
};

const execute = async (content: string) => {
papaparse.parse(content, {
delimiter,
Expand Down Expand Up @@ -183,8 +197,7 @@ const CsvImportForm = ({
const content = header
? (row.data as any)
: createObjectWithoutHeader(row.data as unknown[]);
const what =
"id" in content ? new RecordId(table, content.id) : new Table(table);
const what = getWhat(content);

await executeQuery(/* surql */ `CREATE $what CONTENT $content`, {
what,
Expand Down

0 comments on commit b8db085

Please sign in to comment.