add: lazy loading relationship support.

This commit is contained in:
Darshan
2025-07-03 13:21:58 +05:30
parent aa8a3b23d4
commit be9e781de7
4 changed files with 36 additions and 9 deletions
@@ -4,9 +4,12 @@ import { sdk } from '$lib/stores/sdk';
import { Query } from '@appwrite.io/console';
import type { PageLoad } from './$types';
import { queries, queryParamToMap } from '$lib/components/filters';
import { buildWildcardAttributesQuery } from './document-[document]/attributes/store';
export const load: PageLoad = async ({ params, depends, url, route }) => {
export const load: PageLoad = async ({ params, depends, url, route, parent }) => {
const { collection } = await parent();
depends(Dependencies.DOCUMENTS);
const page = getPage(url);
const limit = getLimit(url, route, PAGE_LIMIT);
const view = getView(url, route, View.Grid);
@@ -15,6 +18,7 @@ export const load: PageLoad = async ({ params, depends, url, route }) => {
const paramQueries = url.searchParams.get('query');
const parsedQueries = queryParamToMap(paramQueries || '[]');
queries.set(parsedQueries);
return {
@@ -28,7 +32,8 @@ export const load: PageLoad = async ({ params, depends, url, route }) => {
Query.limit(limit),
Query.offset(offset),
Query.orderDesc(''),
...parsedQueries.values()
...parsedQueries.values(),
...buildWildcardAttributesQuery(collection)
])
};
};
@@ -4,14 +4,20 @@ import type { LayoutLoad } from './$types';
import Breadcrumbs from './breadcrumbs.svelte';
import Header from './header.svelte';
import type { Attributes } from '../store';
import { buildWildcardAttributesQuery } from './attributes/store';
export const load: LayoutLoad = async ({ params, parent, depends }) => {
depends(Dependencies.DOCUMENT);
const { collection } = await parent();
depends(Dependencies.DOCUMENT);
const document = await sdk
.forProject(params.region, params.project)
.databases.getDocument(params.database, params.collection, params.document);
.databases.getDocument(
params.database,
params.collection,
params.document,
buildWildcardAttributesQuery(collection)
);
/**
* Sanitize DateTime to remove UTC Timezone section.
@@ -39,8 +39,6 @@
}
}
documentList = await getDocuments();
if (editing && $doc?.[attribute.key]) {
if ($doc[attribute.key]?.length) {
relatedList =
@@ -59,7 +57,10 @@
});
async function getDocuments(search: string = null) {
const queries = search ? [Query.startsWith('$id', search), Query.orderDesc('')] : [];
const queries = search
? [Query.select(['$id']), Query.startsWith('$id', search), Query.orderDesc('')]
: [Query.select(['$id'])];
return await sdk
.forProject(page.params.region, page.params.project)
.databases.listDocuments(databaseId, attribute.relatedCollection, queries);
@@ -103,7 +104,7 @@
showInput = false;
}
//Reactive statements
// Reactive statements
$: getDocuments(search).then((res) => (documentList = res));
$: paginatedItems = editing
@@ -1,4 +1,4 @@
import type { Models } from '@appwrite.io/console';
import { type Models, Query } from '@appwrite.io/console';
import type { Attributes } from '../../store';
export function isRelationshipToMany(attribute: Models.AttributeRelationship) {
@@ -20,3 +20,18 @@ export function isString(attribute: Attributes): attribute is Models.AttributeSt
if (!attribute) return false;
return attribute?.type === 'string';
}
/**
* Returns select queries for all main and related fields in a collection.
*/
export function buildWildcardAttributesQuery(
collection: Models.Collection | null = null
): string[] {
return [
...(collection?.attributes
?.filter((attr) => isRelationship(attr))
?.map((attr) => Query.select([`${attr.key}.*`])) ?? []),
Query.select(['*'])
];
}