This plugin helps you import and export data from and to your database in just few clicks.
Import/Export data from and to your database in just few clicks.
At the moment, dynamic zones and media are not unit tested. Tests will be implemented in a near future to improve quality and development speed.
Strapi v4 is required.
Access the publicly available product roadmap and suggest features, report bugs or upvote other people suggestions.
Join the Discord Community to give your feedback 📣 and get some help from the community ⛑️
See the repo Strapi Contribute.
1yarn add strapi-plugin-import-export-entries
or
1npm i strapi-plugin-import-export-entries
Add in the file config/plugins.js
:
1module.exports = ({ env }) => ({
2 //...
3 'import-export-entries': {
4 enabled: true,
5 config: {
6 // See `Config` section.
7 },
8 },
9 //...
10});
New releases can introduce changes to the administration panel that require a rebuild. Rebuild the admin panel with one of the following commands:
1yarn build --clean
or
1npm run build --clean
Once the plugin is installed and setup, the functionnalities for a collection are accessible on its content management page.
You can also export the whole database from the home page of the plugin.
You can define which roles can import and/or export data from the admin UI.
Go to Settings > Roles (under Administration Panel) > Plugins > Import-export-entries
.
Admin view.
User view of the plugin home page that can only export data.
User view of the content management page that can only export data.
For a quick and convenient use, you can set your preferences from the home page of the plugin.
Once set, they will be used each time you import/export data.
In config/plugins.js
:
1module.exports = ({ env }) => ({
2 //...
3 'import-export-entries': {
4 enabled: true,
5 config: {
6 /**
7 * Public hostname of the server.
8 *
9 * If you use the local provider to persist medias,
10 * `serverPublicHostname` should be set to properly export media urls.
11 */
12 serverPublicHostname: 'https://yoga.com', // default: "".
13 },
14 },
15 //...
16});
In any collection schema schema.json
:
1{
2 "collectionName": "my-awesome-collection",
3 "info": {
4 "displayName": "My Awesome Collection",
5 },
6 "pluginOptions": {
7 "import-export-entries": {
8 /**
9 * Define the `idField` used to find an entry of the collection
10 * when importing data.
11 *
12 * `idField` must match the name of an attribute.
13 * See section _Specifying `idField` Per Collection_ for more details
14 */
15 "idField": "name"
16 }
17 },
18 "attributes": {
19 /**
20 * In this example, `name` will be used to find an entry
21 * of this collection when importing data.
22 */
23 "name": {
24 "type": "string",
25 "unique": true
26 },
27 }
28}
idField
Per CollectionImporting data will either create entries if they don't exist, or update them otherwise.
When transfering data from a database to another, relying on the id
of an entry is not reliable. For example, if you are transfering data on hospitals with this schema:
1interface Hospital {
2 id: number;
3 name: string;
4 employees: Employee[];
5 patients: Patient[];
6}
You will have something similar in your source and target databases:
1// data in source database
2{
3 id: 1,
4 name: "Pitié Salpêtrière",
5 employees: [2, 3],
6 patients: [4, 5],
7}
8
9// data in target database
10{
11 id: 11,
12 name: "Pitié Salpêtrière",
13 employees: [12, 13],
14 patients: [14, 15],
15}
Different databases, different id
s. 🫠
That's why we need a way to define the field used to find an entry in a collection. This field is called an idField
.
To define the idField
of a collection, add it in the pluginOptions
of the collection, under the property import-export-entries
. Using the example above, this is how we would define the idField
of the collection hospital
:
1{
2 "collectionName": "hospitals",
3 "info": {
4 "displayName": "Hospital",
5 },
6 "options": {},
7 /**
8 * In the property `pluginOptions`, define the `idField` under the property `import-export-entries`.
9 *
10 * `idField` must match the name of an attribute.
11 */
12 "pluginOptions": {
13 "import-export-entries": {
14 "idField": "name"
15 }
16 },
17 "attributes": {
18 "name": { // 👈 `name` will be used to find a hospital when importing data.
19 "type": "string",
20 "unique": true
21 },
22 "employees": {
23 "type": "relation",
24 "relation": "oneToMany",
25 "target": "api::employees.employees"
26 },
27 "patients": {
28 "type": "relation",
29 "relation": "oneToMany",
30 "target": "api::patients.patients"
31 },
32 }
33}
For each collection of your application, you can define a different idField
. For example, you can set the name
attribute as the idField
of the collection hospital
, and for the collection patients
use the attribute ssn
(I really hope you're not storing uncyphered SSNs in your database 😬).
How does the search behave if I don't define explicitly the
idField
of a collection?
By default, the idField
of a collection is the id
attribute. We can imagine in a near future to automatically detect unique scalar fields of a collection and use them by default, but it's not the case at the moment.
How does the search behave when I specify the
idField
from the strapi admin UI?
The idField
specified from the import modal of the admin UI takes precedence over the one defined in the pluginOptions
of the collection.
This default behavior could change in the future if user feedback shows it's cumbersome to set it manually on each import. You tell me.
When importing data, imported file size may exceed the file size limit of the server. To lift up the limit, configure the Strapi middleware body
:
1// ./config/middlewares.js
2
3module.exports = {
4 // ...
5 {
6 name: 'strapi::body',
7 config: {
8 jsonLimit: '10mb',
9 },
10 },
11 // ...
12}
The filtering and sorting mechanism relies on Strapi filtering and sorting feature:
The exported data is filtered and sorted as expected.
1/*****************************
2 * Service "import".
3 ****************************/
4
5/**
6 * Get the service.
7 */
8const service = strapi.plugin("import-export-entries").service("import");
9
10/**
11 * Method importData.
12 */
13await service.importData(
14 /**
15 * Data to import.
16 * Expected type depends on the specified format:
17 * - csv: string
18 * - jso: object | object[]
19 * - json: string
20 */
21 dataRaw: object | object[] | string,
22 options: {
23 /**
24 * Slug of the imported model.
25 * - "media" is a custom slug to specifically import media. See section Importing Data > Media below.
26 */
27 slug: "media" | string;
28 /**
29 * Format of the imported data.
30 * - csv
31 * - jso: javascript object
32 * - json: javascript object notation
33 */
34 format: "csv" | "jso" | "json";
35 /** User importing data. */
36 user: object;
37 }
38) : Promise<{
39 failures: {
40 /** Error raised. */
41 error: Error;
42 /** Data for which import failed. */
43 data: object;
44 }[]
45}>;
1/*****************************
2 * Service "export".
3 ****************************/
4
5/**
6 * Get the service.
7 */
8const service = strapi.plugin("import-export-entries").service("export");
9
10/**
11 * Method exportData.
12 */
13await service.exportData(
14 options: {
15 /**
16 * Slug of the model to export.
17 * - "media" is a custom slug to specifically export media.
18 */
19 slug: "media" | string;
20 /**
21 * Export format.
22 * - csv
23 * - json: javascript object notation
24 */
25 exportFormat: "csv" | "json";
26 /** Search query used to select the entries to export. The package `qs` is used to parse the query. */
27 search?: string;
28 /** Whether to apply the search query. */
29 applySearch?: boolean;
30 /** Whether to export relations as id instead of plain objects. */
31 relationsAsId?: boolean;
32 /** Deepness of the exported data. */
33 deepness?: number;
34 }
35) : Promise<string>;
Data can be imported/exported through the content api. Endpoints have to be enabled in Settings > Users & Permissions plugin > Roles.
1/*****************************
2 * Import data
3 *
4 * POST /api/import-export-entries/content/import
5 ****************************/
6
7type RouteParams = {
8 /** Slug of the model to export. */
9 slug: string;
10 /**
11 * Data to import.
12 * if `format` is "csv", data must be a string.
13 * if `format` is "json", data must be an object or an array of objects.
14 * */
15 data: string | Object | Object[];
16 /** Format of the passed data to import. */
17 format: 'csv' | 'json';
18 /** Name of the field to use as a unique identifier for entries. Default: "id" */
19 idField?: string;
20};
21
22type RouteReturn = {
23 /** Array of failed imports. */
24 failures: {
25 /** Error raised during import. */
26 error: string;
27 /** Data for which the import failed. */
28 data: Object;
29 }[];
30};
1/*****************************
2 * Export data
3 *
4 * POST /api/import-export-entries/content/export/contentTypes
5 ****************************/
6
7type RouteParams = {
8 /** Slug of the model to export. */
9 slug: string;
10 /** Format to use to export the data. */
11 exportFormat: 'csv' | 'json';
12 /** Search query used to select the entries to export. The package `qs` is used to parse the query. Default: "" */
13 search?: string;
14 /** Whether to apply the search query. Default: false */
15 applySearch?: boolean;
16 /** Whether to export relations as id instead of plain objects. Default: false */
17 relationsAsId?: boolean;
18 /** Deepness of the exported data. Default: 5 */
19 deepness?: number;
20};
21
22type RouteReturn = {
23 /** Exported data. */
24 data: string;
25};
At the moment, the webhook is triggered only for media creation, update and deletion. It is not triggered for other data.
JSON v2 introduces a new supported file structure. Data is flattened and dependencies only relies on id
s (object
s are not supported in this new version). Collection types, single types, media and components are all treated the same for ease of use.
Here is an example:
1{
2 "version": 2, // required for the import to work properly.
3 "data": {
4 // Each collection has a dedicated key in the `data` property.
5 "api::collection-name.collection-name": {
6 // Sub keys are `id`s of imported entries and values hold the data of the entries to import.
7 "1": {
8 "id": 1
9 //...
10 },
11 "2": {
12 "id": 2
13 //...
14 }
15 },
16 "api::other-collection-name.other-collection-name": {
17 "1": {
18 "id": 1,
19 // Relations are specified by `id`s.
20 "collectionAbove": [1]
21 //...
22 },
23 "2": {
24 "id": 2,
25 "collectionAbove": [1, 2]
26 //...
27 }
28 },
29 // Import medias.
30 "plugin::upload.file": {
31 "1": {
32 "id": 1
33 //...
34 },
35 "2": {
36 "id": 2
37 //...
38 }
39 },
40 // Import components.
41 "my.component": {
42 "1": {
43 "id": 1
44 //...
45 },
46 "2": {
47 "id": 2
48 //...
49 }
50 }
51 }
52}
The expected import data structure:
object
the relation is searched in db by id
. If an entry is found, it is updated with the provided data. Otherwise, it is created.
number
The relation is treated as an id.
object
the media must have an id
, hash
, name
or url
property. First the media is searched by id
, then by hash
, then by name
and finally imported from url
if not found previously.
When imported by url
, the hash
and name
of the file are deduced from the url
(the hash
is also deduced because Strapi exports files with their hash
in the url
instead of the name
). The hash
and name
are used to find the media in db. First the media is searched by hash
, then by name
and used if found. Otherwise, the media is uploaded to the db by downloading the file from the url
.
⚠️ Check the server has access to the
url
.
When imported by url
, extra data can be provided to enhance the created file:
id
(defaults to an auto generated id
)name
(defaults to the name
deduced from the url)caption
(defaults to ""
)alternativeText
(defaults to ""
){ id: 1 }
{ hash: "alpaga.jpg" }
{ name: "alpaga.jpg" }
{ url: "https://www.thetimes.co.uk/imageserver/image/alpaga.jpg" }
(Deduced file hash
is alpaga
and deduced name
is imageserver-image-alpaga.jpg
){ url: "http://localhost:1337/alpaga.jpg" }
(Deduced file hash
is alpaga
and deduced name
is alpaga.jpg
){ id: 734, url: "http://localhost:1337/alpaga.jpg", name: "Alpacool", caption: "Alpacool In Da Hood", alternativeText: "Alpacool in da hood" }
string
Same as above, except the media provided is treated as a url
.
"https://www.thetimes.co.uk/imageserver/image/alpaga.jpg"
(Deduced file hash
is alpaga
and deduced name
is imageserver-image-alpaga.jpg
)"http://localhost:1337/alpaga.jpg"
(Deduced file hash
is alpaga
and deduced name
is alpaga.jpg
)number
The media is treated as an id.
7
Baboo - @Baboo7
This plugin (and especially this README) took strong inspiration from the strapi-plugin-import-export-content from EdisonPeM.
npm install strapi-plugin-import-export-entries
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.