From 00c76a72f64a5ca82605e6ce85dd2b32d50a2352 Mon Sep 17 00:00:00 2001 From: Suven-p Date: Tue, 9 May 2023 09:22:33 +0545 Subject: [PATCH 1/5] Add typing support to query --- templates/web/src/query.ts.twig | 52 +++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/templates/web/src/query.ts.twig b/templates/web/src/query.ts.twig index 51cbb7a9f..af25eafd3 100644 --- a/templates/web/src/query.ts.twig +++ b/templates/web/src/query.ts.twig @@ -2,50 +2,58 @@ 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]; + 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 = (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,7 +68,7 @@ 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)) From ef8378a4eca0afb171110d33d858f7982db59d02 Mon Sep 17 00:00:00 2001 From: Suven-p Date: Tue, 9 May 2023 16:07:55 +0545 Subject: [PATCH 2/5] Fix node test --- templates/web/src/query.ts.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/web/src/query.ts.twig b/templates/web/src/query.ts.twig index af25eafd3..3c206ccd7 100644 --- a/templates/web/src/query.ts.twig +++ b/templates/web/src/query.ts.twig @@ -71,7 +71,7 @@ export class Query { 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)}])`; From 30a3521296fae2052ee135cca31add40308621e0 Mon Sep 17 00:00:00 2001 From: Suven-p Date: Tue, 9 May 2023 16:45:34 +0545 Subject: [PATCH 3/5] Update typings for node --- templates/node/index.d.ts.twig | 38 ++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/templates/node/index.d.ts.twig b/templates/node/index.d.ts.twig index adae9cf5d..5ab1df5d5 100644 --- a/templates/node/index.d.ts.twig +++ b/templates/node/index.d.ts.twig @@ -144,36 +144,44 @@ 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]; + 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 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 +191,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; } From 5100a752a8a2eb811f81ad125ca79475656b67cf Mon Sep 17 00:00:00 2001 From: Suven-p Date: Tue, 9 May 2023 17:43:42 +0545 Subject: [PATCH 4/5] Fix type for between query --- templates/node/index.d.ts.twig | 7 ++++++- templates/web/src/query.ts.twig | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/templates/node/index.d.ts.twig b/templates/node/index.d.ts.twig index 5ab1df5d5..77945b531 100644 --- a/templates/node/index.d.ts.twig +++ b/templates/node/index.d.ts.twig @@ -152,6 +152,11 @@ declare module "{{ language.params.npmPackage|caseDash }}" { [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(...args: CustomQueryTypes): string; @@ -169,7 +174,7 @@ declare module "{{ language.params.npmPackage|caseDash }}" { static isNotNull(attribute: string & keyof T): string; - static between(attribute: string, start: T, end: T): string; + static between: betweenOverload; static startsWith(attribute: string & keyof T, value: string): string; diff --git a/templates/web/src/query.ts.twig b/templates/web/src/query.ts.twig index 3c206ccd7..61a63e93d 100644 --- a/templates/web/src/query.ts.twig +++ b/templates/web/src/query.ts.twig @@ -10,6 +10,11 @@ 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 = (...args: CustomQueryTypes): string => Query.addQuery(args[0], "equal", args[1]); @@ -35,7 +40,7 @@ export class Query { static isNotNull = (attribute: string&keyof T): string => `isNotNull("${attribute}")`; - static between = (attribute: string&keyof T, 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&keyof T, value: string): string => From 2918d605e0b76a293fbfb4a996a1050bbf9ffad9 Mon Sep 17 00:00:00 2001 From: Suven-p Date: Tue, 9 May 2023 18:36:31 +0545 Subject: [PATCH 5/5] Update query for deno --- templates/deno/src/query.ts.twig | 63 +++++++++++++++++++------------- 1 file changed, 38 insertions(+), 25 deletions(-) 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)}])`;