From e0803a982fa305993eeaaf5c997e6e99ba34a22c Mon Sep 17 00:00:00 2001 From: Giovanni Abbatepaolo <30571828+bbtgnn@users.noreply.github.com> Date: Mon, 26 Feb 2024 01:36:55 +0100 Subject: [PATCH] feat: add db.collection.get_one (#1) * fix: updated datestring * feat: add database get_one --- src/lib/config.ts | 6 +-- src/lib/database.ts | 53 ++++++++++++++++--- src/lib/fields.ts | 2 +- .../organizations/dyne/+page.svelte | 6 +++ .../2022-10_oggi_dyne/+page.svelte | 7 +++ 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/lib/config.ts b/src/lib/config.ts index 94ea515..f0af4b7 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -25,9 +25,9 @@ export const collections = { work_experiences: { loader: () => import.meta.glob('./work_experiences/*/+page.svelte'), filename_schema: T.Object({ - date_start: F.DateString('yyyy_MM'), - date_end: T.Union([F.DateString('yyyy_MM'), T.Literal('oggi')]), - organizations: F.Relation('organizations') + date_start: F.DateString('yyyy-MM'), + date_end: T.Union([F.DateString('yyyy-MM'), T.Literal('oggi')]), + organization: F.Relation('organizations') }), content_schema: T.Object({}) } diff --git a/src/lib/database.ts b/src/lib/database.ts index a41583b..aba702f 100644 --- a/src/lib/database.ts +++ b/src/lib/database.ts @@ -1,9 +1,12 @@ -import type { TObject } from '@sinclair/typebox'; -// import { collections } from './config'; +import type { StaticDecode, TObject } from '@sinclair/typebox'; +import { collections } from './config'; // // import type { StaticDecode } from '@sinclair/typebox'; // import { Effect as _, ReadonlyRecord as R, pipe, ReadonlyArray as A } from 'effect'; import get_entries_loaders from '$collections'; +import { pipe, ReadonlyRecord as R, ReadonlyArray as A, Option as O } from 'effect'; +// import { page } from '$app/stores'; +// import { get } from 'svelte/store'; export { get_entries_loaders }; @@ -19,12 +22,46 @@ export type CollectionLoader = { // // -// type CollectionName = keyof typeof collections; -// type CollectionConfig = (typeof collections)[C]; -// // type CollectionType = StaticDecode< -// // CollectionConfig['content_schema'] -// // > & -// // StaticDecode['filename_schema']>; +type CollectionName = keyof typeof collections; +type CollectionConfig = (typeof collections)[C]; +type CollectionType = StaticDecode< + CollectionConfig['content_schema'] +> & + StaticDecode['filename_schema']>; + +// + +export interface CollectionService { + get_one: (id: string) => Promise; + current?: () => Promise>; +} + +export function collection(name: C): CollectionService { + return { + get_one: (slug: string) => + pipe( + get_entries_loaders(), + R.toEntries, + A.filter(([entry_name]) => entry_name.includes(`${name}/${slug}`)), + A.head, + O.map(async ([, /* entry_name */ entry_loader]) => { + const o = await entry_loader(); + console.log(o); + return o; + }), + O.getOrThrow + ) + // const e = + // // const o = await import(`../routes/(collections)/${name}/${slug}/+page.svelte`); + // console.log(o); + // return o; + + // async current() { + // const pageStore = get(page) + // pageStore.route + // } + }; +} // // diff --git a/src/lib/fields.ts b/src/lib/fields.ts index 888cf1f..d34c45a 100644 --- a/src/lib/fields.ts +++ b/src/lib/fields.ts @@ -4,7 +4,7 @@ import type { CollectionName } from './config'; /* Date */ -type DateFormats = 'yyyy_MM' | 'yyyy_MM_dd'; +type DateFormats = 'yyyy-MM' | 'yyyy-MM-dd'; export const DateString = (dateFormat: DateFormats) => T.Transform(T.String()).Decode(stringToDate).Encode(dateToString(dateFormat)); diff --git a/src/routes/(collections)/organizations/dyne/+page.svelte b/src/routes/(collections)/organizations/dyne/+page.svelte index 3da2d25..c07ffb0 100644 --- a/src/routes/(collections)/organizations/dyne/+page.svelte +++ b/src/routes/(collections)/organizations/dyne/+page.svelte @@ -9,6 +9,8 @@ name: 'Dyne.org', location: 'Amsterdam, Paesi Bassi' }; + + import { db } from '$lib';

Mo bellissima dyne

@@ -16,3 +18,7 @@
{JSON.stringify(props, null, 2)}
dyne logo + +{#await db.collection('organizations').get_one('dyne') then res} +
{JSON.stringify(res, null, 2)}
+{/await} diff --git a/src/routes/(collections)/work_experiences/2022-10_oggi_dyne/+page.svelte b/src/routes/(collections)/work_experiences/2022-10_oggi_dyne/+page.svelte index e69de29..f17d4e9 100644 --- a/src/routes/(collections)/work_experiences/2022-10_oggi_dyne/+page.svelte +++ b/src/routes/(collections)/work_experiences/2022-10_oggi_dyne/+page.svelte @@ -0,0 +1,7 @@ + + +{#await db.collection('work_experiences').get_one('2022-10_oggi_dyne') then res} +
{JSON.stringify(res, null, 2)}
+{/await}