Skip to content

Standalone CAMO project using Typescript migration files

Sam Bauers edited this page May 26, 2022 · 2 revisions

Create a new project

Using your favourite package manager:

# Create a new project directory
$ mkdir my-camo-ts
$ cd my-camo-ts

# npm
$ npm init

# or yarn
$ yarn init

# or ppm
$ pnpm init

Then answer all the package manager setup questions.

Install CAMO and Typescript packages

# npm
$ npm install --save-dev @sambauers/camo typescript @types/node @tsconfig/node16

# or yarn
$ yarn add --dev @sambauers/camo typescript @types/node @tsconfig/node16

# or pnpm
$ pnpm add --save-dev @sambauers/camo typescript @types/node @tsconfig/node16

Add a build and a migrate script to your package.json

In your projects package.json add these scripts in the scripts definitions. Adjust the migrate script to call your preferred package manager (npm shown):

{
  "scripts": {
    "build": "rm -rf migrations && tsc",
    "migrate": "npm build && camo"
  }
}

Create a source migrations directory

This will contain the Typescript migrations.

$ mkdir -p src/migrations

Create the Typescript configuration

Add a file in the root of your project named tsconfig.json containing:

{
  "extends": "@tsconfig/node16/tsconfig.json",
  "include": [
    "src/migrations/*.ts"
  ],
  "compilerOptions": {
    "outDir": "./migrations"
  }
}

Now when you run tsc on the command line to compile the migrations, it will:

  • Look in src/migrations for the source migration scripts
  • Compile those migration scripts
  • Output the compiled Javascript files to a migrations directory, which is the default location that CAMO looks for migration scripts

Setup the required environment variables

Create a .env file in the project root containing (at least):

# Contentful Active Migration Organiser (CAMO)
# The space ID to connect to
CONTENTFUL_MIGRATION_SPACE_ID='<your-contentful-space-id>'

# The access token to connect with
CONTENTFUL_MIGRATION_ACCESS_TOKEN='<your-contentful-access-token>'

Create the Contentful migrations content type

Run the migrate --list command:

# npm
$ npm run migrate --list

# or yarn
$ yarn migrate --list

# or pnpm
$ pnpm migrate --list

You will be asked if you want CAMO create the content type in Contentful.

Create a local migration file

Create a Typescript migration file in the src/migrations folder with your desired schema.

List the compiled local migration file

The compiled Javascript version of the file you created above should now be visible with the --list option:

# npm
$ npm run migrate --list

# or yarn
$ yarn migrate --list

# or pnpm
$ pnpm migrate --list

Apply the compiled local migration file

The compiled Javascript version of the file you created above should now be applied when running CAMO:

# npm
$ npm run migrate

# or yarn
$ yarn migrate

# or pnpm
$ pnpm migrate