Skip to content

Commit

Permalink
Merge pull request #341 from Meteor-Community-Packages/feature/create…
Browse files Browse the repository at this point in the history
…Index

Use createIndex
  • Loading branch information
StorytellerCZ authored Aug 17, 2021
2 parents 707e99d + ece40d5 commit c48a936
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Changelog

## v3.4.0

* Use the new `createIndex` instead of `_ensureIndex` if available

## v3.3.0

* Update dependencies
Expand Down
4 changes: 2 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Package.describe({
summary: 'Authorization package for Meteor',
version: '3.3.0',
version: '3.4.0',
git: 'https://github.com/Meteor-Community-Packages/meteor-roles.git',
name: 'alanning:roles'
})
Expand Down Expand Up @@ -45,7 +45,7 @@ Package.onTest(function (api) {
'chai': '4.2.0'
})

api.versionsFrom(['1.12', '2.3'])
api.versionsFrom('2.4')

var both = ['client', 'server']

Expand Down
34 changes: 24 additions & 10 deletions roles/roles_server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
/* global Meteor, Roles */

Meteor.roleAssignment._ensureIndex({ 'user._id': 1, 'inheritedRoles._id': 1, scope: 1 })
Meteor.roleAssignment._ensureIndex({ 'user._id': 1, 'role._id': 1, scope: 1 })
Meteor.roleAssignment._ensureIndex({ 'role._id': 1 })
Meteor.roleAssignment._ensureIndex({ scope: 1, 'user._id': 1, 'inheritedRoles._id': 1 }) // Adding userId and roleId might speed up other queries depending on the first index
Meteor.roleAssignment._ensureIndex({ 'inheritedRoles._id': 1 })

Meteor.roles._ensureIndex({ 'children._id': 1 })
if (Meteor.roles.createIndex) {
Meteor.roleAssignment.createIndex({ 'user._id': 1, 'inheritedRoles._id': 1, scope: 1 })
Meteor.roleAssignment.createIndex({ 'user._id': 1, 'role._id': 1, scope: 1 })
Meteor.roleAssignment.createIndex({ 'role._id': 1 })
Meteor.roleAssignment.createIndex({ scope: 1, 'user._id': 1, 'inheritedRoles._id': 1 }) // Adding userId and roleId might speed up other queries depending on the first index
Meteor.roleAssignment.createIndex({ 'inheritedRoles._id': 1 })

Meteor.roles.createIndex({ 'children._id': 1 })
} else {
Meteor.roleAssignment._ensureIndex({ 'user._id': 1, 'inheritedRoles._id': 1, scope: 1 })
Meteor.roleAssignment._ensureIndex({ 'user._id': 1, 'role._id': 1, scope: 1 })
Meteor.roleAssignment._ensureIndex({ 'role._id': 1 })
Meteor.roleAssignment._ensureIndex({ scope: 1, 'user._id': 1, 'inheritedRoles._id': 1 }) // Adding userId and roleId might speed up other queries depending on the first index
Meteor.roleAssignment._ensureIndex({ 'inheritedRoles._id': 1 })

Meteor.roles._ensureIndex({ 'children._id': 1 })
}

/*
* Publish logged-in user's roles so client-side checks can work.
Expand Down Expand Up @@ -356,8 +365,13 @@ Object.assign(Roles, {
_backwardMigrate2: function (assignmentSelector) {
assignmentSelector = assignmentSelector || {}

Meteor.users._ensureIndex({ 'roles._id': 1, 'roles.scope': 1 })
Meteor.users._ensureIndex({ 'roles.scope': 1 })
if (Meteor.users.createIndex) {
Meteor.users.createIndex({ 'roles._id': 1, 'roles.scope': 1 })
Meteor.users.createIndex({ 'roles.scope': 1 })
} else {
Meteor.users._ensureIndex({ 'roles._id': 1, 'roles.scope': 1 })
Meteor.users._ensureIndex({ 'roles.scope': 1 })
}

Meteor.roleAssignment.find(assignmentSelector).forEach(r => {
const roles = Meteor.users.findOne({ _id: r.user._id }).roles || []
Expand Down

0 comments on commit c48a936

Please sign in to comment.