How to extend Users collection

I am looking for the proper way to extend the built-in users collection, like is shown in the docs (for insert, update, remove, etc.):

I used to use Astronomy and this let you just reference the built-in schema and extend it, but if I do the following, I get: Error: There is already a collection named "users"

class UsersCollection extends Mongo.Collection {
	// Any custom insert, update, or delete logic here
}

const Users = new UsersCollection('users');

Instead, the following at least lets me start up, however I am unsure if the other pieces will work yet (i.e. collection2, collection-helpers, etc.) as I am still in the beginning of the app:

//class UsersCollection extends Mongo.Collection {
	// Any custom insert, update, or delete logic here
//}

const Users = Meteor.users;//new UsersCollection('users');

Basically, I have always treated the users collection like one of the custom collections I create and then store fields on it such as phone number, or whatever I need. I am trying to follow the docs/examples as close as I can as I am writing a brand new app with 1.6, however I cannot find how others extend the built-in users collection so I am wondering if I am missing it somewhere or if I am doing something wrong, as the docs do not indicate the answer to me.

Why don’t you:

class Meteor.users extends Mongo.Collection {
	// Any custom insert, update, or delete logic here
}

Hey, thanks for the reply. I tried that, and it fails with the error Unexpected token, expected { on the period in Meteor.users.

Inspired by this, I also tried the following:

class User extends Meteor.users {
	//TODO: add any custom insert, update, or delete logic here
}

…but it fails with the error TypeError: Class extends value [object Object] is not a constructor or null.

So, no dice :frowning_face:

1 Like

This link, Meteor.users is just assigned to Accounts.users in a basic global assignment, you may want to look there at extending? I haven’t looked deeply at this. You could also look at how some of the collection hook packages are extending methods etc.

Could also just copy some code from the collection hooks repo and make a more basic version.

This is where the users collection is getting assigned in.

/**
 * @summary A [Mongo.Collection](#collections) containing user documents.
 * @locus Anywhere
 * @type {Mongo.Collection}
 * @importFromPackage meteor
*/
Meteor.users = Accounts.users;

Hey, thanks for the links. In terms of the Meteor.users collection though, all of that is being done prior to my code, so there’s not much I can do unless I completely roll my own, correct?

I did see that plugin for the meteor-collection-hooks, however I was hoping I did not need it as it seemed silly to add a sizeable plugin for just one of the collections I have, as I am trying to follow the Meteor guide as much as I can to re-familiarize myself with Meteor best practices.

That being said, the plugin does give me additional functionality that might be useful in all collections, so maybe I will just switch to that and use it for all hooks I may need. Still bugs me that I couldn’t find an answer to this though, so if anyone has an answer, share the wealth! :grinning:

Slightly anecdotal, but I’ve been using Users just like any other collection via:

const Users = Meteor.users;

Yeah, that is what I said finally compiled for me up above and what I am currently using, however when you do that you cannot extend the original Mongo.Collection so you cannot hook into insert/update/remove.

This is hacky but works, I believe, by patching into the global variable. I would copy the collection hooks code more carefully, but the quick test below shows it patching in to the insert method. Screenshot from quick test in chrome console.

ignore the error, it was from simpleschema, (you wouldn’t insert users like this anyway.) It just shows that the modified code runs then passes through. To do it properly also, you should import/export the modified collection from a file you have, not use the global users collection any more, or create a new npm package, it’s not difficult.

Hey, thanks! I wanted to try this out myself and then totally forgot to reply here, so my mistake. But, you’re right, this does seem to work albeit very hack-like. I think I will stick with my original plan but its nice to know this is technically possible. :+1: