Skip to content

mongodb seeding port#248

Open
jasonmorais wants to merge 4 commits into
mainfrom
jason/mock-mongo-server-seeding
Open

mongodb seeding port#248
jasonmorais wants to merge 4 commits into
mainfrom
jason/mock-mongo-server-seeding

Conversation

@jasonmorais
Copy link
Copy Markdown
Contributor

@jasonmorais jasonmorais commented May 11, 2026

Summary by Sourcery

Introduce a seedable in-memory MongoDB server and populate it with initial OCOM domain data for verification workflows.

New Features:

  • Add configurable mock MongoDB server that waits for required collections and seeds data via a provided callback.
  • Seed the mock MongoDB instance with initial end users, communities, roles, members, properties, and services for OCOM.
  • Wire the server-mongodb-memory-mock app to use the new seeding pipeline and domain models package.

Enhancements:

  • Refine header container tests to use more concise mocks and typings without changing behavior.
  • Log dotenv environment setup steps in the mock MongoDB server app.
  • Upgrade Docusaurus dependencies in the docs app to version 3.10.1 and align related type packages.
  • Register mongodb-memory-server, mongoose, and additional catalog dependencies in the workspace for shared use.
  • Tidy dev dependency ordering in the OAuth2 mock seedwork package.

Build:

  • Add mongodb-memory-server and mongoose to the seedwork and workspace catalogs, and extend server-mongodb-memory-mock app dependencies with MongoDB-related packages.

Documentation:

  • Update docs app dependencies to the latest Docusaurus 3.10.1 line without content changes.

Tests:

  • Minorly refactor logged-in user header container tests for consistency and readability without altering coverage.

Chores:

  • Normalize formatting in README and lockfile, and add new seed data modules under the mock MongoDB server app.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 11, 2026

Reviewer's Guide

Adds a seedable in-memory MongoDB replica server wired to mongoose and OCOM data models, wires a new seeding workflow into the mock MongoDB app, introduces initial community/user/member/role/property/service seed data, and performs minor dependency/test updates and formatting tweaks.

Sequence diagram for mock MongoDB startup and seeding workflow

sequenceDiagram
    actor Developer
    participant App as server_mongodb_memory_mock
    participant Seedwork as startMockMongoDB
    participant ReplSet as MongoMemoryReplSet
    participant Mongoose as mongoose
    participant Seeder as seedDatabase

    Developer->>App: startMockMongoDB(config)
    App->>Seedwork: startMockMongoDB(config)
    Seedwork->>ReplSet: MongoMemoryReplSet.create({...})
    ReplSet-->>Seedwork: replicaSet
    Seedwork->>ReplSet: getUri(dbName)
    ReplSet-->>Seedwork: uri
    Seedwork->>Mongoose: connect(uri)
    Mongoose-->>Seedwork: connection

    loop [until collectionsToSeed exist]
        Seedwork->>Mongoose: connection.db.listCollections()
        Mongoose-->>Seedwork: collections
    end

    Seedwork->>Seeder: seedDatabase(connection)
    Seeder-->>Seedwork: Promise<void>
    Seedwork-->>App: resolved
    App-->>Developer: mock MongoDB ready
Loading

Entity relationship diagram for seeded MongoDB mock data

