Skip to content

Commit

Permalink
Fix/HCK-4518 couchbase v7 plus alpha 3 fields order in reverse (#6)
Browse files Browse the repository at this point in the history
* RE: add saving field order for documents

* RE: add description for method
  • Loading branch information
serhii-filonenko authored Feb 21, 2024
1 parent db28338 commit c641d44
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
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 || '';
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 {
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 {{
* 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

0 comments on commit c641d44

Please sign in to comment.