@graphcommerce/hygraph-cli 8.1.0-canary.9 → 9.0.0-canary.101

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/CHANGELOG.md +187 -6
  2. package/Config.graphqls +6 -10
  3. package/dist/UpsertClient.js +69 -0
  4. package/dist/index.js +2 -2
  5. package/dist/migrateHygraphCli.js +111 -0
  6. package/dist/migrationActionFactory.js +133 -0
  7. package/dist/migrations/graphcommerce5to6.js +12 -14
  8. package/dist/migrations/graphcommerce6to7.js +25 -27
  9. package/dist/migrations/graphcommerce7to8.js +7 -9
  10. package/dist/migrations/graphcommerce8to9.js +54 -8
  11. package/dist/readSchema.js +10 -21
  12. package/dist/utils/getConfig.js +19 -0
  13. package/dist/utils/getEndpointUrl.js +43 -0
  14. package/dist/utils/getManagementClient.js +15 -0
  15. package/dist/{log-functions.js → utils/graphCommerceLog.js} +1 -1
  16. package/package.json +8 -8
  17. package/src/UpsertClient.ts +99 -0
  18. package/src/index.ts +1 -1
  19. package/src/migrateHygraphCli.ts +113 -0
  20. package/src/migrationActionFactory.ts +230 -0
  21. package/src/migrations/graphcommerce5to6.ts +4 -6
  22. package/src/migrations/graphcommerce6to7.ts +4 -6
  23. package/src/migrations/graphcommerce7to8.ts +4 -6
  24. package/src/migrations/graphcommerce8to9.ts +79 -8
  25. package/src/readSchema.ts +24 -29
  26. package/src/types.ts +6 -1
  27. package/src/utils/getConfig.ts +33 -0
  28. package/src/utils/getEndpointUrl.ts +65 -0
  29. package/src/utils/getManagementClient.ts +15 -0
  30. package/src/{log-functions.ts → utils/graphCommerceLog.ts} +1 -1
  31. package/dist/client.js +0 -21
  32. package/dist/migrateHygraph.js +0 -96
  33. package/dist/migrationAction.js +0 -133
  34. package/src/client.ts +0 -25
  35. package/src/migrateHygraph.ts +0 -86
  36. package/src/migrationAction.ts +0 -223
package/CHANGELOG.md CHANGED
@@ -1,5 +1,189 @@
1
1
  # @graphcommerce/hygraph-cli
2
2
 
