From 833ba0247d738c0754fbb9b64c7d86264200335b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Codo=C3=B1er?= Date: Mon, 12 May 2025 16:54:37 +0200 Subject: [PATCH 1/4] ensure default id generation strategy --- packages/mongo/collection/collection.js | 9 ++++++--- packages/mongo/collection/collection_utils.js | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/mongo/collection/collection.js b/packages/mongo/collection/collection.js index 28776c9a93..5e248c389b 100644 --- a/packages/mongo/collection/collection.js +++ b/packages/mongo/collection/collection.js @@ -3,7 +3,9 @@ import { AsyncMethods } from './methods_async'; import { SyncMethods } from './methods_sync'; import { IndexMethods } from './methods_index'; import { - ID_GENERATORS, normalizeOptions, + ID_GENERATION_DEFAULT, + ID_GENERATORS, + normalizeOptions, setupAutopublish, setupConnection, setupDriver, @@ -41,7 +43,9 @@ Mongo.Collection = function Collection(name, options) { options = normalizeOptions(options); - this._makeNewID = ID_GENERATORS[options.idGeneration]?.(name); + this._makeNewID = ( + ID_GENERATORS[options.idGeneration] || ID_GENERATION_DEFAULT + )?.(name); this._transform = LocalCollection.wrapTransform(options.transform); this.resolverType = options.resolverType; @@ -265,4 +269,3 @@ Meteor.Collection = Mongo.Collection; // Allow deny stuff is now in the allow-deny package Object.assign(Mongo.Collection.prototype, AllowDeny.CollectionPrototype); - diff --git a/packages/mongo/collection/collection_utils.js b/packages/mongo/collection/collection_utils.js index a0f7442ded..1105c0f209 100644 --- a/packages/mongo/collection/collection_utils.js +++ b/packages/mongo/collection/collection_utils.js @@ -13,6 +13,8 @@ export const ID_GENERATORS = { } }; +export const ID_GENERATION_DEFAULT = ID_GENERATORS.STRING; + export function setupConnection(name, options) { if (!name || options.connection === null) return null; if (options.connection) return options.connection; From aa81324fa7936bcbc847be21438290996e8fcfd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Codo=C3=B1er?= Date: Mon, 12 May 2025 17:10:25 +0200 Subject: [PATCH 2/4] ensure default id generation strategy --- packages/mongo/collection/collection.js | 4 +- packages/mongo/collection/collection_utils.js | 9 ++- packages/mongo/tests/collection_tests.js | 55 +++++++++++++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/packages/mongo/collection/collection.js b/packages/mongo/collection/collection.js index 5e248c389b..7d6eac5a6a 100644 --- a/packages/mongo/collection/collection.js +++ b/packages/mongo/collection/collection.js @@ -43,9 +43,7 @@ Mongo.Collection = function Collection(name, options) { options = normalizeOptions(options); - this._makeNewID = ( - ID_GENERATORS[options.idGeneration] || ID_GENERATION_DEFAULT - )?.(name); + this._makeNewID = ID_GENERATORS[options.idGeneration]?.(name); this._transform = LocalCollection.wrapTransform(options.transform); this.resolverType = options.resolverType; diff --git a/packages/mongo/collection/collection_utils.js b/packages/mongo/collection/collection_utils.js index 1105c0f209..e6c79646d5 100644 --- a/packages/mongo/collection/collection_utils.js +++ b/packages/mongo/collection/collection_utils.js @@ -13,8 +13,6 @@ export const ID_GENERATORS = { } }; -export const ID_GENERATION_DEFAULT = ID_GENERATORS.STRING; - export function setupConnection(name, options) { if (!name || options.connection === null) return null; if (options.connection) return options.connection; @@ -90,12 +88,17 @@ export function normalizeOptions(options) { options.connection = options.manager; } + const cleanedOptions = Object.fromEntries( + Object.entries(options || {}).filter(([_, v]) => v !== undefined), + ); + + // 2) Spread defaults first, then only the defined overrides return { connection: undefined, idGeneration: 'STRING', transform: null, _driver: undefined, _preventAutopublish: false, - ...options, + ...cleanedOptions, }; } diff --git a/packages/mongo/tests/collection_tests.js b/packages/mongo/tests/collection_tests.js index a0e9e7fecf..1d10431c7f 100644 --- a/packages/mongo/tests/collection_tests.js +++ b/packages/mongo/tests/collection_tests.js @@ -485,3 +485,58 @@ Meteor.isServer && Tinytest.addAsync('collection - simple add', async function(t test.equal((await collection.findOneAsync(id)).a, 2); await collection.removeAsync({}); }); + +Tinytest.addAsync('collection - default idGeneration when not provided', async function(test) { + if (!Meteor.isServer) { + return; + } + // Create a collection without specifying idGeneration option + var collectionName = 'defaultIdGeneration' + test.id; + var collection = new Mongo.Collection(collectionName); + + // Insert a document + var id = await collection.insertAsync({a: 1}); + + // Verify that the _id is a string (since ID_GENERATION_DEFAULT is ID_GENERATORS.STRING) + test.isTrue(typeof id === 'string', 'Document _id should be a string when no idGeneration option is provided'); + test.isFalse(id instanceof Mongo.ObjectID, 'Document _id should not be a Mongo.ObjectID when no idGeneration option is provided'); + + // Clean up + await collection.removeAsync({}); +}); + +Tinytest.addAsync('collection - compare default idGeneration with explicit idGeneration', async function(test) { + if (!Meteor.isServer) { + return; + } + // Create a collection without specifying idGeneration option (should use ID_GENERATION_DEFAULT) + var defaultCollectionName = 'defaultIdGeneration2' + test.id; + var defaultCollection = new Mongo.Collection(defaultCollectionName); + + // Create a collection with explicit STRING idGeneration + var stringCollectionName = 'stringIdGeneration' + test.id; + var stringCollection = new Mongo.Collection(stringCollectionName, { idGeneration: 'STRING' }); + + // Create a collection with MONGO idGeneration + var mongoCollectionName = 'mongoIdGeneration' + test.id; + var mongoCollection = new Mongo.Collection(mongoCollectionName, { idGeneration: 'MONGO' }); + + // Insert documents + var defaultId = await defaultCollection.insertAsync({a: 1}); + var stringId = await stringCollection.insertAsync({a: 1}); + var mongoId = await mongoCollection.insertAsync({a: 1}); + + // Verify default behaves like STRING + test.isTrue(typeof defaultId === 'string', 'Default idGeneration should produce string IDs'); + test.isTrue(typeof stringId === 'string', 'STRING idGeneration should produce string IDs'); + test.isFalse(defaultId instanceof Mongo.ObjectID, 'Default idGeneration should not produce Mongo.ObjectID'); + test.isFalse(stringId instanceof Mongo.ObjectID, 'STRING idGeneration should not produce Mongo.ObjectID'); + + // Verify MONGO produces ObjectIDs + test.isTrue(mongoId instanceof Mongo.ObjectID, 'MONGO idGeneration should produce Mongo.ObjectID'); + + // Clean up + await defaultCollection.removeAsync({}); + await stringCollection.removeAsync({}); + await mongoCollection.removeAsync({}); +}); From 9ea72dd5f43fbd073159f5160d7296678cde020e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Codo=C3=B1er?= Date: Mon, 12 May 2025 17:12:48 +0200 Subject: [PATCH 3/4] ensure default id generation strategy --- packages/mongo/tests/collection_tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mongo/tests/collection_tests.js b/packages/mongo/tests/collection_tests.js index 1d10431c7f..dd78eb58b3 100644 --- a/packages/mongo/tests/collection_tests.js +++ b/packages/mongo/tests/collection_tests.js @@ -492,7 +492,7 @@ Tinytest.addAsync('collection - default idGeneration when not provided', async f } // Create a collection without specifying idGeneration option var collectionName = 'defaultIdGeneration' + test.id; - var collection = new Mongo.Collection(collectionName); + var collection = new Mongo.Collection(collectionName, { idGeneration: undefined }); // Insert a document var id = await collection.insertAsync({a: 1}); @@ -511,7 +511,7 @@ Tinytest.addAsync('collection - compare default idGeneration with explicit idGen } // Create a collection without specifying idGeneration option (should use ID_GENERATION_DEFAULT) var defaultCollectionName = 'defaultIdGeneration2' + test.id; - var defaultCollection = new Mongo.Collection(defaultCollectionName); + var defaultCollection = new Mongo.Collection(defaultCollectionName, { idGeneration: undefined }); // Create a collection with explicit STRING idGeneration var stringCollectionName = 'stringIdGeneration' + test.id; From fd9b8443b0597e48403734435cb819edbf6a3cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Codo=C3=B1er?= Date: Mon, 12 May 2025 18:08:18 +0200 Subject: [PATCH 4/4] clean references of ID_GENERATION_DEFAULT as not used --- packages/mongo/collection/collection.js | 1 - packages/mongo/tests/collection_tests.js | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/mongo/collection/collection.js b/packages/mongo/collection/collection.js index 7d6eac5a6a..e587f398d0 100644 --- a/packages/mongo/collection/collection.js +++ b/packages/mongo/collection/collection.js @@ -3,7 +3,6 @@ import { AsyncMethods } from './methods_async'; import { SyncMethods } from './methods_sync'; import { IndexMethods } from './methods_index'; import { - ID_GENERATION_DEFAULT, ID_GENERATORS, normalizeOptions, setupAutopublish, diff --git a/packages/mongo/tests/collection_tests.js b/packages/mongo/tests/collection_tests.js index dd78eb58b3..e8841c5892 100644 --- a/packages/mongo/tests/collection_tests.js +++ b/packages/mongo/tests/collection_tests.js @@ -497,7 +497,7 @@ Tinytest.addAsync('collection - default idGeneration when not provided', async f // Insert a document var id = await collection.insertAsync({a: 1}); - // Verify that the _id is a string (since ID_GENERATION_DEFAULT is ID_GENERATORS.STRING) + // Verify that the _id is a string test.isTrue(typeof id === 'string', 'Document _id should be a string when no idGeneration option is provided'); test.isFalse(id instanceof Mongo.ObjectID, 'Document _id should not be a Mongo.ObjectID when no idGeneration option is provided'); @@ -509,7 +509,7 @@ Tinytest.addAsync('collection - compare default idGeneration with explicit idGen if (!Meteor.isServer) { return; } - // Create a collection without specifying idGeneration option (should use ID_GENERATION_DEFAULT) + // Create a collection without specifying idGeneration option var defaultCollectionName = 'defaultIdGeneration2' + test.id; var defaultCollection = new Mongo.Collection(defaultCollectionName, { idGeneration: undefined });