dbCheck action

This commit is contained in:
Eric van der Vlist 2023-02-22 14:54:41 +01:00
parent f966422439
commit 0434b9c626
5 changed files with 76 additions and 11 deletions

19
package-lock.json generated
View File

@ -10,6 +10,7 @@
"@felte/solid": "^1.2.7", "@felte/solid": "^1.2.7",
"@kobalte/core": "^0.6.1", "@kobalte/core": "^0.6.1",
"@types/pouchdb": "^6.4.0", "@types/pouchdb": "^6.4.0",
"lodash": "^4.17.21",
"pouchdb": "^8.0.1", "pouchdb": "^8.0.1",
"pouchdb-server": "^4.2.0", "pouchdb-server": "^4.2.0",
"uuid": "^9.0.0" "uuid": "^9.0.0"
@ -19,8 +20,9 @@
"@solidjs/router": "^0.7.0", "@solidjs/router": "^0.7.0",
"@solidjs/testing-library": "^0.6.1", "@solidjs/testing-library": "^0.6.1",
"@testing-library/jest-dom": "^5.16.5", "@testing-library/jest-dom": "^5.16.5",
"@types/lodash": "^4.14.191",
"@types/testing-library__jest-dom": "^5.14.5", "@types/testing-library__jest-dom": "^5.14.5",
"@types/uuid": "^9.0.0", "@types/uuid": "^9.0.1",
"@vitest/coverage-c8": "^0.28.5", "@vitest/coverage-c8": "^0.28.5",
"@vitest/ui": "^0.28.5", "@vitest/ui": "^0.28.5",
"jsdom": "^21.1.0", "jsdom": "^21.1.0",
@ -2987,6 +2989,12 @@
"integrity": "sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==", "integrity": "sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==",
"dev": true "dev": true
}, },
"node_modules/@types/lodash": {
"version": "4.14.191",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.191.tgz",
"integrity": "sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==",
"dev": true
},
"node_modules/@types/ms": { "node_modules/@types/ms": {
"version": "0.7.31", "version": "0.7.31",
"resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.31.tgz", "resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.31.tgz",
@ -3186,9 +3194,9 @@
} }
}, },
"node_modules/@types/uuid": { "node_modules/@types/uuid": {
"version": "9.0.0", "version": "9.0.1",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.0.tgz", "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.1.tgz",
"integrity": "sha512-kr90f+ERiQtKWMz5rP32ltJ/BtULDI5RVO0uavn1HQUOwjx0R1h0rnDYNL0CepF1zL5bSY6FISAfd9tOdDhU5Q==", "integrity": "sha512-rFT3ak0/2trgvp4yYZo5iKFEPsET7vKydKF+VRCxlQ9bpheehyAJH89dAkaLEq/j/RZXJIqcgsmPJKUP1Z28HA==",
"dev": true "dev": true
}, },
"node_modules/@types/yargs": { "node_modules/@types/yargs": {
@ -7258,8 +7266,7 @@
"node_modules/lodash": { "node_modules/lodash": {
"version": "4.17.21", "version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
"dev": true
}, },
"node_modules/lodash.debounce": { "node_modules/lodash.debounce": {
"version": "4.0.8", "version": "4.0.8",

View File

@ -15,8 +15,9 @@
"@solidjs/router": "^0.7.0", "@solidjs/router": "^0.7.0",
"@solidjs/testing-library": "^0.6.1", "@solidjs/testing-library": "^0.6.1",
"@testing-library/jest-dom": "^5.16.5", "@testing-library/jest-dom": "^5.16.5",
"@types/lodash": "^4.14.191",
"@types/testing-library__jest-dom": "^5.14.5", "@types/testing-library__jest-dom": "^5.14.5",
"@types/uuid": "^9.0.0", "@types/uuid": "^9.0.1",
"@vitest/coverage-c8": "^0.28.5", "@vitest/coverage-c8": "^0.28.5",
"@vitest/ui": "^0.28.5", "@vitest/ui": "^0.28.5",
"jsdom": "^21.1.0", "jsdom": "^21.1.0",
@ -33,6 +34,7 @@
"@felte/solid": "^1.2.7", "@felte/solid": "^1.2.7",
"@kobalte/core": "^0.6.1", "@kobalte/core": "^0.6.1",
"@types/pouchdb": "^6.4.0", "@types/pouchdb": "^6.4.0",
"lodash": "^4.17.21",
"pouchdb": "^8.0.1", "pouchdb": "^8.0.1",
"pouchdb-server": "^4.2.0", "pouchdb-server": "^4.2.0",
"uuid": "^9.0.0" "uuid": "^9.0.0"

View File

@ -8,6 +8,8 @@ import PouchDb from 'pouchdb';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import { put } from '~/lib/db'; import { put } from '~/lib/db';
import { useNavigate } from 'solid-start'; import { useNavigate } from 'solid-start';
import { toHex } from '~/lib/to-hex';
import { cloneDeep } from 'lodash';
interface Props { interface Props {
values?: any; values?: any;
@ -39,6 +41,29 @@ const Invitation: Component<Props> = (props) => {
return id; return id;
}); });
const [dbChecking, dbCheck] = createServerAction$(async (args: any) => {
console.log({
caller: 'Invitation / dbCheck',
args,
});
let baseDbInfo;
try {
const baseDb = new PouchDb(cloneDeep(args));
baseDbInfo = await baseDb.info();
} catch (error) {
baseDbInfo = error;
}
const name = `${args.name}/userdb-${toHex(args.auth.username)}`;
let userDbInfo;
try {
const userDb = new PouchDb({ ...args, name });
userDbInfo = await userDb.info();
} catch (error) {
userDbInfo = error;
}
return { baseDbInfo, userDbInfo, args, name };
});
const submitHandler = async (values: any, context: any) => { const submitHandler = async (values: any, context: any) => {
console.log({ console.log({
caller: 'Invitation / submitHandler', caller: 'Invitation / submitHandler',
@ -65,11 +90,24 @@ const Invitation: Component<Props> = (props) => {
navigate(`/invitations/`); navigate(`/invitations/`);
}; };
const { form } = createForm({ const { form, data } = createForm({
onSubmit: submitHandler, onSubmit: submitHandler,
initialValues: props?.values, initialValues: props?.values,
}); });
const dbCheckHandler = async () => {
const result = await dbCheck({
name: data('database'),
auth: { username: data('username'), password: data('password') },
});
console.log({
caller: 'Invitation / dbCheckHandler',
props,
data: data(),
result,
});
};
console.log({ console.log({
caller: 'Invitation ', caller: 'Invitation ',
props, props,
@ -127,13 +165,13 @@ const Invitation: Component<Props> = (props) => {
<Match when={!!props.values}> <Match when={!!props.values}>
<Button.Root type='submit'>Save</Button.Root> <Button.Root type='submit'>Save</Button.Root>
<Button.Root onclick={deleteHandler}>Delete</Button.Root> <Button.Root onclick={deleteHandler}>Delete</Button.Root>
<Button.Root onclick={cancelHandler}>Cancel</Button.Root>
</Match> </Match>
<Match when={!props.values}> <Match when={!props.values}>
<Button.Root type='submit'>Create</Button.Root> <Button.Root type='submit'>Create</Button.Root>
<Button.Root onclick={cancelHandler}>Cancel</Button.Root>
</Match> </Match>
</Switch> </Switch>
<Button.Root onclick={dbCheckHandler}>DB check</Button.Root>
<Button.Root onclick={cancelHandler}>Cancel</Button.Root>
</form> </form>
); );
}; };

18
src/lib/to-hex.ts Normal file
View File

@ -0,0 +1,18 @@
// see https://9to5answer.com/javascript-unicode-string-to-hex
export const toHex = (str: string) => {
let hex;
try {
hex = unescape(encodeURIComponent(str))
.split('')
.map(function (v) {
return v.charCodeAt(0).toString(16);
})
.join('');
} catch (e) {
hex = str;
console.log('invalid text input: ' + str);
}
return hex;
};

View File

@ -27,7 +27,7 @@ export default () => {
return ( return (
<main> <main>
<h1>Invitation: </h1> <h1>Invitation</h1>
<Show when={!invitation.pending} fallback={<>Loading...</>}> <Show when={!invitation.pending} fallback={<>Loading...</>}>
<Invitation values={invitation.result} /> <Invitation values={invitation.result} />
</Show> </Show>