diff --git a/client/src/api/index.ts b/client/src/api/index.ts index db04599..0cb75b7 100644 --- a/client/src/api/index.ts +++ b/client/src/api/index.ts @@ -1,12 +1,19 @@ import { Grid } from 'grid'; -import { Idatabase, SupaDB } from 'database'; - +import { Idatabase, SupaDB, setDatabase } from 'database'; +import axios from 'axios'; +const debugMigration = false; class API { public idb: Idatabase; public supadb: SupaDB; public _mode: string; constructor(mode: string = 'unknown') { - this.idb = new Idatabase(); + let prepromise: Promise = Promise.resolve(); + if (debugMigration) { + prepromise = prepromise + .then(() => axios.get('/test-db.json')) + .then(({ data }) => setDatabase(data, 4)) + } + this.idb = new Idatabase(prepromise); this.supadb = new SupaDB('https://tnvxmrqhkdlynhtdzmpw.supabase.co', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InRudnhtcnFoa2RseW5odGR6bXB3Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODIyNTM0MTEsImV4cCI6MTk5NzgyOTQxMX0.4PczPPAxbkwBvig7NTHNbR8JumuwPPqfyS_kGnkxP5I'); this._mode = mode; } diff --git a/database/src/idb.ts b/database/src/idb.ts index a988b46..aae24ed 100644 --- a/database/src/idb.ts +++ b/database/src/idb.ts @@ -131,9 +131,12 @@ async function create() { export class Idatabase extends Database { private loadingPromise: ReturnType; - constructor() { + constructor(prepromise?: Promise) { super(); - this.loadingPromise = create() + if (!prepromise) { + prepromise = Promise.resolve(); + } + this.loadingPromise = prepromise.then(() => create()) .then((db) => { return db.get('styles', defaultStyles.id) .then((style) => style ? Promise.resolve('') @@ -266,10 +269,10 @@ export function setDatabase(json: any, version: number) { const objstore = db.createObjectStore(key, { keyPath: 'id', }); - if(key === 'words'){ + if (key === 'words') { // @ts-ignore objstore.createIndex('by-word', 'id'); - }else{ + } else { // @ts-ignore objstore.createIndex('by-id', 'id'); } @@ -277,7 +280,7 @@ export function setDatabase(json: any, version: number) { // @ts-ignore const store = transaction.objectStore(key); promise = promise - // @ts-ignore + // @ts-ignore .then(() => Promise.all(values.map((value: any) => store.put(value)))) }); }