erDiagram
    EndUser {
        string _id
    }

    Community {
        string _id
        string createdBy
    }

    EndUserRole {
        string _id
        string community
    }

    Member {
        string _id
        string community
        string role
    }

    Property {
        string _id
        string community
        string owner
    }

    Service {
        string _id
        string community
    }

    EndUser ||--o{ Community : createdBy
    Community ||--o{ EndUserRole : has_roles
    Community ||--o{ Member : has_members
    Community ||--o{ Property : has_properties
    Community ||--o{ Service : has_services
    EndUser ||--o{ Member : has_accounts
    EndUserRole ||--o{ Member : assigned_to
    Member ||--o{ Property : owns_properties
Loading

File-Level Changes

Change Details Files
Replace generic Mongo memory replica helper with a seeding-aware mock MongoDB bootstrap wired to mongoose and connection-ready callbacks.
  • Renamed the replica-set configuration interface and start function to MongoMemoryServerConfig/startMockMongoDB and added fields for collectionsToSeed and seedDatabase.
  • Changed MongoMemoryReplSet startup to a promise chain that logs readiness, connects mongoose to the replica URI, and conditionally waits for specific collections before invoking a seedDatabase callback.
  • Added error handling for seeding and startup that logs failures and terminates the process on error.
packages/cellix/server-mongodb-memory-mock-seedwork/src/index.ts
Wire the mock MongoDB app to the new seeding bootstrap and provide an initial list of collections to seed.
  • Defined a static list of collections to seed and mapped env vars into a MongoMemoryServerConfig object passed to startMockMongoDB.
  • Replaced the previous disposer-based lifecycle management and signal handlers with a simple startMockMongoDB call and top-level error handler that exits on failure.
apps/server-mongodb-memory-mock/src/index.ts
Introduce a reusable seed dataset for communities, users, members, roles, properties, and services along with a centralized seeding routine.
  • Added seed modules defining deterministic IDs and documents for end users, communities, roles with permission sets, members with profiles/accounts, properties with location/listing details, and services.
  • Implemented seedDatabase to transform string IDs into ObjectId instances and insert documents into their respective collections using a provided mongoose Connection, logging counts per collection.
apps/server-mongodb-memory-mock/src/seed/communities.ts
apps/server-mongodb-memory-mock/src/seed/end-users.ts
apps/server-mongodb-memory-mock/src/seed/members.ts
apps/server-mongodb-memory-mock/src/seed/properties.ts
apps/server-mongodb-memory-mock/src/seed/roles.ts
apps/server-mongodb-memory-mock/src/seed/services.ts
apps/server-mongodb-memory-mock/src/seed/seed.ts
Ensure environment variables for the mock MongoDB server are loaded with explicit logging.
  • Wrapped dotenv.config calls with console logs indicating environment setup start and completion.
apps/server-mongodb-memory-mock/src/setup-environment.ts
Align package dependencies and workspace catalog entries for Mongo/mongoose tooling and docs, and tweak test imports.
  • Switched mongodb-memory-server and mongoose dependencies to use workspace catalog references and added mongoose dependency to the seedwork package.
  • Added @ocom/data-sources-mongoose-models, mongodb, and mongoose dependencies to the mock server app package and catalogued mongodb-memory-server and related tooling in pnpm-workspace and root package.json.
  • Bumped Docusaurus-related dependencies from 3.9.2 to 3.10.1 and updated express-rate-limit and dev tooling ordering.
  • Refactored Vitest test files to use more compact mocks and import ordering without changing behavior.
  • Added a trailing newline to the main README for markdown formatting consistency.
apps/docs/package.json
apps/server-mongodb-memory-mock/package.json
packages/cellix/server-mongodb-memory-mock-seedwork/package.json
packages/cellix/server-oauth2-mock-seedwork/package.json
packages/ocom/ui-shared/src/components/organisms/header/logged-in-user-root.container.test.tsx
packages/ocom/ui-shared/src/components/organisms/header/logged-in-user-community.container.test.tsx
package.json
pnpm-workspace.yaml
readme.md
pnpm-lock.yaml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@jasonmorais jasonmorais marked this pull request as ready for review May 12, 2026 15:42
@jasonmorais jasonmorais requested a review from a team May 12, 2026 15:42
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The new startMockMongoDB helper no longer exposes a disposer or hooks into process shutdown signals, so there’s currently no clean way to stop the in-memory replica set; consider returning the replicaSet (or a stop function) and wiring it into SIGINT/SIGTERM handling as before.
  • The while (true) loop that waits for collectionsToSeed has no timeout or backoff and could hang the process indefinitely if collections never appear; adding a maximum wait duration and clearer error reporting would make failures easier to diagnose.
  • Several code paths in the seedwork now call process.exit(1) directly (e.g., on seeding failure or startup errors), which can be surprising in a reusable package; consider surfacing errors to the caller and letting the top-level app decide when to exit.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `startMockMongoDB` helper no longer exposes a disposer or hooks into process shutdown signals, so there’s currently no clean way to stop the in-memory replica set; consider returning the `replicaSet` (or a stop function) and wiring it into SIGINT/SIGTERM handling as before.
- The `while (true)` loop that waits for `collectionsToSeed` has no timeout or backoff and could hang the process indefinitely if collections never appear; adding a maximum wait duration and clearer error reporting would make failures easier to diagnose.
- Several code paths in the seedwork now call `process.exit(1)` directly (e.g., on seeding failure or startup errors), which can be surprising in a reusable package; consider surfacing errors to the caller and letting the top-level app decide when to exit.

## Individual Comments

### Comment 1
<location path="apps/server-mongodb-memory-mock/src/seed/seed.ts" line_range="25-34" />
<code_context>
+	await connection.collection('users').insertMany(users);
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider making seeding idempotent or handling duplicate-key errors for repeated runs.

Because `insertMany` uses deterministic `_id` values, rerunning this seeding against the same DB will raise duplicate-key errors. To avoid that, consider clearing the collections first (e.g. `deleteMany({})`), using `ordered: false` and tolerating duplicate errors, or upserting by `_id` so the seeding is idempotent.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread apps/server-mongodb-memory-mock/src/seed/seed.ts Outdated
@jasonmorais jasonmorais changed the title base commit for verify to work mongodb seeding port May 13, 2026
@jasonmorais jasonmorais requested a review from a team as a code owner May 18, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant