@strapi/upgrade 5.0.0-beta.3 → 5.0.0-beta.5

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.
@@ -142,12 +142,39 @@ const objectParam_10 = [...objectParam_10_2];
142
142
 
143
143
  strapi.entityService.findOne(...[...objectParam_10]);
144
144
 
145
+ Case: find, create, update, delete with entityId as first argument
146
+
147
+ strapi.entityService.findMany(uid, {
148
+ fields: ["id", "name", "description"],
149
+ populate: ["author", "comments"],
150
+ publicationState: "preview",
151
+ });
152
+
153
+ strapi.entityService.create(uid, {
154
+ data: {
155
+ name: "John Doe",
156
+ age: 30,
157
+ },
158
+ });
159
+
160
+ strapi.entityService.update(uid, entityId, {
161
+ data: {
162
+ name: "John Doe",
163
+ age: 30,
164
+ },
165
+ });
166
+
167
+ strapi.entityService.delete(uid, entityId);
168
+ strapi.entityService.findOne(uid, entityId);
169
+
145
170
  */
146
171
 
147
- const movedFunctions = ['findOne', 'find', 'count', 'create', 'update', 'delete'];
172
+ const movedFunctions = ['findOne', 'findMany', 'count', 'create', 'update', 'delete'];
173
+
174
+ const functionsWithEntityId = ['findOne', 'update', 'delete'];
148
175
 
149
176
  const transformDeclaration = (path: ASTPath<any>, name: any, j: JSCodeshift) => {
150
- const declaration = findClosesDeclaration(path, name, j);
177
+ const declaration = findClosestDeclaration(path, name, j);
151
178
 
152
179
  if (!declaration) {
153
180
  return;
@@ -222,7 +249,7 @@ const transformObjectParam = (path: ASTPath<any>, expression: ObjectExpression,
222
249
  break;
223
250
  }
224
251
  case j.Identifier.check(prop.value): {
225
- const declaration = findClosesDeclaration(path, prop.value.name, j);
252
+ const declaration = findClosestDeclaration(path, prop.value.name, j);
226
253
 
227
254
  if (!declaration) {
228
255
  return;
@@ -253,7 +280,7 @@ const transformObjectParam = (path: ASTPath<any>, expression: ObjectExpression,
253
280
  });
254
281
  };
255
282
 
256
- const findClosesDeclaration = (path: ASTPath<any>, name: string, j) => {
283
+ const findClosestDeclaration = (path: ASTPath<any>, name: string, j) => {
257
284
  // find Identifier declaration
258
285
  const scope = path.scope.lookup(name);
259
286
 
@@ -318,7 +345,7 @@ const transform: Transform = (file, api) => {
318
345
  case j.Identifier.check(arg.argument): {
319
346
  const identifier = arg.argument;
320
347
 
321
- const declaration = findClosesDeclaration(path, identifier.name, j);
348
+ const declaration = findClosestDeclaration(path, identifier.name, j);
322
349
 
323
350
  if (!declaration) {
324
351
  return arg;
@@ -351,6 +378,42 @@ const transform: Transform = (file, api) => {
351
378
 
352
379
  const [docUID, ...rest] = resolvedArgs;
353
380
 
381
+ // function with entityId as first argument
382
+ if (
383
+ j.Identifier.check(path.value.callee.property) &&
384
+ functionsWithEntityId.includes(path.value.callee.property.name)
385
+ ) {
386
+ rest.splice(0, 1);
387
+
388
+ // in case no extra params are passed in the function e.g delete(uid, entityId)
389
+ if (rest.length === 0) {
390
+ rest.push(
391
+ j.objectExpression.from({
392
+ properties: [],
393
+ })
394
+ );
395
+ }
396
+
397
+ const params = rest[0];
398
+
399
+ const placeholder = j.objectProperty(j.identifier('documentId'), j.literal('__TODO__'));
400
+
401
+ // add documentId to params with a placeholder
402
+ if (j.ObjectExpression.check(params)) {
403
+ params.properties.unshift(placeholder);
404
+ } else if (j.Identifier.check(params)) {
405
+ const declaration = findClosestDeclaration(path, params.name, j);
406
+
407
+ if (!declaration) {
408
+ return;
409
+ }
410
+
411
+ if (j.ObjectExpression.check(declaration.init)) {
412
+ declaration.init.properties.unshift(placeholder);
413
+ }
414
+ }
415
+ }
416
+
354
417
  path.value.arguments.forEach((arg) => {
355
418
  transformElement(path, arg, j);
356
419
  });