Skip to content

Commit

Permalink
feat: add db.collection.get_one (#1)
Browse files Browse the repository at this point in the history
* fix: updated datestring

* feat: add database get_one
  • Loading branch information
bbtgnn authored Feb 26, 2024
1 parent de41dea commit e0803a9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
}
Expand Down
53 changes: 45 additions & 8 deletions src/lib/database.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand All @@ -19,12 +22,46 @@ export type CollectionLoader = {

// //

// type CollectionName = keyof typeof collections;
// type CollectionConfig<C extends CollectionName> = (typeof collections)[C];
// // type CollectionType<C extends CollectionName> = StaticDecode<
// // CollectionConfig<C>['content_schema']
// // > &
// // StaticDecode<CollectionConfig<C>['filename_schema']>;
type CollectionName = keyof typeof collections;
type CollectionConfig<C extends CollectionName> = (typeof collections)[C];
type CollectionType<C extends CollectionName> = StaticDecode<
CollectionConfig<C>['content_schema']
> &
StaticDecode<CollectionConfig<C>['filename_schema']>;

//

export interface CollectionService<C extends CollectionName> {
get_one: (id: string) => Promise<unknown>;
current?: () => Promise<CollectionType<C>>;
}

export function collection<C extends CollectionName>(name: C): CollectionService<C> {
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
// }
};
}

// //

Expand Down
2 changes: 1 addition & 1 deletion src/lib/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 6 additions & 0 deletions src/routes/(collections)/organizations/dyne/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
name: 'Dyne.org',
location: 'Amsterdam, Paesi Bassi'
};
import { db } from '$lib';
</script>

<p>Mo bellissima dyne</p>

<pre>{JSON.stringify(props, null, 2)}</pre>

<img src="./logo.jpeg" alt="dyne logo" />

{#await db.collection('organizations').get_one('dyne') then res}
<pre>{JSON.stringify(res, null, 2)}</pre>
{/await}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="ts">
import { db } from '$lib';
</script>

{#await db.collection('work_experiences').get_one('2022-10_oggi_dyne') then res}
<pre>{JSON.stringify(res, null, 2)}</pre>
{/await}

0 comments on commit e0803a9

Please sign in to comment.