feat: relationship creation, attr update

This commit is contained in:
Arman committed 2023-03-28 14:48:09 +02:00
1 parent 88cb197166
commit 481986cff8
18 files changed
+143 -160

No files matched your search

+4 -4
View File
@@ -7,7 +7,7 @@
"name": "@appwrite/console",
"dependencies": {
"@analytics/google-analytics": "^1.0.5",
"@appwrite.io/console": "0.1.0-preview-0.0",
"@appwrite.io/console": "0.1.0-preview-0.1",
"@appwrite.io/pink": "^0.0.6-rc.5",
"@popperjs/core": "^2.11.6",
"@sentry/svelte": "^7.44.2",
@@ -146,9 +146,9 @@
"integrity": "sha512-1Yw7u/COtxx06BfwlI+kVhsa/upKYzmCNrT4c8QDeCY2KMYlnijkUjtHiPU08HxyTIVB5j6d75O0YWVIHwQS8g=="
},
"node_modules/@appwrite.io/console": {
"version": "0.1.0-preview-0.0",
"resolved": "https://registry.npmjs.org/@appwrite.io/console/-/console-0.1.0-preview-0.0.tgz",
"integrity": "sha512-ROCZICle2N/76UMXRm40fIDhjPrxplqa8SCuiCSDu68P4x54lybOw/8ecpwHspq/yIxlQ5LYsyJ4U8Fw76JyIg==",
"version": "0.1.0-preview-0.1",
"resolved": "https://registry.npmjs.org/@appwrite.io/console/-/console-0.1.0-preview-0.1.tgz",
"integrity": "sha512-908URjvRoQ87zLJXW7UD0zOgy9DvShgs/YKQBgA9mdNbNmFIzls1XwtbUeNWWLBnx0EVTMjyWiaSou+QIZnXfA==",
"dependencies": {
"cross-fetch": "3.1.5",
"isomorphic-form-data": "2.0.0"
+1 -1
View File
@@ -19,7 +19,7 @@
},
"dependencies": {
"@analytics/google-analytics": "^1.0.5",
"@appwrite.io/console": "0.1.0-preview-0.0",
"@appwrite.io/console": "0.1.0-preview-0.1",
"@appwrite.io/pink": "^0.0.6-rc.5",
"@popperjs/core": "^2.11.6",
"@sentry/svelte": "^7.44.2",
@@ -54,7 +54,19 @@
<div class="u-flex u-main-space-between u-cross-center">
<div class="u-flex u-cross-center u-gap-16">
<div class="avatar is-size-small u-color-text-gray">
<span class={`icon-${option.icon}`} aria-hidden="true" />
{#if option.icon === 'relationship'}
<span
class={`icon-${
attribute?.twoWay
? 'switch-horizontal'
: 'arrow-sm-right'
}`}
aria-hidden="true" />
{:else}
<span
class={`icon-${option.icon}`}
aria-hidden="true" />
{/if}
</div>
<span class="text u-trim">{attribute.key}</span>
</div>
@@ -75,7 +87,9 @@
{`${attribute.type}${attribute.array ? '[]' : ''}`}
</TableCellText>
<TableCellText onlyDesktop title="Default Value">
{attribute.default !== null ? attribute.default : '-'}
{attribute?.default !== null && attribute?.default !== undefined
? attribute?.default
: '-'}
</TableCellText>
<TableCell showOverflow>
<DropList
@@ -23,13 +23,12 @@
data: Partial<Models.AttributeBoolean>
) {
await sdk.forProject.databases.createBooleanAttribute(
await sdk.forProject.databases.updateBooleanAttribute(
databaseId,
collectionId,
data.key,
data.required,
data.default ? data.default : undefined,
data.array
data.default ? data.default : undefined
);
}
</script>
@@ -23,13 +23,12 @@
collectionId: string,
data: Partial<Models.AttributeDatetime>
) {
await sdk.forProject.databases.createDatetimeAttribute(
await sdk.forProject.databases.updateDatetimeAttribute(
databaseId,
collectionId,
data.key,
data.required,
data.default ? data.default : undefined,
data.array
data.default ? data.default : undefined
);
}
</script>
@@ -43,7 +43,12 @@
};
</script>
<Modal icon="exclamation" state="warning" bind:show={showDelete} onSubmit={handleDelete}>
<Modal
icon="exclamation"
state="warning"
headerDivider={false}
bind:show={showDelete}
onSubmit={handleDelete}>
<svelte:fragment slot="header">Delete Attribute</svelte:fragment>
<p class="text" data-private>
Are you sure you want to delete <b>'{selectedAttribute?.key}' from {$collection?.name}</b>?
@@ -12,29 +12,19 @@
export let showEdit = false;
export let selectedAttribute: Attributes;
const databaseId = $page.params.database;
const collectionId = $page.params.collection;
let updateButtonDisabled = true;
let error: string;
let currentAttr: string;
$: attr = selectedAttribute as {
type?: string;
size?: string;
format?: string;
array?: boolean;
required?: boolean;
status?: string;
key?: string;
min?: string;
max?: string;
default: string;
elements?: string[];
};
$: option = options.find((option) => option?.type === attr?.type) as Option;
$: option = options.find((option) => option?.type === selectedAttribute?.type) as Option;
async function submit() {
try {
await option.update(databaseId, collectionId, attr);
await option.update(databaseId, collectionId, selectedAttribute);
await Promise.allSettled([invalidate(Dependencies.COLLECTION)]);
if (!$page.url.pathname.includes('attributes')) {
await goto(
@@ -43,42 +33,44 @@
}
addNotification({
type: 'success',
message: `Attribute ${attr.key} has been created`
message: `Attribute ${selectedAttribute.key} has been created`
});
showEdit = false;
trackEvent(Submit.AttributeUpdate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.AttributeUpdate);
} catch (e) {
error = e.message;
trackError(e, Submit.AttributeUpdate);
}
}
$: if (JSON.stringify(attr) !== JSON.stringify(selectedAttribute)) {
updateButtonDisabled = false;
} else updateButtonDisabled = true;
$: updateButtonDisabled = JSON.stringify(selectedAttribute) === currentAttr;
$: if (showEdit) {
currentAttr ??= JSON.stringify(selectedAttribute);
error = null;
}
</script>
<Modal size="big" bind:show={showEdit} onSubmit={submit} icon={option?.icon}>
<svelte:fragment slot="header">
{attr?.type}
{#if attr?.type === 'Relationship'}
<div class="tag eyebrow-heading-3">
<span class="text u-x-small">Beta</span>
{#if selectedAttribute}
<Modal {error} size="big" bind:show={showEdit} onSubmit={submit} icon={option?.icon}>
<svelte:fragment slot="header">
<div class="u-flex u-cross-center u-gap-8">
{option?.name}
{#if option.type === 'relationship'}
<div class="tag eyebrow-heading-3">
<span class="text u-x-small">Beta</span>
</div>
{/if}
</div>
{/if}
</svelte:fragment>
<FormList>
{#if selectedAttribute}
{#if option.name !== 'Relationship'}
</svelte:fragment>
<FormList>
{#if selectedAttribute?.type !== 'relationship'}
<div>
<InputText
id="key"
label="Attribute Key"
placeholder="Enter Key"
bind:value={attr.key}
bind:value={selectedAttribute.key}
autofocus
required />
@@ -94,13 +86,13 @@
{/if}
<svelte:component
this={option.component}
bind:data={attr}
bind:data={selectedAttribute}
editing
on:close={() => (option = null)} />
{/if}
</FormList>
<svelte:fragment slot="footer">
<Button secondary on:click={() => (showEdit = false)}>Cancel</Button>
<Button submit disabled={attr === selectedAttribute}>Update</Button>
</svelte:fragment>
</Modal>
</FormList>
<svelte:fragment slot="footer">
<Button secondary on:click={() => (showEdit = false)}>Cancel</Button>
<Button submit disabled={updateButtonDisabled}>Update</Button>
</svelte:fragment>
</Modal>
{/if}
@@ -24,13 +24,12 @@
collectionId: string,
data: Partial<Models.AttributeEmail>
) {
await sdk.forProject.databases.createEmailAttribute(
await sdk.forProject.databases.updateEmailAttribute(
databaseId,
collectionId,
data.key,
data.required,
data.default ? data.default : undefined,
data.array
data.default ? data.default : undefined
);
}
</script>
@@ -24,14 +24,13 @@
collectionId: string,
data: Partial<Models.AttributeEnum>
) {
await sdk.forProject.databases.createEnumAttribute(
await sdk.forProject.databases.updateEnumAttribute(
databaseId,
collectionId,
data.key,
data.elements,
data.required,
data.default ? data.default : undefined,
data.array
data.default ? data.default : undefined
);
}
</script>
@@ -25,15 +25,14 @@
collectionId: string,
data: Partial<Models.AttributeFloat>
) {
await sdk.forProject.databases.createFloatAttribute(
await sdk.forProject.databases.updateFloatAttribute(
databaseId,
collectionId,
data.key,
data.required,
data.min,
data.max,
typeof data.default === 'number' ? data.default : undefined,
data.array
typeof data.default === 'number' ? data.default : undefined
);
}
</script>
@@ -25,15 +25,14 @@
collectionId: string,
data: Partial<Models.AttributeInteger>
) {
await sdk.forProject.databases.createIntegerAttribute(
await sdk.forProject.databases.updateIntegerAttribute(
databaseId,
collectionId,
data.key,
data.required,
data.min,
data.max,
typeof data.default === 'number' ? data.default : undefined,
data.array
typeof data.default === 'number' ? data.default : undefined
);
}
</script>
@@ -22,13 +22,12 @@
collectionId: string,
data: Partial<Models.AttributeIp>
) {
await sdk.forProject.databases.createIpAttribute(
await sdk.forProject.databases.updateIpAttribute(
databaseId,
collectionId,
data.key,
data.required,
data.default ? data.default : undefined,
data.array
data.default ? data.default : undefined
);
}
</script>
@@ -5,33 +5,32 @@
export async function submitRelationship(
databaseId: string,
collectionId: string,
key: string,
data: Partial<Models.AttributeString>
_: string,
data: Partial<Models.AttributeRelationship>
) {
await sdk.forProject.databases.createRelationshipAttribute(
databaseId,
collectionId,
key,
data.size,
data.required,
data.default ? (data.default as string) : undefined,
data.array
data.relatedCollection,
data.relationType,
data.twoWay,
data.key,
data.twoWayKey,
data.onDelete
);
}
export async function updateRelationship(
databaseId: string,
collectionId: string,
data: Partial<Models.AttributeString>
data: Partial<Models.AttributeRelationship>
) {
await sdk.forProject.databases.updateRelationshipAttribute(
databaseId,
collectionId,
data.key,
data.size,
data.required,
data.default ? (data.default as string) : undefined,
data.array
data.twoWay,
data.onDelete
);
}
</script>
@@ -47,39 +46,24 @@
import { camelize } from '$lib/helpers/string';
// Props
export let selectedAttribute: Models.AttributeString;
type Data = Partial<Models.AttributeString> & {
relation: 'one' | 'many';
related?: string | number | boolean;
keyRelated?: string;
del?: string | number | boolean;
};
export let data: Data = {
required: false,
size: 0,
default: null,
array: false,
relation: 'one'
};
export let data: Partial<Models.AttributeRelationship>;
export let editing = false;
// Constants
const databaseId = $page.params.database;
const oneWay = [
{ value: 'one', label: 'One to one' },
{ value: 'many', label: 'One to many' }
{ value: 'onetoone', label: 'One to one' },
{ value: 'onetomany', label: 'One to many' }
];
const twoWay = [
{ value: 'one', label: 'One to one' },
{ value: 'many', label: 'One to many' },
{ value: 'manyOne', label: 'Many to one' },
{ value: 'manyMany', label: 'Many to many' }
...oneWay,
{ value: 'manytoone', label: 'Many to one' },
{ value: 'manytomany', label: 'Many to many' }
];
const deleteOptions = [
{ value: 'setnull', label: 'Null' },
{ value: 'cascade', label: 'Cascade' },
{ value: 'null', label: 'Null' },
{ value: 'restrict', label: 'Restrict' }
];
@@ -105,34 +89,30 @@
onMount(async () => {
collectionList = await getCollections();
if (editing) {
way = data.twoWay ? 'two' : 'one';
search = data.relatedCollection;
}
});
// Reactive statements
$: getCollections(search).then((res) => (collectionList = res));
$: collections = collectionList?.collections?.filter((n) => n.$id !== $collection.$id) ?? [];
$: if (selectedAttribute) {
data = {
...data,
required: selectedAttribute.required,
array: selectedAttribute.array,
size: selectedAttribute.size,
default: selectedAttribute.default
};
}
$: if (data.required || data.array) {
data.default = null;
}
$: console.log(data.related);
$: if (data.related && !data.key) {
const collection = collectionList.collections.find((n) => n.$id === data.related);
$: if (data?.relatedCollection && !data.key) {
const collection = collectionList.collections.find((n) => n.$id === data.relatedCollection);
data.key = camelize(collection.name);
}
$: if (way === 'two' && !data.keyRelated) {
data.keyRelated = camelize($collection.name);
$: if (way === 'two' && !data.twoWayKey) {
data.twoWayKey = camelize($collection.name);
}
$: if (way === 'two') {
data.twoWay = true;
} else {
// data.twoWay = false;
}
</script>
@@ -160,12 +140,13 @@
label="Related Collection"
name="collections"
bind:search
bind:value={data.related}
bind:value={data.relatedCollection}
required
placeholder="Select a collection"
disabled={editing}
options={collections?.map((n) => ({ value: n.$id, label: n.name })) ?? []} />
{#if data?.related}
{#if data?.relatedCollection}
<div>
<InputText
id="key"
@@ -190,8 +171,9 @@
id="keyRelated"
label="Attribute Key "
placeholder="Enter Key"
bind:value={data.keyRelated}
required />
bind:value={data.twoWayKey}
required
disabled={editing} />
<div class="u-flex u-gap-4 u-margin-block-start-8 u-small">
<span
@@ -207,10 +189,11 @@
<InputSelect
id="relationship"
label="Relation"
bind:value={data.relation}
bind:value={data.relationType}
required
placeholder="Select a relation"
options={way === 'one' ? oneWay : twoWay} />
options={data?.twoWay ? oneWay : twoWay}
disabled={editing} />
<div class="box">
<div class="u-flex u-align u-cross-center u-main-center u-gap-32">
@@ -224,14 +207,14 @@
</div>
</div>
<p class="u-text-center">
<b> {camelize($collection.name)}</b> has {data.relation === 'one' ? 'one' : 'many'}
<b> {camelize($collection.name)}</b> has {way === 'one' ? 'one' : 'many'}
<b>{data.key}</b>
</p>
<InputSelect
id="deleting"
label="On deleting a document"
bind:value={data.del}
bind:value={data.onDelete}
required
placeholder="Select a deletion method"
options={deleteOptions} />
@@ -23,14 +23,12 @@
collectionId: string,
data: Partial<Models.AttributeString>
) {
await sdk.forProject.databases.createStringAttribute(
await sdk.forProject.databases.updateStringAttribute(
databaseId,
collectionId,
data.key,
data.size,
data.required,
data.default ? (data.default as string) : undefined,
data.array
data.default ? (data.default as string) : undefined
);
}
</script>
@@ -23,13 +23,12 @@
collectionId: string,
data: Partial<Models.AttributeUrl>
) {
await sdk.forProject.databases.createUrlAttribute(
await sdk.forProject.databases.updateUrlAttribute(
databaseId,
collectionId,
data.key,
data.required,
data.default ? data.default : undefined,
data.array
data.default ? data.default : undefined
);
}
</script>
@@ -12,6 +12,8 @@
export let showCreate = false;
export let selectedOption: string = null;
const databaseId = $page.params.database;
const collectionId = $page.params.collection;
let key: string = null;
let data: Partial<Attributes> = {
@@ -19,8 +21,7 @@
array: false,
default: null
};
const databaseId = $page.params.database;
const collectionId = $page.params.collection;
let error: string;
async function submit() {
try {
@@ -33,16 +34,13 @@
}
addNotification({
type: 'success',
message: `Attribute ${key} has been created`
message: `Attribute ${key ?? data?.key} has been created`
});
showCreate = false;
trackEvent(Submit.AttributeCreate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.AttributeCreate);
} catch (e) {
error = e.message;
trackError(e, Submit.AttributeCreate);
}
}
@@ -51,6 +49,7 @@
}
$: if (!showCreate) {
error = null;
key = null;
selectedOption = null;
$option = null;
@@ -62,7 +61,7 @@
}
</script>
<Modal size="big" bind:show={showCreate} onSubmit={submit} icon={$option?.icon}>
<Modal {error} size="big" bind:show={showCreate} onSubmit={submit} icon={$option?.icon}>
<svelte:fragment slot="header">
{#if selectedOption === 'Relationship'}
<span class="u-flex u-gap-16 u-cross-center">
@@ -1,5 +1,4 @@
import { page } from '$app/stores';
import type { Column } from '$lib/components/viewSelector.svelte';
import type { Models } from '@appwrite.io/console';
import { derived, writable } from 'svelte/store';
@@ -11,7 +10,8 @@ export type Attributes =
| Models.AttributeInteger
| Models.AttributeIp
| Models.AttributeString
| Models.AttributeUrl;
| Models.AttributeUrl
| Models.AttributeRelationship;
type Collection = Omit<Models.Collection, 'attributes'> & {
attributes: Array<Attributes>;
@@ -21,7 +21,7 @@ export type Column = {
title: string;
show: boolean;
type?: string;
direction?: string;
twoWay?: string;
width?: number;
};
@@ -77,7 +77,7 @@
<TableScroll isSticky>
<TableHeader>
<TableCellHead width={100} eyebrow={false}>Document ID</TableCellHead>
<TableCellHead width={150} eyebrow={false}>Document ID</TableCellHead>
{#each $columns.filter((n) => n.show) as column}
{#if column.show}
<TableCellHead eyebrow={false}>{column.title}</TableCellHead>
@@ -100,7 +100,7 @@
{#each $columns as column}
{#if column.show}
{#if column.type === 'relationship'}
{#if column.direction === 'one'}
{#if !column.twoWay}
<TableCell title={column.title}>
{document[column.id]}
</TableCell>