Skip to content

Add index property to addCollection method #979

Description

@paulrobertlloyd

Is your feature request related to a problem?

Following the decision to remove _id (a MongoDB ObjectID) on post and media data and index on properties.uid, we added the following:

// `_id` came indexed; `properties.uid` does not. Every paginated query
// sorts and ranges on it, and MongoDB abandons an unindexed sort once it
// needs more than 32MB, so a large enough collection would stop listing
// at all. Creating an index that already exists is a no-op.
for (const name of ["posts", "media"]) {
const collection = this.collections.get(name);
if (collection) {
await collection.createIndex({ "properties.uid": 1 });
debug(`Indexed ‘properties.uid’ on ‘${name}’`);
}
}

We may need to add an indexable property for the Microsub endpoint too (see #944).

The indexable property should be declared using the addCollection method and via the respective plugins, rather than something that is hardcoded into the core package.

Describe the solution you’d like

Add an optional second parameter to addCollection, which is the property to index items on:

new Indiekit.addCollection(name, "properties.uid");

Activity

  1. paulrobertlloyd commented on Oct 10, 2026

    @paulrobertlloyd
    CollaboratorAuthor

    @rmdes What do you think to this idea?

  2. rmdes commented on Oct 10, 2026

    @rmdes
    Collaborator

    Yes, and I'd make the second parameter the index specs rather than one property, because Microsub's indexes are compound and two are unique:

    Indiekit.addCollection("microsub_items", [
      { key: { channel: 1, id: 1 }, unique: true },
      { key: { channel: 1, uid: 1 }, unique: true },
      { key: { channel: 1, url: 1 } },
    ]);

    Core creates them when the database is up, posts and media declare { key: { "properties.uid": 1 } } from their own plugins, and the literal list in index.js goes. The uid backfill stays in core, since it writes a properties subdocument and must not touch other collections.

    Happy to do it as its own PR, and move #944's createIndexes onto it once it lands.

  3. paulrobertlloyd commented on Oct 10, 2026

    @paulrobertlloyd
    CollaboratorAuthor

    I like it! Let’s do a PR for this once #944 is merged 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreCore indiekit package

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions