Migrate from the previous SDK

To migrate from the previous SDK, which can be found here, there are a couple of changes that need to be made in order to use it with existing migration scripts.

The old SDK has been deprecated but won't be removed from NPM, so old scripts using it will still work in the future. New features, though, will only be available in the new SDK.

While the old SDK will continue to work, it has been deprecated and support will no longer be provided for it.

First of all the import of the NPM package has to be changed. For this, the package name needs to be changed to @hygraph/management-sdk. In the previous version, the SDK offered a named export newMigration that could be used to create a new migration. This changed with the new version, so a new migration must be created as follows:

import { Client } from '@hygraph/management-sdk';
const migration = new Client({
authToken: '...',
endpoint: '...',
});

The general methods on the migration class stayed the same, so running - or dryRunning - a migration will still work as before.

The major change are the operations that are supported to actually execute changes:

Before, the SDK was built in a fluent API style. This means you could chain operations like the following:

migration
.createModel({
apiId: 'Author',
apiIdPlural: 'Authors',
displayName: 'Author',
})
.addSimpleField({
apiId: 'firstName',
displayName: 'First Name',
type: FieldType.String,
});

The new SDK version no longer supports chaining. The reason behind that is that the SDK is now fully generated by the schema it is using to execute the migration. The previous example needs to be changed as follows:

migration.createModel({
apiId: 'Post',
apiIdPlural: 'Posts',
displayName: 'Post',
});
migration.createSimpleField({
apiId: 'firstName',
displayName: 'First Name',
type: SimpleFieldType.String,
modelApiId: 'Post',
});

To link the second operation to the first one - to create the field on the created model in the first operation - the modelApiId must be passed into the operation.