@entity-access/entity-access 1.0.111 → 1.0.112
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/common/ObjectPool.d.ts +6 -2
- package/dist/common/ObjectPool.d.ts.map +1 -1
- package/dist/common/ObjectPool.js +33 -27
- package/dist/common/ObjectPool.js.map +1 -1
- package/dist/common/sleep.d.ts.map +1 -1
- package/dist/common/sleep.js +23 -2
- package/dist/common/sleep.js.map +1 -1
- package/dist/decorators/Relate.d.ts +16 -10
- package/dist/decorators/Relate.d.ts.map +1 -1
- package/dist/decorators/Relate.js +52 -46
- package/dist/decorators/Relate.js.map +1 -1
- package/dist/tests/model/ShoppingContext.d.ts.map +1 -1
- package/dist/tests/model/ShoppingContext.js +2 -1
- package/dist/tests/model/ShoppingContext.js.map +1 -1
- package/dist/tests/pool/pool-test.d.ts.map +1 -1
- package/dist/tests/pool/pool-test.js +55 -47
- package/dist/tests/pool/pool-test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/common/ObjectPool.ts +44 -28
- package/src/common/sleep.ts +23 -2
- package/src/decorators/Relate.ts +91 -97
- package/src/tests/model/ShoppingContext.ts +3 -2
- package/src/tests/pool/pool-test.ts +58 -51
package/src/decorators/Relate.ts
CHANGED
|
@@ -2,95 +2,58 @@ import type { IClassOf } from "./IClassOf.js";
|
|
|
2
2
|
import SchemaRegistry from "./SchemaRegistry.js";
|
|
3
3
|
import NameParser from "./parser/NameParser.js";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}: {
|
|
11
|
-
|
|
12
|
-
foreignKey: (item: T) => any;
|
|
13
|
-
|
|
14
|
-
inverseProperty: (item: TRelated) => T[];
|
|
15
|
-
|
|
16
|
-
inverseKey?: (item: TRelated) => any;
|
|
17
|
-
dotNotCreateIndex?: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
) {
|
|
21
|
-
return (target: T, key: string): any => {
|
|
22
|
-
|
|
23
|
-
const cn = target.constructor ?? target;
|
|
24
|
-
const type = SchemaRegistry.model(cn);
|
|
25
|
-
|
|
26
|
-
type.addRelation({
|
|
27
|
-
type,
|
|
28
|
-
name: key,
|
|
29
|
-
foreignKey: NameParser.parseMember(name),
|
|
30
|
-
relatedTypeClass: c,
|
|
31
|
-
relatedName: NameParser.parseMember(inv),
|
|
32
|
-
relatedKey: invKey ? NameParser.parseMember(invKey) : void 0,
|
|
33
|
-
dotNotCreateIndex
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
};
|
|
5
|
+
export interface IRelatedType<T, TRelated> {
|
|
6
|
+
property: (item: T) => TRelated;
|
|
7
|
+
inverseProperty: (item: TRelated) => T[];
|
|
8
|
+
inverseKey?: (item: TRelated) => any;
|
|
9
|
+
dotNotCreateIndex?: boolean;
|
|
37
10
|
}
|
|
38
11
|
|
|
39
|
-
export
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
inverseProperty: (item: TRelated) => T;
|
|
47
|
-
|
|
48
|
-
inverseKey?: (item: TRelated) => any;
|
|
49
|
-
dotNotCreateIndex?: boolean;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
) {
|
|
53
|
-
return (target: T, key: string): any => {
|
|
12
|
+
export interface IRelatedTypeOne<T, TRelated> {
|
|
13
|
+
property: (item: T) => TRelated;
|
|
14
|
+
inverseProperty: (item: TRelated) => T;
|
|
15
|
+
inverseKey?: (item: TRelated) => any;
|
|
16
|
+
dotNotCreateIndex?: boolean;
|
|
17
|
+
}
|
|
54
18
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
foreignKey: NameParser.parseMember(name),
|
|
62
|
-
relatedTypeClass: c,
|
|
63
|
-
relatedName: NameParser.parseMember(inv),
|
|
64
|
-
relatedKey: invKey ? NameParser.parseMember(invKey) : void 0,
|
|
65
|
-
dotNotCreateIndex
|
|
66
|
-
});
|
|
67
|
-
r.relatedRelation.isCollection = false;
|
|
68
|
-
};
|
|
19
|
+
export interface IRelatedTypeWithType<T, TRelated> {
|
|
20
|
+
type: () => IClassOf<TRelated>,
|
|
21
|
+
property: (item: T) => TRelated;
|
|
22
|
+
inverseProperty: (item: TRelated) => T[];
|
|
23
|
+
inverseKey?: (item: TRelated) => any;
|
|
24
|
+
dotNotCreateIndex?: boolean;
|
|
69
25
|
}
|
|
70
26
|
|
|
71
|
-
export
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
27
|
+
export interface IRelatedTypeOneWithType<T, TRelated> {
|
|
28
|
+
type: () => IClassOf<TRelated>,
|
|
29
|
+
property: (item: T) => TRelated;
|
|
30
|
+
inverseProperty: (item: TRelated) => T;
|
|
31
|
+
inverseKey?: (item: TRelated) => any;
|
|
32
|
+
dotNotCreateIndex?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export function RelateTo<T, TRelated>(p: IRelatedTypeWithType<T, TRelated>): (target: T, key: string) => any;
|
|
35
|
+
export function RelateTo<T, TRelated>(c: IClassOf<TRelated>, p: IRelatedType<T, TRelated>): (target: T, key: string) => any;
|
|
36
|
+
export function RelateTo(c, p?): any {
|
|
76
37
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
dotNotCreateIndex?: boolean;
|
|
38
|
+
if (p === void 0) {
|
|
39
|
+
p = c;
|
|
40
|
+
c = p.type?.();
|
|
81
41
|
}
|
|
82
42
|
|
|
83
|
-
|
|
84
|
-
|
|
43
|
+
const { property, inverseKey: invKey, inverseProperty, dotNotCreateIndex } = p;
|
|
44
|
+
|
|
45
|
+
return (target: any, foreignKey: string): any => {
|
|
85
46
|
|
|
86
47
|
const cn = target.constructor ?? target;
|
|
87
|
-
const
|
|
48
|
+
const entityType = SchemaRegistry.model(cn);
|
|
49
|
+
|
|
50
|
+
const name = NameParser.parseMember(property);
|
|
88
51
|
|
|
89
|
-
|
|
90
|
-
type,
|
|
91
|
-
name
|
|
52
|
+
entityType.addRelation({
|
|
53
|
+
type: entityType,
|
|
54
|
+
name,
|
|
92
55
|
foreignKey,
|
|
93
|
-
relatedTypeClass: c,
|
|
56
|
+
relatedTypeClass: c ?? (Reflect as any).getMetadata("design:type", target, name),
|
|
94
57
|
relatedName: NameParser.parseMember(inverseProperty),
|
|
95
58
|
relatedKey: invKey ? NameParser.parseMember(invKey) : void 0,
|
|
96
59
|
dotNotCreateIndex
|
|
@@ -99,34 +62,65 @@ export function RelateTo<T, TRelated>(c: IClassOf<TRelated>,
|
|
|
99
62
|
};
|
|
100
63
|
}
|
|
101
64
|
|
|
65
|
+
export function RelateToOne<T, TRelated>(p: IRelatedTypeOneWithType<T, TRelated>): (target: T, key: string) => any;
|
|
66
|
+
export function RelateToOne<T, TRelated>(c: IClassOf<TRelated>, p: IRelatedTypeOne<T, TRelated>): (target: T, key: string) => any;
|
|
67
|
+
export function RelateToOne(c, p?): any {
|
|
102
68
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}: {
|
|
107
|
-
|
|
108
|
-
property: (item: T) => TRelated;
|
|
109
|
-
inverseProperty: (item: TRelated) => T;
|
|
110
|
-
|
|
111
|
-
inverseKey?: (item: TRelated) => any;
|
|
112
|
-
dotNotCreateIndex?: boolean;
|
|
69
|
+
if (p === void 0) {
|
|
70
|
+
p = c;
|
|
71
|
+
c = p.type?.();
|
|
113
72
|
}
|
|
114
73
|
|
|
115
|
-
|
|
116
|
-
|
|
74
|
+
const { property, inverseKey: invKey, inverseProperty, dotNotCreateIndex } = p;
|
|
75
|
+
|
|
76
|
+
return (target: any, foreignKey: string): any => {
|
|
117
77
|
|
|
118
78
|
const cn = target.constructor ?? target;
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
79
|
+
const entityType = SchemaRegistry.model(cn);
|
|
80
|
+
|
|
81
|
+
const name = NameParser.parseMember(property);
|
|
82
|
+
|
|
83
|
+
entityType.addRelation({
|
|
84
|
+
type: entityType,
|
|
85
|
+
name,
|
|
86
|
+
foreignKey,
|
|
87
|
+
relatedTypeClass: c ?? (Reflect as any).getMetadata("design:type", target, name),
|
|
88
|
+
relatedName: NameParser.parseMember(inverseProperty),
|
|
127
89
|
relatedKey: invKey ? NameParser.parseMember(invKey) : void 0,
|
|
128
90
|
dotNotCreateIndex,
|
|
129
91
|
singleInverseRelation: true
|
|
130
92
|
});
|
|
93
|
+
|
|
131
94
|
};
|
|
132
95
|
}
|
|
96
|
+
|
|
97
|
+
// export function RelateToOne<T, TRelated>(c: IClassOf<TRelated>,
|
|
98
|
+
// {
|
|
99
|
+
// property: name, inverseProperty: inv, inverseKey: invKey, dotNotCreateIndex
|
|
100
|
+
// }: {
|
|
101
|
+
|
|
102
|
+
// property: (item: T) => TRelated;
|
|
103
|
+
// inverseProperty: (item: TRelated) => T;
|
|
104
|
+
|
|
105
|
+
// inverseKey?: (item: TRelated) => any;
|
|
106
|
+
// dotNotCreateIndex?: boolean;
|
|
107
|
+
// }
|
|
108
|
+
|
|
109
|
+
// ) {
|
|
110
|
+
// return (target: T, key: string): any => {
|
|
111
|
+
|
|
112
|
+
// const cn = target.constructor ?? target;
|
|
113
|
+
// const type = SchemaRegistry.model(cn);
|
|
114
|
+
|
|
115
|
+
// const r = type.addRelation({
|
|
116
|
+
// type,
|
|
117
|
+
// name: NameParser.parseMember(name),
|
|
118
|
+
// foreignKey: key,
|
|
119
|
+
// relatedTypeClass: c,
|
|
120
|
+
// relatedName: NameParser.parseMember(inv),
|
|
121
|
+
// relatedKey: invKey ? NameParser.parseMember(invKey) : void 0,
|
|
122
|
+
// dotNotCreateIndex,
|
|
123
|
+
// singleInverseRelation: true
|
|
124
|
+
// });
|
|
125
|
+
// };
|
|
126
|
+
// }
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import EntityContext from "../../model/EntityContext.js";
|
|
2
2
|
import Column from "../../decorators/Column.js";
|
|
3
|
-
import
|
|
3
|
+
import { RelateTo, RelateToOne } from "../../decorators/Relate.js";
|
|
4
4
|
import Table from "../../decorators/Table.js";
|
|
5
5
|
import Index from "../../decorators/Index.js";
|
|
6
6
|
import DateTime from "../../types/DateTime.js";
|
|
@@ -263,7 +263,8 @@ export class OrderItem {
|
|
|
263
263
|
public orderItemID: number;
|
|
264
264
|
|
|
265
265
|
@Column()
|
|
266
|
-
@RelateTo(
|
|
266
|
+
@RelateTo({
|
|
267
|
+
type: () => Order,
|
|
267
268
|
property: (orderItem) => orderItem.order,
|
|
268
269
|
inverseProperty: (order) => order.orderItems
|
|
269
270
|
})
|
|
@@ -3,70 +3,77 @@ import assert from "assert";
|
|
|
3
3
|
import ObjectPool from "../../common/ObjectPool.js";
|
|
4
4
|
import sleep from "../../common/sleep.js";
|
|
5
5
|
|
|
6
|
-
export default async function () {
|
|
7
|
-
const pool = new ObjectPool({
|
|
8
|
-
asyncFactory: async () => {
|
|
9
|
-
await sleep(10);
|
|
10
|
-
return Promise.resolve({});
|
|
11
|
-
},
|
|
12
|
-
subscribeForRemoval: (po, clear) => void 0,
|
|
13
|
-
destroy(item) {
|
|
14
|
-
return sleep(10);
|
|
15
|
-
},
|
|
16
|
-
maxSize: 5,
|
|
17
|
-
poolSize: 2,
|
|
18
|
-
maxWait: 100
|
|
19
|
-
});
|
|
20
6
|
|
|
21
|
-
|
|
22
|
-
|
|
7
|
+
export default async function () {
|
|
8
|
+
let id = 1;
|
|
9
|
+
const logs = [] as string[];
|
|
10
|
+
try {
|
|
11
|
+
const pool = new ObjectPool({
|
|
12
|
+
asyncFactory: async () => {
|
|
13
|
+
await sleep(1);
|
|
14
|
+
return Promise.resolve({ id: id++, toString() { return `item-${this.id}`; } });
|
|
15
|
+
},
|
|
16
|
+
subscribeForRemoval: (po, clear) => void 0,
|
|
17
|
+
destroy(item) {
|
|
18
|
+
return Promise.resolve();
|
|
19
|
+
},
|
|
20
|
+
maxSize: 5,
|
|
21
|
+
poolSize: 2,
|
|
22
|
+
maxWait: 5000,
|
|
23
|
+
logger: (t) => logs.push(t)
|
|
24
|
+
});
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
const c1 = await pool.acquire();
|
|
27
|
+
const c2 = await pool.acquire();
|
|
25
28
|
|
|
26
|
-
|
|
29
|
+
assert.equal(2, c2.id);
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
assert.equal(c1, c3);
|
|
31
|
+
assert.notStrictEqual(c1, c2);
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
await c1[Symbol.asyncDisposable]();
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
assert.notEqual(c4, c1);
|
|
35
|
-
assert.notEqual(c4, c2);
|
|
35
|
+
assert.equal(pool.freeSize, 1);
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
const c3 = await pool.acquire();
|
|
38
|
+
assert.strictEqual(c1, c3);
|
|
39
|
+
assert.notStrictEqual(c2, c3);
|
|
40
|
+
assert.strictEqual(pool.freeSize, 0);
|
|
38
41
|
|
|
39
|
-
|
|
42
|
+
const c4 = await pool.acquire();
|
|
43
|
+
assert.notStrictEqual(c4, c1);
|
|
44
|
+
assert.notStrictEqual(c4, c2);
|
|
40
45
|
|
|
41
|
-
|
|
46
|
+
assert.strictEqual(pool.currentSize, 3);
|
|
42
47
|
|
|
43
|
-
|
|
44
|
-
|
|
48
|
+
await c4[Symbol.asyncDisposable]();
|
|
49
|
+
await c2[Symbol.asyncDisposable]();
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
assert.equal(pool.currentSize, 3);
|
|
47
52
|
|
|
48
|
-
|
|
53
|
+
assert.equal(pool.freeSize, 2);
|
|
49
54
|
|
|
50
|
-
await pool.acquire();
|
|
51
|
-
await pool.acquire();
|
|
52
|
-
await pool.acquire();
|
|
53
|
-
await pool.acquire();
|
|
54
|
-
const last = await pool.acquire();
|
|
55
|
-
let lastError;
|
|
56
|
-
try {
|
|
57
55
|
await pool.acquire();
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
await pool.acquire();
|
|
57
|
+
await pool.acquire();
|
|
58
|
+
const last = await pool.acquire();
|
|
59
|
+
let lastError;
|
|
60
|
+
try {
|
|
61
|
+
await pool.acquire();
|
|
62
|
+
} catch (error) {
|
|
63
|
+
lastError = error;
|
|
64
|
+
}
|
|
65
|
+
if (!lastError) {
|
|
66
|
+
assert.fail("Failed");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// free last after few milliseconds
|
|
70
|
+
setTimeout(() => {
|
|
71
|
+
last[Symbol.asyncDisposable]().catch(console.error);
|
|
72
|
+
}, 10);
|
|
73
|
+
|
|
74
|
+
// this should not fail
|
|
75
|
+
await pool.acquire();
|
|
76
|
+
} finally {
|
|
77
|
+
console.log(logs.join("\n"));
|
|
63
78
|
}
|
|
64
|
-
|
|
65
|
-
// free last after few milliseconds
|
|
66
|
-
setTimeout(() => {
|
|
67
|
-
last[Symbol.asyncDisposable]().catch(console.error);
|
|
68
|
-
}, 10);
|
|
69
|
-
|
|
70
|
-
// this should not fail
|
|
71
|
-
await pool.acquire();
|
|
72
79
|
}
|