@entity-access/entity-access 1.0.10 → 1.0.12

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.
Files changed (45) hide show
  1. package/README.md +66 -8
  2. package/dist/compiler/ISqlHelpers.d.ts +1 -1
  3. package/dist/compiler/ISqlHelpers.d.ts.map +1 -1
  4. package/dist/compiler/postgres/PostgreSqlMethodTransformer.d.ts +2 -1
  5. package/dist/compiler/postgres/PostgreSqlMethodTransformer.d.ts.map +1 -1
  6. package/dist/compiler/postgres/PostgreSqlMethodTransformer.js +13 -6
  7. package/dist/compiler/postgres/PostgreSqlMethodTransformer.js.map +1 -1
  8. package/dist/compiler/sql-server/SqlServerSqlMethodTransformer.d.ts +2 -1
  9. package/dist/compiler/sql-server/SqlServerSqlMethodTransformer.d.ts.map +1 -1
  10. package/dist/compiler/sql-server/SqlServerSqlMethodTransformer.js +13 -6
  11. package/dist/compiler/sql-server/SqlServerSqlMethodTransformer.js.map +1 -1
  12. package/dist/decorators/Relate.d.ts +8 -0
  13. package/dist/decorators/Relate.d.ts.map +1 -0
  14. package/dist/decorators/Relate.js +18 -0
  15. package/dist/decorators/Relate.js.map +1 -0
  16. package/dist/di/di.d.ts +2 -1
  17. package/dist/di/di.d.ts.map +1 -1
  18. package/dist/di/di.js +6 -6
  19. package/dist/di/di.js.map +1 -1
  20. package/dist/query/ast/ExpressionToSql.d.ts.map +1 -1
  21. package/dist/query/ast/ExpressionToSql.js +1 -2
  22. package/dist/query/ast/ExpressionToSql.js.map +1 -1
  23. package/dist/query/ast/IStringTransformer.d.ts +3 -2
  24. package/dist/query/ast/IStringTransformer.d.ts.map +1 -1
  25. package/dist/query/ast/IStringTransformer.js.map +1 -1
  26. package/dist/sql/Sql.d.ts.map +1 -1
  27. package/dist/sql/Sql.js +1 -1
  28. package/dist/sql/Sql.js.map +1 -1
  29. package/dist/tests/model/ShoppingContext.d.ts.map +1 -1
  30. package/dist/tests/model/ShoppingContext.js +25 -33
  31. package/dist/tests/model/ShoppingContext.js.map +1 -1
  32. package/dist/tests/security/tests/place-order.js +4 -4
  33. package/dist/tests/security/tests/place-order.js.map +1 -1
  34. package/dist/tsconfig.tsbuildinfo +1 -1
  35. package/package.json +1 -1
  36. package/src/compiler/ISqlHelpers.ts +1 -1
  37. package/src/compiler/postgres/PostgreSqlMethodTransformer.ts +15 -7
  38. package/src/compiler/sql-server/SqlServerSqlMethodTransformer.ts +17 -8
  39. package/src/decorators/Relate.ts +37 -0
  40. package/src/di/di.ts +6 -6
  41. package/src/query/ast/ExpressionToSql.ts +1 -2
  42. package/src/query/ast/IStringTransformer.ts +4 -2
  43. package/src/sql/Sql.ts +2 -1
  44. package/src/tests/model/ShoppingContext.ts +25 -33
  45. package/src/tests/security/tests/place-order.ts +4 -4
@@ -1,8 +1,11 @@
1
1
 
2
- import { prepare, prepareAny } from "../../query/ast/IStringTransformer.js";
3
- import { ISqlHelpers, flattenHelpers } from "../ISqlHelpers.js";
2
+ import { prepareAny } from "../../query/ast/IStringTransformer.js";
3
+ import Sql from "../../sql/Sql.js";
4
+ import { ISqlHelpers } from "../ISqlHelpers.js";
5
+ import type QueryCompiler from "../QueryCompiler.js";
4
6
 
5
7
  export const SqlServerSqlHelper: ISqlHelpers = {
8
+ ... Sql,
6
9
  in(a, array) {
7
10
  return prepareAny `${a} IN ${array}`;
8
11
  },
@@ -151,12 +154,18 @@ export const SqlServerSqlHelper: ISqlHelpers = {
151
154
  }
152
155
  };
153
156
 
