@entity-access/entity-access 1.0.323 → 1.0.325

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.
@@ -32,14 +32,15 @@ export default class SqlServerDriver extends BaseDriver {
32
32
  const values = [];
33
33
  let returning = "";
34
34
  let i = 1;
35
+ const { quote } = this.compiler;
35
36
  for (const iterator of type.columns) {
36
37
  if (iterator.generated || iterator.computed) {
37
38
  if (returning) {
38
- returning += ",";
39
+ returning += ",\r\n\t\t";
39
40
  } else {
40
41
  returning = "OUTPUT ";
41
42
  }
42
- returning += "INSERTED." + iterator.columnName + " as " + this.compiler.quote(iterator.name);
43
+ returning += "INSERTED." + iterator.columnName + " as " + quote(iterator.name);
43
44
  continue;
44
45
  }
45
46
  const field = iterator.columnName;
@@ -49,8 +50,8 @@ export default class SqlServerDriver extends BaseDriver {
49
50
  }
50
51
  values.push(value);
51
52
  if (fields) {
52
- fields += ",";
53
- valueParams += ",";
53
+ fields += ",\r\n\t\t";
54
+ valueParams += ",\r\n\t\t";
54
55
  }
55
56
  fields += field;
56
57
  valueParams += `$${i++}`;
@@ -65,40 +65,69 @@ export class EntityStatements<T = any> {
65
65
  this.context = source[contextSymbol];
66
66
  }
67
67
 
68
- async select(keys: Partial<T>, loadChangeEntry = false): Promise<T> {
69
- const q = this.context.driver.selectQueryWithKeys(this.model, keys);
68
+ async select(entity: Partial<T>, loadChangeEntry = false): Promise<T> {
69
+ const q = this.context.driver.selectQueryWithKeys(this.model, entity);
70
70
  const r = await this.context.connection.executeQuery(q);
71
71
  const result = r.rows[0];
72
+ if (result) {
73
+ for (const key in result) {
74
+ if (Object.prototype.hasOwnProperty.call(result, key)) {
75
+ const element = result[key];
76
+ entity[key] = element;
77
+ }
78
+ }
79
+ }
72
80
  if (loadChangeEntry) {
73
- const ce = this.context.changeSet.getEntry(result);
74
- ce.apply(result);
75
- return ce.entity;
81
+ const ce = this.context.changeSet.getEntry(entity, entity);
82
+ return ce.entity as T;
76
83
  }
77
- return result;
84
+ return entity as T;
78
85
  }
79
86
 
80
87
  async insert(entity: Partial<T>, loadChangeEntry = false): Promise<T> {
81
88
  const q = this.context.driver.insertQuery(this.model, entity);
89
+ // console.log(q.text);
82
90
  const r = await this.context.connection.executeQuery(q);
83
91
  const result = r.rows[0];
92
+ if (result) {
93
+ for (const key in result) {
94
+ if (Object.prototype.hasOwnProperty.call(result, key)) {
95
+ const element = result[key];
96
+ entity[key] = element;
97
+ }
98
+ }
99
+ }
84
100
  if (loadChangeEntry) {
85
- const ce = this.context.changeSet.getEntry(result);
86
- ce.apply(result);
87
- return ce.entity;
101
+ const ce = this.context.changeSet.getEntry(entity, entity);
102
+ return ce.entity as any;
88
103
  }
89
- return r.rows[0];
104
+ return entity as any;
90
105
  }
91
106
 
92
107
  async update(entity: Partial<T>, loadChangeEntry = false): Promise<T> {
93
- const q = this.context.driver.updateQuery(this.model, entity);
94
- const r = await this.context.connection.executeQuery(q);
95
- const result = r.rows?.[0];
108
+ const { context } = this;
109
+ const { driver } = context;
110
+ const q = driver.updateQuery(this.model, entity);
111
+ // console.log(q.text);
112
+ const r = await context.connection.executeQuery(q);
113
+ if (!r.updated) {
114
+ return void 0;
115
+ }
116
+ const result = r.rows[0];
117
+ if (result) {
118
+ for (const key in result) {
119
+ if (Object.prototype.hasOwnProperty.call(result, key)) {
120
+ const element = result[key];
121
+ entity[key] = element;
122
+ }
123
+ }
124
+ }
96
125
  if (loadChangeEntry) {
97
- const ce = this.context.changeSet.getEntry(result);
98
- ce.apply(result ?? {});
99
- return ce.entity;
126
+ const ce = this.context.changeSet.getEntry(entity);
127
+ ce.apply(entity ?? {});
128
+ return ce.entity as any;
100
129
  }
101
- return r.rows[0];
130
+ return entity as any;
102
131
  }
103
132
 
104
133
  async selectOrInsert(entity: Partial<T>, retry = 3): Promise<T> {
@@ -128,7 +157,7 @@ export class EntityStatements<T = any> {
128
157
  }
129
158
  }
130
159
 
131
- async upsert(entity: Partial<T>, updateAfterSelect: (x:T) => T, retry = 3): Promise<T> {
160
+ async upsert(entity: Partial<T>, updateAfterSelect?: (x:T) => T, retry = 3): Promise<T> {
132
161
 
133
162
  const tx = this.context.connection.currentTransaction;
134
163
  let tid: string;
@@ -142,13 +171,11 @@ export class EntityStatements<T = any> {
142
171
  let existing = await this.select(entity) as T;
143
172
  if (existing) {
144
173
  existing = updateAfterSelect(existing);
145
- for (const key in entity) {
146
- if (Object.prototype.hasOwnProperty.call(entity, key)) {
147
- const element = entity[key];
148
- existing[key] = element;
174
+ for (const key in existing) {
175
+ if (Object.prototype.hasOwnProperty.call(existing, key)) {
176
+ entity[key] = existing[key];
149
177
  }
150
178
  }
151
- entity = existing;
152
179
  }
153
180
  }
154
181
  const r = await this.update(entity);
@@ -230,9 +230,11 @@ export default class WorkflowStorage {
230
230
  state.taskGroup ||= "default";
231
231
  // await db.saveChanges();
232
232
  if(state[loadedFromDb]) {
233
- await db.workflows.saveDirect({ mode: "update", changes: state });
233
+ // await db.workflows.saveDirect({ mode: "update", changes: state });
234
+ await db.workflows.statements.update(state);
234
235
  } else {
235
- await db.workflows.saveDirect({ mode: "upsert", changes: state });
236
+ // await db.workflows.saveDirect({ mode: "upsert", changes: state });
237
+ await db.workflows.statements.upsert(state);
236
238
  }
237
239
  await tx.commit();
238
240
  }