Skip to content

Commit

Permalink
fix(type): make sure forwarded type arguments reset Ω at the origin
Browse files Browse the repository at this point in the history
closes #619
  • Loading branch information
marcj committed Jan 26, 2025
1 parent f2f6ed0 commit 3138671
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ export function zip<T extends (readonly unknown[])[]>(
*/
export function forwardTypeArguments(x: any, y: any): void {
y.Ω = x.Ω;
x.Ω = undefined;
}

export function formatError(error: any, withStack: boolean = false): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/sql/src/sql-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ export class SqlRawFactory implements RawFactory<[SqlQuery]> {
) {
}

create<T>(sql: SqlQuery, type?: ReceiveType<T>): RawQuery<T> {
create<T = unknown>(sql: SqlQuery, type?: ReceiveType<T>): RawQuery<T> {
type = type ? resolveReceiveType(type) : { kind: ReflectionKind.any };
return new RawQuery(this.session, this.connectionPool, this.platform, sql, type);
}
Expand Down
41 changes: 19 additions & 22 deletions packages/sqlite/tests/sqlite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,7 @@ import { databaseFactory } from './factory.js';
import { User, UserCredentials } from '@deepkit/orm-integration';
import { SQLiteDatabaseAdapter, SQLiteDatabaseTransaction } from '../src/sqlite-adapter.js';
import { sleep } from '@deepkit/core';
import {
AutoIncrement,
BackReference,
cast,
Entity,
entity,
getPrimaryKeyExtractor,
getPrimaryKeyHashGenerator,
isReferenceInstance,
PrimaryKey,
Reference,
ReflectionClass,
serialize,
typeOf,
Unique,
UUID,
uuid,
} from '@deepkit/type';
import { AutoIncrement, BackReference, cast, Entity, entity, getPrimaryKeyExtractor, getPrimaryKeyHashGenerator, isReferenceInstance, PrimaryKey, Reference, ReflectionClass, serialize, typeOf, Unique, UUID, uuid } from '@deepkit/type';
import { DatabaseEntityRegistry, UniqueConstraintFailure } from '@deepkit/orm';
import { sql } from '@deepkit/sql';

Expand Down Expand Up @@ -770,7 +753,7 @@ test('deep join population', async () => {

{
const basket = await database.query(Basket)
.joinWith('items', v=> v.joinWith('product'))
.joinWith('items', v => v.joinWith('product'))
.findOne();
expect(basket).toBeInstanceOf(Basket);
expect(basket.items[0]).toBeInstanceOf(BasketItem);
Expand Down Expand Up @@ -943,7 +926,9 @@ test('uuid 3', async () => {
test('unique constraint 1', async () => {
class Model {
id: number & PrimaryKey & AutoIncrement = 0;
constructor(public username: string & Unique = '') {}

constructor(public username: string & Unique = '') {
}
}

const database = await databaseFactory([Model]);
Expand All @@ -965,15 +950,27 @@ test('unique constraint 1', async () => {
}

{
const m = await database.query(Model).filter({username: 'paul'}).findOne();
const m = await database.query(Model).filter({ username: 'paul' }).findOne();
m.username = 'peter';
await expect(database.persist(m)).rejects.toThrow('constraint failed');
await expect(database.persist(m)).rejects.toBeInstanceOf(UniqueConstraintFailure);
}

{
const p = database.query(Model).filter({username: 'paul'}).patchOne({username: 'peter'});
const p = database.query(Model).filter({ username: 'paul' }).patchOne({ username: 'peter' });
await expect(p).rejects.toThrow('constraint failed');
await expect(p).rejects.toBeInstanceOf(UniqueConstraintFailure);
}
});

test('forward raw types', async () => {
const database = await databaseFactory([]);

const q1 = await database.raw<{ count1: number }>(sql`SELECT 42 AS count1`);
const r1 = await q1.findOne();

const q2 = await database.raw(sql`SELECT 42 AS count2`);
const r2: any = await q2.findOne();

expect(r1.count1).toEqual(r2.count2);
});
28 changes: 28 additions & 0 deletions packages/type/tests/issues/argument-state.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect, test } from '@jest/globals';
import { ReceiveType, resolveReceiveType } from '../../src/reflection/reflection';
import { Type } from '../../src/reflection/type';
import { forwardTypeArguments } from '@deepkit/core';

test('function default', () => {
class Clazz {
create<T>(sql: string, type?: ReceiveType<T>): Type {
return resolveReceiveType(type);
}
}

class Fascade {
create: Clazz['create'];

constructor() {
this.create = (...args: any) => {
const clazz = new Clazz;
forwardTypeArguments(this.create, clazz.create);
return clazz.create.apply(clazz, args);
};
}
}

const clazz = new Fascade();
const t1 = clazz.create<{count1: string}>('');
expect(() => clazz.create('')).toThrow('No type information received');
});

0 comments on commit 3138671

Please sign in to comment.