JSPM

@vsaas/loopback-connector-mongodb

11.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 208
  • Score
    100M100P100Q84543F
  • License MIT

Fork of LoopBack MongoDB connector for @vsaas LoopBack projects, providing seamless integration with MongoDB databases

Package Exports

  • @vsaas/loopback-connector-mongodb

Readme

@vsaas/loopback-connector-mongodb

Maintained MongoDB connector fork for LoopBack 3.

This fork stays focused on practical LB3 compatibility while simplifying the codebase:

  • runtime moved to TypeScript
  • modern build and test tooling (tsdown, vitest, oxlint)
  • English-only messages
  • legacy repo assets removed (intl, docs site, benchmarks, examples, old CI files)
  • MongoDB driver kept on the 5.x line to remain compatible with MongoDB Server 4.2

Install

npm install @vsaas/loopback-connector-mongodb

Why mongodb@5.x

This fork intentionally stays on mongodb@5.9.2.

That keeps the connector on a conservative driver line that still works with MongoDB Server 4.2, while avoiding the larger behavior and runtime changes introduced by newer major driver lines.

LoopBack 3 datasource example

{
  "db": {
    "name": "db",
    "connector": "@vsaas/loopback-connector-mongodb",
    "host": "127.0.0.1",
    "port": 27017,
    "database": "app",
    "user": "app",
    "password": "secret",
    "authSource": "admin"
  }
}

You can also use a full connection string:

{
  "db": {
    "name": "db",
    "connector": "@vsaas/loopback-connector-mongodb",
    "url": "mongodb://app:secret@127.0.0.1:27017/app?authSource=admin"
  }
}

Supported connector settings

  • allowExtendedOperators
  • authSource
  • collation
  • database
  • disableDefaultSort
  • enableGeoIndexing
  • host
  • lazyConnect
  • password
  • port
  • protocol
  • strictObjectIDCoercion
  • url
  • user

The connector also forwards common MongoDB driver options such as:

  • connectTimeoutMS
  • serverSelectionTimeoutMS
  • socketTimeoutMS
  • readPreference
  • replicaSet
  • retryWrites
  • writeConcern

Notes

  • enableGeoIndexing: true is required for indexed near queries on GeoPoint.
  • Default ids use MongoDB ObjectId.
  • Custom collection names are supported via model settings:
{
  mongodb: {
    collection: 'CustomCollection';
  }
}
  • Custom field names are supported for non-id properties:
{
  title: {
    type: String,
    mongodb: { fieldName: 'custom_title' }
  }
}

LoopBack still must keep the primary key stored as _id; custom field names for the id property are not supported.

ObjectId handling

You can enforce ObjectId coercion in two common ways.

Per model:

{
  options: {
    strictObjectIDCoercion: true;
  }
}

Per property:

{
  id: {
    type: 'String',
    id: true,
    mongodb: { dataType: 'ObjectId' }
  }
}

Update operators

Set allowExtendedOperators: true in the datasource to allow MongoDB update operators like:

  • $set
  • $unset
  • $inc
  • $max
  • $min
  • $mul
  • $rename
  • $push
  • $pull

Example:

Product.updateAll({ category: 'furniture' }, { $max: { price: 100 } }, cb);

Security

The connector removes $where and mapReduce from filters by default before sending them to MongoDB.

If you really need them for trusted internal code, pass:

{
  disableSanitization: true;
}

Development

Local test defaults:

npm test
npm run lint
npm run typecheck

Test connection settings come from:

  • MONGODB_HOST
  • MONGODB_PORT
  • MONGODB_DATABASE

If they are not set, the suite uses localhost:27017.