3
+ ## 9.0.0-canary.101
4
+
5
+ ## 9.0.0-canary.100
6
+
7
+ ## 9.0.0-canary.99
8
+
9
+ ## 9.0.0-canary.98
10
+
11
+ ## 9.0.0-canary.97
12
+
13
+ ## 9.0.0-canary.96
14
+
15
+ ## 9.0.0-canary.95
16
+
17
+ ## 9.0.0-canary.94
18
+
19
+ ## 9.0.0-canary.93
20
+
21
+ ## 9.0.0-canary.92
22
+
23
+ ## 9.0.0-canary.91
24
+
25
+ ## 9.0.0-canary.90
26
+
27
+ ## 9.0.0-canary.89
28
+
29
+ ## 9.0.0-canary.88
30
+
31
+ ## 9.0.0-canary.87
32
+
33
+ ## 9.0.0-canary.86
34
+
35
+ ## 9.0.0-canary.85
36
+
37
+ ## 9.0.0-canary.84
38
+
39
+ ## 9.0.0-canary.83
40
+
41
+ ## 9.0.0-canary.82
42
+
43
+ ## 9.0.0-canary.81
44
+
45
+ ## 9.0.0-canary.80
46
+
47
+ ## 9.0.0-canary.79
48
+
49
+ ## 9.0.0-canary.78
50
+
51
+ ## 9.0.0-canary.77
52
+
53
+ ## 9.0.0-canary.76
54
+
55
+ ## 9.0.0-canary.75
56
+
57
+ ## 9.0.0-canary.74
58
+
59
+ ## 9.0.0-canary.73
60
+
61
+ ## 9.0.0-canary.72
62
+
63
+ ## 9.0.0-canary.71
64
+
65
+ ## 9.0.0-canary.70
66
+
67
+ ## 9.0.0-canary.69
68
+
69
+ ## 9.0.0-canary.68
70
+
71
+ ## 9.0.0-canary.67
72
+
73
+ ## 9.0.0-canary.66
74
+
75
+ ## 9.0.0-canary.65
76
+
77
+ ## 9.0.0-canary.64
78
+
79
+ ## 9.0.0-canary.63
80
+
81
+ ## 9.0.0-canary.62
82
+
83
+ ## 9.0.0-canary.61
84
+
85
+ ## 9.0.0-canary.60
86
+
87
+ ## 9.0.0-canary.59
88
+
89
+ ## 9.0.0-canary.58
90
+
91
+ ## 9.0.0-canary.57
92
+
93
+ ## 9.0.0-canary.56
94
+
95
+ ## 9.0.0-canary.55
96
+
97
+ ## 9.0.0-canary.54
98
+
99
+ ## 8.1.0-canary.53
100
+
101
+ ## 8.1.0-canary.52
102
+
103
+ ## 8.1.0-canary.51
104
+
105
+ ## 8.1.0-canary.50
106
+
107
+ ## 8.1.0-canary.49
108
+
109
+ ## 8.1.0-canary.48
110
+
111
+ ## 8.1.0-canary.47
112
+
113
+ ## 8.1.0-canary.46
114
+
115
+ ## 8.1.0-canary.45
116
+
117
+ ## 8.1.0-canary.44
118
+
119
+ ## 8.1.0-canary.43
120
+
121
+ ## 8.1.0-canary.42
122
+
123
+ ## 8.1.0-canary.41
124
+
125
+ ## 8.1.0-canary.40
126
+
127
+ ## 8.1.0-canary.39
128
+
129
+ ## 8.1.0-canary.38
130
+
131
+ ## 8.1.0-canary.37
132
+
133
+ ## 8.1.0-canary.36
134
+
135
+ ## 8.1.0-canary.35
136
+
137
+ ## 8.1.0-canary.34
138
+
139
+ ## 8.1.0-canary.33
140
+
141
+ ## 8.1.0-canary.32
142
+
143
+ ## 8.1.0-canary.31
144
+
145
+ ## 8.1.0-canary.30
146
+
147
+ ## 8.1.0-canary.29
148
+
149
+ ## 8.1.0-canary.28
150
+
151
+ ## 8.1.0-canary.27
152
+
153
+ ## 8.1.0-canary.26
154
+
155
+ ## 8.1.0-canary.25
156
+
157
+ ## 8.1.0-canary.24
158
+
159
+ ## 8.1.0-canary.23
160
+
161
+ ## 8.1.0-canary.22
162
+
163
+ ## 8.1.0-canary.21
164
+
165
+ ## 8.1.0-canary.20
166
+
167
+ ## 8.1.0-canary.19
168
+
169
+ ## 8.1.0-canary.18
170
+
171
+ ## 8.1.0-canary.17
172
+
173
+ ## 8.1.0-canary.16
174
+
175
+ ## 8.1.0-canary.15
176
+
177
+ ## 8.1.0-canary.14
178
+
179
+ ## 8.1.0-canary.13
180
+
181
+ ## 8.1.0-canary.12
182
+
183
+ ## 8.1.0-canary.11
184
+
185
+ ## 8.1.0-canary.10
186
+
3
187
  ## 8.1.0-canary.9
4
188
 
5
189
  ## 8.1.0-canary.8
@@ -92,11 +276,9 @@
92
276
 
93
277
  ### Patch Changes
94
278
 