154
- const names = flattenHelpers(SqlServerSqlHelper, "Sql");
157
+ export default function SqlServerSqlMethodTransformer(compiler: QueryCompiler, callee: string[], args: any[]): string {
155
158
 
156
- export default function SqlServerSqlMethodTransformer(callee: string, args: any[]): string {
157
- const name = names[callee];
158
- if (!name) {
159
+ let start = SqlServerSqlHelper;
160
+ for (const iterator of callee) {
161
+ start = start[iterator];
162
+ if (!start) {
163
+ return;
164
+ }
165
+ }
166
+ if (!start) {
159
167
  return;
160
168
  }
161
- return names[callee]?.(... args);
162
- }
169
+ // eslint-disable-next-line @typescript-eslint/ban-types
170
+ return (start as unknown as Function).apply(compiler, args);
171
+ }
@@ -0,0 +1,37 @@
1
+ import type { IClassOf } from "./IClassOf.js";
2
+ import SchemaRegistry from "./SchemaRegistry.js";
3
+ import NameParser from "./parser/NameParser.js";
4
+
5
+
6
+
7
+ export default function Relate<T, TRelated>(c: IClassOf<TRelated>,
8
+ {
9
+ foreignKey: name, inverseProperty: inv, inverseKey: invKey, dotNotCreateIndex
10
+ }: {
11
+
12
+ foreignKey: (item: T) => any;
13
+
14
+ inverseProperty: (item: TRelated) => any;
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
+ };
37
+ }
package/src/di/di.ts CHANGED
@@ -41,12 +41,12 @@ export class ServiceProvider implements IDisposable {
41
41
  this[parentServiceProvider] = parent;
42
42
  }
43
43
 
44
- // add<T1, T extends T1>(type: IAbstractClassOf<T1> | IClassOf<T1>, instance: T) {
45
- // this.getRegistration(type, true);
46
- // this.map.set(type, instance);
47
- // instance[serviceProvider] = this;
48
- // return instance;
49
- // }
44
+ add<T1, T extends T1>(type: IAbstractClassOf<T1> | IClassOf<T1>, instance: T) {
45
+ this.getRegistration(type, true);
46
+ this.map.set(type, instance);
47
+ instance[serviceProvider] = this;
48
+ return instance;
49
+ }
50
50
 
51
51
 
52
52
  createScope() {
@@ -228,9 +228,8 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
228
228
  }
229
229
 
230
230
  if (identifier?.value === "Sql") {
231
- const names = `${identifier.value}.${chain.join(".")}`;
232
231
  const argList = e.arguments.map((x) => this.visit(x));
233
- const transformedCallee = this.compiler.sqlMethodTransformer(names, argList as any[]);
232
+ const transformedCallee = this.compiler.sqlMethodTransformer(this.compiler, chain, argList as any[]);
234
233
  if (transformedCallee) {
235
234
  return prepare `${transformedCallee}`;
236
235
  }
@@ -1,9 +1,11 @@
1
+ import type QueryCompiler from "../../compiler/QueryCompiler.js";
2
+
1
3
  export type ITextQueryFragment = string | ((p: any) => any) | { toString(): string };
2
4
  export type ITextQuery = ITextQueryFragment[];
3
5
 
4
6
  export type IStringTransformer = (s: string) => string;
5
7
 
6
- export type ISqlMethodTransformer = (callee: string, args: string[]) => string;
8
+ export type ISqlMethodTransformer = (compiler: QueryCompiler, callee: string[], args: string[]) => string;
7
9
 
8
10
  export class QueryParameter {
9
11
 
@@ -18,7 +20,7 @@ export class QueryParameter {
18
20
  }
19
21
  }
20
22
 
21
- export const prepareAny = (a: TemplateStringsArray, ... p: any[]): any => {
23
+ export const prepareAny = (a: TemplateStringsArray, ... p: any[]): string[] => {
22
24
  const r = [];
23
25
  for (let index = 0; index < a.length; index++) {
24
26
  const element = a[index];
package/src/sql/Sql.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ISql } from "./ISql.js";
2
2
 
3
- const Sql: ISql = void 0;
3
+ const Sql: ISql = {
4
+ } as any;
4
5
  export default Sql;
@@ -1,6 +1,6 @@
1
1
  import EntityContext from "../../model/EntityContext.js";
2
2
  import Column from "../../decorators/Column.js";
3
- import ForeignKey from "../../decorators/ForeignKey.js";
3
+ import Relate from "../../decorators/Relate.js";
4
4
  import Table from "../../decorators/Table.js";
5
5
 
6
6
  export const statusPublished = "published";
@@ -73,10 +73,9 @@ export class Product {
73
73
 
74
74
  public categories: ProductCategory[];
75
75
 
76
- @ForeignKey({
77
- key: (product) => product.ownerID,
78
- related: User,
79
- relatedProperty: (user) => user.ownedProducts
76
+ @Relate(User, {
77
+ foreignKey: (product) => product.ownerID,
78
+ inverseProperty: (user) => user.ownedProducts
80
79
  })
81
80
  public owner: User;
82
81
 
@@ -95,17 +94,15 @@ export class ProductCategory {
95
94
  @Column({ dataType: "Char", length: 200})
96
95
  public categoryID: string;
97
96
 
98
- @ForeignKey({
99
- key: (pc) => pc.productID,
100
- related: Product,
101
- relatedProperty: (c) => c.categories
97
+ @Relate(Product, {
98
+ foreignKey: (pc) => pc.productID,
99
+ inverseProperty: (c) => c.categories
102
100
  })
103
101
  public product: Product;
104
102
 
105
- @ForeignKey({
106
- key: (pc) => pc.categoryID,
107
- related: Category,
108
- relatedProperty: (c) => c.productCategories
103
+ @Relate(Category, {
104
+ foreignKey: (pc) => pc.categoryID,
105
+ inverseProperty: (c) => c.productCategories
109
106
  })
110
107
  public category: Category;
111
108
  }
@@ -131,10 +128,9 @@ export class ProductPrice {
131
128
  @Column({})
132
129
  public productID: number;
133
130
 
134
- @ForeignKey({
135
- key: (productPrice) => productPrice.productID,
136
- related: Product,
137
- relatedProperty: (product) => product.prices
131
+ @Relate(Product, {
132
+ foreignKey: (productPrice) => productPrice.productID,
133
+ inverseProperty: (product) => product.prices
138
134
  })
139
135
  public product: Product;
140
136
 
@@ -155,10 +151,9 @@ export class Order {
155
151
 
156
152
  public orderItems: OrderItem[];
157
153
 
158
- @ForeignKey({
159
- key: (order) => order.customerID,
160
- related: User,
161
- relatedProperty: (user) => user.orders
154
+ @Relate(User, {
155
+ foreignKey: (order) => order.customerID,
156
+ inverseProperty: (user) => user.orders
162
157
  })
163
158
  public customer: User;
164
159
 
@@ -182,25 +177,22 @@ export class OrderItem {
182
177
  @Column()
183
178
  public amount: number;
184
179
 
185
- @ForeignKey({
186
- key: (orderItem) => orderItem.orderID,
187
- related: Order,
188
- relatedProperty: (order) => order.orderItems
180
+ @Relate(Order, {
181
+ foreignKey: (orderItem) => orderItem.orderID,
182
+ inverseProperty: (order) => order.orderItems
189
183
  })
190
184
  public order: Order;
191
185
 
192
- @ForeignKey({
193
- key: (orderItem) => orderItem.productID,
194
- related: Product,
195
- relatedProperty:(product) => product.orderItems
186
+ @Relate(Product, {
187
+ foreignKey: (orderItem) => orderItem.productID,
188
+ inverseProperty:(product) => product.orderItems
196
189
  })
197
190
  public product: Product;
198
191
 
199
192
 
200
- @ForeignKey({
201
- key: (orderItem) => orderItem.priceID,
202
- related: ProductPrice,
203
- relatedProperty: (productPrice) => productPrice.orderItems
193
+ @Relate(ProductPrice, {
194
+ foreignKey: (orderItem) => orderItem.priceID,
195
+ inverseProperty: (productPrice) => productPrice.orderItems
204
196
  })
205
197
  public productPrice: ProductPrice;
206
198
 
@@ -32,8 +32,8 @@ async function getNewOrders(this: TestConfig) {
32
32
  const user = new UserInfo();
33
33
  user.userID = 2;
34
34
  ServiceCollection.register("Singleton", Logger, () => Logger.instance);
35
- ServiceCollection.register("Singleton", BaseDriver, () => this.driver);
36
- ServiceCollection.register("Scoped", UserInfo, () => user);
35
+ scope.add(BaseDriver, this.driver);
36
+ scope.add(UserInfo, user);
37
37
  ServiceCollection.register("Singleton", ContextEvents, () => new ShoppingContextEvents());
38
38
  const context = scope.create(ShoppingContext);
39
39
  context.verifyFilters = true;
@@ -54,8 +54,8 @@ async function addNewOrder(this: TestConfig, customer: User, userID?) {
54
54
  const user = new UserInfo();
55
55
  user.userID = userID ?? customer.userID;
56
56
  ServiceCollection.register("Singleton", Logger, () => Logger.instance);
57
- ServiceCollection.register("Singleton", BaseDriver, () => this.driver);
58
- ServiceCollection.register("Scoped", UserInfo, () => user);
57
+ scope.add(BaseDriver, this.driver);
58
+ scope.add(UserInfo, user);
59
59
  ServiceCollection.register("Singleton", ContextEvents, () => new ShoppingContextEvents());
60
60
  const context = scope.create(ShoppingContext);
61
61
  context.verifyFilters = true;