mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Merge pull request #13739 from meteor/fix-id-strategy-default
Ensure normalize collection options properly respecting defaults
This commit is contained in:
@@ -3,7 +3,8 @@ import { AsyncMethods } from './methods_async';
|
||||
import { SyncMethods } from './methods_sync';
|
||||
import { IndexMethods } from './methods_index';
|
||||
import {
|
||||
ID_GENERATORS, normalizeOptions,
|
||||
ID_GENERATORS,
|
||||
normalizeOptions,
|
||||
setupAutopublish,
|
||||
setupConnection,
|
||||
setupDriver,
|
||||
@@ -266,5 +267,3 @@ Meteor.Collection = Mongo.Collection;
|
||||
|
||||
// Allow deny stuff is now in the allow-deny package
|
||||
Object.assign(Mongo.Collection.prototype, AllowDeny.CollectionPrototype);
|
||||
|
||||
|
||||
|
||||
@@ -88,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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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, { idGeneration: undefined });
|
||||
|
||||
// Insert a document
|
||||
var id = await collection.insertAsync({a: 1});
|
||||
|
||||
// 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');
|
||||
|
||||
// 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
|
||||
var defaultCollectionName = 'defaultIdGeneration2' + test.id;
|
||||
var defaultCollection = new Mongo.Collection(defaultCollectionName, { idGeneration: undefined });
|
||||
|
||||
// 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({});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user