Skip to content
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

Adding the ability to manually restore a database from a file. #26

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions SqliteWasmHelper/BrowserCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public async Task<int> SyncDbWithCacheAsync(string filename)
return await module.InvokeAsync<int>("synchronizeDbWithCache", filename);
}

/// <summary>
/// Calls the code to restore the database from a given ArrayBuffer.
/// </summary>
/// <param name="arrayBuffer">The ArrayBuffer containing the database file.</param>
/// <param name="filename">The name of the file to process.</param>
/// <returns>0 if successful, -1 otherwise.</returns>
public async Task<int> ManualRestore(byte[] arrayBuffer, string filename)
{
var module = await moduleTask.Value;
return await module.InvokeAsync<int>("manualRestore", arrayBuffer, filename);
}

/// <summary>
/// Creates an anchor tag to download the database and injects it into the parent.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions SqliteWasmHelper/IBrowserCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public interface IBrowserCache
/// <returns>Either -1 (no cache), 0 (restored), or 1 (cached).</returns>
Task<int> SyncDbWithCacheAsync(string filename);

/// <summary>
/// Calls the code to restore the database from a given ArrayBuffer.
/// </summary>
/// <param name="arrayBuffer">The ArrayBuffer containing the database file.</param>
/// <param name="filename">The name of the file to process.</param>
/// <returns>0 if successful, -1 otherwise.</returns>
Task<int> ManualRestore(byte[] arrayBuffer, string filename);

/// <summary>
/// Creates an anchor tag to download the database and injects it into the parent.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions SqliteWasmHelper/ISqliteWasmDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,12 @@ public interface ISqliteWasmDbContextFactory<TContext>
/// </summary>
/// <returns>The new context.</returns>
Task<TContext> CreateDbContextAsync();

/// <summary>
/// Calls the code to restore the database from a given ArrayBuffer.
/// </summary>
/// <param name="arrayBuffer">The ArrayBuffer containing the database file.</param>
/// <returns>0 if successful, -1 otherwise.</returns>
Task<int> ManualRestore(byte[] arrayBuffer);
}
}
17 changes: 17 additions & 0 deletions SqliteWasmHelper/SqliteWasmDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ public async Task<TContext> CreateDbContextAsync()
return ctx;
}

/// <summary>
/// Calls the code to restore the database from a given ArrayBuffer.
/// </summary>
/// <param name="arrayBuffer">The ArrayBuffer containing the database file.</param>
/// <returns>0 if successful, -1 otherwise.</returns>
public async Task<int> ManualRestore(byte[] arrayBuffer)
{
var filename = $"{GetFilename()}_bak";
int restoreStatus = await cache.ManualRestore(arrayBuffer, filename);
if (restoreStatus == 0)
{
DoSwap(filename, FileNames[typeof(TContext)]);
}

return restoreStatus;
}

private void DoSwap(string source, string target) =>
swap.DoSwap(source, target);

Expand Down
37 changes: 37 additions & 0 deletions SqliteWasmHelper/wwwroot/browserCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,40 @@ export async function generateDownloadLink(parent, file) {

return false;
}

export async function manualRestore(arrayBuffer, file) {
window.sqlitedb = window.sqlitedb || {
init: false,
cache: await caches.open('SqliteWasmHelper')
};

const db = window.sqlitedb;

const backupPath = `/${file}`;
const cachePath = `/data/cache/${file.substring(0, file.indexOf('_bak'))}`;

if (arrayBuffer) {
console.log(`Restoring ${arrayBuffer.byteLength} bytes.`);
window.Module.FS.writeFile(backupPath, new Uint8Array(arrayBuffer));

const blob = new Blob([arrayBuffer], {
type: 'application/octet-stream',
ok: true,
status: 200
});

const headers = new Headers({
'content-length': blob.size
});

const response = new Response(blob, {
headers
});

await db.cache.put(cachePath, response);

return 0;
}

return -1;
}
5 changes: 5 additions & 0 deletions SqliteWasmTests/TestHelpers/MockBrowserCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@ public Task<int> SyncDbWithCacheAsync(string filename)
}
return Task.FromResult(SyncDbResult);
}

public Task<int> ManualRestore(byte[] arrayBuffer, string filename)
{
throw new System.NotImplementedException();
}
}
}
Loading