Skip to content

Commit

Permalink
feat: improved error handling when parsing collection
Browse files Browse the repository at this point in the history
  • Loading branch information
bbtgnn committed Mar 1, 2024
1 parent 307457b commit 76e0aa7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
15 changes: 13 additions & 2 deletions src/_modules/dao/base_document_handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,21 @@ function convert_document_module_record_to_base_documents(
);
}

export function get_base_documents(): Effect.Effect<BaseDocument[], Error, never> {
type GetBaseDocumentOptions = {
path_includes: string | undefined;
};

export function get_base_documents(
options: Partial<GetBaseDocumentOptions> = {}
): Effect.Effect<BaseDocument[], Error, never> {
return pipe(
get_document_module_record(),
Effect.map(convert_document_module_record_to_base_documents)
Effect.map(convert_document_module_record_to_base_documents),
Effect.map((documents) => {
const { path_includes } = options;
if (path_includes) return documents.filter((doc) => doc._path.includes(path_includes));
else return documents;
})
);
}

Expand Down
40 changes: 29 additions & 11 deletions src/_modules/dao/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipe, Effect, ReadonlyArray as A } from 'effect';
import { pipe, Effect, ReadonlyArray as A, Either as E } from 'effect';
// Effect must be imported before other imports that use effect!

import type {
Expand All @@ -14,7 +14,8 @@ import database_config from '$database/_config';
import {
get_base_document,
get_base_documents,
parse_base_document
parse_base_document,
type BaseDocument
} from './base_document_handling';

import { Value } from '@sinclair/typebox/value';
Expand Down Expand Up @@ -71,21 +72,38 @@ export function get_document<C extends CollectionName>(
export function get_collection<C extends CollectionName>(
collection_name: C,
options: GetCollectionOptions<C> = {}
): Document<C>[] {
) {
return pipe(
Effect.all([
pipe(
get_base_documents(),
Effect.map(A.filter((doc) => doc._path.includes(`${base}/${collection_name}`)))
),
get_base_documents({ path_includes: `${base}/${collection_name}` }),
get_collection_schema(collection_name)
]),
Effect.flatMap(([documents, schema]) =>
Effect.flatMap(([documents, schema]) => parse_base_documents(documents, schema)),
Effect.map((documents) => sort_documents(documents, options.sort)),
Effect.runSync
);
}

function parse_base_documents<C extends CollectionName>(
documents: BaseDocument[],
schema: CollectionSchema<C>
) {
return pipe(
documents,
A.map((doc) =>
pipe(
Effect.all(documents.map((doc) => parse_base_document(doc, schema))),
Effect.map((documents) => sort_documents(documents, options.sort))
parse_base_document(doc, schema),
Effect.either,
Effect.tap(
E.match({
onLeft: (e) => console.warn(e.message),
onRight: () => {}
})
)
)
),
Effect.runSync
Effect.all,
Effect.map(A.filter(E.isRight)),
Effect.map(A.map(E.getOrThrow))
);
}

0 comments on commit 76e0aa7

Please sign in to comment.