Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/HCK-4518 couchbase v7 plus alpha 3 fields order in reverse #6

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 59 additions & 16 deletions reverse_engineering/helpers/clusterHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,21 +401,21 @@ const getDbCollectionData = async ({
});
const documentKind = data.documentKinds?.[bucketName]?.documentKindName || '';
taras-dubyk marked this conversation as resolved.
Show resolved Hide resolved
const options = { limit, pagination: data.pagination, bucketName, scopeName, collectionName };
const fieldInference = data.fieldInference;

let documents = [];
let standardDocument = null;
let query = queryHelper.getSelectCollectionDocumentsQuery({ bucketName, scopeName, collectionName });

try {
documents = await getPaginatedQuery({ cluster, options, query, logger });

return schemaHelper.getDbCollectionData({
standardDocument = await getCollectionDocumentByDocumentId({
cluster,
bucketName,
scopeName,
collectionName,
documentKind,
documents,
collectionIndexes,
includeEmptyCollection,
documentId: documents[0]?.docid,
logger,
});
} catch (error) {
try {
taras-dubyk marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -429,6 +429,14 @@ const getDbCollectionData = async ({
collectionName,
});
documents = await getPaginatedQuery({ cluster, options, query, logger });
standardDocument = await getCollectionDocumentByDocumentId({
cluster,
bucketName,
scopeName,
collectionName: DEFAULT_NAME,
documentId: documents[0]?.docid,
logger,
});
break;
case COUCHBASE_ERROR_CODE.primaryIndexDoesNotExist:
documents = await getCollectionDocumentsByInfer({
Expand Down Expand Up @@ -463,17 +471,19 @@ const getDbCollectionData = async ({
}

logger.error(error);

return schemaHelper.getDbCollectionData({
bucketName,
scopeName,
collectionName,
documentKind,
documents,
collectionIndexes,
includeEmptyCollection,
});
}

return schemaHelper.getDbCollectionData({
bucketName,
scopeName,
collectionName,
documentKind,
documents,
collectionIndexes,
includeEmptyCollection,
standardDocument,
fieldInference,
});
};

/**
Expand Down Expand Up @@ -522,6 +532,39 @@ const getSelectedCollections = async ({ cluster, data, logger, app }) => {
}, {});
};

/**
* @description Function returns a document with original order of fields
* @param {{
serhii-filonenko marked this conversation as resolved.
Show resolved Hide resolved
* cluster: Cluster;
* bucketName: string;
* scopeName: string;
* collectionName: string;
* documentId?: string;
* logger: Logger;
* }} param0
* @returns {Promise<Document|null>}
*/
const getCollectionDocumentByDocumentId = async ({
cluster,
bucketName,
scopeName,
collectionName,
documentId,
logger,
}) => {
try {
const bucket = cluster.bucket(bucketName);
const scope = bucket.scope(scopeName);
const collection = scope.collection(collectionName);
const { content } = await collection.get(documentId);

return content;
} catch (error) {
logger.error(error);
return null;
}
};

module.exports = {
isBucketHasDefaultCollection,
getAllBuckets,
Expand Down
10 changes: 8 additions & 2 deletions reverse_engineering/helpers/schemaHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @typedef {import('../../shared/types').DbCollectionData} DbCollectionData
* @typedef {import('../../shared/types').Document} Document
* @typedef {import('../../shared/types').NameMap} NameMap
* @typedef {{ active: 'field' | 'alphabetical' }} FieldInference
*/
const _ = require('lodash');
const { DEFAULT_KEY_NAME, DEFAULT_NAME } = require('../../shared/constants');
Expand All @@ -14,7 +15,10 @@ const { DEFAULT_KEY_NAME, DEFAULT_NAME } = require('../../shared/constants');
* collectionName: string;
* documentKind: string;
* collectionIndexes: object[];
* includeEmptyCollection: boolean }} param0
* includeEmptyCollection: boolean;
* standardDocument: Document | null;
* fieldInference: FieldInference
* }} param0
* @returns {DbCollectionData}
*/
const getDbCollectionData = ({
Expand All @@ -25,14 +29,16 @@ const getDbCollectionData = ({
documentKind,
collectionIndexes,
includeEmptyCollection,
standardDocument,
fieldInference,
}) => {
const jsonDocuments = documents
.filter(item => _.isPlainObject(item[bucketName]))
.map(item => ({
[DEFAULT_KEY_NAME]: item.docid,
...item[bucketName],
}));
const standardDoc = _.first(jsonDocuments);
const standardDoc = fieldInference.active === 'field' ? standardDocument : null;
const emptyBucket = !includeEmptyCollection && _.isEmpty(jsonDocuments);

return {
Expand Down