Skip to content

Commit

Permalink
Merge pull request #449 from Meteor-Community-Packages/fix-bypasscoll…
Browse files Browse the repository at this point in the history
…ection2

Make compatible with the new RC
  • Loading branch information
jankapunkt committed Jun 14, 2024
2 parents a0a3944 + 3a12b53 commit 282c242
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 641 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [4.0.0-beta.5](#400-beta.3)
- [4.0.2](#402)
- [4.0.1](#401)
- [4.0.0](#400)
- [3.5.0](#350)
- [3.4.1](#341)
- [3.4.0](#340)
Expand Down Expand Up @@ -78,6 +80,16 @@

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## 4.0.2

- Make collection2 compatible with the newly released RC
- Move common code between `_methodMutation` and `_methodMutationAsync` into its own code

## 4.0.1

- Fix dynamic import https://github.com/Meteor-Community-Packages/meteor-collection2/pull/450


## 4.0.0

- Make collection2 compatible with Meteor 3.0 thanks to the [awesome work](https://github.com/Meteor-Community-Packages/meteor-collection2/pull/443) by @klablink
Expand Down
29 changes: 15 additions & 14 deletions package/collection2/.versions
186 changes: 79 additions & 107 deletions package/collection2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,121 +173,93 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
return null;
};
});

function getArgumentsAndValidationContext(methodName, args, async) {
let options = isInsertType(methodName) ? args[1] : args[2];

// Support missing options arg
if (!options || typeof options === 'function') {
options = {};
}

let validationContext = {};
if (this._c2 && options.bypassCollection2 !== true) {
let userId = null;
try {
// https://github.com/aldeed/meteor-collection2/issues/175
userId = Meteor.userId();
} catch (err) {}

[args, validationContext] = doValidate(
this,
methodName,
args,
Meteor.isServer || this._connection === null, // getAutoValues
userId,
Meteor.isServer, // isFromTrustedCode
async
);

if (!args) {
// doValidate already called the callback or threw the error, so we're done.
// But insert should always return an ID to match core behavior.
return isInsertType(methodName) ? this._makeNewID() : undefined;
}
} else {
// We still need to adjust args because insert does not take options
if (isInsertType(methodName) && typeof args[1] !== 'function') args.splice(1, 1);
}

return [args, validationContext];
}

function _methodMutation(async, methodName) {
function _methodMutation(async, methodName) {
const _super = Meteor.isFibersDisabled
? Mongo.Collection.prototype[methodName]
: Mongo.Collection.prototype[methodName.replace('Async', '')];

? Mongo.Collection.prototype[methodName]
: Mongo.Collection.prototype[methodName.replace('Async', '')];
if (!_super) return;

Mongo.Collection.prototype[methodName] = function (...args) {
let options = isInsertType(methodName) ? args[1] : args[2];

// Support missing options arg
if (!options || typeof options === 'function') {
options = {};
}

let validationContext = {};
let error;
if (this._c2 && options.bypassCollection2 !== true) {
let userId = null;
try {
// https://github.com/aldeed/meteor-collection2/issues/175
userId = Meteor.userId();
} catch (err) {}

[args, validationContext] = doValidate(
this,
methodName,
args,
Meteor.isServer || this._connection === null, // getAutoValues
userId,
Meteor.isServer, // isFromTrustedCode
async
);

if (!args) {
// doValidate already called the callback or threw the error, so we're done.
// But insert should always return an ID to match core behavior.
return isInsertType(methodName) ? this._makeNewID() : undefined;
}
} else {
// We still need to adjust args because insert does not take options
if (isInsertType(methodName) && typeof args[1] !== 'function') args.splice(1, 1);
}

if (async && !Meteor.isFibersDisabled) {
try {
this[methodName.replace('Async', '')].isCalledFromAsync = true;
_super.isCalledFromAsync = true;
return Promise.resolve(_super.apply(this, args));
} catch (err) {
const addValidationErrorsPropName =
typeof validationContext.addValidationErrors === 'function'
? 'addValidationErrors'
: 'addInvalidKeys';
parsingServerError([err], validationContext, addValidationErrorsPropName);
error = getErrorObject(validationContext, err.message, err.code);
return Promise.reject(error);
}
} else {
return _super.apply(this, args);
}
[args, validationContext] = getArgumentsAndValidationContext.call(this, methodName, args, async);

if (async && !Meteor.isFibersDisabled) {
try {
this[methodName.replace('Async', '')].isCalledFromAsync = true;
_super.isCalledFromAsync = true;
return Promise.resolve(_super.apply(this, args));
} catch (err) {
const addValidationErrorsPropName =
typeof validationContext.addValidationErrors === 'function'
? 'addValidationErrors'
: 'addInvalidKeys';
parsingServerError([err], validationContext, addValidationErrorsPropName);
const error = getErrorObject(validationContext, err.message, err.code);
return Promise.reject(error);
}
} else {
return _super.apply(this, args);
}
};
}

function _methodMutationAsync(methodName) {
}
function _methodMutationAsync(methodName) {
const _super = Mongo.Collection.prototype[methodName];
Mongo.Collection.prototype[methodName] = async function (...args) {
let options = isInsertType(methodName) ? args[1] : args[2];

// Support missing options arg
if (!options || typeof options === 'function') {
options = {};
}

let validationContext = {};
if (this._c2 && options.bypassCollection2 !== true) {
let userId = null;
try {
// https://github.com/aldeed/meteor-collection2/issues/175
userId = Meteor.userId();
} catch (err) {}

[args, validationContext] = doValidate(
this,
methodName,
args,
Meteor.isServer || this._connection === null, // getAutoValues
userId,
Meteor.isServer, // isFromTrustedCode
true
);

if (!args) {
// doValidate already called the callback or threw the error, so we're done.
// But insert should always return an ID to match core behavior.
return isInsertType(methodName) ? this._makeNewID() : undefined;
}
} else {
// We still need to adjust args because insert does not take options
if (methodName === 'insert' && typeof args[1] !== 'function') args.splice(1, 1);
}

try {
return await _super.apply(this, args);
} catch (err) {
const addValidationErrorsPropName =
typeof validationContext.addValidationErrors === 'function'
? 'addValidationErrors'
: 'addInvalidKeys';
parsingServerError([err], validationContext, addValidationErrorsPropName);
throw getErrorObject(validationContext, err.message, err.code);
}
[args, validationContext] = getArgumentsAndValidationContext.call(this, methodName, args, true);

try {
return await _super.apply(this, args);
} catch (err) {
const addValidationErrorsPropName =
typeof validationContext.addValidationErrors === 'function'
? 'addValidationErrors'
: 'addInvalidKeys';
parsingServerError([err], validationContext, addValidationErrorsPropName);
throw getErrorObject(validationContext, err.message, err.code);
}
};
}
}


// Wrap DB write operation methods
if (Mongo.Collection.prototype.insertAsync) {
Expand Down
17 changes: 9 additions & 8 deletions package/collection2/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Package.describe({
name: 'aldeed:collection2',
summary:
'Automatic validation of Meteor Mongo insert and update operations on the client and server',
version: '4.0.1',
version: '4.0.2',
documentation: '../../README.md',
git: 'https://github.com/aldeed/meteor-collection2.git'
});
Expand All @@ -16,14 +16,14 @@ Npm.depends({
});

Package.onUse(function (api) {
api.versionsFrom(['1.12.1', '2.3', '3.0-beta.0']);
api.versionsFrom(['1.12.1', '2.3', '3.0-rc.4']);
api.use('mongo');
api.imply('mongo');
api.use('minimongo');
api.use('ejson');
api.use('raix:[email protected]');
api.use('ecmascript');
api.use('aldeed:[email protected] || 1.13.1');
api.use('raix:[email protected]');
api.use('aldeed:[email protected] || 2.0.0-beta300.0');

api.addFiles(['./collection2.js']);

Expand All @@ -34,8 +34,9 @@ Package.onUse(function (api) {
});

Package.onTest(function (api) {
api.versionsFrom(['1.12.1', '2.3', '3.0-rc.4']);
api.use([
'meteortesting:[email protected]beta300.0',
'aldeed:[email protected].1'
])
});
'meteortesting:[email protected]rc.1',
'aldeed:[email protected].2'
]);
});
30 changes: 15 additions & 15 deletions tests/.meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.

[email protected]beta300.0 # Packages every Meteor app needs to have
[email protected]beta300.0 # The database Meteor supports right now
[email protected]beta300.0 # Reactive variable for tracker
[email protected]rc300.4 # Packages every Meteor app needs to have
[email protected]rc300.4 # The database Meteor supports right now
[email protected]rc300.4 # Reactive variable for tracker
jquery # Helpful client-side library
[email protected].3-beta300.0 # Meteor's client-side reactive programming library
[email protected].4-rc300.4 # Meteor's client-side reactive programming library

[email protected]beta300.0 # CSS minifier run for production mode
[email protected]beta300.0 # JS minifier run for production mode
[email protected]beta300.0 # ECMAScript 5 compatibility for older browsers.
[email protected].8-beta300.0 # Enable ECMAScript2015+ syntax in app code
[email protected]beta300.0 # Server-side component of the `meteor shell` command
[email protected]rc300.4 # CSS minifier run for production mode
[email protected]rc300.4 # JS minifier run for production mode
[email protected]rc300.4 # ECMAScript 5 compatibility for older browsers.
[email protected].9-rc300.4 # Enable ECMAScript2015+ syntax in app code
[email protected]rc300.4 # Server-side component of the `meteor shell` command

[email protected]beta300.0 # Publish all data to the clients (for prototyping)
[email protected]beta300.0 # Allow all DB writes from clients (for prototyping)
[email protected]rc300.4 # Publish all data to the clients (for prototyping)
[email protected]rc300.4 # Allow all DB writes from clients (for prototyping)

underscore@1.0.14-beta300.0
[email protected]beta300.0
underscore@1.6.2-rc300.4
[email protected]rc300.4

aldeed:simple-schema
aldeed:[email protected].0
aldeed:simple-schema@2.0.0-beta300.0
aldeed:[email protected].2-beta.2
meteortesting:[email protected]
meteortesting:[email protected]
2 changes: 1 addition & 1 deletion tests/.meteor/release
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[email protected]beta.0
[email protected]rc.4
Loading

0 comments on commit 282c242

Please sign in to comment.