Skip to content

Commit

Permalink
feat(orm): exclude function fields from FieldName type
Browse files Browse the repository at this point in the history
  • Loading branch information
Char2sGu committed May 18, 2022
1 parent e135620 commit c25871a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
28 changes: 21 additions & 7 deletions packages/orm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ import { getInstanceStateFromItem } from './identity-map';
import { getClassTypeFromInstance } from '@deepkit/core';

export type FlattenIfArray<T> = T extends Array<any> ? T[0] : T;
export type FieldName<T> = keyof T & string;
export type FieldName<T> = {
[Key in keyof T & string]: T[Key] extends Function ? never : Key;
}[keyof T & string];

export function getClassSchemaInstancePairs<T extends OrmEntity>(items: Iterable<T>): Map<ReflectionClass<any>, T[]> {
export function getClassSchemaInstancePairs<T extends OrmEntity>(
items: Iterable<T>
): Map<ReflectionClass<any>, T[]> {
const map = new Map<ReflectionClass<any>, 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 = [];
Expand All @@ -34,13 +40,18 @@ export function getClassSchemaInstancePairs<T extends OrmEntity>(items: Iterable
return map;
}


export function findQuerySatisfied<T extends { [index: string]: any }>(target: T, query: FilterQuery<T>): boolean {
export function findQuerySatisfied<T extends { [index: string]: any }>(
target: T,
query: FilterQuery<T>
): boolean {
//get rid of "Excessive stack depth comparing types 'any' and 'SiftQuery<T[]>'."
return (sift as any)(query as any, [target] as any[]).length > 0;
}

export function findQueryList<T extends { [index: string]: any }>(items: T[], query: FilterQuery<T>): T[] {
export function findQueryList<T extends { [index: string]: any }>(
items: T[],
query: FilterQuery<T>
): T[] {
//get rid of "Excessive stack depth comparing types 'any' and 'SiftQuery<T[]>'."
return (sift as any)(query as any, items as any[]);
}
Expand All @@ -53,5 +64,8 @@ export function buildChangesFromInstance<T>(item: T): Changes<T> {
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()
);
}
3 changes: 2 additions & 1 deletion packages/orm/tests/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -54,7 +55,7 @@ test('query lift', async () => {

class UserQuery<T extends { username: string }> extends MyBase<T> {
findAllUserNames() {
return this.findField('username');
return this.findField('username' as FieldName<T>);
}

//query classes should be able to infer the actual used class
Expand Down

0 comments on commit c25871a

Please sign in to comment.