Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing support to query #655

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions templates/deno/src/query.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,63 @@ type QueryTypesSingle = string | number | boolean;
export type QueryTypesList = string[] | number[] | boolean[];
export type QueryTypes = QueryTypesSingle | QueryTypesList;

export class Query {
static equal = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "equal", value);
type ModelType = {
[key: string]: QueryTypes;
}

export type CustomQueryTypes<T extends ModelType> = {
[K in keyof T]: [attribute: string & K, value: T[K] | T[K][]];
}[keyof T];

static notEqual = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "notEqual", value);
type betweenOverload = {
<T extends ModelType>(attribute: string & keyof T, start: string, end: string): string,
<T extends ModelType>(attribute: string & keyof T, start: number, end: number): string,
}

static lessThan = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "lessThan", value);
export class Query {
static equal = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "equal", args[1]);

static lessThanEqual = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "lessThanEqual", value);
static notEqual = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "notEqual", args[1]);

static greaterThan = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "greaterThan", value);
static lessThan = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "lessThan", args[1]);

static greaterThanEqual = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "greaterThanEqual", value);
static lessThanEqual = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "lessThanEqual", args[1]);

static search = (attribute: string, value: string): string =>
Query.addQuery(attribute, "search", value);
static greaterThan = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "greaterThan", args[1]);

static isNull = (attribute: string): string =>
static greaterThanEqual = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "greaterThanEqual", args[1]);

static isNull = <T extends ModelType>(attribute: string&keyof T): string =>
`isNull("${attribute}")`;

static isNotNull = (attribute: string): string =>
static isNotNull = <T extends ModelType>(attribute: string&keyof T): string =>
`isNotNull("${attribute}")`;

static between = (attribute: string, start: string|number, end: string|number): string =>
static between: betweenOverload = <T extends ModelType>(attribute: string&keyof T, start: string|number, end: string|number): string =>
`between("${attribute}", [${Query.parseValues(start)},${Query.parseValues(end)}])`;

static startsWith = (attribute: string, value: string): string =>
static startsWith = <T extends ModelType>(attribute: string&keyof T, value: string): string =>
Query.addQuery(attribute, "startsWith", value);

static endsWith = (attribute: string, value: string): string =>
static endsWith = <T extends ModelType>(attribute: string&keyof T, value: string): string =>
Query.addQuery(attribute, "endsWith", value);

static select = (attributes: string[]): string =>
static select = <T extends ModelType>(attributes: (string&keyof T)[]): string =>
`select([${attributes.map((attr: string) => `"${attr}"`).join(",")}])`;

static orderDesc = (attribute: string): string =>
static search = <T extends ModelType>(attribute: string&keyof T, value: string): string =>
Query.addQuery(attribute, "search", value);

static orderDesc = <T extends ModelType>(attribute: string&keyof T): string =>
`orderDesc("${attribute}")`;

static orderAsc = (attribute: string): string =>
static orderAsc = <T extends ModelType>(attribute: string&keyof T): string =>
`orderAsc("${attribute}")`;

