Skip to content

Commit

Permalink
chore: deprecate entity-service and delegate to document service
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Mar 12, 2024
1 parent 020096d commit 3a67086
Show file tree
Hide file tree
Showing 74 changed files with 1,329 additions and 2,401 deletions.
Expand Up @@ -224,13 +224,10 @@ describe.skip('Relations', () => {
[]
);

const updatedShop2 = await strapi.entityService.findOne(
'api::shop.shop',
shops[1].documentId,
{
populate: populateShop,
}
);
const updatedShop2 = await strapi.db.query('api::shop.shop').findOne({
where: { documentId: shops[1].documentId },
populate: populateShop,
});

// shop2 relations should be unchanged
expect(updatedShop2.products_om).toMatchObject([{ id: id1 }, { id: id2 }]);
Expand Down
58 changes: 27 additions & 31 deletions api-tests/core/content-manager/api/relations.test.api.js
Expand Up @@ -670,9 +670,9 @@ describe.skip('Relations', () => {

const expectedRelations = [{ id: id2 }, { id: id1 }, { id: id3 }];

const updatedShop = await strapi.entityService.findOne('api::shop.shop', id, {
populate: populateShop,
});
const updatedShop = await strapi.db
.query('api::shop.shop')
.findOne({ where: { id }, populate: populateShop });

expect(updatedShop.myCompo.compo_products_mw).toMatchObject(expectedRelations);
expect(updatedShop.products_mm).toMatchObject(expectedRelations);
Expand Down Expand Up @@ -711,9 +711,9 @@ describe.skip('Relations', () => {
},
});

const updatedShop = await strapi.entityService.findOne('api::shop.shop', id, {
populate: populateShop,
});
const updatedShop = await strapi.db
.query('api::shop.shop')
.findOne({ where: { id }, populate: populateShop });

const expectedRelations = [{ id: id3 }, { id: id1 }, { id: id2 }];

Expand Down Expand Up @@ -753,9 +753,9 @@ describe.skip('Relations', () => {
});

const expectedRelations = [{ id: id2 }, { id: id1 }];
const updatedShop = await strapi.entityService.findOne('api::shop.shop', id, {
populate: populateShop,
});
const updatedShop = await strapi.db
.query('api::shop.shop')
.findOne({ where: { id }, populate: populateShop });

expect(updatedShop.myCompo.compo_products_mw).toMatchObject(expectedRelations);
expect(updatedShop.products_mm).toMatchObject(expectedRelations);
Expand Down Expand Up @@ -812,9 +812,9 @@ describe.skip('Relations', () => {
populateShop
);

const updatedShop = await strapi.entityService.findOne('api::shop.shop', id, {
populate: populateShop,
});
const updatedShop = await strapi.db
.query('api::shop.shop')
.findOne({ where: { id }, populate: populateShop });

expect(updatedShop.myCompo.compo_products_mw).toMatchObject([]);
expect(updatedShop.myCompo.compo_products_ow).toBe(null);
Expand Down Expand Up @@ -868,9 +868,9 @@ describe.skip('Relations', () => {
populateShop
);

const updatedShop = await strapi.entityService.findOne('api::shop.shop', id, {
populate: populateShop,
});
const updatedShop = await strapi.db
.query('api::shop.shop')
.findOne({ where: { id }, populate: populateShop });

expect(updatedShop.myCompo.compo_products_mw).toMatchObject([{ id: id1 }]);
expect(updatedShop.myCompo.compo_products_ow).toMatchObject({ id: id1 });
Expand Down Expand Up @@ -950,9 +950,9 @@ describe.skip('Relations', () => {

expect(name).toBe('Cazotte Shop 2');

const clonedShop = await strapi.entityService.findOne('api::shop.shop', id, {
populate: populateShop,
});
const clonedShop = await strapi.db
.query('api::shop.shop')
.findOne({ where: { id }, populate: populateShop });

expect(clonedShop.myCompo.compo_products_mw).toMatchObject([{ id: id1 }, { id: id2 }]);
expect(clonedShop.myCompo.compo_products_ow).toMatchObject({ id: id2 });
Expand Down Expand Up @@ -1000,9 +1000,9 @@ describe.skip('Relations', () => {

expect(name).toBe('Cazotte Shop 2');

const clonedShop = await strapi.entityService.findOne('api::shop.shop', id, {
populate: populateShop,
});
const clonedShop = await strapi.db
.query('api::shop.shop')
.findOne({ where: { id }, populate: populateShop });

expect(clonedShop.myCompo.compo_products_mw).toMatchObject([{ id: id2 }]);
expect(clonedShop.myCompo.compo_products_ow).toBe(null);
Expand Down Expand Up @@ -1039,11 +1039,9 @@ describe.skip('Relations', () => {
products_om: { disconnect: [id1] },
});

const populatedCreatedShop = await strapi.entityService.findOne(
'api::shop.shop',
createdShop.id,
{ populate: populateShop }
);
const populatedCreatedShop = await strapi.db
.query('api::shop.shop')
.findOne({ where: { id: createdShop.id }, populate: populateShop });

expect(populatedCreatedShop.products_om).toMatchObject([{ id: id1 }]);
expect(populatedCreatedShop.products_oo).toMatchObject({ id: id1 });
Expand Down Expand Up @@ -1074,11 +1072,9 @@ describe.skip('Relations', () => {
products_om: { set: [id2] }, // id 1 should not be stolen from createdShop products_om
});

const populatedCreatedShop = await strapi.entityService.findOne(
'api::shop.shop',
createdShop.id,
{ populate: populateShop }
);
const populatedCreatedShop = await strapi.db
.query('api::shop.shop')
.findOne({ where: { id: createdShop.id }, populate: populateShop });

expect(populatedCreatedShop.products_om).toMatchObject([{ id: id1 }]);
expect(populatedCreatedShop.products_oo).toMatchObject({ id: id1 });
Expand Down
Expand Up @@ -35,7 +35,7 @@ let strapi;
let utils;

const createEntry = async (data) => {
return strapi.entityService.create('api::nonvisible.nonvisible', {
return strapi.db.query('api::nonvisible.nonvisible').create({
data,
});
};
Expand Down
Expand Up @@ -141,7 +141,7 @@ let singleMedia;
let mediaEntry = {};
let model;

describe('Upload Plugin url signing', () => {
describe.skip('Upload Plugin url signing', () => {
const expectMedia = (media, expectedUrl) => {
expect(media.url).toEqual(expectedUrl);
};
Expand Down
59 changes: 20 additions & 39 deletions api-tests/core/upload/content-api/upload.test.api.js
Expand Up @@ -2,7 +2,6 @@

const fs = require('fs');
const path = require('path');
const get = require('lodash/get');

// Helpers.
const { createTestBuilder } = require('api-tests/builder');
Expand Down Expand Up @@ -164,19 +163,25 @@ describe('Upload plugin', () => {
])
);
});

test('Get one file', async () => {
const dogEntity = await strapi.entityService.create('api::dog.dog', {
data: {},
files: {
profilePicture: {
path: path.join(__dirname, '../utils/rec.jpg'),
name: 'rec',
type: 'jpg',
size: 0,
},
const res = await rq({
method: 'POST',
url: '/upload',
formData: {
files: fs.createReadStream(path.join(__dirname, '../utils/thumbnail_target.png')),
},
});

const dogEntity = await strapi.db.query('api::dog.dog').create({
data: {
profilePicture: res.body[0].id,
},
populate: {
profilePicture: true,
},
populate: 'profilePicture',
});

const getRes = await rq({
method: 'GET',
url: `/upload/files/${dogEntity.profilePicture.id}`,
Expand All @@ -189,35 +194,11 @@ describe('Upload plugin', () => {
url: expect.any(String),
})
);
await strapi.entityService.delete('api::dog.dog', dogEntity.id);
await strapi.entityService.delete('plugin::upload.file', dogEntity.profilePicture.id);
});
});

// see https://github.com/strapi/strapi/issues/14125
describe('File relations are correctly removed', () => {
// TODO V5: Remove when entity service is deprecated
test.skip('Update a file with an entity correctly removes the relation between the entity and its old file', async () => {
const fileId = data.dogs[1].data.attributes.profilePicture.data.id;
await strapi.entityService.update('plugin::upload.file', fileId, {
data: {
related: [
{
id: data.dogs[0].data.id,
__type: 'api::dog.dog',
__pivot: { field: 'profilePicture' },
},
],
},
});

const res = await rq({
method: 'GET',
url: `/dogs/${data.dogs[0].data.id}?populate=*`,
});
expect(res.body.data.attributes.profilePicture.data.id).toBe(fileId);

data.dogs[0] = res.body;
await strapi.db.query('api::dog.dog').delete({ where: { id: dogEntity.id } });
await strapi.db
.query('plugin::upload.file')
.delete({ where: { id: dogEntity.profilePicture.id } });
});
});
});
6 changes: 5 additions & 1 deletion api-tests/plugins/graphql/dp-relations.test.api.js
Expand Up @@ -47,6 +47,7 @@ const labelModel = {
};

const labels = [{ name: 'label 1' }, { name: 'label 2' }];

const articles = ({ label: labels }) => {
const labelIds = labels.map((label) => label.id);
return [
Expand Down Expand Up @@ -110,7 +111,10 @@ describe('Test Graphql Relations with Draft and Publish enabled', () => {
expect(body).toMatchObject({
data: {
labels_connection: {
data: labels.map((label) => ({ documentId: expect.any(String), attributes: label })),
data: labels.map((label) => ({
documentId: expect.any(String),
attributes: pick('name', label),
})),
},
},
});
Expand Down
4 changes: 4 additions & 0 deletions api-tests/plugins/i18n/content-api/content-api.test.api.js
Expand Up @@ -60,21 +60,25 @@ const homepageModel = {

const homepages = [
{
document: 'a',
title: 'homepage title',
locale: 'en',
},
{
document: 'a',
title: '홈페이지 제목',
locale: 'ko',
},
];

const categories = [
{
documentId: 'a',
name: 'post',
locale: 'en',
},
{
documentId: 'a',
name: '게시물',
locale: 'ko',
},
Expand Down
14 changes: 7 additions & 7 deletions docs/docs/docs/01-core/database/02-transactions.md
Expand Up @@ -23,15 +23,15 @@ Transactions are handled by passing a handler function into `strapi.db.transacti
```js
await strapi.db.transaction(async ({ trx, rollback, commit, onCommit, onRollback }) => {
// It will implicitly use the transaction
await strapi.entityService.create();
await strapi.entityService.create();
await strapi.db.create();
await strapi.db.create();
});
```

After the transaction handler is executed, the transaction is committed if all operations succeed. If any of the operations throws, the transaction is rolled back and the data is restored to its previous state.

:::note
Every `strapi.entityService` or `strapi.db.query` operation performed in a transaction block will implicitly use the transaction.
Every `strapi.db.query` operation performed in a transaction block will implicitly use the transaction.
:::

### Transaction handler properties
Expand All @@ -53,11 +53,11 @@ Transactions can be nested. When a transaction is nested, the inner transaction
```js
await strapi.db.transaction(async () => {
// It will implicitly use the transaction
await strapi.entityService.create();
await strapi.db.create();

// Nested transactions will implicitly use the outer transaction
await strapi.db.transaction(async ({}) => {
await strapi.entityService.create();
await strapi.db.create();
});
});
```
Expand All @@ -69,8 +69,8 @@ The `onCommit` and `onRollback` hooks can be used to execute code after the tran
```js
await strapi.db.transaction(async ({ onCommit, onRollback }) => {
// It will implicitly use the transaction
await strapi.entityService.create();
await strapi.entityService.create();
await strapi.db.create();
await strapi.db.create();

onCommit(() => {
// This will be executed after the transaction is committed
Expand Down
6 changes: 4 additions & 2 deletions packages/core/admin/ee/server/src/bootstrap.ts
Expand Up @@ -21,8 +21,10 @@ export default async (args: any) => {
await actionProvider.registerMany(actions.reviewWorkflows);
// Decorate the entity service with review workflow logic
const { decorator } = getService('review-workflows-decorator');
// @ts-expect-error - add decorator to entity service
strapi.entityService.decorate(decorator);

// TODO: use document service middleware
// strapi.entityService.decorate(decorator);

await getService('review-workflows-weekly-metrics').registerCron();
}
await getService('seat-enforcement').seatEnforcementWorkflow();
Expand Down

0 comments on commit 3a67086

Please sign in to comment.