-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
78 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
<script lang="ts"> | ||
import { db } from '$modules'; | ||
import { type BaseRelationTransform } from '$modules/fields'; | ||
import type { RelationField } from '$modules/fields'; | ||
import type { CollectionName } from '$modules/database'; | ||
type C = $$Generic<CollectionName>; | ||
export let relation: BaseRelationTransform<C> | undefined; | ||
export let relation: RelationField<C> | undefined; | ||
</script> | ||
|
||
{#if relation} | ||
<slot relation={db.get_document(relation?.collection, relation.id)} /> | ||
<slot relation={db.get_document(relation?.collection, relation.document)} /> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { Collection, CollectionName, Document } from '$modules/types'; | ||
import { Type as T, type Static } from '@sinclair/typebox'; | ||
import { Value } from '@sinclair/typebox/value'; | ||
import _ from 'lodash'; | ||
|
||
const sort_order_schema = T.Union([T.Literal('asc'), T.Literal('desc')]); | ||
type SortOrder = Static<typeof sort_order_schema>; | ||
|
||
const base_sort_prop_schema = T.Tuple([T.String(), sort_order_schema]); | ||
type BaseSortProp<C extends CollectionName> = [keyof Collection<C>, SortOrder]; | ||
|
||
export type SortProp<C extends CollectionName> = BaseSortProp<C> | BaseSortProp<C>[]; | ||
|
||
type ParsedSortProp = { keys: string[]; orders: SortOrder[] }; | ||
|
||
export type GetCollectionOptions<C extends CollectionName> = { | ||
sort?: SortProp<C>; | ||
}; | ||
|
||
function parse_sort_prop<C extends CollectionName>(sortProp: SortProp<C>): ParsedSortProp { | ||
if (Value.Check(base_sort_prop_schema, sortProp)) | ||
return { | ||
keys: [sortProp[0]], | ||
orders: [sortProp[1]] | ||
}; | ||
else if (Value.Check(T.Array(base_sort_prop_schema), sortProp)) { | ||
return { | ||
keys: sortProp.map((base) => base[0]), | ||
orders: sortProp.map((base) => base[1]) | ||
}; | ||
} else | ||
return { | ||
keys: [], | ||
orders: [] | ||
}; | ||
} | ||
|
||
export function sort_documents<C extends CollectionName>( | ||
documents: Document<C>[], | ||
sort_prop: SortProp<C> | undefined = undefined | ||
): Document<C>[] { | ||
if (sort_prop) { | ||
const sort = parse_sort_prop(sort_prop); | ||
return _.orderBy( | ||
documents, | ||
sort.keys.map((k) => `props.${k}`), | ||
sort.orders | ||
); | ||
} else { | ||
return documents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters