Skip to content

Commit

Permalink
Merge pull request crcn#251 from Helveg/export-query-types
Browse files Browse the repository at this point in the history
Export constituent query types and operator union
  • Loading branch information
crcn authored Oct 31, 2022
2 parents 133132b + fe12fe4 commit 3469099
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type OperationCreator<TItem> = (
name: string
) => Operation<TItem>;

type BasicValueQuery<TValue> = {
export type BasicValueQuery<TValue> = {
$eq?: TValue;
$ne?: TValue;
$lt?: TValue;
Expand All @@ -56,27 +56,29 @@ type BasicValueQuery<TValue> = {
$and?: NestedQuery<TValue>[];
};

type ArrayValueQuery<TValue> = {
export type ArrayValueQuery<TValue> = {
$elemMatch?: Query<TValue>;
} & BasicValueQuery<TValue>;
type Unpacked<T> = T extends (infer U)[] ? U : T;

type ValueQuery<TValue> = TValue extends Array<any>
export type ValueQuery<TValue> = TValue extends Array<any>
? ArrayValueQuery<Unpacked<TValue>>
: BasicValueQuery<TValue>;

type NotObject = string | number | Date | boolean | Array<any>;
type ShapeQuery<TItemSchema> = TItemSchema extends NotObject
export type ShapeQuery<TItemSchema> = TItemSchema extends NotObject
? {}
: { [k in keyof TItemSchema]?: TItemSchema[k] | ValueQuery<TItemSchema[k]> };

type NestedQuery<TItemSchema> = ValueQuery<TItemSchema> &
export type NestedQuery<TItemSchema> = ValueQuery<TItemSchema> &
ShapeQuery<TItemSchema>;
export type Query<TItemSchema> =
| TItemSchema
| RegExp
| NestedQuery<TItemSchema>;

export type QueryOperators<TValue = any> = keyof ValueQuery<TValue>;

/**
* Walks through each value given the context - used for nested operations. E.g:
* { "person.address": { $eq: "blarg" }}
Expand Down
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as defaultOperations from "./operations";
import {
Query,
QueryOperators,
BasicValueQuery,
ArrayValueQuery,
ValueQuery,
NestedQuery,
ShapeQuery,
Options,
createQueryTester,
EqualsOperation,
Expand Down Expand Up @@ -30,6 +36,12 @@ const createDefaultQueryTester = <TItem, TSchema extends TItem = TItem>(

export {
Query,
QueryOperators,
BasicValueQuery,
ArrayValueQuery,
ValueQuery,
NestedQuery,
ShapeQuery,
EqualsOperation,
createQueryTester,
createOperationTester,
Expand Down

0 comments on commit 3469099

Please sign in to comment.