Skip to content

Commit

Permalink
feat: add option to delete data to the import process
Browse files Browse the repository at this point in the history
  • Loading branch information
katie-gardner committed Dec 23, 2024
1 parent 2d1b892 commit 8d3a6ea
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
6 changes: 5 additions & 1 deletion contentful/content-management/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ ____

- importing content can be achieved using the `import-content` script, which will import content from a json file into a specified environment. The json file should be in the format of the `export-processor` export outputs
- `import-content` uses the `contentful-import` npm package to do the importing
- By default a content import will add to an environment and not remove unrelated data
- It will overwrite entries with the same id as one in the import file, and leave other entries alone
- To delete all existing data prior to import so that the environment exactly matches the import file, set `DELETE_ALL_DATA` to true in `.env`

### Usage

Expand All @@ -39,11 +42,12 @@ Required Environment variables
`MANAGEMENT_TOKEN`
`ENVIRONMENT` (environmentId default is 'master')
`SKIP_CONTENT_MODEL` (optional, default is false)
`DELETE_ALL_DATA` (optional, default is false)

## Deleting all content

You may wish to remove all content from an environment, for example, before importing from a backup.
This can be done with the `delete-all-content` script, which will clear down the entire environment.
This is an environment variable option in the import process, or it can be run standalone with the `delete-all-content` script, which will clear down the entire environment.

### Setup

Expand Down
2 changes: 0 additions & 2 deletions contentful/content-management/delete-all-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const deleteContentfulContent = require("./helpers/delete-all-content-and-conten

async function deleteAllContentfulData() {
const client = await getClient();
await validateEnvironment(client);

const contentTypesToDelete = process.env.CONTENT_TYPES_TO_DELETE?.split(",");

console.log(`Deleting ${process.env.CONTENT_TYPES_TO_DELETE ?? 'all data'} from the following environment: ${process.env.ENVIRONMENT}`)
Expand Down
22 changes: 1 addition & 21 deletions contentful/content-management/helpers/validate-environment.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
const contentful = require("contentful-management");

function createClient() {
return contentful.createClient(
{
accessToken: process.env.MANAGEMENT_TOKEN,
},
{
type: "plain",
defaults: {
spaceId: process.env.SPACE_ID,
environmentId: process.env.ENVIRONMENT,
},
}
);
}

/**
* Verifies that the environment specified in .env is a valid contentful environment
* This is important because if it isn't, the management api fails silently
* and falls back to using the master environment
*
* @param {ClientAPI | null} client
*/
module.exports = async function validateEnvironment(client = null) {
if (!client) {
client = createClient();
}
module.exports = async function validateEnvironment(client) {
const environments = await client.environment.getMany({
spaceId: process.env.SPACE_ID,
});
Expand Down
13 changes: 9 additions & 4 deletions contentful/content-management/import-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ require('dotenv').config();

const contentfulImport = require('contentful-import');
const fs = require('fs');
const validateEnvironment = require("./helpers/validate-environment");
const getClient = require("./helpers/get-client");
const deleteContentfulContent = require("./helpers/delete-all-content-and-content-types");

async function importContentfulData() {
const options = {
Expand All @@ -12,14 +13,18 @@ async function importContentfulData() {
environmentId: process.env.ENVIRONMENT,
skipContentModel: process.env.SKIP_CONTENT_MODEL === 'true' ? true : false
};

await validateEnvironment();
const client = await getClient();

if (!fs.existsSync(options.contentFile)) {
throw new Error(`File not found: ${options.contentFile}`);
}

console.log("Attempting import with the following options:", options)
if (process.env.DELETE_ALL_DATA == 'true') {
console.log(`Deleting all existing data from ${options.environmentId}`);
await deleteContentfulContent({ client: client });
}

console.log("Starting import with the following options:", options)

try {
await contentfulImport(options);
Expand Down

0 comments on commit 8d3a6ea

Please sign in to comment.