Skip to content

Commit 5ac3c48

Browse files
authored
fix(types): fix TS 4.9 excessive depth error on InferAttributes (sequelize#15134)
* fix(types): fix TS 4.9 error with InferAttributes * fix: fix types
1 parent a1a20c6 commit 5ac3c48

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

src/associations/has-many.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import isPlainObject from 'lodash/isPlainObject';
21
import upperFirst from 'lodash/upperFirst';
32
import { AssociationError } from '../errors/index.js';
43
import type {
@@ -15,7 +14,7 @@ import type {
1514
import { Op } from '../operators';
1615
import { col, fn } from '../sequelize';
1716
import type { AllowArray } from '../utils';
18-
import { removeUndefined } from '../utils';
17+
import { isPlainObject, removeUndefined } from '../utils';
1918
import { isSameInitialModel } from '../utils/model-utils.js';
2019
import type { MultiAssociationAccessors, MultiAssociationOptions, Association, AssociationOptions } from './base';
2120
import { MultiAssociation } from './base';

src/dialects/abstract/replication-pool.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ReplicationPoolConfig<Resource> = {
1616
validate(connection: Resource): boolean,
1717
};
1818

19-
const OwningPool = Symbol('owning-pool');
19+
const owningPools = new WeakMap<object, 'read' | 'write'>();
2020

2121
export class ReplicationPool<Resource extends object> {
2222
/**
@@ -42,7 +42,7 @@ export class ReplicationPool<Resource extends object> {
4242
const nextRead = reads++ % readConfig.length;
4343
const connection = await connect(readConfig[nextRead]);
4444

45-
Reflect.set(connection, OwningPool, 'read');
45+
owningPools.set(connection, 'read');
4646

4747
return connection;
4848
},
@@ -62,7 +62,7 @@ export class ReplicationPool<Resource extends object> {
6262
create: async () => {
6363
const connection = await connect(writeConfig);
6464

65-
Reflect.set(connection, OwningPool, 'write');
65+
owningPools.set(connection, 'write');
6666

6767
return connection;
6868
},
@@ -90,13 +90,19 @@ export class ReplicationPool<Resource extends object> {
9090
}
9191

9292
release(client: Resource): void {
93-
const connectionType = Reflect.get(client, OwningPool);
93+
const connectionType = owningPools.get(client);
94+
if (!connectionType) {
95+
throw new Error('Unable to determine to which pool the connection belongs');
96+
}
9497

9598
this.getPool(connectionType).release(client);
9699
}
97100

98101
async destroy(client: Resource): Promise<void> {
99-
const connectionType = Reflect.get(client, OwningPool);
102+
const connectionType = owningPools.get(client);
103+
if (!connectionType) {
104+
throw new Error('Unable to determine to which pool the connection belongs');
105+
}
100106

101107
await this.getPool(connectionType).destroy(client);
102108
debug('connection destroy');

src/model.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3796,10 +3796,10 @@ export type InferCreationAttributes<
37963796
* - Excluded manually using {@link InferAttributesOptions#omit}
37973797
*/
37983798
type InternalInferAttributeKeysFromFields<M extends Model, Key extends keyof M, Options extends InferAttributesOptions<keyof M | never | ''>> =
3799-
// functions are always excluded
3800-
M[Key] extends AnyFunction ? never
38013799
// fields inherited from Model are all excluded
3802-
: Key extends keyof Model ? never
3800+
Key extends keyof Model ? never
3801+
// functions are always excluded
3802+
: M[Key] extends AnyFunction ? never
38033803
// fields branded with NonAttribute are excluded
38043804
: IsBranded<M[Key], typeof NonAttributeBrand> extends true ? never
38053805
// check 'omit' option is provided & exclude those listed in it

src/utils/check.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import isEmpty from 'lodash/isEmpty';
2-
import isPlainObject from 'lodash/isPlainObject';
32
import type { DataType } from '..';
43
import { getOperators } from './format';
54
// eslint-disable-next-line import/order -- caused by temporarily mixing require with import
@@ -25,6 +24,16 @@ export function isString(val: any): val is string {
2524
return typeof val === 'string';
2625
}
2726

27+
export function isPlainObject(value: unknown): value is object {
28+
if (value === null || typeof value !== 'object') {
29+
return false;
30+
}
31+
32+
const prototype = Object.getPrototypeOf(value);
33+
34+
return prototype === null || prototype === Object.prototype;
35+
}
36+
2837
/**
2938
* Returns whether `value` is using the nested syntax for attributes.
3039
*

0 commit comments

Comments
 (0)