Skip to content

Commit cb9c87c

Browse files
Merge pull request balderdashy#81 from dqydj/master
Adding 'url' option in adapter
2 parents bd3f4a4 + 8ded3cd commit cb9c87c

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,42 @@ Adds MySQL support for Sails.
77
# Sails.js Repo
88
http://SailsJs.org
99

10+
## Installation
11+
12+
Install from NPM.
13+
14+
```bash
15+
$ npm install sails-mysql
16+
```
17+
18+
## Sails Configuration
19+
20+
Add the mysql config to the config/adapters.js file. Basic options:
21+
22+
```javascript
23+
module.exports.adapters = {
24+
'default': 'mysql',
25+
26+
mysql: {
27+
module : 'sails-mysql',
28+
host : 'localhost',
29+
port : 3306,
30+
user : 'username',
31+
password : 'password',
32+
database : 'MySQL Database Name'
33+
34+
// OR (exlicit sets take precedence)
35+
module : 'sails-mysql',
36+
url : 'mysql2://USER:PASSWORD@HOST:PORT/DATABASENAME'
37+
}
38+
}
39+
};
40+
```
1041

1142
## About Waterline
1243
Waterline is a new kind of storage and retrieval engine. It provides a uniform API for accessing stuff from different kinds of databases, protocols, and 3rd party APIs. That means you write the same code to get users, whether they live in mySQL, LDAP, MongoDB, or Facebook.
1344
Waterline also comes with built-in transaction support, as well as a configurable environment setting.
1445

15-
16-
## Writing your own adapters
17-
It's easy to add your own adapters for integrating with proprietary systems or existing open APIs. For most things, it's as easy as `require('some-module')` and mapping the appropriate methods to match waterline semantics.
18-
46+
1947

2048
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/a22d3919de208c90c898986619efaa85 "githalytics.com")](http://githalytics.com/mikermcneil/sails-mysql)

lib/adapter.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ module.exports = (function() {
5555
},
5656

5757
registerCollection: function(collection, cb) {
58+
59+
// If 'url' is set, extract config
60+
collection.config = utils.parseUrl(collection.config);
61+
62+
5863
var def = _.clone(collection);
5964
var key = def.identity;
6065
var definition = def.definition || {};

lib/utils.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,36 @@
44

55
// Dependencies
66
var _ = require('underscore');
7-
7+
url = require('url');
88
// Module Exports
99

10-
var utils = module.exports = {};
10+
var utils = module.exports = {
11+
12+
/**
13+
* Parse URL string from config
14+
*
15+
* Parse URL string into connection config parameters
16+
*/
17+
parseUrl: function (config) {
18+
if(!_.isString(config.url)) return config;
19+
20+
var obj = url.parse(config.url);
21+
22+
config.host = obj.hostname || config.host;
23+
config.port = obj.port || config.port;
24+
25+
if(_.isString(obj.path)) {
26+
config.database = obj.path.split("/")[1] || config.database;
27+
}
28+
29+
if(_.isString(obj.auth)) {
30+
config.user = obj.auth.split(":")[0] || config.user;
31+
config.password = obj.auth.split(":")[1] || config.password;
32+
}
33+
return config;
34+
}
35+
36+
};
1137

1238
/**
1339
* Prepare values

0 commit comments

Comments
 (0)