ffc-database 1.0.1
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 +82 -0
- package/app/database/base.js +39 -0
- package/app/database/database.js +5 -0
- package/app/database/index.js +5 -0
- package/index.js +5 -0
- package/package.json +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# ffc-database
|
|
2
|
+
|
|
3
|
+
Database utility npm module for FFC services
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
### Installation
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install --save ffc-database
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Configuration
|
|
14
|
+
|
|
15
|
+
`dialect` - Database dialect, e.g. `postgres`, `mssql`, `sqlite`, etc.
|
|
16
|
+
|
|
17
|
+
`host` - Database server hostname, e.g. `localhost` or `mydb.postgres.database.azure.com`
|
|
18
|
+
|
|
19
|
+
`port` - Database server port, e.g. `5432` for PostgreSQL
|
|
20
|
+
|
|
21
|
+
`database` - Name of the database to connect to
|
|
22
|
+
|
|
23
|
+
`username` - Database user name
|
|
24
|
+
|
|
25
|
+
`password` - Database user password
|
|
26
|
+
|
|
27
|
+
`ssl` - Boolean or object for SSL configuration (optional, recommended for production)
|
|
28
|
+
|
|
29
|
+
`logging` - Enable or disable SQL query logging (default: `false`)
|
|
30
|
+
|
|
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
|
+
```
|
|
35
|
+
|
|
36
|
+
### Connecting to the database
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
const { createDatabaseConnection } = require('ffc-database') const db = createDatabaseConnection(config)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Running migrations
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
const { runMigrations } = require('ffc-database') await runMigrations(db)
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Executing queries
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
const [results, metadata] = await db.query('SELECT * FROM payments WHERE status = $1', { replacements: ['pending'] })
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Closing the connection
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
await db.close()
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Licence
|
|
62
|
+
|
|
63
|
+
THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT
|
|
64
|
+
LICENCE found at:
|
|
65
|
+
|
|
66
|
+
<http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3>
|
|
67
|
+
|
|
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
|
|
75
|
+
|
|
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.
|
|
80
|
+
|
|
81
|
+
It is designed to encourage use and re-use of information freely and flexibly,
|
|
82
|
+
with only a few conditions.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const { Sequelize, DataTypes } = require('sequelize')
|
|
4
|
+
const db = {}
|
|
5
|
+
|
|
6
|
+
class Base {
|
|
7
|
+
constructor (config) {
|
|
8
|
+
this.database = config.database
|
|
9
|
+
this.username = config.username
|
|
10
|
+
this.password = config.password
|
|
11
|
+
this.modelPath = config.modelPath
|
|
12
|
+
this.dbConfig = config
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
connect () {
|
|
16
|
+
const sequelize = new Sequelize(this.database, this.username, this.password, this.dbConfig)
|
|
17
|
+
fs.readdirSync(this.modelPath)
|
|
18
|
+
.filter(file => {
|
|
19
|
+
return (file.indexOf('.') !== 0) && (file !== 'index.js') && (file.slice(-3) === '.js')
|
|
20
|
+
})
|
|
21
|
+
.forEach(file => {
|
|
22
|
+
const model = require(path.join(this.modelPath, file))(sequelize, DataTypes)
|
|
23
|
+
db[model.name] = model
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
Object.keys(db).forEach(modelName => {
|
|
27
|
+
if (db[modelName].associate) {
|
|
28
|
+
db[modelName].associate(db)
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
db.sequelize = sequelize
|
|
33
|
+
db.Sequelize = Sequelize
|
|
34
|
+
|
|
35
|
+
return db
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = Base
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ffc-database",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Databas npm module for FCP services",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/DEFRA/ffc-database.git"
|
|
9
|
+
},
|
|
10
|
+
"author": "Defra",
|
|
11
|
+
"contributors": [
|
|
12
|
+
"John Barnard john.barnard.external@atos.net"
|
|
13
|
+
],
|
|
14
|
+
"license": "OGL-UK-3.0",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/DEFRA/ffc-database/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/DEFRA/ffc-database#readme",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"pg": "8.7.3",
|
|
21
|
+
"pg-hstore": "2.3.4",
|
|
22
|
+
"sequelize": "6.31.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"standard": "17.1.2"
|
|
26
|
+
}
|
|
27
|
+
}
|