-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt at making on demand instance data queries
- Loading branch information
Showing
10 changed files
with
181 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# This file is regularyl overwritten for pregeneration | ||
src/lib/easydb.js | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
<script> | ||
import { easydb_api_object } from "../lib/apiaccess"; | ||
import { pregen_instance } from "../lib/easydbPregen"; | ||
import { maskObj } from "../lib/easydbHelpers"; | ||
import { setContext } from "svelte"; | ||
import { l10nStore } from "../lib/stores"; | ||
import { l10nStore, easydbDataStore, easydbInstanceStore, easydbDataPromiseStore } from "../lib/stores"; | ||
import RecursiveEasyDbDetailView from "./RecursiveEasyDBDetailView.svelte"; | ||
export let uuid = ""; | ||
export let l10n = "de-DE"; | ||
export let easydb_instance = pregen_instance; | ||
$: l10nStore.set(l10n); | ||
$: easydbInstanceStore.set(easydb_instance); | ||
setContext("l10n", l10nStore); | ||
</script> | ||
|
||
{#await easydb_api_object(uuid) } | ||
Waiting for API response... | ||
{:then data } | ||
<RecursiveEasyDbDetailView fields={maskObj(data).fields} data={data} table={maskObj(data).table_name_hint}/> | ||
{#await $easydbDataPromiseStore } | ||
Accessing the EasyDB instance... | ||
{:then} | ||
{#await easydb_api_object(uuid) } | ||
Waiting for API response... | ||
{:then data } | ||
<RecursiveEasyDbDetailView fields={maskObj(data).fields} data={data} table={maskObj(data).table_name_hint}/> | ||
{/await} | ||
{/await} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// TODO: In the reorganization functions below we could also cull data that we don't need | ||
// in order to reduce the bundle size. This is a low priority performance optimization. | ||
|
||
// We massage the API response a bit to fit our needs better. This makes it more convenient | ||
// to work with the data in the Svelte component. | ||
function reorganize_masks(maskdata) { | ||
let newdata = {}; | ||
for (let mask of maskdata.masks) { | ||
newdata[mask.name] = mask; | ||
} | ||
return newdata; | ||
} | ||
|
||
// We do the same for the schema data, where we want to access the tables directly by | ||
// their name in the Svelte component. | ||
function reorganize_schemas(schemadata) { | ||
let newdata = {}; | ||
for (let table of schemadata.tables) { | ||
newdata[table.name] = table; | ||
} | ||
return newdata; | ||
} | ||
|
||
export async function accessInstance(instance) { | ||
// Get a session token from the EasyDB instance | ||
const token_response = await fetch(`${instance}/api/session`); | ||
if(token_response.status != 200) { | ||
throw new Error("Could not get a session token from the EasyDB instance."); | ||
} | ||
const token_json = await token_response.json(); | ||
const token = token_json.token; | ||
|
||
// Authenticcate the session token | ||
const auth_response = await fetch( | ||
`${instance}/api/session/authenticate?` + | ||
new URLSearchParams({ | ||
token: token, | ||
method: 'anonymous', | ||
}), | ||
{ | ||
method: 'POST', | ||
} | ||
); | ||
if(auth_response.status != 200) { | ||
throw new Error("Could not authenticate the session token."); | ||
} | ||
|
||
// Fetch all the masks for this instance | ||
const mask_response = await fetch( | ||
`${instance}/api/v1/mask/HEAD?` + | ||
new URLSearchParams({ | ||
"token": token, | ||
"format": "json", | ||
}), | ||
); | ||
if(mask_response.status != 200) { | ||
throw new Error("Could not fetch the masks for this instance"); | ||
} | ||
const masks = await mask_response.json(); | ||
|
||
// Fetch all the l10n data for this instance | ||
const l10n_response = await fetch( | ||
`${instance}/api/v1/l10n/user/HEAD?` + | ||
new URLSearchParams({ | ||
"token": token, | ||
}), | ||
); | ||
if(l10n_response.status != 200) { | ||
throw new Error("Could not fetch the l10n data for this instance"); | ||
} | ||
const l10n = await l10n_response.json(); | ||
|
||
// Fetch the schema data for this instance | ||
const schema_response = await fetch( | ||
`${instance}/api/v1/schema/user/HEAD?` + | ||
new URLSearchParams({ | ||
"token": token, | ||
"format": "json", | ||
}), | ||
); | ||
if(schema_response.status != 200) { | ||
throw new Error("Could not fetch the schema data for this instance"); | ||
} | ||
const schema = await schema_response.json(); | ||
|
||
return { | ||
instance: instance, | ||
masks: reorganize_masks(masks), | ||
l10n: l10n, | ||
schemas: reorganize_schemas(schema), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const pregen_instance = null; | ||
export const pregen_masks = null; | ||
export const pregen_l10n = null; | ||
export const pregen_schemas = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,44 @@ | ||
import { writable } from "svelte/store"; | ||
import { derived, writable } from "svelte/store"; | ||
import { accessInstance } from "./easydbData"; | ||
import { pregen_instance, pregen_l10n, pregen_masks, pregen_schemas } from "./easydbPregen"; | ||
|
||
// A derived store that resolves a promise | ||
function derivedPromise(store) { | ||
return derived(store, ($store, set) => { | ||
$store.then(value => { | ||
set(value); | ||
}); | ||
}); | ||
} | ||
|
||
// Our pregenerated defaults wrapped in a Promise | ||
async function pregenDefaults() { | ||
return { | ||
instance: pregen_instance, | ||
masks: pregen_masks, | ||
schemas: pregen_schemas, | ||
l10n: pregen_l10n, | ||
}; | ||
} | ||
|
||
// This manages the global state of the current language | ||
export const l10nStore = writable(null); | ||
|
||
// This manages the global state of the EasyDB instance we are talking to | ||
export const easydbInstanceStore = writable(null); | ||
|
||
// A derived store that delivers a promise. | ||
export const easydbDataPromiseStore = derived( | ||
easydbInstanceStore, | ||
($instance) => { | ||
if ($instance === pregen_instance) { | ||
return pregenDefaults(); | ||
} | ||
return accessInstance($instance); | ||
} | ||
); | ||
|
||
// A derived store that awaits above promise. It would be better to not even | ||
// expose the promise store, but I did not get this to work without the explicit | ||
// await in EasyDBDetailView.svelte. | ||
export const easydbDataStore = derivedPromise(easydbDataPromiseStore); |