-
Notifications
You must be signed in to change notification settings - Fork 417
add data importer #1719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
add data importer #1719
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { | ||
| BaseDirectory, | ||
| exists, | ||
| readTextFile, | ||
| remove, | ||
| } from "@tauri-apps/plugin-fs"; | ||
| import { createMergeableStore } from "tinybase/with-schemas"; | ||
|
|
||
| import { SCHEMA, type Store } from "./main"; | ||
|
|
||
| export const maybeImportFromJson = async (store: Store) => { | ||
| const path = "hyprnote/import/main.json"; | ||
| const baseDir = BaseDirectory.Data; | ||
|
|
||
| if (!(await exists(path, { baseDir }))) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const content = await readTextFile(path, { baseDir }); | ||
| const [tables, values] = JSON.parse(content) as [unknown, unknown]; | ||
|
|
||
| const importStore = createMergeableStore() | ||
| .setTablesSchema(SCHEMA.table) | ||
| .setValuesSchema(SCHEMA.value); | ||
|
|
||
| if (tables) { | ||
| importStore.setTables(tables as never); | ||
| } | ||
| if (values) { | ||
| importStore.setValues(values as never); | ||
| } | ||
|
Comment on lines
+27
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve type safety with schema validation. Using Consider validating the imported data against the schema or using TinyBase's built-in validation mechanisms more explicitly. 🤖 Prompt for AI Agents |
||
| store.merge(importStore); | ||
| await remove(path, { baseDir }); | ||
| } catch (error) { | ||
| console.error(error); | ||
| } | ||
|
Comment on lines
+33
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clarify error handling and cleanup strategy. Current behavior:
Consider:
Example with try-finally for guaranteed cleanup: try {
const content = await readTextFile(path, { baseDir });
const [tables, values] = JSON.parse(content) as [unknown, unknown];
const importStore = createMergeableStore()
.setTablesSchema(SCHEMA.table)
.setValuesSchema(SCHEMA.value);
if (tables) {
importStore.setTables(tables as never);
}
if (values) {
importStore.setValues(values as never);
}
+
+ // Remove file before merging to prevent retry of malformed data
+ await remove(path, { baseDir });
+
store.merge(importStore);
- await remove(path, { baseDir });
+ console.log("Successfully imported data from", path);
} catch (error) {
console.error("Failed to import data:", error);
+ // Consider whether to rethrow or handle differently
}
🤖 Prompt for AI Agents |
||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add runtime validation for JSON structure.
The code assumes the JSON content is a two-element array but doesn't validate this structure. If the file contains malformed data (e.g., a single object, an array with a different length, or invalid JSON), the destructuring may fail silently or produce unexpected results.
const content = await readTextFile(path, { baseDir }); -const [tables, values] = JSON.parse(content) as [unknown, unknown]; +const parsed = JSON.parse(content); +if (!Array.isArray(parsed) || parsed.length !== 2) { + throw new Error("Invalid import file format: expected [tables, values] tuple"); +} +const [tables, values] = parsed as [unknown, unknown];📝 Committable suggestion
🤖 Prompt for AI Agents