Skip to content

Commit

Permalink
save of datasource segment
Browse files Browse the repository at this point in the history
  • Loading branch information
islxyqwe committed Apr 12, 2024
1 parent 45d3b9e commit 0e2b6e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/duckdb-wasm-computation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const transformData = (table: Table) => {
export async function getMemoryProvider(): Promise<IDataSourceProvider> {
await init();
const conn = await db.connect();
const files: { id: string; content: any }[] = [];
const datasets: { name: string; id: string }[] = [];
const metaDict = new Map<string, IMutField[]>();
const specDict = new Map<string, string>();
Expand All @@ -81,6 +82,7 @@ export async function getMemoryProvider(): Promise<IDataSourceProvider> {
const filename = `${id}.json`;
await db.registerFileText(filename, JSON.stringify(data));
await conn.insertJSONFromPath(filename, { name: id });
files.push({ id, content: data });
datasets.push({ id, name });
metaDict.set(id, meta);
specDict.set(id, JSON.stringify([exportFullRaw(fromFields(meta, 'Chart 1'))]));
Expand Down Expand Up @@ -132,6 +134,40 @@ export async function getMemoryProvider(): Promise<IDataSourceProvider> {
listeners.filter((x) => x !== cb);
};
},
async onExportFile() {
const data = {
files,
datasets,
metaDict: Array.from(metaDict.entries()),
specDict: Array.from(specDict.entries()),
};
const result = new Blob([JSON.stringify(data)], { type: 'text/plain' });
return result;
},
async onImportFile(file) {
const data = JSON.parse(await file.text()) as {
files: {
id: string;
content: any;
}[];
datasets: {
name: string;
id: string;
}[];
metaDict: [string, IMutField[]][];
specDict: [string, string][];
};
files.push(...data.files);
for (const { id, content } of data.files) {
const filename = `${id}.json`;
await db.registerFileText(filename, JSON.stringify(content));
await conn.insertJSONFromPath(filename, { name: id });
}
data.datasets.forEach((x) => datasets.push(x));
data.metaDict.forEach(([k, v]) => metaDict.set(k, v));
data.specDict.forEach(([k, v]) => specDict.set(k, v));
listeners.forEach((cb) => cb(1, ''));
},
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/graphic-walker/src/dataSourceProvider/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function createMemoryProvider(initData?: string | null): IDataSourceProvi
async onImportFile(file) {
const data = await file.text();
store.importData(JSON.parse(data));
listeners.forEach((cb) => cb(IDataSourceEventType.updateList, ''));
listeners.forEach((cb) => cb(1, ''));
},
registerCallback(cb) {
listeners.push(cb);
Expand Down

0 comments on commit 0e2b6e0

Please sign in to comment.