ffc-database 1.0.1 → 1.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -46
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,82 +1,106 @@
|
|
|
1
1
|
# ffc-database
|
|
2
2
|
|
|
3
|
-
Database utility npm module for FFC services
|
|
4
|
-
|
|
5
|
-
## Usage
|
|
3
|
+
Database utility npm module for FFC services.
|
|
6
4
|
|
|
7
5
|
### Installation
|
|
8
6
|
|
|
9
|
-
```
|
|
7
|
+
```bash
|
|
10
8
|
npm install --save ffc-database
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
### API and configuration
|
|
14
|
+
|
|
15
|
+
- **Constructor argument**: a single **config** object with the following important properties:
|
|
16
|
+
- **database** - Name of the database to connect to.
|
|
17
|
+
- **username** - Database user name.
|
|
18
|
+
- **password** - Database user password.
|
|
19
|
+
- **modelPath** - Filesystem path to the directory containing your Sequelize model files.
|
|
20
|
+
- **Other Sequelize options** - Any other Sequelize options may be included on the same config object and are passed to Sequelize, for example **dialect**, **host**, **port**, **logging**, **ssl**, etc.
|
|
21
|
+
|
|
22
|
+
Example config:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
const config = {
|
|
26
|
+
dialect: 'postgres',
|
|
27
|
+
host: 'localhost',
|
|
28
|
+
port: 5432,
|
|
29
|
+
database: 'ffc_pay',
|
|
30
|
+
username: 'ffc_user',
|
|
31
|
+
password: 'ffc_password',
|
|
32
|
+
modelPath: './models',
|
|
33
|
+
ssl: true,
|
|
34
|
+
logging: false
|
|
35
|
+
}
|
|
36
|
+
```
|
|
14
37
|
|
|
15
|
-
|
|
38
|
+
---
|
|
16
39
|
|
|
17
|
-
|
|
40
|
+
### Usage
|
|
18
41
|
|
|
19
|
-
|
|
42
|
+
- Instantiate the exported class and call **connect**. The returned object contains each model keyed by model name, plus **sequelize** and **Sequelize**.
|
|
20
43
|
|
|
21
|
-
|
|
44
|
+
Example:
|
|
22
45
|
|
|
23
|
-
|
|
46
|
+
```js
|
|
47
|
+
const Base = require('ffc-database')
|
|
24
48
|
|
|
25
|
-
|
|
49
|
+
const dbBase = new Base(config)
|
|
50
|
+
const db = dbBase.connect()
|
|
26
51
|
|
|
27
|
-
|
|
52
|
+
// Access models
|
|
53
|
+
// e.g. db.Payment, db.User
|
|
54
|
+
// Access sequelize instance
|
|
55
|
+
// e.g. db.sequelize.authenticate(), db.sequelize.close()
|
|
56
|
+
```
|
|
28
57
|
|
|
29
|
-
|
|
58
|
+
- Model file shape example (models/payment.js):
|
|
30
59
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
60
|
+
```js
|
|
61
|
+
module.exports = (sequelize, DataTypes) => {
|
|
62
|
+
const Payment = sequelize.define('Payment', {
|
|
63
|
+
amount: { type: DataTypes.DECIMAL },
|
|
64
|
+
status: { type: DataTypes.STRING }
|
|
65
|
+
})
|
|
35
66
|
|
|
36
|
-
|
|
67
|
+
Payment.associate = (models) => {
|
|
68
|
+
// e.g. Payment.belongsTo(models.User)
|
|
69
|
+
}
|
|
37
70
|
|
|
38
|
-
|
|
39
|
-
|
|
71
|
+
return Payment
|
|
72
|
+
}
|
|
40
73
|
```
|
|
41
74
|
|
|
42
|
-
|
|
75
|
+
- The module reads all `.js` files in **modelPath**, ignores files starting with a dot and `index.js`, requires each file and invokes it with `(sequelize, DataTypes)`, and then runs `associate` on models that provide it.
|
|
43
76
|
|
|
44
|
-
|
|
45
|
-
const { runMigrations } = require('ffc-database') await runMigrations(db)
|
|
77
|
+
---
|
|
46
78
|
|
|
47
|
-
|
|
79
|
+
### Querying and closing
|
|
48
80
|
|
|
49
|
-
|
|
81
|
+
- Run raw queries via the returned **sequelize** instance:
|
|
50
82
|
|
|
51
|
-
```
|
|
52
|
-
const [results, metadata] = await db.query(
|
|
83
|
+
```js
|
|
84
|
+
const [results, metadata] = await db.sequelize.query(
|
|
85
|
+
'SELECT * FROM payments WHERE status = $1',
|
|
86
|
+
{ bind: ['pending'] }
|
|
87
|
+
)
|
|
53
88
|
```
|
|
54
89
|
|
|
55
|
-
|
|
90
|
+
- Close the connection:
|
|
56
91
|
|
|
57
|
-
```
|
|
58
|
-
await db.close()
|
|
92
|
+
```js
|
|
93
|
+
await db.sequelize.close()
|
|
59
94
|
```
|
|
60
95
|
|
|
61
|
-
|
|
96
|
+
### Licence
|
|
62
97
|
|
|
63
|
-
THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT
|
|
64
|
-
LICENCE found at:
|
|
98
|
+
THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE
|
|
65
99
|
|
|
66
100
|
<http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3>
|
|
67
101
|
|
|
68
|
-
|
|
69
|
-
applications when using this information.
|
|
70
|
-
|
|
71
|
-
> Contains public sector information licensed under the Open Government license
|
|
72
|
-
> v3
|
|
73
|
-
|
|
74
|
-
### About the licence
|
|
102
|
+
**Attribution statement**
|
|
75
103
|
|
|
76
|
-
|
|
77
|
-
Majesty's Stationery Office (HMSO) to enable information providers in the
|
|
78
|
-
public sector to license the use and re-use of their information under a common
|
|
79
|
-
open licence.
|
|
104
|
+
> Contains public sector information licensed under the Open Government licence v3
|
|
80
105
|
|
|
81
|
-
|
|
82
|
-
with only a few conditions.
|
|
106
|
+
---
|
package/package.json
CHANGED