@sap/cds 1.15.1 → 1.18.2

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/xsjs-cds.js CHANGED
@@ -1,248 +1,258 @@
1
- var cds = require("./cds");
2
- var origExecFct = cds.queries.Query.prototype.$execute;
1
+ /* eslint-disable prefer-rest-params */
2
+ const cds = require('./cds');
3
+ const origExecFct = cds.queries.Query.prototype.$execute;
3
4
 
4
5
  exports.init = function(tx) {
6
+ function resolveEntityReferences(obj) {
7
+ if (typeof obj === 'object' || typeof obj === 'function') {
8
+ if (obj.$_metadata) {
9
+ return obj.$_metadata.entityName;
10
+ }
11
+ const result = {};
12
+ for (const p in obj) {
13
+ result[p] = resolveEntityReferences(obj[p]);
14
+ }
15
+ return result;
16
+ } else {
17
+ return obj;
18
+ }
19
+ }
5
20
 
6
- function resolveEntityReferences(obj) {
7
- if (typeof obj === 'object' || typeof obj === 'function') {
8
- if (obj.$_metadata) {
9
- return obj.$_metadata.entityName;
10
- }
11
- var result = {};
12
- for (var p in obj) {
13
- result[p] = resolveEntityReferences(obj[p]);
14
- }
15
- return result;
16
- } else {
17
- return obj;
18
- }
21
+ function adaptImport(importSpec) {
22
+ const result = {
23
+ $entity: `${importSpec[0] }::${ importSpec[1]}`,
24
+ $fields: resolveEntityReferences(importSpec[2]),
25
+ $options: importSpec[3],
26
+ };
27
+ if (importSpec[3] && importSpec[3].$entityName) {
28
+ result.$name = importSpec[3].$entityName;
29
+ }
30
+ if (importSpec[3] && importSpec[3].$viaTable) {
31
+ result.$viaEntity = importSpec[3].$viaTable;
32
+ }
33
+ return [result];
34
+ }
35
+
36
+ function adaptDefine(importSpec) {
37
+ const matches = /"(\w+)"\."([\w.:]+)"/g.exec(importSpec[1]);
38
+ const result = {
39
+ $name: importSpec[0],
40
+ $table: matches[2],
41
+ $schema: matches[1],
42
+ $fields: resolveEntityReferences(importSpec[2]),
43
+ $options: importSpec[3],
44
+ };
45
+ return [result];
46
+ }
47
+
48
+ const wrapperRepo = {};
49
+
50
+ function makeSkeleton(values, mapping) {
51
+ let res;
52
+ let nextMapping;
53
+ for (const p in mapping) {
54
+ if (p.substring(0, 1) === '$') {
55
+ continue;
56
+ }
57
+ if (!res) {
58
+ res = {};
59
+ }
60
+ if (values[p] && (values[p].$__entity || values[p].$_entity)) {
61
+ res[p] = values[p];
62
+ } else if (Object.prototype.toString.call(values[p]) === '[object Array]') {
63
+ nextMapping = mapping[p].$association ?
64
+ mapping[p].$association.$class.$_mapping : mapping[p];
65
+ res[p] = values[p].map(function(e) {
66
+ if (e.$__entity || e.$_entity) {
67
+ return e;
68
+ } else {
69
+ return makeSkeleton(e, nextMapping);
70
+ }
71
+ });
72
+ } else if (Object.prototype.toString.call(values[p]) === '[object Object]') {
73
+ nextMapping = mapping[p].$association ?
74
+ mapping[p].$association.$class.$_mapping : mapping[p];
75
+ res[p] = makeSkeleton(values[p], nextMapping);
76
+ } else if (typeof values[p] !== 'undefined') {
77
+ res[p] = values[p];
78
+ } else if (
79
+ mapping[p].$association &&
80
+ (mapping[p].$viaBacklink || mapping[p].$viaEntity)
81
+ ) {
82
+ res[p] = [];
83
+ } else if (mapping[p].$association) {
84
+ res[p] = null;
85
+ } else {
86
+ res[p] = makeSkeleton({}, mapping[p]);
87
+ }
19
88
  }
89
+ return res;
90
+ }
20
91
 
21
- function adaptImport(importSpec) {
22
- var result = {
23
- $entity: importSpec[0] + "::" + importSpec[1],
24
- $fields: resolveEntityReferences(importSpec[2]),
25
- $options: importSpec[3]
26
- };
27
- if (importSpec[3] && importSpec[3].$entityName) {
28
- result.$name = importSpec[3].$entityName;
92
+ function getWrapper(name, entity) {
93
+ if (!wrapperRepo[name]) {
94
+ const result = function(skeleton) { // constructor
95
+ const e = entity;
96
+ if (!skeleton.$__entity) {
97
+ skeleton = makeSkeleton(skeleton, e.$_mapping);
29
98
  }
30
- if (importSpec[3] && importSpec[3].$viaTable) {
31
- result.$viaEntity = importSpec[3].$viaTable;
99
+ Object.defineProperty(skeleton, '$save',
100
+ {
101
+ value: function() {
102
+ return tx.$save.sync(entity.$prepare(this));
103
+ },
104
+ enumerable: false,
105
+ configurable: true,
106
+ });
107
+ Object.defineProperty(skeleton, '$persist',
108
+ {
109
+ value: function() {
110
+ return tx.$save.sync(entity.$prepare(this));
111
+ },
112
+ enumerable: false,
113
+ configurable: true,
114
+ });
115
+ Object.defineProperty(skeleton, '$discard',
116
+ {
117
+ value: function() {
118
+ return tx.$discard.sync(entity.$prepare(this));
119
+ },
120
+ enumerable: false,
121
+ configurable: true,
122
+ });
123
+ return skeleton;
124
+ };
125
+
126
+ for (const p in entity) {
127
+ result[p] = entity[p];
128
+ }
129
+ cds.queries.addExpressionFunctions(entity, result);
130
+
131
+ result.$findAll = function(cond) {
132
+ if (arguments.length === 0) {
133
+ cond = {};
32
134
  }
33
- return [result];
34
- }
135
+ const r = entity.$findAll.sync.call(entity, cond);
136
+ return r;
137
+ };
35
138
 
36
- function adaptDefine(importSpec) {
37
- var matches = /"(\w+)"\."([\w.:]+)"/g.exec(importSpec[1]);
38
- var result = {
39
- $name: importSpec[0],
40
- $table: matches[2],
41
- $schema: matches[1],
42
- $fields: resolveEntityReferences(importSpec[2]),
43
- $options: importSpec[3]
44
- };
45
- return [result];
46
- }
139
+ result.$saveAll = function(instances) {
140
+ tx.$save(instances.map(function(inst) {
141
+ return entity.$prepare(inst);
142
+ }));
143
+ };
144
+
145
+ result.$discardAll = function(instances) {
146
+ tx.$discard(instances.map(function(inst) {
147
+ return entity.$prepare(inst);
148
+ }));
149
+ };
150
+
151
+ result.$persistAll = result.$saveAll;
47
152
 
48
- var wrapperRepo = {};
49
-
50
- function makeSkeleton(values, mapping) {
51
- var res;
52
- for (var p in mapping) {
53
- if (p.substring(0, 1) === '$') continue;
54
- if (!res) res = {};
55
- if (values[p] && (values[p].$__entity || values[p].$_entity)) {
56
- res[p] = values[p];
57
- } else if (Object.prototype.toString.call(values[p]) === "[object Array]") {
58
- var nextMapping = mapping[p].$association ? mapping[p].$association.$class.$_mapping : mapping[p];
59
- res[p] = values[p].map(function (e) {
60
- if (e.$__entity || e.$_entity) {
61
- return e;
62
- } else {
63
- return makeSkeleton(e, nextMapping);
64
- }
65
- });
66
- } else if (Object.prototype.toString.call(values[p]) === "[object Object]") {
67
- var nextMapping = mapping[p].$association ? mapping[p].$association.$class.$_mapping : mapping[p];
68
- res[p] = makeSkeleton(values[p], nextMapping);
69
- } else if (typeof values[p] !== 'undefined') {
70
- res[p] = values[p];
71
- } else if (mapping[p].$association && (mapping[p].$viaBacklink || mapping[p].$viaEntity)) {
72
- res[p] = [];
73
- } else if (mapping[p].$association) {
74
- res[p] = null;
75
- } else {
76
- res[p] = makeSkeleton({}, mapping[p]);
77
- }
153
+ result.$find = function(cond) {
154
+ if (arguments.length === 0) {
155
+ cond = {};
78
156
  }
79
- return res;
80
- }
157
+ const r = entity.$findAll.sync.call(entity, cond)[0];
158
+ return r;
159
+ };
81
160
 
82
- function getWrapper(name, entity) {
83
- if (!wrapperRepo[name]) {
84
- var result = function (skeleton) { // constructor
85
- var e = entity;
86
- if (!skeleton.$__entity) {
87
- skeleton = makeSkeleton(skeleton, e.$_mapping);
88
- }
89
- Object.defineProperty(skeleton, '$save',
90
- {
91
- value: function () {
92
- return tx.$save.sync(entity.$prepare(this));
93
- },
94
- enumerable: false,
95
- configurable: true
96
- });
97
- Object.defineProperty(skeleton, '$persist',
98
- {
99
- value: function () {
100
- return tx.$save.sync(entity.$prepare(this));
101
- },
102
- enumerable: false,
103
- configurable: true
104
- });
105
- Object.defineProperty(skeleton, '$discard',
106
- {
107
- value: function () {
108
- return tx.$discard.sync(entity.$prepare(this));
109
- },
110
- enumerable: false,
111
- configurable: true
112
- });
113
- return skeleton;
114
- };
115
-
116
- for (var p in entity) {
117
- result[p] = entity[p];
118
- }
119
- cds.queries.addExpressionFunctions(entity, result);
120
-
121
- result.$findAll = function (cond) {
122
- if (arguments.length === 0) {
123
- cond = {};
124
- }
125
- var r = entity.$findAll.sync.call(entity, cond);
126
- return r;
127
- };
128
-
129
- result.$saveAll = function (instances) {
130
- tx.$save(instances.map(function (inst) {
131
- return entity.$prepare(inst);
132
- }));
133
- };
134
-
135
- result.$discardAll = function (instances) {
136
- tx.$discard(instances.map(function (inst) {
137
- return entity.$prepare(inst);
138
- }));
139
- };
140
-
141
- result.$persistAll = result.$saveAll;
142
-
143
- result.$find = function (cond) {
144
- if (arguments.length === 0) {
145
- cond = {};
146
- }
147
- var r = entity.$findAll.sync.call(entity, cond)[0];
148
- return r;
149
- };
150
-
151
- result.$get = function (cond) {
152
- if (arguments.length === 0) {
153
- cond = {};
154
- }
155
- var r = entity.$get.sync.call(entity, cond);
156
- return r;
157
- };
158
-
159
- result.$query = function () {
160
- var q = entity.$query();
161
- q.syncExecute = true;
162
- return q;
163
- }
164
-
165
- wrapperRepo[name] = result;
161
+ result.$get = function(cond) {
162
+ if (arguments.length === 0) {
163
+ cond = {};
166
164
  }
167
- return wrapperRepo[name];
168
- }
165
+ const r = entity.$get.sync.call(entity, cond);
166
+ return r;
167
+ };
168
+
169
+ result.$query = function() {
170
+ const q = entity.$query();
171
+ q.syncExecute = true;
172
+ return q;
173
+ };
169
174
 
170
- var retrieveEntity = function (nodeImport) {
171
- var alias = nodeImport[0].$name;
172
- var entityName = nodeImport[0].$entity;
173
- var imp = cds.$importEntities.sync(nodeImport);
174
- var entity = imp[alias || entityName];
175
- var name = entity.$_metadata.entityName;
176
- var wrapped = getWrapper(name, entity);
177
- return wrapped;
175
+ wrapperRepo[name] = result;
178
176
  }
177
+ return wrapperRepo[name];
178
+ }
179
179
 
180
- exports.importEntity = function () {
181
- var args = Array.prototype.slice.call(arguments);
182
- return retrieveEntity.call(this, adaptImport(args));
183
- };
180
+ const retrieveEntity = function(nodeImport) {
181
+ const alias = nodeImport[0].$name;
182
+ const entityName = nodeImport[0].$entity;
183
+ const imp = cds.$importEntities.sync(nodeImport);
184
+ const entity = imp[alias || entityName];
185
+ const name = entity.$_metadata.entityName;
186
+ const wrapped = getWrapper(name, entity);
187
+ return wrapped;
188
+ };
184
189
 
185
- exports.$importEntity = exports.importEntity;
190
+ exports.importEntity = function() {
191
+ const args = Array.prototype.slice.call(arguments);
192
+ return retrieveEntity.call(this, adaptImport(args));
193
+ };
186
194
 
187
- exports.defineEntity = function () {
188
- var args = Array.prototype.slice.call(arguments);
189
- return retrieveEntity.call(this, adaptDefine(args));
190
- };
195
+ exports.$importEntity = exports.importEntity;
191
196
 
192
- exports.getEntity = function (name) {
193
- return getWrapper(name, cds.$getEntitySync(name));
194
- };
197
+ exports.defineEntity = function() {
198
+ const args = Array.prototype.slice.call(arguments);
199
+ return retrieveEntity.call(this, adaptDefine(args));
200
+ };
195
201
 
196
- cds.queries.Query.prototype.$execute = function () {
197
- if (!this.syncExecute) {
198
- return origExecFct.apply(this, arguments);
199
- } else {
200
- var args = Array.prototype.slice.call(arguments);
201
- if (args.length === 0) {
202
- args = [{}];
203
- }
204
- return origExecFct.sync.apply(this, args);
205
- }
202
+ exports.getEntity = function(name) {
203
+ return getWrapper(name, cds.$getEntitySync(name));
204
+ };
205
+
206
+ cds.queries.Query.prototype.$execute = function() {
207
+ if (!this.syncExecute) {
208
+ return origExecFct.apply(this, arguments);
209
+ } else {
210
+ let args = Array.prototype.slice.call(arguments);
211
+ if (args.length === 0) {
212
+ args = [{}];
213
+ }
214
+ return origExecFct.sync.apply(this, args);
206
215
  }
216
+ };
207
217
 
208
- exports.cdsAsync = cds;
209
- exports.Manager = {
210
- clearCache: function () {
211
- cds._clearCaches();
212
- },
213
- resetCaches: function () {
214
- cds._clearCaches();
215
- }
216
- };
218
+ exports.cdsAsync = cds;
219
+ exports.Manager = {
220
+ clearCache: function() {
221
+ cds._clearCaches();
222
+ },
223
+ resetCaches: function() {
224
+ cds._clearCaches();
225
+ },
226
+ };
217
227
 
218
- exports.Transaction = {
219
- $commit: function (callback) {
220
- tx.$commit(function(err) {
221
- callback(err);
222
- });
223
- },
228
+ exports.Transaction = {
229
+ $commit: function(callback) {
230
+ tx.$commit(function(err) {
231
+ callback(err);
232
+ });
233
+ },
224
234
 
225
- $close: function () {
226
- tx.$close();
227
- }
228
- }
235
+ $close: function() {
236
+ tx.$close();
237
+ },
238
+ };
229
239
 
230
- exports.Rename = {
231
- $lowercase: "lowercase"
232
- };
240
+ exports.Rename = {
241
+ $lowercase: 'lowercase',
242
+ };
233
243
 
234
- exports.SqlQuery = cds.queries;
235
- exports.Query = cds.createQuery;
244
+ exports.SqlQuery = cds.queries;
245
+ exports.Query = cds.createQuery;
236
246
 
237
- cds.extensionPoints.instanceMethods = function (instance) {
238
- instance.$save = function() {
239
- tx.$save(instance);
240
- }
241
- instance.$persist = instance.$save;
242
- instance.$discard = function() {
243
- tx.$discard(instance);
244
- }
245
- return instance;
247
+ cds.extensionPoints.instanceMethods = function(instance) {
248
+ instance.$save = function() {
249
+ tx.$save(instance);
250
+ };
251
+ instance.$persist = instance.$save;
252
+ instance.$discard = function() {
253
+ tx.$discard(instance);
246
254
  };
247
- return exports;
248
- }
255
+ return instance;
256
+ };
257
+ return exports;
258
+ };
package/.project DELETED
@@ -1,11 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <projectDescription>
3
- <name>node-cds</name>
4
- <comment></comment>
5
- <projects>
6
- </projects>
7
- <buildSpec>
8
- </buildSpec>
9
- <natures>
10
- </natures>
11
- </projectDescription>