Skip to content

Commit

Permalink
Updated indexing system toname indices correctly when create (#70) an…
Browse files Browse the repository at this point in the history
…d updated lists collection to have a f text index for searching multiple fields by text values (#18)
  • Loading branch information
zachsa committed Jan 9, 2023
1 parent d5d45a8 commit e05158b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ObjectId } from 'mongodb'
import userModel from '../../../../user-model/index.js'
import PERMISSIONS from '../../../../user-model/permissions.js'
import { PASSPORT_SSO_SESSION_ID } from '../../../../config/index.js'

export default async (self, args, ctx) => {
await userModel.ensurePermission({ ctx, permission: PERMISSIONS['list:update'] })
const { id, filter = {}, createdBy, ...otherFields } = args
const { id, filter = {}, createdBy, type, ...otherFields } = args
const { Lists } = await ctx.mongo.collections

const _id = ObjectId()
Expand All @@ -17,6 +18,9 @@ export default async (self, args, ctx) => {
$setOnInsert: {
_id,
createdAt: new Date(),
type,
userId: ctx.user.info(ctx)?.id || undefined,
clientSession: ctx.cookies.get(PASSPORT_SSO_SESSION_ID) || 'no-session', // This can happen if the user blocks cookies
},
$set: {
modifiedAt: new Date(),
Expand Down
23 changes: 23 additions & 0 deletions api/src/mongo/collections/_lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,39 @@ export default {
bsonType: 'string',
description: 'List description',
},
createdBy: {
bsonType: 'string',
description: 'The person or application that created the list',
},
userId: {
description:
'ID of logged in user who requested the list be created (if user is logged in)',
},
clientSession: {
bsonType: 'string',
description: 'ID of the browser session if it exists',
},
},
},
},
indices: [
{
index: 'hashOfFilter',
options: {
name: 'hashOfFilter',
unique: true,
partialFilterExpression: { hashOfFilter: { $exists: true } },
},
},
{
_type: 'text',
createdBy: 'text',
description: 'text',
title: 'text',
type: 'text',
options: {
name: 'text',
},
},
],
}
17 changes: 9 additions & 8 deletions api/src/mongo/collections/_permissions.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
export default {
name: 'permissions',
indices: [
{
index: 'name',
options: {
unique: true,
},
},
],
validator: {
$jsonSchema: {
bsonType: 'object',
Expand All @@ -21,4 +13,13 @@ export default {
},
},
},
indices: [
{
index: 'name',
options: {
name: 'name',
unique: true,
},
},
],
}
17 changes: 9 additions & 8 deletions api/src/mongo/collections/_roles.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
export default {
name: 'roles',
indices: [
{
index: 'name',
options: {
unique: true,
},
},
],
validator: {
$jsonSchema: {
bsonType: 'object',
Expand All @@ -21,4 +13,13 @@ export default {
},
},
},
indices: [
{
index: 'name',
options: {
name: 'name',
unique: true,
},
},
],
}
1 change: 1 addition & 0 deletions api/src/mongo/collections/_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default {
{
index: 'emailAddress',
options: {
name: 'emailAddress',
unique: true,
},
},
Expand Down
47 changes: 30 additions & 17 deletions api/src/mongo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,37 @@ export const updateValidationRules = async () => {
Object.entries(_collections)
.map(([, { name, indices = [] }]) => {
return Promise.all(
indices.map(async ({ index, options }) => {
console.info('Applying index', index, 'to collection', name, options)
return _db
.collection(name)
.createIndex(index, options)
.catch(async error => {
if (error.code === 85) {
console.info('Recreating index on', name, ':: Index name:', index)
try {
await _db.collection(name).dropIndex(`${index}_1`)
await _db.collection(name).createIndex(index, options)
} catch (error) {
throw new Error(`Unable to recreate index. ${error.message}`)
}
} else {
throw error
indices.map(async ({ _type = undefined, options, ...indexFields }) => {
const collection = await _db.collection(name)
try {
if (_type === 'text') {
console.info(
'Applying text index',
options.name,
'to collection',
name,
indexFields,
options
)
return collection.createIndex(indexFields, options)
} else {
const { index } = indexFields
console.info('Applying index', index, 'to collection', name, options)
return collection.createIndex(index, options)
}
} catch (error) {
if (error.code === 85) {
console.info('Recreating index on', name, ':: Index', indexFields)
try {
await _db.collection(name).dropIndex(options.name)
await _db.collection(name).createIndex(index, options)
} catch (error) {
throw new Error(`Unable to recreate index. ${error.message}`)
}
})
} else {
throw error
}
}
})
)
})
Expand Down
2 changes: 1 addition & 1 deletion clients/src/pages/index/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default [
),
},
{
label: 'Data lists',
label: 'Curated lists',
to: '/data-lists',
includeInFooter: true,
Icon: CollectionsIcon,
Expand Down

0 comments on commit e05158b

Please sign in to comment.