20260604174200_zotero_libraryReferences_sources.mjs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
  2. import Helpers from './lib/helpers.mjs'
  3. const tags = ['saas']
  4. const indexes = [
  5. {
  6. key: {
  7. userId: 1,
  8. 'sources.zotero.libraryType': 1,
  9. 'sources.zotero.libraryId': 1,
  10. 'sources.zotero.itemKey': 1,
  11. },
  12. name: 'userId_1_sources.zotero.libraryType_1_sources.zotero.libraryId_1_sources.zotero.itemKey_1',
  13. unique: true,
  14. // Not `sparse`: userId is always present, so a sparse compound index would
  15. // include every doc and collide native entries on (userId, null, null,
  16. // null). Scope uniqueness to Zotero-linked docs only.
  17. partialFilterExpression: { 'sources.zotero.itemKey': { $exists: true } },
  18. },
  19. ]
  20. const migrate = async client => {
  21. const { db } = client
  22. await batchedUpdate(
  23. db.libraryReferences,
  24. { sources: { $exists: false } },
  25. { $set: { sources: {} } }
  26. )
  27. await Helpers.addIndexesToCollection(db.libraryReferences, indexes)
  28. }
  29. const rollback = async client => {
  30. const { db } = client
  31. await Helpers.dropIndexesFromCollection(db.libraryReferences, indexes)
  32. }
  33. export default {
  34. tags,
  35. migrate,
  36. rollback,
  37. }