Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bbtgnn committed Mar 7, 2024
1 parent 2376081 commit 4e778c4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/__cms/dao/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function get_paths(): string[] {
export function create<C extends CollectionName>(
collection_name: C,
data: CollectionInput<C>
): Collection<C> {
): Collection<C> | unknown {
return pipe(
get_collection_schema(collection_name),
Effect.flatMap((schema) =>
Expand All @@ -50,6 +50,13 @@ export function create<C extends CollectionName>(
catch: () => new Error(`Invalid data: ${JSON.stringify(data, null, 2)}`)
})
),
Effect.match({
onFailure: (e) => {
console.log(e);
return data;
},
onSuccess: (a) => a
}),
Effect.runSync
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/__cms/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ export const File = () => T.String({});

type Relation<C extends CollectionName> = (typeof database_index_schema)['properties'][C];

export function BaseRelation<C extends CollectionName>(collection_name: C): Relation<C> {
function BaseRelation<C extends CollectionName>(collection_name: C): Relation<C> {
const collection_entries = database_index[collection_name];
// @ts-expect-error - Avoid type overlap
return T.Union(collection_entries.map((name: CollectionEntry<C>) => T.Literal(name)));
}

export function Relation<C extends CollectionName>(collection_name: C) {
export const Relation = <C extends CollectionName>(collection_name: C) => {
return T.Transform(BaseRelation(collection_name))
.Decode((document) => ({
collection: collection_name,
document,
get: () => get_document(collection_name, document)
}))
.Encode((field) => field.document);
}
};

export type RelationField<C extends CollectionName> = StaticDecode<ReturnType<typeof Relation<C>>>;
1 change: 1 addition & 0 deletions src/__cms/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { database } from './database';
export { get_collection, get_document, get_paths, create } from './dao/db';
export * as P from './fields';
3 changes: 1 addition & 2 deletions src/routes/(database)/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as F from '$cms/fields'; // eslint-disable-line
import { database } from '$cms';
import { database, P } from '$cms'; // eslint-disable-line

/* --- */

Expand Down
1 change: 1 addition & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { get_collection } from '$cms';
</script>

Home page

0 comments on commit 4e778c4

Please sign in to comment.