Skip to content

Commit f041357

Browse files
authored
[HCK-10854]Fix/ RE/FE and gremlin with node 22 (#41)
* we don't use ssh in cosmosdb (at least not here) * Update deps and fix gremlin with node 22 * Cleanup * Fix plugin RE/FE apply * Add backward compatibility
1 parent a68abc9 commit f041357

File tree

12 files changed

+659
-473
lines changed

12 files changed

+659
-473
lines changed

forward_engineering/api.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = {
2929
}
3030
const progress = createLogger(logger, containerProps.dbId, graphName);
3131

32-
const cosmosClient = applyToInstanceHelper(_).setUpDocumentClient(data);
32+
const cosmosClient = applyToInstanceHelper().setUpDocumentClient(data);
3333

3434
progress('Create database if not exists ...');
3535

@@ -41,9 +41,9 @@ module.exports = {
4141

4242
const containerResponse = await cosmosClient.database(containerProps.dbId).containers.createIfNotExists({
4343
id: graphName,
44-
partitionKey: getPartitionKey(_)(data.containerData),
45-
...applyToInstanceHelper(_).getContainerThroughputProps(containerProps),
46-
defaultTtl: applyToInstanceHelper(_).getTTL(containerProps),
44+
partitionKey: getPartitionKey(data.containerData),
45+
...applyToInstanceHelper().getContainerThroughputProps(containerProps),
46+
defaultTtl: applyToInstanceHelper().getTTL(containerProps),
4747
});
4848

4949
progress('Applying Cosmos DB script ...');
@@ -66,19 +66,19 @@ module.exports = {
6666
const storedProcs = _.get(cosmosDBScript, 'Stored Procedures', []);
6767
if (storedProcs.length) {
6868
progress('Upload stored procs ...');
69-
await applyToInstanceHelper(_).createStoredProcs(storedProcs, containerResponse.container);
69+
await applyToInstanceHelper().createStoredProcs(storedProcs, containerResponse.container);
7070
}
7171

7272
const udfs = _.get(cosmosDBScript, 'User Defined Functions', []);
7373
if (udfs.length) {
7474
progress('Upload user defined functions ...');
75-
await applyToInstanceHelper(_).createUDFs(udfs, containerResponse.container);
75+
await applyToInstanceHelper().createUDFs(udfs, containerResponse.container);
7676
}
7777

7878
const triggers = _.get(cosmosDBScript, 'Triggers', []);
7979
if (triggers.length) {
8080
progress('Upload triggers ...');
81-
await applyToInstanceHelper(_).createTriggers(triggers, containerResponse.container);
81+
await applyToInstanceHelper().createTriggers(triggers, containerResponse.container);
8282
}
8383

8484
if (!gremlinScript) {
@@ -87,16 +87,16 @@ module.exports = {
8787

8888
progress('Applying Gremlin script ...');
8989

90-
const { labels, edges } = applyToInstanceHelper(_).parseScriptStatements(gremlinScript);
91-
const gremlinClient = await applyToInstanceHelper(_).getGremlinClient(data, containerProps.dbId, graphName);
90+
const { labels, edges } = applyToInstanceHelper().parseScriptStatements(gremlinScript);
91+
const gremlinClient = await applyToInstanceHelper().getGremlinClient(data, containerProps.dbId, graphName);
9292

9393
progress('Uploading labels ...');
9494

95-
await applyToInstanceHelper(_).runGremlinQueries(gremlinClient, labels);
95+
await applyToInstanceHelper().runGremlinQueries(gremlinClient, labels);
9696

9797
progress('Uploading edges ...');
9898

99-
await applyToInstanceHelper(_).runGremlinQueries(gremlinClient, edges);
99+
await applyToInstanceHelper().runGremlinQueries(gremlinClient, edges);
100100

101101
cb();
102102
} catch (err) {
@@ -109,8 +109,8 @@ module.exports = {
109109
logger.clear();
110110
logger.log('info', connectionInfo, 'Test connection', connectionInfo.hiddenKeys);
111111
try {
112-
const client = applyToInstanceHelper(_).setUpDocumentClient(connectionInfo);
113-
await applyToInstanceHelper(_).testConnection(client);
112+
const client = applyToInstanceHelper().setUpDocumentClient(connectionInfo);
113+
await applyToInstanceHelper().testConnection(client);
114114
return cb();
115115
} catch (err) {
116116
logger.log('error', mapError(err), 'Connection failed');

forward_engineering/applyToInstanceHelper.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
const { CosmosClient, StoredProcedure, UserDefinedFunction, Trigger } = require('@azure/cosmos');
22
const gremlin = require('gremlin');
3+
const _ = require('lodash');
34

4-
const applyToInstanceHelper = _ => ({
5+
const applyToInstanceHelper = () => ({
56
setUpDocumentClient(connectionInfo) {
6-
const dbNameRegExp = /wss:\/\/(\S*).gremlin\.cosmos\./i;
7-
const dbName = dbNameRegExp.exec(connectionInfo.gremlinEndpoint);
8-
if (!dbName?.[1]) {
9-
throw new Error('Incorrect endpoint provided. Expected format: wss://<account name>.gremlin.cosmos.');
10-
}
11-
const endpoint = `https://${dbName[1]}.documents.azure.com:443/`;
7+
const dbName = connectionInfo.azureCosmosdbAccount;
8+
const endpoint = `https://${dbName}.documents.azure.com:443/`;
129
const key = connectionInfo.accountKey;
1310

1411
return new CosmosClient({ endpoint, key });
1512
},
1613

1714
async getGremlinClient(connectionInfo, databaseId, collection) {
15+
const gremlinEndpoint = `wss://${connectionInfo.azureCosmosdbAccount}.gremlin.cosmos.azure.com`;
1816
const traversalSource = 'g';
1917

2018
const authenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator(
2119
`/dbs/${databaseId}/colls/${collection}`,
2220
connectionInfo.accountKey,
2321
);
2422

25-
const client = new gremlin.driver.Client(connectionInfo.gremlinEndpoint, {
23+
const client = new gremlin.driver.Client(gremlinEndpoint, {
2624
authenticator,
2725
traversalSource,
2826
rejectUnauthorized: true,
@@ -154,7 +152,8 @@ const applyToInstanceHelper = _ => ({
154152
},
155153

156154
getContainerThroughputProps(containerProps) {
157-
if (containerProps.capacityMode === 'Serverless') {
155+
const capacityModesToSkip = ['Serverless', 'Provisioned throughput'];
156+
if (capacityModesToSkip.includes(containerProps.capacityMode)) {
158157
return {};
159158
}
160159
if (containerProps.autopilot) {

forward_engineering/generateContainerScript.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const generateEdges = (collections, relationships, jsonData) => {
113113
return edges.join(';\n\n') + ';';
114114
};
115115

116-
const getGremlinScript = (_, data) => {
116+
const getGremlinScript = data => {
117117
let { collections, relationships, jsonData, containerData, options } = data;
118118
let resultScript = '';
119119
const traversalSource = _.get(containerData, [0, 'traversalSource'], 'g');
@@ -147,16 +147,23 @@ const getGremlinScript = (_, data) => {
147147
return resultScript;
148148
};
149149

150-
const getCosmosDbScript = (_, containerData) => {
151-
return JSON.stringify(
152-
{
153-
partitionKey: getPartitionKey(_)(containerData),
154-
indexingPolicy: getIndexPolicyScript(_)(containerData),
155-
...scriptHelper.addItems(_)(containerData),
156-
},
157-
null,
158-
2,
159-
);
150+
const getCosmosDbScript = containerData => {
151+
const partitionKey = getPartitionKey(containerData);
152+
153+
const getContainerConfig = () => {
154+
const baseConfig = {
155+
indexingPolicy: getIndexPolicyScript(containerData),
156+
...scriptHelper.addItems(containerData),
157+
};
158+
if (partitionKey) {
159+
return {
160+
partitionKey,
161+
...baseConfig,
162+
};
163+
}
164+
return baseConfig;
165+
};
166+
return JSON.stringify(getContainerConfig(), null, 2);
160167
};
161168

162169
const generateContainerScript = (data, logger, cb, app) => {
@@ -167,16 +174,16 @@ const generateContainerScript = (data, logger, cb, app) => {
167174
if (data.options.origin === 'ui') {
168175
cb(null, [
169176
{
170-
script: getGremlinScript(_, data),
177+
script: getGremlinScript(data),
171178
},
172179
{
173-
script: getCosmosDbScript(_, data.containerData),
180+
script: getCosmosDbScript(data.containerData),
174181
},
175182
]);
176183
} else if (scriptId === 'cosmosdb') {
177-
cb(null, getCosmosDbScript(_, data.containerData));
184+
cb(null, getCosmosDbScript(data.containerData));
178185
} else {
179-
cb(null, getGremlinScript(_, data));
186+
cb(null, getGremlinScript(data));
180187
}
181188
} catch (e) {
182189
logger.log('error', { message: e.message, stack: e.stack }, 'Forward-Engineering Error');

forward_engineering/getIndexPolicyScript.js

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _ = require('lodash');
2+
13
const add = (key, value) => obj => {
24
if (value === undefined || value === '' || (Array.isArray(value) && value.length === 0)) {
35
return obj;
@@ -61,73 +63,65 @@ const filterDeactivated = items => {
6163
});
6264
};
6365

64-
const getIncludedPath =
65-
_ =>
66-
(includedPaths = []) => {
67-
return filterDeactivated(includedPaths)
68-
.map(item => {
69-
return _.flow(add('path', getPath(item.indexIncludedPath)))({});
70-
})
71-
.filter(item => !_.isEmpty(item));
72-
};
66+
const getIncludedPath = (includedPaths = []) => {
67+
return filterDeactivated(includedPaths)
68+
.map(item => {
69+
return _.flow(add('path', getPath(item.indexIncludedPath)))({});
70+
})
71+
.filter(item => !_.isEmpty(item));
72+
};
7373

74-
const getExcludedPath =
75-
_ =>
76-
(excludedPaths = []) => {
77-
return filterDeactivated(excludedPaths)
78-
.map(item => {
79-
return _.flow(add('path', getPath(item.indexExcludedPath)))({});
80-
})
81-
.filter(item => !_.isEmpty(item));
82-
};
74+
const getExcludedPath = (excludedPaths = []) => {
75+
return filterDeactivated(excludedPaths)
76+
.map(item => {
77+
return _.flow(add('path', getPath(item.indexExcludedPath)))({});
78+
})
79+
.filter(item => !_.isEmpty(item));
80+
};
8381

84-
const getCompositeIndexes =
85-
_ =>
86-
(compositeIndexes = []) => {
87-
return filterDeactivated(compositeIndexes)
88-
.map(item => {
89-
if (!Array.isArray(item.compositeFieldPath)) {
90-
return;
91-
}
92-
93-
return _.uniqWith(
94-
item.compositeFieldPath.map(item => {
95-
const path = item.name.split('/');
96-
97-
return {
98-
path: ['', ...path.slice(1).map(prepareName)].join('/'),
99-
order: item.type || 'ascending',
100-
};
101-
}),
102-
(a, b) => a.path === b.path,
103-
).filter(item => !_.isEmpty(item));
104-
})
105-
.filter(item => !_.isEmpty(item));
106-
};
82+
const getCompositeIndexes = (compositeIndexes = []) => {
83+
return filterDeactivated(compositeIndexes)
84+
.map(item => {
85+
if (!Array.isArray(item.compositeFieldPath)) {
86+
return;
87+
}
10788

108-
const getSpatialIndexes =
109-
_ =>
110-
(spatialIndexes = []) => {
111-
return filterDeactivated(spatialIndexes)
112-
.map(item => {
113-
return _.flow(
114-
add('path', getPath(item.indexIncludedPath)),
115-
add('types', (item.dataTypes || []).map(dataType => dataType.spatialType).filter(Boolean)),
116-
)({});
117-
})
118-
.filter(item => !_.isEmpty(item) && item.path);
119-
};
89+
return _.uniqWith(
90+
item.compositeFieldPath.map(item => {
91+
const path = item.name.split('/');
92+
93+
return {
94+
path: ['', ...path.slice(1).map(prepareName)].join('/'),
95+
order: item.type || 'ascending',
96+
};
97+
}),
98+
(a, b) => a.path === b.path,
99+
).filter(item => !_.isEmpty(item));
100+
})
101+
.filter(item => !_.isEmpty(item));
102+
};
103+
104+
const getSpatialIndexes = (spatialIndexes = []) => {
105+
return filterDeactivated(spatialIndexes)
106+
.map(item => {
107+
return _.flow(
108+
add('path', getPath(item.indexIncludedPath)),
109+
add('types', (item.dataTypes || []).map(dataType => dataType.spatialType).filter(Boolean)),
110+
)({});
111+
})
112+
.filter(item => !_.isEmpty(item) && item.path);
113+
};
120114

121-
const getIndexPolicyScript = _ => containerData => {
115+
const getIndexPolicyScript = containerData => {
122116
const indexTab = containerData[1] || {};
123117

124118
return _.flow(
125119
add('automatic', indexTab.indexingAutomatic === 'true'),
126120
add('indexingMode', indexTab.indexingMode),
127-
add('includedPaths', getIncludedPath(_)(indexTab.includedPaths)),
128-
add('excludedPaths', getExcludedPath(_)(indexTab.excludedPaths)),
129-
add('spatialIndexes', getSpatialIndexes(_)(indexTab.spatialIndexes)),
130-
add('compositeIndexes', getCompositeIndexes(_)(indexTab.compositeIndexes)),
121+
add('includedPaths', getIncludedPath(indexTab.includedPaths)),
122+
add('excludedPaths', getExcludedPath(indexTab.excludedPaths)),
123+
add('spatialIndexes', getSpatialIndexes(indexTab.spatialIndexes)),
124+
add('compositeIndexes', getCompositeIndexes(indexTab.compositeIndexes)),
131125
)({});
132126
};
133127

forward_engineering/helpers/getPartitionKey.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
const getPartitionKey = _ => containerData => {
2-
return _.get(containerData, '[0].partitionKey[0].name', '').trim().replace(/\/$/, '');
1+
const _ = require('lodash');
2+
3+
const getPartitionKey = containerData => {
4+
return _.get(containerData, '[0].partitionKey[0].name', '').trim().replace(/\/$/, null);
35
};
46

57
module.exports = {

0 commit comments

Comments
 (0)