@vsaas/loopback-connector-mongodb
v11.0.2
Published
Fork of LoopBack MongoDB connector for @vsaas LoopBack projects, providing seamless integration with MongoDB databases
Maintainers
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.xline to remain compatible with MongoDB Server4.2
Install
npm install @vsaas/loopback-connector-mongodbWhy [email protected]
This fork intentionally stays on [email protected].
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:[email protected]:27017/app?authSource=admin"
}
}Supported connector settings
allowExtendedOperatorsauthSourcecollationdatabasedisableDefaultSortenableGeoIndexinghostlazyConnectpasswordportprotocolstrictObjectIDCoercionurluser
The connector also forwards common MongoDB driver options such as:
connectTimeoutMSserverSelectionTimeoutMSsocketTimeoutMSreadPreferencereplicaSetretryWriteswriteConcern
Notes
enableGeoIndexing: trueis required for indexednearqueries onGeoPoint.- 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 typecheckTest connection settings come from:
MONGODB_HOSTMONGODB_PORTMONGODB_DATABASE
If they are not set, the suite uses localhost:27017.
