@entity-access/entity-access 1.0.198 → 1.0.200

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.
@@ -693,8 +693,10 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
693
693
  return join.as;
694
694
  }
695
695
  const joinType = select.preferLeftJoins ? "LEFT" : (fkColumn.nullable ? "LEFT" : "INNER");
696
- const joinParameter = ParameterExpression.create({ name: relation.relatedEntity.name[0]});
697
- joinParameter.model = relation.relatedEntity;
696
+ const joinParameter = ParameterExpression.create({
697
+ name: relation.relatedEntity.name[0],
698
+ model: relation.relatedEntity
699
+ });
698
700
  join = JoinExpression.create({
699
701
  as: joinParameter,
700
702
  joinType,
@@ -779,79 +781,79 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
779
781
  // };
780
782
  }
781
783
 
782
- private flatten(pc: IPropertyChain) : IPropertyChain {
783
-
784
- if (!pc) {
785
- return pc;
786
- }
787
-
788
- // check if we have parameter..
789
- let { parameter } = pc;
790
- if (!parameter) {
791
- return pc;
792
- }
793
- if (pc.chain.length <= 1) {
794
- return pc;
795
- }
796
-
797
- const chain = [ ... pc.chain];
798
-
799
- const scope = this.scope.get(parameter);
800
- const select = scope?.selectStatement ?? this.source?.selectStatement;
801
- if (!select) {
802
- return pc;
803
- }
804
- let type = select.model;
805
-
806
- select.joins ??= [];
807
-
808
- while(chain.length > 1) {
809
- const property = chain.shift();
810
- const propertyInfo = type.getProperty(property);
811
- if (!propertyInfo.relation || propertyInfo.relation.isInverseRelation) {
812
- return pc;
813
- }
814
-
815
- const relation = propertyInfo.relation;
816
- // check if relation is optional...
817
- if (!relation.fkColumn) {
818
- return pc;
819
- }
820
-
821
- const { fkColumn } = relation;
822
-
823
- const join = select.joins.find((x) => x.model === relation.relatedEntity);
824
- if (!join) {
825
- const joinType = fkColumn.nullable ? "LEFT" : "INNER";
826
- const joinParameter = ParameterExpression.create({ name: relation.relatedEntity.name[0]});
827
- joinParameter.model = relation.relatedEntity;
828
- select.joins.push(JoinExpression.create({
829
- as: joinParameter,
830
- joinType,
831
- model: joinParameter.model,
832
- source: Expression.identifier(relation.relatedEntity.name),
833
- where: Expression.equal(
834
- Expression.member(parameter, fkColumn.columnName),
835
- Expression.member(joinParameter, relation.relatedEntity.keys[0].columnName)
836
- )
837
- }));
838
- parameter = joinParameter;
839
- type = relation.relatedEntity;
840
- this.scope.create({ parameter, model: type, selectStatement: select });
841
- pc.parameter = parameter;
842
- pc.chain = [ ... chain ];
843
- } else {
844
- // we will add parameter in scope in case if it is not there
845
- // this is the case when query is composed over already existing
846
- // there is still an error on this one...
847
- pc.parameter = parameter = join.as as ParameterExpression;
848
- type = join.model;
849
- pc.chain = [... chain];
850
- this.scope.create({ parameter, model: type, selectStatement: select });
851
- }
852
- }
853
-
854
- return pc;
855
- }
784
+ // private flatten(pc: IPropertyChain) : IPropertyChain {
785
+
786
+ // if (!pc) {
787
+ // return pc;
788
+ // }
789
+
790
+ // // check if we have parameter..
791
+ // let { parameter } = pc;
792
+ // if (!parameter) {
793
+ // return pc;
794
+ // }
795
+ // if (pc.chain.length <= 1) {
796
+ // return pc;
797
+ // }
798
+
799
+ // const chain = [ ... pc.chain];
800
+
801
+ // const scope = this.scope.get(parameter);
802
+ // const select = scope?.selectStatement ?? this.source?.selectStatement;
803
+ // if (!select) {
804
+ // return pc;
805
+ // }
806
+ // let type = select.model;
807
+
808
+ // select.joins ??= [];
809
+
810
+ // while(chain.length > 1) {
811
+ // const property = chain.shift();
812
+ // const propertyInfo = type.getProperty(property);
813
+ // if (!propertyInfo.relation || propertyInfo.relation.isInverseRelation) {
814
+ // return pc;
815
+ // }
816
+
817
+ // const relation = propertyInfo.relation;
818
+ // // check if relation is optional...
819
+ // if (!relation.fkColumn) {
820
+ // return pc;
821
+ // }
822
+
823
+ // const { fkColumn } = relation;
824
+
825
+ // const join = select.joins.find((x) => x.model === relation.relatedEntity);
826
+ // if (!join) {
827
+ // const joinType = fkColumn.nullable ? "LEFT" : "INNER";
828
+ // const joinParameter = ParameterExpression.create({ name: relation.relatedEntity.name[0]});
829
+ // joinParameter.model = relation.relatedEntity;
830
+ // select.joins.push(JoinExpression.create({
831
+ // as: joinParameter,
832
+ // joinType,
833
+ // model: joinParameter.model,
834
+ // source: Expression.identifier(relation.relatedEntity.name),
835
+ // where: Expression.equal(
836
+ // Expression.member(parameter, fkColumn.columnName),
837
+ // Expression.member(joinParameter, relation.relatedEntity.keys[0].columnName)
838
+ // )
839
+ // }));
840
+ // parameter = joinParameter;
841
+ // type = relation.relatedEntity;
842
+ // this.scope.create({ parameter, model: type, selectStatement: select });
843
+ // pc.parameter = parameter;
844
+ // pc.chain = [ ... chain ];
845
+ // } else {
846
+ // // we will add parameter in scope in case if it is not there
847
+ // // this is the case when query is composed over already existing
848
+ // // there is still an error on this one...
849
+ // pc.parameter = parameter = join.as as ParameterExpression;
850
+ // type = join.model;
851
+ // pc.chain = [... chain];
852
+ // this.scope.create({ parameter, model: type, selectStatement: select });
853
+ // }
854
+ // }
855
+
856
+ // return pc;
857
+ // }
856
858
 
857
859
  }
