Skip to content
This repository was archived by the owner on Feb 25, 2022. It is now read-only.

Commit ac78c85

Browse files
committedMay 21, 2016
chore(README): Update documentation
1 parent ae90fb6 commit ac78c85

File tree

1 file changed

+104
-17
lines changed

1 file changed

+104
-17
lines changed
 

‎README.md

+104-17
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,52 @@ sequelize-connect is a simple singleton wrapper for the sequelize ORM, making it
1717

1818
***NOTE:*** `sequelize-connect` must be configured upon app initialization, prior to [accessing your models](#accessing-sequelize)
1919

20-
The sequelize-connect `connect()` method accepts the same parameters as the Sequelize() object `database, username, password, options`. It is important to configure the `discover` array of the set of paths where your models should be discovered.
20+
The sequelize-connect `connect()` method accepts the same parameters as the Sequelize() object `database, username, password, options`.
2121
```js
2222
// app.js
23-
var orm = require('sequelize-connect');
23+
var Connection = require('sequelize-connect');
2424

25-
orm.discover = [__dirname + '/models'];
26-
orm.connect(
25+
var orm = new Connection(
2726
'test-db',
2827
'test-user',
2928
'secret1234',
3029
{
3130
dialect: "mysql",
3231
port: 3306
33-
})
34-
.then(function(){
35-
// Connection is completed
36-
});
32+
}
33+
)
34+
.then(function(instance){
35+
// Connection is completed
36+
});
37+
```
38+
39+
It is important to configure the `discover` array of the set of paths where your models should be discovered.
40+
```js
41+
// app.js
42+
var Connection = require('sequelize-connect');
43+
44+
var discover = [__dirname + '/models', ...];
45+
var orm = new Connection(
46+
'test-db',
47+
'test-user',
48+
'secret1234',
49+
{
50+
dialect: "mysql",
51+
port: 3306
52+
},
53+
discover,
54+
)
55+
.then(function(instance){
56+
// Connection is completed
57+
});
3758
```
38-
Upon `connect()` sequelize-connect will ***ASYNCHRONOUSLY recurse*** through all of the subfolders located at the provided file paths looking for any files with the naming default convention `*.model.js`. Connect will return a Promise that is called on it's completion.
59+
Upon the first initialization of the `Connection` e.g. `new Connection(...);` sequelize-connect will ***ASYNCHRONOUSLY recurse*** through all of the subfolders located at the provided file paths looking for any files with the naming default convention `*.model.js`. Connect will return a Promise that is called on it's completion.
3960

4061
#### Connection String
4162
You can use a connection string to connect as well:
4263

4364
```js
44-
orm.connect(
65+
new Connection(
4566
'MyConnectionString',
4667
{
4768
dialect: "mysql",
@@ -56,15 +77,27 @@ orm.connect(
5677
## Custom matcher
5778
If you prefer to define your own naming convention instead of the default you can create a custom matching function which receives the file name as the parameter returns a `boolean` indicating if sequelize-connect should attempt to load the file as a model.
5879

59-
This function should be attached to `matcher` like so:
80+
This function should be injected to `Connection` like so:
6081

6182
```js
62-
orm.matcher = function(file){
83+
var matcher = function(file){
6384
if(//some condition or regex here)
6485
return true;
6586

6687
return false;
6788
};
89+
90+
new Connection(
91+
'test-db',
92+
'test-user',
93+
'secret1234',
94+
{
95+
dialect: "mysql",
96+
port: 3306
97+
},
98+
discover,
99+
matcher
100+
)
68101
```
69102
70103
@@ -74,13 +107,51 @@ After connecting you can access the sequelize instance and models wherever you n
74107
```js
75108
// somefile.js
76109

77-
var orm = require('sequelize-connect');
110+
var Connection = require('sequelize-connect');
111+
var orm = new Connection(); // singleton pattern - returns the created instance
78112
var sequelize = orm.sequelize;
79113
var Sequelize = orm.Sequelize;
80114
var models = orm.models;
81115
var User = models.User;
82116
```
83117
118+
If you prefer, rather than waiting for the connection to load before initializing every part of your application, you can wrap the code that has dependencies on your models in a `then`. e.g.
119+
120+
121+
```js
122+
// app.js
123+
new Connection(
124+
'MyConnectionString',
125+
{
126+
dialect: "mysql",
127+
port: 3306
128+
})
129+
```
130+
131+
```js
132+
// foobar.js
133+
134+
var Promise = require('bluebird');
135+
var Connection = require('sequelize-connect');
136+
var orm = new Connection();
137+
138+
// This will ensure that the connection has been established
139+
// if you load foobar.js before you wait for your initial connection
140+
// to return
141+
var Promise.resolve(orm)
142+
.then(function(instance) {
143+
var User = instance.models.Foo;
144+
145+
/**
146+
* Get list of users
147+
* restriction: 'admin'
148+
*/
149+
var index = function(req, res) {
150+
Foo.bar()
151+
};
152+
})
153+
```
154+
84155
## Defining Models
85156
86157
Models are defined as per the suggestion the article here: http://sequelizejs.com/articles/express. All associations are done via the class method `associate` which is injected with the models object.
@@ -107,16 +178,32 @@ module.exports = function(sequelize, DataTypes) {
107178
108179
## Logging
109180
110-
Logging is done via the [winston](https://github.com/winstonjs/winston), the winston object can be accessed at via `orm.logger`. If you want to control the log level you can do it like so:
181+
Logging is optional, but is turned off by default. In order to enable logging, simply inject the logger of your choice:
111182
112183
```js
113-
orm.logger.level = "debug";
184+
185+
myLogger = console;
186+
// myLogger = winston;
187+
// myLogger = ...;
188+
189+
new Connection(
190+
'test-db',
191+
'test-user',
192+
'secret1234',
193+
{
194+
dialect: "mysql",
195+
port: 3306
196+
},
197+
discover,
198+
matcher,
199+
myLogger
200+
)
114201
```
115202
116-
To disable logging entirely:
203+
Your logger must comply with the following interface:
117204
118205
```js
119-
orm.logger.level = null
206+
logger.log(level, message);
120207
```
121208
122209

0 commit comments

Comments
 (0)
This repository has been archived.