Skip to content

Commit

Permalink
updating supported sql alchemy ops (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam Mason <sam.mason@blackcape.io>
  • Loading branch information
ukitake and Sam Mason committed May 6, 2024
1 parent 6237ada commit b8e9797
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
61 changes: 56 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
const SQL_OPS = ['*in_', '*eq', '*neq', '*contains', '*icontains'] as const;
const SQL_OPS = [
'*in_',
'*eq',
'*neq',
'*contains',
'*icontains',
'*websearch_to_tsquery',
'*gt',
'*gte',
'*lt',
'*lte',
'*like',
'*contained_by',
'*has_key',
'*has_all',
'*intersects',
'*intersects_nd',
'*same',
'*above',
'*below',
'*datetime',
'*datetime_naive',
'*startswith',
'*endswith'
] as const;

// request params
export type BaseAuthRequestParams = {
Expand Down Expand Up @@ -39,23 +63,50 @@ type NotGrouping<TModel> = {

export type LogicalGrouping<TModel> = OneOf<[AndGrouping<TModel>, OrGrouping<TModel>, NotGrouping<TModel>]>;

type Value = string | number | null;
type Value = string | number | boolean | null;

type SqlOps = (typeof SQL_OPS)[number];

type SqlOp<S extends SqlOps> = {
[key in Exclude<SqlOps, Exclude<SqlOps, S>>]: Value | Value[];
};

export type Logic = OneOf<[SqlOp<'*in_'>, SqlOp<'*eq'>, SqlOp<'*neq'>, SqlOp<'*contains'>, SqlOp<'*icontains'>]>;
export type Logic = OneOf<
[
SqlOp<'*in_'>,
SqlOp<'*eq'>,
SqlOp<'*neq'>,
SqlOp<'*contains'>,
SqlOp<'*icontains'>,
SqlOp<'*websearch_to_tsquery'>,
SqlOp<'*gt'>,
SqlOp<'*gte'>,
SqlOp<'*lt'>,
SqlOp<'*lte'>,
SqlOp<'*like'>,
SqlOp<'*contained_by'>,
SqlOp<'*has_key'>,
SqlOp<'*has_all'>,
SqlOp<'*intersects'>,
SqlOp<'*intersects_nd'>,
SqlOp<'*same'>,
SqlOp<'*above'>,
SqlOp<'*below'>,
SqlOp<'*datetime'>,
SqlOp<'*datetime_naive'>,
SqlOp<'*startswith'>,
SqlOp<'*endswith'>
]
>;

// (SM) make a type where all keys of T with '.${any string}'following them are valid keys
type Dots<T> = {
[K in keyof T & string as `${K}.${string}`]: T[K]
[K in keyof T & string as `${K}.${string}`]: T[K];
};

export type Clause<TModel> = {
[key in keyof (TModel & Dots<TModel>)]: Record<key, Logic> & Partial<Record<Exclude<keyof TModel, key>, never>> extends infer O
[key in keyof (TModel & Dots<TModel>)]: Record<key, Logic> &
Partial<Record<Exclude<keyof TModel, key>, never>> extends infer O
? Expand<O>
: never;
}[keyof (TModel & Dots<TModel>)];
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cruddy-types",
"version": "1.1.0",
"version": "1.2.0",
"type": "module",
"description": "Typescript types for front end integration with REST APIs produced by https://github.com/mdconaway/fastapi-cruddy-framework",
"license": "MIT",
Expand Down

0 comments on commit b8e9797

Please sign in to comment.