@@ -58,8 +58,8 @@ export abstract class Expression {
58
58
  return Constant.create({ value });
59
59
  }
60
60
 
61
- static parameter(name: string) {
62
- return ParameterExpression.create({ name });
61
+ static parameter(name: string, model?: EntityType) {
62
+ return ParameterExpression.create({ name, model });
63
63
  }
64
64
 
65
65
  static identifier(name: string) {
@@ -70,6 +70,10 @@ export abstract class Expression {
70
70
  return BinaryExpression.create({ left, operator: "AND", right});
71
71
  }
72
72
 
73
+ static logicalOr(left: Expression, right: Expression): BinaryExpression {
74
+ return BinaryExpression.create({ left, operator: "OR", right});
75
+ }
76
+
73
77
  static member(target: Expression, identifier: string |Expression): MemberExpression {
74
78
  return MemberExpression.create({
75
79
  target,
@@ -83,6 +87,14 @@ export abstract class Expression {
83
87
  return BinaryExpression.create({ left, right, operator: "="});
84
88
  }
85
89
 
90
+ static less(left: Expression, right: Expression) {
91
+ return BinaryExpression.create({ left, right, operator: "<"});
92
+ }
93
+
94
+ static lessOrEqual(left: Expression, right: Expression) {
95
+ return BinaryExpression.create({ left, right, operator: "<="});
96
+ }
97
+
86
98
  static assign(left: Expression, right: Expression) {
87
99
  return BinaryExpression.create({ left, right, operator: "="});
88
100
  }
@@ -109,7 +109,7 @@ export class QueryExpander {
109
109
 
110
110
 
111
111
  const joins = (select.joins ??= []);
112
- const joinParameter = Expression.parameter(parent.sourceParameter.name);
112
+ const joinParameter = Expression.parameter(parent.sourceParameter.name, parent.model);
113
113
 
114
114
  // This join has to be INNER JOIN as we are only interested
115
115
  // in the results that matches parent query exactly
@@ -175,7 +175,7 @@ export class QueryExpander {
175
175
  // : existsWhere;
176
176
 
177
177
  const selectJoins = (select.joins ??= []);
178
- const selectJoinParameter = Expression.parameter(parent.sourceParameter.name);
178
+ const selectJoinParameter = Expression.parameter(parent.sourceParameter.name, parent.model);
179
179
 
180
180
  // This join has to be INNER JOIN as we are only interested
181
181
  // in the results that matches parent query exactly
@@ -251,9 +251,17 @@ export default class WorkflowStorage {
251
251
  where: Expression.logicalAnd(Expression.equal(
252
252
  Expression.identifier("id"),
253
253
  Expression.member(px, "id")
254
- ), Expression.equal(
255
- Expression.identifier(lockTokenField),
256
- NullExpression.create({})
254
+ ), Expression.logicalOr(
255
+ Expression.equal(
256
+ Expression.identifier(lockTTLField),
257
+ NullExpression.create({})
258
+ ),
259
+ Expression.lessOrEqual(
260
+ Expression.identifier(lockTTLField),
261
+ CallExpression.create({
262
+ callee: Expression.member(Expression.member(Expression.identifier("Sql"), "date"), "now")
263
+ })
264
+ )
257
265
  ))
258
266
  });
259
267
 
@@ -279,9 +287,13 @@ export default class WorkflowStorage {
279
287
  for (const iterator of items) {
280
288
  // try to acquire lock...
281
289
  iterator.lockToken = uuid;
282
- const r = await q.invoke(db.connection, iterator);
283
- if (r.updated > 0) {
284
- list.push(iterator);
290
+ try {
291
+ const r = await q.invoke(db.connection, iterator);
292
+ if (r.updated > 0) {
293
+ list.push(iterator);
294
+ }
295
+ } catch (error) {
296
+ console.error(error);
285
297
  }
286
298
  }
287
299
  return list;