You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 25, 2022. It is now read-only.
Copy file name to clipboardexpand all lines: README.md
+104-17
Original file line number
Diff line number
Diff line change
@@ -17,31 +17,52 @@ sequelize-connect is a simple singleton wrapper for the sequelize ORM, making it
17
17
18
18
***NOTE:***`sequelize-connect` must be configured upon app initialization, prior to [accessing your models](#accessing-sequelize)
19
19
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`.
21
21
```js
22
22
// app.js
23
-
varorm=require('sequelize-connect');
23
+
varConnection=require('sequelize-connect');
24
24
25
-
orm.discover= [__dirname+'/models'];
26
-
orm.connect(
25
+
var orm =newConnection(
27
26
'test-db',
28
27
'test-user',
29
28
'secret1234',
30
29
{
31
30
dialect:"mysql",
32
31
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 =newConnection(
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
+
});
37
58
```
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.
39
60
40
61
#### Connection String
41
62
You can use a connection string to connect as well:
42
63
43
64
```js
44
-
orm.connect(
65
+
newConnection(
45
66
'MyConnectionString',
46
67
{
47
68
dialect:"mysql",
@@ -56,15 +77,27 @@ orm.connect(
56
77
## Custom matcher
57
78
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.
58
79
59
-
This function should be attached to `matcher` like so:
80
+
This function should be injected to `Connection` like so:
60
81
61
82
```js
62
-
orm.matcher=function(file){
83
+
varmatcher=function(file){
63
84
if(//some condition or regex here)
64
85
returntrue;
65
86
66
87
returnfalse;
67
88
};
89
+
90
+
newConnection(
91
+
'test-db',
92
+
'test-user',
93
+
'secret1234',
94
+
{
95
+
dialect:"mysql",
96
+
port:3306
97
+
},
98
+
discover,
99
+
matcher
100
+
)
68
101
```
69
102
70
103
@@ -74,13 +107,51 @@ After connecting you can access the sequelize instance and models wherever you n
74
107
```js
75
108
// somefile.js
76
109
77
-
var orm =require('sequelize-connect');
110
+
var Connection =require('sequelize-connect');
111
+
var orm =newConnection(); // singleton pattern - returns the created instance
78
112
var sequelize =orm.sequelize;
79
113
var Sequelize =orm.Sequelize;
80
114
var models =orm.models;
81
115
var User =models.User;
82
116
```
83
117
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
+
newConnection(
124
+
'MyConnectionString',
125
+
{
126
+
dialect:"mysql",
127
+
port:3306
128
+
})
129
+
```
130
+
131
+
```js
132
+
// foobar.js
133
+
134
+
varPromise=require('bluebird');
135
+
var Connection =require('sequelize-connect');
136
+
var orm =newConnection();
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
+
varPromise.resolve(orm)
142
+
.then(function(instance) {
143
+
var User =instance.models.Foo;
144
+
145
+
/**
146
+
* Get list of users
147
+
* restriction: 'admin'
148
+
*/
149
+
varindex=function(req, res) {
150
+
Foo.bar()
151
+
};
152
+
})
153
+
```
154
+
84
155
## Defining Models
85
156
86
157
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.
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:
111
182
112
183
```js
113
-
orm.logger.level="debug";
184
+
185
+
myLogger =console;
186
+
// myLogger = winston;
187
+
// myLogger = ...;
188
+
189
+
newConnection(
190
+
'test-db',
191
+
'test-user',
192
+
'secret1234',
193
+
{
194
+
dialect:"mysql",
195
+
port:3306
196
+
},
197
+
discover,
198
+
matcher,
199
+
myLogger
200
+
)
114
201
```
115
202
116
-
To disable logging entirely:
203
+
Your logger must comply with the following interface:
0 commit comments