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.
Files changed (2) hide show
  1. package/README.md +70 -46
  2. 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
- ### Configuration
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
- `dialect` - Database dialect, e.g. `postgres`, `mssql`, `sqlite`, etc.
38
+ ---
16
39
 
17
- `host` - Database server hostname, e.g. `localhost` or `mydb.postgres.database.azure.com`
40
+ ### Usage
18
41
 
19
- `port` - Database server port, e.g. `5432` for PostgreSQL
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
- `database` - Name of the database to connect to
44
+ Example:
22
45
 
23
- `username` - Database user name
46
+ ```js
47
+ const Base = require('ffc-database')
24
48
 
25
- `password` - Database user password
49
+ const dbBase = new Base(config)
50
+ const db = dbBase.connect()
26
51
 
27
- `ssl` - Boolean or object for SSL configuration (optional, recommended for production)
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
- `logging` - Enable or disable SQL query logging (default: `false`)
58
+ - Model file shape example (models/payment.js):
30
59
 
31
- #### Example
32
- ```
33
- const config = { dialect: 'postgres', host: 'localhost', port: 5432, database: 'ffc_pay', username: 'ffc_user', password: 'ffc_password', ssl: true, logging: false }
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
- ### Connecting to the database
67
+ Payment.associate = (models) => {
68
+ // e.g. Payment.belongsTo(models.User)
69
+ }
37
70
 
38
- ```
39
- const { createDatabaseConnection } = require('ffc-database') const db = createDatabaseConnection(config)
71
+ return Payment
72
+ }
40
73
  ```
41
74
 
42
- ### Running migrations
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
- ### Executing queries
81
+ - Run raw queries via the returned **sequelize** instance:
50
82
 
51
- ```
52
- const [results, metadata] = await db.query('SELECT * FROM payments WHERE status = $1', { replacements: ['pending'] })
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
- ### Closing the connection
90
+ - Close the connection:
56
91
 
57
- ```
58
- await db.close()
92
+ ```js
93
+ await db.sequelize.close()
59
94
  ```
60
95
 
61
- ## Licence
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
- The following attribution statement MUST be cited in your products and
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
- The Open Government Licence (OGL) was developed by the Controller of Her
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
- It is designed to encourage use and re-use of information freely and flexibly,
82
- with only a few conditions.
106
+ ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ffc-database",
3
- "version": "1.0.1",
3
+ "version": "1.0.22",
4
4
  "description": "Databas npm module for FCP services",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -24,4 +24,4 @@
24
24
  "devDependencies": {
25
25
  "standard": "17.1.2"
26
26
  }
27
- }
27
+ }