opral / lix-sdk

1 stars 0 forks source link

Add memory support to sqlocal #23

Closed martin-lysk closed 2 months ago

martin-lysk commented 3 months ago

Context

Lix's provides a ArrayBuffer that represents inlangs sqlite file. The current approach creates an file handler in the browsers opfs and uses SQLocal to load the db. The apps in inlangs ecosystem have to run in various env's other than classic Browser apps that don't support opfs. The current workaround will not work.

Paraglide - node based cli Sherlock - vs-code extension Parrot - null origin iframe

To support those systems we need to add support for in memory db to Sqlocal

Proposal

We need to fix other issues in the near future anyway and won't be able to wait until a fork is merged in - to keep the iteration fast - clone Sqlocal into our mono repo.

Add memory support to SQLocal

  1. Change client Config from:
export type ClientConfig = {
    databasePath: string;
    readOnly?: boolean;
    verbose?: boolean;
}; 

to

export type ClientConfig = {
    storage: StorageType;
    readOnly?: boolean;
    verbose?: boolean;
}; 
export type StorageType = 
    | { type: 'memory', dbFile: ArrayBuffer }
    | { type: 'fs'; path: string } // needed?
    | { type: 'opfs'; path: string };
  1. change path handling (quick search)
    1. lib constructor: https://github.com/DallasHoff/sqlocal/blob/main/src/client.ts#L57
    2. processor in worker
      1. https://github.com/DallasHoff/sqlocal/blob/main/src/processor.ts#L47
      2. https://github.com/DallasHoff/sqlocal/blob/main/src/processor.ts#L66
  2. Add logic to load a db from an ArrayBuffer
  3. Add logic to save a db to an ArrayBuffer - https://github.com/DallasHoff/sqlocal/blob/4148bfa6f8631d8d0d5ae37fc6682f4fd07faa94/src/client.ts#L332
samuelstroschein commented 2 months ago

yes