Skip to content
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
8 changes: 8 additions & 0 deletions __testsUtils__/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { MongoMemoryServer } from 'mongodb-memory-server';
import type { MongoClient, Db } from 'mongodb';

declare global {
var mongoServer: MongoMemoryServer;
var mongoClient: MongoClient;
var db: Db;
}
2 changes: 1 addition & 1 deletion __testsUtils__/mongoValidation.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
export async function disableValidation(collections: string[]) {

Check warning on line 1 in __testsUtils__/mongoValidation.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
await setValidationLevel(collections, 'off');
}

export async function enableValidation(collections: string[]) {

Check warning on line 5 in __testsUtils__/mongoValidation.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
await setValidationLevel(collections, 'moderate');
}

async function setValidationLevel(

Check warning on line 9 in __testsUtils__/mongoValidation.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
collections: string[],
validationLevel: string,
) {
const errors: Record<string, any> = {};

Check warning on line 13 in __testsUtils__/mongoValidation.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
for (const collection of collections) {
try {
await global.db.runCommand({
await global.db.command({
collMod: collection,
validationLevel: validationLevel,
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/MongoBulkDataMigration.update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('MongoBulkDataMigration', () => {

describe('Bulk splitting', () => {
const END_OF_BULK_LOG = 'Documents migration is successful';
let update: UpdateFilter<{ value: number }>;
let update: (arg: { value: number }) => UpdateFilter<{ value: number }>;
beforeEach(async () => {
await collection.insertMany(
Array.from({ length: 100 }, (_, i) => ({ value: i + 1 })),
Expand Down
7 changes: 6 additions & 1 deletion src/MongoBulkDataMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,12 @@ export default class MongoBulkDataMigration<
try {
await migrationCollection.drop();
} catch (err) {
if (err?.message === 'ns not found') {
if (
err &&
typeof err === 'object' &&
'message' in err &&
err.message === 'ns not found'
) {
return;
}
throw err;
Expand Down
13 changes: 5 additions & 8 deletions src/lib/AbstractBulkOperationResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export abstract class AbstractBulkOperationResults<TSchema extends Document> {
this.collection = collection;
this.totalOperations = totalOperations;
this.logger = logger;
this.bulk = this.collection.initializeUnorderedBulkOp();
this.totalBulkOps = 0;
this.results = _.cloneDeep(INITIAL_BULK_INFOS);
this.initialize();
}

abstract logExecutionStatus(executionResults: BulkOperationResult): this;
Expand All @@ -78,12 +78,6 @@ export abstract class AbstractBulkOperationResults<TSchema extends Document> {
);
}

private initialize(): this {
this.bulk = this.collection.initializeUnorderedBulkOp();
this.totalBulkOps = 0;
return this;
}

public async execute(continueOnBulkWriteError = false): Promise<this> {
if (this.totalBulkOps === 0) {
return this;
Expand Down Expand Up @@ -117,7 +111,10 @@ export abstract class AbstractBulkOperationResults<TSchema extends Document> {
this.mergeResults(resultPartial);
this.logExecutionStatus(resultPartial);

return this.initialize();
this.bulk = this.collection.initializeUnorderedBulkOp();
this.totalBulkOps = 0;

return this;
}

private mergeResults(resultB: BulkOperationResult): this {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/computeRollbackQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function computeArrayFilterAndUnsetWithPositionals(
updateQuery: any,
backup: any,
): { arrayFilters: Document[]; unsetAdditionalPositional: any } {
const unsetAdditionalPositional = {};
const unsetAdditionalPositional: Record<string, number> = {};
const filteredObject = Object.keys(flattenDocument(backup));
const arrayFilters: Document[] = [];
const update = Object.keys(flattenDocument(updateQuery.$set ?? {}));
Expand Down
4 changes: 2 additions & 2 deletions src/testUtils/doRollbackAndAssertForInitialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import _ from 'lodash';
import MongoBulkDataMigration from '../MongoBulkDataMigration';
import { BulkOperationResult } from '../lib/AbstractBulkOperationResults';

let globalExpect = null;
let globalExpect: ((value: unknown) => unknown) | null = null;

export function setGlobalExpect(expect: any) {
globalExpect = expect;
Expand Down Expand Up @@ -59,7 +59,7 @@ function assertDeepHaveMembers(
effectiveDocs: any,
expectedDocs: any,
) {
const compareObjectId = (objA, objB) => {
const compareObjectId = (objA: string, objB: string) => {
const sortedIds = [objA, objB].sort();
return sortedIds[0] === objA ? -1 : 1;
};
Expand Down
6 changes: 4 additions & 2 deletions src/tools/arrayFilters.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Document } from 'mongodb';

/**
* Spots "positional operator" path in string for array filters
* Example: xxx.$[element_name].yyy
Expand Down Expand Up @@ -72,7 +74,7 @@ export function hasPathMatchingPositionalOperators(
*/
export function buildArrayFiltersOptionToUnset(path: string) {
let positionalElementNameMatch;
const arrayFilters = [];
const arrayFilters: Document[] = [];
do {
positionalElementNameMatch = ARRAY_FILTER_OPERATION_PATTERN.exec(path);
if (positionalElementNameMatch) {
Expand Down Expand Up @@ -115,7 +117,7 @@ function _buildSubPathWithPositionalElementAndNextChild(
`\\$\\[${positionalElementName}]\\.(\\w+)(?:\\.|$)`,
);

const nextChildName = matchRegexp.exec(completePath)[1];
const nextChildName = matchRegexp.exec(completePath)?.[1];

return `${positionalElementName}.${nextChildName}`;
}
22 changes: 14 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
{
"compilerOptions": {
/* Type Checking */
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"skipLibCheck": true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reviewers

❓ What is the error?

node_modules/mongodb-memory-server-core/lib/util/utils.d.ts:10:25 - error TS7016: Could not find a declaration file for module 'semver'. '/Users/hugo.prunaux/Desktop/dev/work/mongo-bulk-data-migration/node_modules/semver/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/semver` if it exists or add a new declaration (.d.ts) file containing `declare module 'semver';`

10 import * as semver from 'semver';
                           ~~~~~~~~


Found 1 error in node_modules/mongodb-memory-server-core/lib/util/utils.d.ts:10

"strict": true,
"useUnknownInCatchVariables": true,

"target": "es2022",
"module": "commonjs",
"lib": ["ES2022"],
Expand All @@ -10,14 +23,7 @@
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
"forceConsistentCasingInFileNames": true
},
"include": [
"jest.config.ts",
Expand Down
Loading