95
- - [#2095](https://github.com/graphcommerce-org/graphcommerce/pull/2095) [`519531e`](https://github.com/graphcommerce-org/graphcommerce/commit/519531eb48bf6c5b68703509d2bd64c8dfe5b92c) - Fix Hygraph CLI (Management SDK) | Add support for updating and deleting enumerations
96
- ([@JoshuaS98](https://github.com/JoshuaS98))
279
+ - [#2095](https://github.com/graphcommerce-org/graphcommerce/pull/2095) [`519531e`](https://github.com/graphcommerce-org/graphcommerce/commit/519531eb48bf6c5b68703509d2bd64c8dfe5b92c) - Fix Hygraph CLI (Management SDK) | Add support for updating and deleting enumerations ([@JoshuaS98](https://github.com/JoshuaS98))
97
280
 
98
- - [#2169](https://github.com/graphcommerce-org/graphcommerce/pull/2169) [`eab3f0b`](https://github.com/graphcommerce-org/graphcommerce/commit/eab3f0b0b459f5b6cc4e50d787ac1e8ae545b708) - Solve issue where a Hygraph DynamicRow conditions were missing fields in the OrCondition
99
- ([@Jessevdpoel](https://github.com/Jessevdpoel))
281
+ - [#2169](https://github.com/graphcommerce-org/graphcommerce/pull/2169) [`eab3f0b`](https://github.com/graphcommerce-org/graphcommerce/commit/eab3f0b0b459f5b6cc4e50d787ac1e8ae545b708) - Solve issue where a Hygraph DynamicRow conditions were missing fields in the OrCondition ([@Jessevdpoel](https://github.com/Jessevdpoel))
100
282
 
101
283
  ## 8.0.0-canary.100
102
284
 
@@ -114,8 +296,7 @@
114
296
 
115
297
  ### Patch Changes
116
298
 
117
- - [#2169](https://github.com/graphcommerce-org/graphcommerce/pull/2169) [`eab3f0b`](https://github.com/graphcommerce-org/graphcommerce/commit/eab3f0b0b459f5b6cc4e50d787ac1e8ae545b708) - Solve issue where a Hygraph DynamicRow conditions were missing fields in the OrCondition
118
- ([@Jessevdpoel](https://github.com/Jessevdpoel))
299
+ - [#2169](https://github.com/graphcommerce-org/graphcommerce/pull/2169) [`eab3f0b`](https://github.com/graphcommerce-org/graphcommerce/commit/eab3f0b0b459f5b6cc4e50d787ac1e8ae545b708) - Solve issue where a Hygraph DynamicRow conditions were missing fields in the OrCondition ([@Jessevdpoel](https://github.com/Jessevdpoel))
119
300
 
120
301
  ## 8.0.0-canary.93
121
302
 
package/Config.graphqls CHANGED
@@ -1,13 +1,4 @@
1
1
  extend input GraphCommerceConfig {
2
- """
3
- Content API. **Only used for migrations.**
4
-
5
- > Regular read & write endpoint that allows querying and mutating data in your project.
6
-
7
- Project settings -> API Access -> Content API
8
- """
9
- hygraphWriteAccessEndpoint: String
10
-
11
2
  """
12
3
  Hygraph Management SDK Authorization Token. **Only used for migrations.**
13
4
 
@@ -34,7 +25,6 @@ extend input GraphCommerceConfig {
34
25
  - Can see schema view
35
26
 
36
27
  ```
37
- GC_HYGRAPH_WRITE_ACCESS_ENDPOINT="https://...hygraph.com/v2/..."
38
28
  GC_HYGRAPH_WRITE_ACCESS_TOKEN="AccessTokenFromHygraph"
39
29
  yarn graphcommerce hygraph-migrate
40
30
  ```
@@ -43,11 +33,17 @@ extend input GraphCommerceConfig {
43
33
 
44
34
  """
45
35
  Hygraph Project ID. **Only used for migrations.**
36
+
37
+ Optional: If the hygraphEndpoint is configured with the 'High Performance Content
38
+ API', this field is not required.
46
39
  """
47
40
  hygraphProjectId: String
48
41
 
49
42
  """
50
43
  Hygraph Management API. **Only used for migrations.**
44
+
45
+ Optional: If the hygraphEndpoint is configured with the 'High Performance Content
46
+ API', this field is not required.
51
47
  """
52
48
  hygraphManagementApi: String
53
49
  }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UpsertClient = void 0;
4
+ const management_sdk_1 = require("@hygraph/management-sdk");
5
+ class UpsertClient extends management_sdk_1.Client {
6
+ schema;
7
+ constructor(params, schema) {
8
+ super(params);
9
+ this.schema = schema;
10
+ }
11
+ upsertModel(data) {
12
+ const exists = this.schema.models.some((m) => m.apiId === data.apiId);
13
+ return exists ? this.createModel(data) : this.updateModel(data);
14
+ }
15
+ upsertComponent(data) {
16
+ const exists = this.schema.models.some((m) => m.apiId === data.apiId);
17
+ return exists ? this.createComponent(data) : this.updateComponent(data);
18
+ }
19
+ upsertSimpleField(data) {
20
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId);
21
+ const exists = model?.fields.some((f) => f.apiId === data.apiId);
22
+ return exists
23
+ ? this.createSimpleField(data)
24
+ : this.updateSimpleField({ ...data, embeddableModels: undefined });
25
+ }
26
+ // upsertRemoteField(data: BatchMigrationCreateRemoteFieldInput) {
27
+ // const model = this.schema.models.find((m) => m.apiId === data.parentApiId)
28
+ // const exists = model?.fields.some((f) => f.apiId === data.apiId)
29
+ // return exists ? this.createRemoteField(data) : this.updateRemoteField(data)
30
+ // }
31
+ upsertRelationalField(data) {
32
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId);
33
+ const exists = model?.fields.some((f) => f.apiId === data.apiId);
34
+ return exists ? this.createRelationalField(data) : this.updateRelationalField(data);
35
+ }
36
+ upsertUnionField(data) {
37
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId);
38
+ const exists = model?.fields.some((f) => f.apiId === data.apiId);
39
+ return exists ? this.createUnionField(data) : this.updateUnionField(data);
40
+ }
41
+ upsertComponentField(data) {
42
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId);
43
+ const exists = model?.fields.some((f) => f.apiId === data.apiId);
44
+ return exists ? this.createComponentField(data) : this.updateComponentField(data);
45
+ }
46
+ upsertComponentUnionField(data) {
47
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId);
48
+ const exists = model?.fields.some((f) => f.apiId === data.apiId);
49
+ return exists ? this.createComponentUnionField(data) : this.updateComponentUnionField(data);
50
+ }
51
+ upsertEnumeration(data) {
52
+ const exists = this.schema.enumerations.some((e) => e.apiId === data.apiId);
53
+ return exists ? this.createEnumeration(data) : this.updateEnumeration(data);
54
+ }
55
+ upsertEnumerableField(data) {
56
+ const model = this.schema.models.find((m) => m.apiId === data.parentApiId);
57
+ const exists = model?.fields.some((f) => f.apiId === data.apiId);
58
+ return exists ? this.createEnumerableField(data) : this.updateEnumerableField(data);
59
+ }
60
+ upsertStage(data) {
61
+ const exists = this.schema.stages.some((m) => m.apiId === data.apiId);
62
+ return exists ? this.createStage(data) : this.updateStage(data);
63
+ }
64
+ upsertLocale(data) {
65
+ const exists = this.schema.locales.some((m) => m.apiId === data.apiId);
66
+ return exists ? this.createLocale(data) : this.updateLocale(data);
67
+ }
68
+ }
69
+ exports.UpsertClient = UpsertClient;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.migrateHygraph = void 0;
4
- var migrateHygraph_1 = require("./migrateHygraph");
5
- Object.defineProperty(exports, "migrateHygraph", { enumerable: true, get: function () { return migrateHygraph_1.migrateHygraph; } });
4
+ var migrateHygraphCli_1 = require("./migrateHygraphCli");
5
+ Object.defineProperty(exports, "migrateHygraph", { enumerable: true, get: function () { return migrateHygraphCli_1.migrateHygraphCli; } });
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.migrateHygraphCli = migrateHygraphCli;
30
+ const fs_1 = __importDefault(require("fs"));
31
+ const next_config_1 = require("@graphcommerce/next-config");
32
+ const dotenv_1 = __importDefault(require("dotenv"));
33
+ const prompts_1 = __importDefault(require("prompts"));
34
+ const UpsertClient_1 = require("./UpsertClient");
35
+ const availableMigrations = __importStar(require("./migrations"));
36
+ const readSchema_1 = require("./readSchema");
37
+ const getConfig_1 = require("./utils/getConfig");
38
+ const getEndpointUrl_1 = require("./utils/getEndpointUrl");
39
+ const getManagementClient_1 = require("./utils/getManagementClient");
40
+ const graphCommerceLog_1 = require("./utils/graphCommerceLog");
41
+ dotenv_1.default.config();
42
+ async function migrateHygraphCli() {
43
+ const hygraphConfig = (0, getConfig_1.getConfig)((0, next_config_1.loadConfig)(process.cwd()));
44
+ /**
45
+ * Extracting the current GC version. Are we gonna use the current version to determine which
46
+ * scripts should be runned? Or do we let the user pick the migration from a list? 🤔
47
+ */
48
+ const packageJson = fs_1.default.readFileSync('package.json', 'utf8');
49
+ const packageData = JSON.parse(packageJson);
50
+ const graphcommerceVersion = packageData.dependencies['@graphcommerce/next-ui'];
51
+ (0, graphCommerceLog_1.graphcommerceLog)(`Graphcommerce version: ${graphcommerceVersion}`, 'info');
52
+ const mangementClient = (0, getManagementClient_1.getManagementClient)(hygraphConfig);
53
+ // Extract the currently existing models, components and enumerations from the Hygraph schema.
54
+ const schemaViewer = await (0, readSchema_1.readSchema)(mangementClient, hygraphConfig.projectId);
55
+ const schema = schemaViewer.viewer.project.environment.contentModel;
56
+ // A list of possible migrations
57
+ const possibleMigrations = Object.entries(availableMigrations);
58
+ // Here we setup the list we ask the user to choose from
59
+ const selectMigrationInput = {
60
+ type: 'select',
61
+ name: 'selectedMigration',
62
+ message: '\x1b[36m\x1b[1m[]: Select migration',
63
+ choices: possibleMigrations.map(([name, migration]) => ({
64
+ title: name,
65
+ value: { name, migration },
66
+ })),
67
+ };
68
+ // Here we ask the user to choose a migration from a list of possible migrations
69
+ try {
70
+ (0, graphCommerceLog_1.graphcommerceLog)('Available migrations: ', 'info');
71
+ const selectMigrationOutput = await (0, prompts_1.default)(selectMigrationInput);
72
+ let { migration, name } = selectMigrationOutput.selectedMigration;
73
+ (0, graphCommerceLog_1.graphcommerceLog)(`You have selected the ${selectMigrationOutput.selectedMigration.name} migration`, 'info');
74
+ try {
75
+ const { endpoint, migrations } = await (0, getEndpointUrl_1.getEnvironment)(mangementClient, hygraphConfig);
76
+ const migrationExists = migrations.find((m) => m.name?.startsWith(name) && m.status === 'SUCCESS');
77
+ if (migrationExists) {
78
+ if (!process.argv.includes('--force')) {
79
+ (0, graphCommerceLog_1.graphcommerceLog)(`Migration ${name} as ${migrationExists.name} already exists in Hygraph with the status SUCCESS. To rerun this migration use the --force option. Exiting now..`, 'info');
80
+ process.exit(1);
81
+ }
82
+ else {
83
+ (0, graphCommerceLog_1.graphcommerceLog)(`Migration ${name} as ${migrationExists.name} already exists in Hygraph with the status SUCCESS. Using --force, rerunning migration..`, 'warning');
84
+ }
85
+ name = `${name}-${Date.now()}`;
86
+ }
87
+ // Here we try to run the migration
88
+ const result = await migration(schema, new UpsertClient_1.UpsertClient({ authToken: hygraphConfig.authToken, endpoint, name }, schema));
89
+ (0, graphCommerceLog_1.graphcommerceLog)(`Migration result: ${JSON.stringify(result)}`, 'info');
90
+ if (!result) {
91
+ (0, graphCommerceLog_1.graphcommerceLog)('No migration client found. Please make sure your GC_HYGRAPH_WRITE_ACCESS_TOKEN in your env file is correct.');
92
+ process.exit(1);
93
+ }
94
+ if (result.status !== 'SUCCESS') {
95
+ (0, graphCommerceLog_1.graphcommerceLog)(`Migration not successful: ${result.status} ${name}:\n${result.errors}`);
96
+ process.exit(1);
97
+ }
98
+ (0, graphCommerceLog_1.graphcommerceLog)(`Migration successful: ${name}`, 'info');
99
+ }
100
+ catch (err) {
101
+ if (err instanceof Error) {
102
+ const garbledErrorIndex = err.message.indexOf(': {"');
103
+ const msg = garbledErrorIndex > 0 ? err.message.slice(0, garbledErrorIndex) : err.message;
104
+ (0, graphCommerceLog_1.graphcommerceLog)(`${msg}`, 'error');
105
+ }
106
+ }
107
+ }
108
+ catch (error) {
109
+ (0, graphCommerceLog_1.graphcommerceLog)(`An error occurred: ${error}`, 'error');
110
+ }
111
+ }
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.migrationActionFactory = migrationActionFactory;
7
+ const dotenv_1 = __importDefault(require("dotenv"));
8
+ const graphCommerceLog_1 = require("./utils/graphCommerceLog");
9
+ dotenv_1.default.config();
10
+ function migrationActionFactory(schema, client) {
11
+ /**
12
+ * This constant is used to assign the right action of the management SDK to the migratioAction
13
+ * function
14
+ */
15
+ const actionMap = client
16
+ ? {
17
+ create: {
18
+ model: (innerprops) => client.createModel(innerprops),
19
+ component: (innerprops) => client.createComponent(innerprops),
20
+ enumeration: (innerprops) => client.createEnumeration(innerprops),
21
+ simpleField: (innerprops) => client.createSimpleField(innerprops),
22
+ enumerableField: (innerprops) => client.createEnumerableField(innerprops),
23
+ componentField: (innerprops) => client.createComponentField(innerprops),
24
+ relationalField: (innerprops) => client.createRelationalField(innerprops),
25
+ unionField: (innerprops) => client.createUnionField(innerprops),
26
+ componentUnionField: (innerprops) => client.createComponentUnionField(innerprops),
27
+ },
28
+ update: {
29
+ model: (innerprops) => client.updateModel(innerprops),
30
+ component: (innerprops) => client.updateComponent(innerprops),
31
+ enumeration: (innerprops) => client.updateEnumeration(innerprops),
32
+ simpleField: (innerprops) => client.updateSimpleField(innerprops),
33
+ enumerableField: (innerprops) => client.updateEnumerableField(innerprops),
34
+ componentField: (innerprops) => client.updateComponentField(innerprops),
35
+ relationalField: (innerprops) => client.updateRelationalField(innerprops),
36
+ unionField: (innerprops) => client.updateUnionField(innerprops),
37
+ componentUnionField: (innerprops) => client.updateComponentUnionField(innerprops),
38
+ },
39
+ delete: {
40
+ model: (innerprops) => client.deleteModel(innerprops),
41
+ component: (innerprops) => client.deleteComponent(innerprops),
42
+ enumeration: (innerprops) => client.deleteEnumeration(innerprops),
43
+ simpleField: (innerprops) => client.deleteField(innerprops),
44
+ enumerableField: (innerprops) => client.deleteField(innerprops),
45
+ componentField: (innerprops) => client.deleteField(innerprops),
46
+ relationalField: (innerprops) => client.deleteField(innerprops),
47
+ unionField: (innerprops) => client.deleteField(innerprops),
48
+ componentUnionField: (innerprops) => client.deleteField(innerprops),
49
+ },
50
+ }
51
+ : undefined;
52
+ /**
53
+ * This function is our variation on the client.migrationAction functions from the hygraph
54
+ * management sdk.
55
+ *
56
+ * MigrationAction() is better suited because it is a single function for all actions. More
57
+ * importantly, if the action fails, because a field with the apiID already exists for instance. it
58
+ * will skip this action but still continue the whole migration, while the management sdk function
59
+ * will bail.
60
+ *
61
+ * It takes the schema as argument, which is always the same.
62
+ *
63
+ * Then it takes the type of schema entity you want to do an action upon, and the action you want to
64
+ * do.
65
+ *
66
+ * The fourth arguments are the props that belong to the action you want to do. For instance, if you
67
+ * want to create a model, you need to pass the props that belong to a model.
68
+ *
69
+ * The last two arguments are optional. If you want to create a field, you need to pass the apiId of
70
+ * the model or component you want to create the field on. If you want to create a field on a
71
+ * component, you also need to pass the parentType, which is either 'model' or 'component'.
72
+ */
73
+ const migrationAction = (_schema, type, action, props, parentApiId, parentType) => {
74
+ /**
75
+ * Check if the entity already exists.
76
+ * If an update or deletion is made, it does not matter if the entity already exists
77
+ */
78
+ const alreadyExists = () => {
79
+ if (action !== 'create') {
80
+ return false;
81
+ }
82
+ switch (type) {
83
+ case 'model':
84
+ return schema.models.some((model) => model.apiId === props.apiId);
85
+ case 'component':
86
+ return schema.components.some((component) => component.apiId === props.apiId);
87
+ case 'enumeration':
88
+ return schema.enumerations.some((enumeration) => enumeration.apiId === props.apiId);
89
+ case 'simpleField':
90
+ case 'enumerableField':
91
+ case 'relationalField':
92
+ case 'unionField':
93
+ case 'componentUnionField': {
94
+ let parent;
95
+ switch (parentType) {
96
+ case 'model': {
97
+ parent = schema.models.find((model) => model.apiId === parentApiId);
98
+ break;
99
+ }
100
+ case 'component': {
101
+ parent = schema.components.find((component) => component.apiId === parentApiId);
102
+ break;
103
+ }
104
+ default:
105
+ return false;
106
+ }
107
+ return parent?.fields.some((field) => field.apiId === props.apiId);
108
+ }
109
+ default: {
110
+ return false;
111
+ }
112
+ }
113
+ };
114
+ const actionFunc = actionMap && actionMap[action] && actionMap[action][type];
115
+ if (!alreadyExists()) {
116
+ if (actionFunc) {
117
+ (0, graphCommerceLog_1.graphcommerceLog)(`${(0, graphCommerceLog_1.capitalize)(action)} ${type} with apiId ${props.apiId}...`);
118
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
119
+ // @ts-ignore | This error is a loss on typescript autocomplete, but the function is called correctly
120
+ actionFunc(props);
121
+ }
122
+ else {
123
+ (0, graphCommerceLog_1.graphcommerceLog)(`Action ${action} is not supported for ${type}`, 'error');
124
+ }
125
+ }
126
+ else {
127
+ (0, graphCommerceLog_1.graphcommerceLog)(`${(0, graphCommerceLog_1.capitalize)(type)} with apiId ${props.apiId} on ${parentApiId} already exists`, 'warning');
128
+ }
129
+ };
130
+ return {
131
+ migrationAction,
132
+ };
133
+ }
@@ -2,13 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.graphcommerce5to6 = void 0;
4
4
  const management_sdk_1 = require("@hygraph/management-sdk");
5
- const migrationAction_1 = require("../migrationAction");
6
- const graphcommerce5to6 = async (schema) => {
7
- if (!migrationAction_1.client) {
8
- return 0;
9
- }
5
+ const migrationActionFactory_1 = require("../migrationActionFactory");
6
+ const graphcommerce5to6 = async (schema, client) => {
7
+ const { migrationAction } = (0, migrationActionFactory_1.migrationActionFactory)(schema, client);
10
8
  // ? ENUMERATIONS
11
- (0, migrationAction_1.migrationAction)(schema, 'enumeration', 'create', {
9
+ migrationAction(schema, 'enumeration', 'create', {
12
10
  displayName: 'Row Links Variants',
13
11
  apiId: 'RowLinksVariants',
14
12
  values: [
@@ -19,13 +17,13 @@ const graphcommerce5to6 = async (schema) => {
19
17
  ],
20
18
  });
21
19
  // ? MODEL
22
- (0, migrationAction_1.migrationAction)(schema, 'model', 'create', {
20
+ migrationAction(schema, 'model', 'create', {
23
21
  apiId: 'RowLinks',
24
22
  apiIdPlural: 'RowLinksMultiple',
25
23
  displayName: 'Row Links',
26
24
  description: 'Row Links is a Row of PageLinks with different variants',
27
25
  });
28
- (0, migrationAction_1.migrationAction)(schema, 'simpleField', 'create', {
26
+ migrationAction(schema, 'simpleField', 'create', {
29
27
  displayName: 'Identity',
30
28
  apiId: 'identity',
31
29
  description: 'Only used for internal reference',
@@ -35,14 +33,14 @@ const graphcommerce5to6 = async (schema) => {
35
33
  isUnique: true,
36
34
  modelApiId: 'RowLinks',
37
35
  }, 'RowLinks', 'model');
38
- (0, migrationAction_1.migrationAction)(schema, 'enumerableField', 'create', {
36
+ migrationAction(schema, 'enumerableField', 'create', {
39
37
  displayName: 'Variant',
40
38
  apiId: 'linksVariant',
41
39
  parentApiId: 'RowLinks',
42
40
  enumerationApiId: 'RowLinksVariants',
43
41
  description: 'Different variants for Row Links',
44
42
  }, 'RowLinks', 'model');
45
- (0, migrationAction_1.migrationAction)(schema, 'simpleField', 'create', {
43
+ migrationAction(schema, 'simpleField', 'create', {
46
44
  displayName: 'Title',
47
45
  apiId: 'title',
48
46
  type: management_sdk_1.SimpleFieldType.String,
@@ -50,14 +48,14 @@ const graphcommerce5to6 = async (schema) => {
50
48
  modelApiId: 'RowLinks',
51
49
  isLocalized: true,
52
50
  }, 'RowLinks', 'model');
53
- (0, migrationAction_1.migrationAction)(schema, 'simpleField', 'create', {
51
+ migrationAction(schema, 'simpleField', 'create', {
54
52
  displayName: 'Copy',
55
53
  apiId: 'rowLinksCopy',
56
54
  type: management_sdk_1.SimpleFieldType.Richtext,
57
55
  isLocalized: true,
58
56
  modelApiId: 'RowLinks',
59
57
  }, 'RowLinks', 'model');
60
- (0, migrationAction_1.migrationAction)(schema, 'relationalField', 'create', {
58
+ migrationAction(schema, 'relationalField', 'create', {
61
59
  displayName: 'Links',
62
60
  apiId: 'pageLinks',
63
61
  modelApiId: 'RowLinks',
@@ -72,7 +70,7 @@ const graphcommerce5to6 = async (schema) => {
72
70
  visibility: management_sdk_1.VisibilityTypes.ReadWrite,
73
71
  isList: true,
74
72
  }, 'RowLinks', 'model');
75
- (0, migrationAction_1.migrationAction)(schema, 'unionField', 'update', {
73
+ migrationAction(schema, 'unionField', 'update', {
76
74
  apiId: 'content',
77
75
  displayName: 'Content',
78
76
  modelApiId: 'Page',
@@ -95,6 +93,6 @@ const graphcommerce5to6 = async (schema) => {
95
93
  // visibility: VisibilityTypes.Hidden, => Currently not supported for updateUnionField | https://github.com/hygraph/management-sdk/issues/34
96
94
  },
97
95
  }, 'Page', 'model');
98
- return migrationAction_1.client.run(true);
96
+ return client.run(true);
99
97
  };
100
98
  exports.graphcommerce5to6 = graphcommerce5to6;