Skip to content

Commit

Permalink
Merge 4fc72de into 4551cc0
Browse files Browse the repository at this point in the history
  • Loading branch information
hacksparrow committed Nov 30, 2020
2 parents 4551cc0 + 4fc72de commit 7e1d934
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 244 deletions.
191 changes: 12 additions & 179 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ This installs the module from npm and adds it as a dependency to the application

If you create a MongoDB data source using the data source generator as described below, you don't have to do this, since the generator will run `npm install` for you.

## Supported versions

**Starting from the version 6.0.0, this connector is no longer compatible with LoopBack 3. Please use the latest 5.x version in your LoopBack 3 applications.**

This module adopts the [Module Long Term Support (LTS)](http://github.com/CloudNativeJS/ModuleLTS) policy, with the following End Of Life (EOL) dates:

| Version | Status | Published | EOL | LoopBack | Juggler |
| ---------- | -------------------- | --------- | -------------------- | ---------|----------|
| 6.x | Current | Nov 2020 | Apr 2023 _(minimum)_ | 4 | 4.x |
| 5.x | Active LTS | Jun 2019 | Apr 2023 | 3, 4 | 3.x, 4.x |
| 4.x | Maintenance LTS | Nov 2018 | Apr 2021 | 3, 4 | 3.x, 4.x |

## Creating a MongoDB data source

For LoopBack 4 users, use the LB4 [Command-line interface](https://loopback.io/doc/en/lb4/Command-line-interface.html) to generate a DataSource with MongoDB connector to your LB4 application. Run [`lb4 datasource`](https://loopback.io/doc/en/lb4/DataSource-generator.html), it will prompt for configurations such as host, post, etc. that are required to connect to a MongoDB database.
Expand All @@ -33,32 +45,6 @@ const config = {
};
```

<details><summary markdown="span"><strong>For LoopBack 3 users</strong></summary>

Use the [Data source generator](http://loopback.io/doc/en/lb3/Data-source-generator.html) to add a MongoDB data source to your application.
The generator will prompt for the database server hostname, port, and other settings
required to connect to a MongoDB database. It will also run the `npm install` command above for you.

The entry in the application's `/server/datasources.json` will look like this:

```javascript
"mydb": {
"host": "myserver",
"port": 27017,
"url": "",
"database": "test",
"password": "mypassword",
"name": "mydb",
"user": "me",
"authSource" : "admin",
"connector": "mongodb"
}
```

Edit `datasources.json` to add any other additional properties that you require.

</details>

If your username or password contains special characters like `@`, `$` etc, encode the whole
username or password using [encodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent).

Expand Down Expand Up @@ -131,30 +117,6 @@ If you run a MongoDB with authentification ([Docker's example here](https://gith

You can set the `url` property to a connection URL in `<datasourceName>.datasources.ts` to override individual connection parameters such as `host`, `user`, and `password`. E.g `loopback:pa55w0rd@localhost:27017/testdb`.

<details><summary markdown="span"><strong>For LoopBack 3 users</strong></summary>

For LB3 users, you can override the global `url` property in environment-specific data source configuration files, for example for production in `datasources.production.json`, and use the individual connection parameters `host`, `user`, `password`, and `port`. To do this, you _must_ set `url` to `false`, null, or “” (empty string).
If you set `url` to `undefined` or remove the `url` property altogether, the override will not work.

For example, for production, use `datasources.production.json` as follows (for example) to overide the `url` setting in `datasources.json:

```javascript
"mydb": {
"host": "myserver",
"port": 27017,
"url": false,
"database": "test",
"password": "mypassword",
"name": "mydb",
"user": "me",
"connector": "mongodb"
}
```

For more information on setting data source configurations for different environments, see [Environment-specific configuration](https://loopback.io/doc/en/lb3/Environment-specific-configuration.html#data-source-configuration).

</details>

### Using the mongodb+srv protocol

MongoDB supports a protocol called `mongodb+srv` for connecting to replica sets without having to give the hostname of every server in the replica set.
Expand All @@ -170,19 +132,6 @@ const config = {
};
```

<details><summary markdown="span"><strong>For LoopBack 3 users</strong></summary>

```javascript
"mydb": {
"connector": "mongodb",
"host": "myserver",
"database": "test",
"protocol": "mongodb+srv"
}
```

</details>

Note: the port is not specified when using the `mongodb+srv` protocol and will be ignored if given.

## Security Considerations
Expand All @@ -208,47 +157,6 @@ See [LoopBack 4 types](http://loopback.io/doc/en/lb4/LoopBack-types.html) (or [L

Type conversion is mainly handled by MongoDB. See ['node-mongodb-native'](http://mongodb.github.io/node-mongodb-native/) for details.

<details><summary markdown="span"><strong>For LoopBack 3 users</strong></summary>

## Customizing MongoDB configuration for tests/examples

By default, examples and tests from this module assume there is a MongoDB server
instance running on localhost at port 27017.

To customize the settings, you can drop in a `.loopbackrc` file to the root directory
of the project or the home folder.

**Note**: Tests and examples in this project configure the data source using the deprecated '.loopbackrc' file method,
which is not supported in general.
For information on configuring the connector in a LoopBack application, please refer to [loopback.io](http://loopback.io/doc/en/lb2/MongoDB-connector.html).

The .loopbackrc file is in JSON format, for example:

{
"dev": {
"mongodb": {
"host": "127.0.0.1",
"database": "test",
"user": "youruser",
"password": "yourpass",
"port": 27017
}
},
"test": {
"mongodb": {
"host": "127.0.0.1",
"database": "test",
"user": "youruser",
"password": "yourpass",
"port": 27017
}
}
}

**Note**: user/password is only required if the MongoDB server has authentication enabled. `"authSource"` should be used if you cannot log in to your database using your credentials.

</details>

## Update Operators

Except the comparison and logical operators LoopBack supports in the [operator list](https://loopback.io/doc/en/lb4/Where-filter.html#operators) of `Where` filter, you can also enable [MongoDB update operators](https://docs.mongodb.com/manual/reference/operator/update/) for `update*` methods by setting the flag `allowExtendedOperators` to `true` in the datasource configuration.
Expand All @@ -259,19 +167,6 @@ Here is an example of updating the price for all the products under category `fu
await productRepo.updateAll({ $max: { price: 100 }}, { category: {eq: 'furniture'} // where clause goes in here });
```

<details><summary markdown="span"><strong>For LoopBack 3 users</strong></summary>

```
Product.updateAll(
{ category: {eq: 'furniture'} // where clause goes in here },
{$max: {price: 100}},
options,
function(err, updateproducts) {
...
```

</details>

{% include tip.html content="you **will not** need the dollar sign `'$'` for operators in the Where
clause." %}

Expand Down Expand Up @@ -318,23 +213,6 @@ export class User extends Entity {
}
```

<details><summary markdown="span"><strong>For LoopBack 3 users</strong></summary>

```js
{
"name": "User",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true,
"strictObjectIDCoercion": true
},
...
}
```

</details>

### Property scope

This scope would only convert an ObjectId-like string to `ObjectId` with a certain property in the model.
Expand All @@ -348,24 +226,6 @@ This scope would only convert an ObjectId-like string to `ObjectId` with a certa
id: string;
```
<details><summary markdown="span"><strong>For LoopBack 3 users</strong></summary>
```js
"properties": {
{
"id": {
"type": "String",
"id": true,
"required":true,
"mongodb": {"dataType":"ObjectId"}
},
// ..
}
}
```
</details>
Also notice that for RELATIONS, if the primary key/source key has set to enforce ObjectId coercion
(no matter by `strictObjectIDCoercion: true` or `dataType: 'ObjectId'`). The corresponding foreign key will need to have it
set as well to make sure relations work smoothly.
Expand Down Expand Up @@ -427,33 +287,6 @@ export class User extends Entity {
}
```
<details><summary markdown="span"><strong>For LoopBack 3 users</strong></summary>
```js
{
"name": "User",
"options": {
"mongodb": {
"collection": "Custom_Collection_User", //custom name
},
},
"properties": {
{
"id": {
"type": "String",
"id": true,
"required":true,
},
"name": {
"type": "String",
"mongodb": {"fieldName": "Custom_Name"},}, //custom name
}
},
}
```
</details>
{% include important.html content="Since in MongoDB `_id` is reserved for the primary key, LoopBack **does not** allow customization of the field name for the id property. Please use `id` as is. Customizing the id property would cause errors." %}
## Running tests
Expand Down
8 changes: 0 additions & 8 deletions deps/juggler-v3/package.json

This file was deleted.

49 changes: 0 additions & 49 deletions deps/juggler-v3/test.js

This file was deleted.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loopback-connector-mongodb",
"version": "5.4.0",
"version": "6.0.0-dev",
"description": "The official MongoDB connector for the LoopBack framework.",
"engines": {
"node": ">=10"
Expand All @@ -11,7 +11,7 @@
"coverage": "nyc report --reporter=text-lcov | coveralls",
"benchmarks": "make benchmarks",
"leak-detection": "make leak-detection",
"test": "nyc mocha test/*.test.js node_modules/juggler-v3/test.js node_modules/juggler-v4/test.js",
"test": "nyc mocha test/*.test.js node_modules/juggler-v4/test.js",
"lint": "eslint .",
"posttest": "npm run lint"
},
Expand Down Expand Up @@ -48,9 +48,8 @@
"coveralls": "^3.0.4",
"eslint": "^6.6.0",
"eslint-config-loopback": "^13.0.0",
"juggler-v3": "file:./deps/juggler-v3",
"juggler-v4": "file:./deps/juggler-v4",
"loopback-datasource-juggler": "^3.0.0 || ^4.0.0",
"loopback-datasource-juggler": "^4.0.0",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"rc": "^1.2.8",
Expand Down
8 changes: 4 additions & 4 deletions test/objectid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ describe('ObjectID', function() {
title: 'arrayOfObjectID',
});
const found = await Article.find({where: {title: 'arrayOfObjectID'}});
// the type of the returned array is actually string even it's stored as ObjectIds in the db as expected
// the type of the returned array is actually string even though they are stored as ObjectIds in the db
found[0].xidArr.should.containDeep([
new ds.ObjectID(objectIDLikeString),
new ds.ObjectID(objectIDLikeString2),
objectIDLikeString,
objectIDLikeString2,
]);
// check if the array is stored in ObjectId
// check if the array is stored as ObjectId in the db
const raw = await findRawModelDataAsync('ArticleC', found[0].id);
raw.xidArr[0].should.be.an.instanceOf(ds.ObjectID);
raw.xidArr[1].should.be.an.instanceOf(ds.ObjectID);
Expand Down

0 comments on commit 7e1d934

Please sign in to comment.