From c25871a080b3b1e360cf119ffc02e3298787bd55 Mon Sep 17 00:00:00 2001 From: Char2s Date: Thu, 19 May 2022 00:53:00 +0800 Subject: [PATCH] feat(orm): exclude function fields from `FieldName` type --- packages/orm/src/utils.ts | 28 +++++++++++++++++++++------- packages/orm/tests/query.spec.ts | 3 ++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/orm/src/utils.ts b/packages/orm/src/utils.ts index 6842ee353..4f65569b2 100644 --- a/packages/orm/src/utils.ts +++ b/packages/orm/src/utils.ts @@ -16,13 +16,19 @@ import { getInstanceStateFromItem } from './identity-map'; import { getClassTypeFromInstance } from '@deepkit/core'; export type FlattenIfArray = T extends Array ? T[0] : T; -export type FieldName = keyof T & string; +export type FieldName = { + [Key in keyof T & string]: T[Key] extends Function ? never : Key; +}[keyof T & string]; -export function getClassSchemaInstancePairs(items: Iterable): Map, T[]> { +export function getClassSchemaInstancePairs( + items: Iterable +): Map, T[]> { const map = new Map, T[]>(); for (const item of items) { - const classSchema = ReflectionClass.from(getClassTypeFromInstance(item)); + const classSchema = ReflectionClass.from( + getClassTypeFromInstance(item) + ); let items = map.get(classSchema); if (!items) { items = []; @@ -34,13 +40,18 @@ export function getClassSchemaInstancePairs(items: Iterable return map; } - -export function findQuerySatisfied(target: T, query: FilterQuery): boolean { +export function findQuerySatisfied( + target: T, + query: FilterQuery +): boolean { //get rid of "Excessive stack depth comparing types 'any' and 'SiftQuery'." return (sift as any)(query as any, [target] as any[]).length > 0; } -export function findQueryList(items: T[], query: FilterQuery): T[] { +export function findQueryList( + items: T[], + query: FilterQuery +): T[] { //get rid of "Excessive stack depth comparing types 'any' and 'SiftQuery'." return (sift as any)(query as any, items as any[]); } @@ -53,5 +64,8 @@ export function buildChangesFromInstance(item: T): Changes { const state = getInstanceStateFromItem(item); const lastSnapshot = state.getSnapshot(); const currentSnapshot = state.classState.snapshot(item); - return state.classState.changeDetector(lastSnapshot, currentSnapshot, item) || new Changes; + return ( + state.classState.changeDetector(lastSnapshot, currentSnapshot, item) || + new Changes() + ); } diff --git a/packages/orm/tests/query.spec.ts b/packages/orm/tests/query.spec.ts index 507fc1834..79f3bfdbe 100644 --- a/packages/orm/tests/query.spec.ts +++ b/packages/orm/tests/query.spec.ts @@ -4,6 +4,7 @@ import { assert, IsExact } from 'conditional-type-checks'; import { Database } from '../src/database'; import { MemoryDatabaseAdapter, MemoryQuery } from '../src/memory-db'; import { Query } from '../src/query'; +import { FieldName } from "../src/utils"; test('query select', async () => { class s { @@ -54,7 +55,7 @@ test('query lift', async () => { class UserQuery extends MyBase { findAllUserNames() { - return this.findField('username'); + return this.findField('username' as FieldName); } //query classes should be able to infer the actual used class