Skip to content

Commit b2f0c4a

Browse files
committed
add marking partition keys
1 parent 0b3b279 commit b2f0c4a

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

reverse_engineering/api.js

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ module.exports = {
132132
fieldInference,
133133
includeEmptyCollection,
134134
indexes,
135-
bucketInfo
135+
bucketInfo,
136+
partitionKey,
136137
});
137138
packages.labels.push(nodesData);
138139
const labelNames = nodesData.reduce((result, packageData) => result.concat([packageData.collectionName]), []);
@@ -206,7 +207,8 @@ const getNodesData = (dbName, labels, logger, data) => {
206207
fieldInference: data.fieldInference,
207208
indexes: [],
208209
bucketIndexes: data.indexes,
209-
bucketInfo: data.bucketInfo
210+
bucketInfo: data.bucketInfo,
211+
partitionKey: data.partitionKey,
210212
});
211213
if (packageData) {
212214
packages.push(packageData);
@@ -274,15 +276,16 @@ const getRelationshipData = (schema, dbName, recordSamplingSettings, fieldInfere
274276
});
275277
};
276278

277-
const getLabelPackage = ({dbName, labelName, documents, includeEmptyCollection, bucketInfo, bucketIndexes, fieldInference}) => {
279+
const getLabelPackage = ({dbName, labelName, documents, includeEmptyCollection, bucketInfo, bucketIndexes, fieldInference, partitionKey}) => {
278280
let packageData = {
279281
dbName,
280282
collectionName: labelName,
281283
documents,
282284
views: [],
283285
emptyBucket: false,
284286
bucketInfo,
285-
bucketIndexes
287+
bucketIndexes,
288+
validation: createSchemaByPartitionKeyPath(partitionKey, documents),
286289
};
287290
if (fieldInference.active === 'field') {
288291
packageData.documentTemplate = documents.reduce((tpl, doc) => _.merge(tpl, doc), {});
@@ -302,6 +305,52 @@ const mapError = (error) => {
302305
};
303306
};
304307

308+
function createSchemaByPartitionKeyPath(path, documents = []) {
309+
const checkIfDocumentContaintPath = (path, document = {}) => {
310+
if (_.isEmpty(path)) {
311+
return true;
312+
}
313+
const value = _.get(document, `${path[0]}`);
314+
if (value) {
315+
return checkIfDocumentContaintPath(_.tail(path), value);
316+
}
317+
return false;
318+
}
319+
320+
const getNestedObject = (path) => {
321+
if (path.length === 1) {
322+
return {
323+
[path[0]]: {
324+
primaryKey: true,
325+
partitionKey: true,
326+
}
327+
}
328+
}
329+
return {
330+
[path[0]]: {
331+
properties: getNestedObject(_.tail(path))
332+
}
333+
};
334+
}
335+
336+
if (!path || typeof path !== 'string') {
337+
return false;
338+
}
339+
const namePath = _.tail(path.split('/'));
340+
if (namePath.length === 0) {
341+
return false;
342+
}
343+
if (!documents.some(doc => checkIfDocumentContaintPath(namePath, doc))) {
344+
return false;
345+
}
346+
347+
return {
348+
jsonSchema: {
349+
properties: getNestedObject(namePath)
350+
}
351+
};
352+
}
353+
305354
const setUpDocumentClient = (connectionInfo) => {
306355
const dbNameRegExp = /wss:\/\/(\S*).gremlin\.cosmos\./i;
307356
const dbName = dbNameRegExp.exec(connectionInfo.gremlinEndpoint)[1];

0 commit comments

Comments
 (0)