static cursorAfter = (documentId: string): string =>
Expand All @@ -60,10 +73,10 @@ export class Query {
static offset = (offset: number): string =>
`offset(${offset})`;

private static addQuery = (attribute: string, method: string, value: QueryTypes): string =>
private static addQuery = <T extends ModelType>(attribute: CustomQueryTypes<T>[0], method: string, value: CustomQueryTypes<T>[1]): string =>
value instanceof Array
? `${method}("${attribute}", [${value
.map((v: QueryTypesSingle) => Query.parseValues(v))
.map((v) => Query.parseValues(v))
.join(",")}])`
: `${method}("${attribute}", [${Query.parseValues(value)}])`;

Expand Down
45 changes: 29 additions & 16 deletions templates/node/index.d.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,49 @@ declare module "{{ language.params.npmPackage|caseDash }}" {
type QueryTypesList = string[] | number[] | boolean[];
type QueryTypes = QueryTypesSingle | QueryTypesList;

type ModelType = {
[key: string]: QueryTypes;
}

type CustomQueryTypes<T extends ModelType> = {
[K in keyof T]: [attribute: string & K, value: T[K] | T[K][]];
}[keyof T];

type betweenOverload = {
<T extends ModelType>(attribute: string & keyof T, start: string, end: string): string,
<T extends ModelType>(attribute: string & keyof T, start: number, end: number): string,
}

export class Query {
static equal(attribute: string, value: QueryTypes): string;
static equal<T extends ModelType>(...args: CustomQueryTypes<T>): string;

static notEqual(attribute: string, value: QueryTypes): string;
static notEqual<T extends ModelType>(...args: CustomQueryTypes<T>): string;

static lessThan(attribute: string, value: QueryTypes): string;
static lessThan<T extends ModelType>(...args: CustomQueryTypes<T>): string;

static lessThanEqual(attribute: string, value: QueryTypes): string;
static lessThanEqual<T extends ModelType>(...args: CustomQueryTypes<T>): string;

static greaterThan(attribute: string, value: QueryTypes): string;
static greaterThan<T extends ModelType>(...args: CustomQueryTypes<T>): string;

static greaterThanEqual(attribute: string, value: QueryTypes): string;
static greaterThanEqual<T extends ModelType>(...args: CustomQueryTypes<T>): string;

static isNull(attribute: string): string;
static isNull<T extends ModelType>(attribute: string & keyof T): string;

static isNotNull(attribute: string): string;
static isNotNull<T extends ModelType>(attribute: string & keyof T): string;

static between<T extends string | number>(attribute: string, start: T, end: T): string;
static between: betweenOverload;

static startsWith(attribute: string, value: string): string;
static startsWith<T extends ModelType>(attribute: string & keyof T, value: string): string;

static endsWith(attribute: string, value: string): string;
static endsWith<T extends ModelType>(attribute: string & keyof T, value: string): string;

static select(attributes: string[]): string;
static select<T extends ModelType>(attributes: (string & keyof T)[]): string;

static search(attribute: string, value: string): string;
static search<T extends ModelType>(attribute: string & keyof T, value: string): string;

static orderDesc(attribute: string): string;
static orderDesc<T extends ModelType>(attribute: string & keyof T): string;

static orderAsc(attribute: string): string;
static orderAsc<T extends ModelType>(attribute: string & keyof T): string;

static cursorAfter(documentId: string): string;

Expand All @@ -183,7 +196,7 @@ declare module "{{ language.params.npmPackage|caseDash }}" {

static offset(value: number): string;

private static addQuery(attribute: string, method: string, value: QueryTypes): string;
private static addQuery<T extends ModelType>(attribute: CustomQueryTypes<T>[0], method: string, value: CustomQueryTypes<T>[1]): string;

private static parseValues(value: QueryTypes): string;
}
Expand Down
59 changes: 36 additions & 23 deletions templates/web/src/query.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,63 @@ type QueryTypesSingle = string | number | boolean;
export type QueryTypesList = string[] | number[] | boolean[];
export type QueryTypes = QueryTypesSingle | QueryTypesList;

type ModelType = {
[key: string]: QueryTypes;
}

export type CustomQueryTypes<T extends ModelType> = {
[K in keyof T]: [attribute: string & K, value: T[K] | T[K][]];
}[keyof T];

type betweenOverload = {
<T extends ModelType>(attribute: string & keyof T, start: string, end: string): string,
<T extends ModelType>(attribute: string & keyof T, start: number, end: number): string,
}

export class Query {
static equal = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "equal", value);
static equal = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "equal", args[1]);

static notEqual = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "notEqual", value);
static notEqual = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "notEqual", args[1]);

static lessThan = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "lessThan", value);
static lessThan = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "lessThan", args[1]);

static lessThanEqual = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "lessThanEqual", value);
static lessThanEqual = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "lessThanEqual", args[1]);

static greaterThan = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "greaterThan", value);
static greaterThan = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "greaterThan", args[1]);

static greaterThanEqual = (attribute: string, value: QueryTypes): string =>
Query.addQuery(attribute, "greaterThanEqual", value);
static greaterThanEqual = <T extends ModelType>(...args: CustomQueryTypes<T>): string =>
Query.addQuery(args[0], "greaterThanEqual", args[1]);

static isNull = (attribute: string): string =>
static isNull = <T extends ModelType>(attribute: string&keyof T): string =>
`isNull("${attribute}")`;

static isNotNull = (attribute: string): string =>
static isNotNull = <T extends ModelType>(attribute: string&keyof T): string =>
`isNotNull("${attribute}")`;

static between = (attribute: string, start: string|number, end: string|number): string =>
static between: betweenOverload = <T extends ModelType>(attribute: string&keyof T, start: string|number, end: string|number): string =>
`between("${attribute}", [${Query.parseValues(start)},${Query.parseValues(end)}])`;

static startsWith = (attribute: string, value: string): string =>
static startsWith = <T extends ModelType>(attribute: string&keyof T, value: string): string =>
Query.addQuery(attribute, "startsWith", value);

static endsWith = (attribute: string, value: string): string =>
static endsWith = <T extends ModelType>(attribute: string&keyof T, value: string): string =>
Query.addQuery(attribute, "endsWith", value);

static select = (attributes: string[]): string =>
static select = <T extends ModelType>(attributes: (string&keyof T)[]): string =>
`select([${attributes.map((attr: string) => `"${attr}"`).join(",")}])`;

static search = (attribute: string, value: string): string =>
static search = <T extends ModelType>(attribute: string&keyof T, value: string): string =>
Query.addQuery(attribute, "search", value);

static orderDesc = (attribute: string): string =>
static orderDesc = <T extends ModelType>(attribute: string&keyof T): string =>
`orderDesc("${attribute}")`;

static orderAsc = (attribute: string): string =>
static orderAsc = <T extends ModelType>(attribute: string&keyof T): string =>
`orderAsc("${attribute}")`;

static cursorAfter = (documentId: string): string =>
Expand All @@ -60,10 +73,10 @@ export class Query {
static offset = (offset: number): string =>
`offset(${offset})`;

private static addQuery = (attribute: string, method: string, value: QueryTypes): string =>
private static addQuery = <T extends ModelType>(attribute: CustomQueryTypes<T>[0], method: string, value: CustomQueryTypes<T>[1]): string =>
value instanceof Array
? `${method}("${attribute}", [${value
.map((v: QueryTypesSingle) => Query.parseValues(v))
.map((v) => Query.parseValues(v))
.join(",")}])`
: `${method}("${attribute}", [${Query.parseValues(value)}])`;

Expand Down