diff --git a/templates/deno/src/query.ts.twig b/templates/deno/src/query.ts.twig index dd7c7a1cc..61a63e93d 100644 --- a/templates/deno/src/query.ts.twig +++ b/templates/deno/src/query.ts.twig @@ -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 = { + [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 = { + (attribute: string & keyof T, start: string, end: string): string, + (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 = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "equal", args[1]); - static lessThanEqual = (attribute: string, value: QueryTypes): string => - Query.addQuery(attribute, "lessThanEqual", value); + static notEqual = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "notEqual", args[1]); - static greaterThan = (attribute: string, value: QueryTypes): string => - Query.addQuery(attribute, "greaterThan", value); + static lessThan = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "lessThan", args[1]); - static greaterThanEqual = (attribute: string, value: QueryTypes): string => - Query.addQuery(attribute, "greaterThanEqual", value); + static lessThanEqual = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "lessThanEqual", args[1]); - static search = (attribute: string, value: string): string => - Query.addQuery(attribute, "search", value); + static greaterThan = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "greaterThan", args[1]); - static isNull = (attribute: string): string => + static greaterThanEqual = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "greaterThanEqual", args[1]); + + static isNull = (attribute: string&keyof T): string => `isNull("${attribute}")`; - static isNotNull = (attribute: string): string => + static isNotNull = (attribute: string&keyof T): string => `isNotNull("${attribute}")`; - static between = (attribute: string, start: string|number, end: string|number): string => + static between: betweenOverload = (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 = (attribute: string&keyof T, value: string): string => Query.addQuery(attribute, "startsWith", value); - static endsWith = (attribute: string, value: string): string => + static endsWith = (attribute: string&keyof T, value: string): string => Query.addQuery(attribute, "endsWith", value); - static select = (attributes: string[]): string => + static select = (attributes: (string&keyof T)[]): string => `select([${attributes.map((attr: string) => `"${attr}"`).join(",")}])`; - static orderDesc = (attribute: string): string => + static search = (attribute: string&keyof T, value: string): string => + Query.addQuery(attribute, "search", value); + + static orderDesc = (attribute: string&keyof T): string => `orderDesc("${attribute}")`; - static orderAsc = (attribute: string): string => + static orderAsc = (attribute: string&keyof T): string => `orderAsc("${attribute}")`; static cursorAfter = (documentId: string): string => @@ -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 = (attribute: CustomQueryTypes[0], method: string, value: CustomQueryTypes[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)}])`; diff --git a/templates/node/index.d.ts.twig b/templates/node/index.d.ts.twig index adae9cf5d..77945b531 100644 --- a/templates/node/index.d.ts.twig +++ b/templates/node/index.d.ts.twig @@ -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 = { + [K in keyof T]: [attribute: string & K, value: T[K] | T[K][]]; + }[keyof T]; + + type betweenOverload = { + (attribute: string & keyof T, start: string, end: string): string, + (attribute: string & keyof T, start: number, end: number): string, + } + export class Query { - static equal(attribute: string, value: QueryTypes): string; + static equal(...args: CustomQueryTypes): string; - static notEqual(attribute: string, value: QueryTypes): string; + static notEqual(...args: CustomQueryTypes): string; - static lessThan(attribute: string, value: QueryTypes): string; + static lessThan(...args: CustomQueryTypes): string; - static lessThanEqual(attribute: string, value: QueryTypes): string; + static lessThanEqual(...args: CustomQueryTypes): string; - static greaterThan(attribute: string, value: QueryTypes): string; + static greaterThan(...args: CustomQueryTypes): string; - static greaterThanEqual(attribute: string, value: QueryTypes): string; + static greaterThanEqual(...args: CustomQueryTypes): string; - static isNull(attribute: string): string; + static isNull(attribute: string & keyof T): string; - static isNotNull(attribute: string): string; + static isNotNull(attribute: string & keyof T): string; - static between(attribute: string, start: T, end: T): string; + static between: betweenOverload; - static startsWith(attribute: string, value: string): string; + static startsWith(attribute: string & keyof T, value: string): string; - static endsWith(attribute: string, value: string): string; + static endsWith(attribute: string & keyof T, value: string): string; - static select(attributes: string[]): string; + static select(attributes: (string & keyof T)[]): string; - static search(attribute: string, value: string): string; + static search(attribute: string & keyof T, value: string): string; - static orderDesc(attribute: string): string; + static orderDesc(attribute: string & keyof T): string; - static orderAsc(attribute: string): string; + static orderAsc(attribute: string & keyof T): string; static cursorAfter(documentId: string): string; @@ -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(attribute: CustomQueryTypes[0], method: string, value: CustomQueryTypes[1]): string; private static parseValues(value: QueryTypes): string; } diff --git a/templates/web/src/query.ts.twig b/templates/web/src/query.ts.twig index 51cbb7a9f..61a63e93d 100644 --- a/templates/web/src/query.ts.twig +++ b/templates/web/src/query.ts.twig @@ -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 = { + [K in keyof T]: [attribute: string & K, value: T[K] | T[K][]]; +}[keyof T]; + +type betweenOverload = { + (attribute: string & keyof T, start: string, end: string): string, + (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 = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "equal", args[1]); - static notEqual = (attribute: string, value: QueryTypes): string => - Query.addQuery(attribute, "notEqual", value); + static notEqual = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "notEqual", args[1]); - static lessThan = (attribute: string, value: QueryTypes): string => - Query.addQuery(attribute, "lessThan", value); + static lessThan = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "lessThan", args[1]); - static lessThanEqual = (attribute: string, value: QueryTypes): string => - Query.addQuery(attribute, "lessThanEqual", value); + static lessThanEqual = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "lessThanEqual", args[1]); - static greaterThan = (attribute: string, value: QueryTypes): string => - Query.addQuery(attribute, "greaterThan", value); + static greaterThan = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "greaterThan", args[1]); - static greaterThanEqual = (attribute: string, value: QueryTypes): string => - Query.addQuery(attribute, "greaterThanEqual", value); + static greaterThanEqual = (...args: CustomQueryTypes): string => + Query.addQuery(args[0], "greaterThanEqual", args[1]); - static isNull = (attribute: string): string => + static isNull = (attribute: string&keyof T): string => `isNull("${attribute}")`; - static isNotNull = (attribute: string): string => + static isNotNull = (attribute: string&keyof T): string => `isNotNull("${attribute}")`; - static between = (attribute: string, start: string|number, end: string|number): string => + static between: betweenOverload = (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 = (attribute: string&keyof T, value: string): string => Query.addQuery(attribute, "startsWith", value); - static endsWith = (attribute: string, value: string): string => + static endsWith = (attribute: string&keyof T, value: string): string => Query.addQuery(attribute, "endsWith", value); - static select = (attributes: string[]): string => + static select = (attributes: (string&keyof T)[]): string => `select([${attributes.map((attr: string) => `"${attr}"`).join(",")}])`; - static search = (attribute: string, value: string): string => + static search = (attribute: string&keyof T, value: string): string => Query.addQuery(attribute, "search", value); - static orderDesc = (attribute: string): string => + static orderDesc = (attribute: string&keyof T): string => `orderDesc("${attribute}")`; - static orderAsc = (attribute: string): string => + static orderAsc = (attribute: string&keyof T): string => `orderAsc("${attribute}")`; static cursorAfter = (documentId: string): string => @@ -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 = (attribute: CustomQueryTypes[0], method: string, value: CustomQueryTypes[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)}])`;