Commit d65116e1 authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

Serve REST API for "acteurs" & "organes".

parent 11bf2e8f
......@@ -2,6 +2,7 @@ module.exports = {
env: {
node: true,
},
extends: "eslint:recommended",
globals: {
Promise: true,
Set: true,
......@@ -9,10 +10,9 @@ module.exports = {
parser: "babel-eslint",
rules: {
"comma-dangle": ["error", "always-multiline"],
extends: "eslint:recommended",
"max-len": [
"error",
{ code: 120, ignoreRegExpLiterals: true, ignoreUrls: true, tabWidth: 2 },
{ code: 90, ignoreRegExpLiterals: true, ignoreUrls: true, tabWidth: 2 },
],
"no-undef": "error",
"no-unused-expressions": "error",
......
# Tricoteuses-Assemblee-API-Server
## _GraphQL API based on open data of French Assemblée nationale_
## _REST & GraphQL API based on open data of French Assemblée nationale_
By: Emmanuel Raviart <mailto:emmanuel@raviart.com>
Copyright (C) 2019 Emmanuel Raviart
https://framagit.org/tricoteuses/tricoteuses-assemblee-api_server
https://framagit.org/tricoteuses/tricoteuses-assemblee-api-server
> Tricoteuses-Assemblee-API-Server is free software; you can redistribute
> it and/or modify it under the terms of the GNU Affero General Public
......
# Tricoteuses-Assemblee-API-Server
## _GraphQL API based on open data of French Assemblée nationale_
## _REST & GraphQL API based on open data of French Assemblée nationale_
## Installation
```bash
git clone https://framagit.org/tricoteuses/tricoteuses-assemblee-api-server
cd tricoteuses-assemblee-api-server/
```
Create a `.env` file to set PostgreSQL database informations and other configuration variables (you can use `example.env` as a template). Then
```bash
npm install
```
### Database Creation
#### When using _Debian GNU/Linux_
As `root` user:
```bash
apt install postgresql postgresql-contrib
su - postgres
createuser -D -P -R -S assemblee
Enter password for new role: assemblee
Enter it again: assemblee
createdb -E utf-8 -O assemblee assemblee
```
### Configuration
```bash
npm run configure
```
## Usage
### Retrieve & clean open data of Assemblée nationale
### Retrieval & cleaning of open data from Assemblée nationale
_See README file of [Tricoteuses Assemblée](https://framagit.org/tricoteuses/tricoteuses-assemblee) for details._
```bash
npx babel-node --extensions ".ts" -- node_modules/tricoteuses-assemblee/src/scripts/retrieve_open_data.ts ../assemblee-data/
npx babel-node --extensions ".ts" -- node_modules/tricoteuses-assemblee/src/scripts/reorganize_data.ts ../assemblee-data/
npx babel-node --extensions ".ts" -- node_modules/tricoteuses-assemblee/src/scripts/clean_reorganized_data.ts ../assemblee-data/
npx babel-node --extensions ".ts" -- node_modules/tricoteuses-assemblee/src/scripts/retrieve_deputes_photos.ts --fetch ../assemblee-data/
```
### Import of open data files to database
```bash
# Retrieve open data (in JSON format) from Assemblée nationale's website:
npx babel-node --max-old-space-size=5120 -- node_modules/tricoteuses-assemblee/lib/scripts/retrieve_open_data.js ../assemblee-data/
# Split open data big files into small files (one for each entity).
npx babel-node --max-old-space-size=2048 -- node_modules/tricoteuses-assemblee/lib/scripts/split_data.js ../assemblee-data/
# Validate & clean JSON data:
npx babel-node -- node_modules/tricoteuses-assemblee/lib/scripts/clean_split_data.js ../assemblee-data/
# Retrieve députés' pictures from Assemblée nationale's website:
npx babel-node -- node_modules/tricoteuses-assemblee/lib/scripts/retrieve_deputes_photos.js --fetch ../assemblee-data/
# Retrieve députés' non open data informations (email...) from Assemblée nationale's website:
npx babel-node -- node_modules/tricoteuses-assemblee/lib/scripts/retrieve_deputes_infos.js --fetch --parse ../assemblee-data/
npx babel-node --extensions ".ts" -- src/scripts/update_db.ts ../assemblee-data/
```
### Launch development web server
......
# To use this file, rename it to `.env`.
# PostgreSQL database configuration
TRICOTEUSES_ASSEMBLEE_API_DB_HOST="localhost"
TRICOTEUSES_ASSEMBLEE_API_DB_PORT=5432
TRICOTEUSES_ASSEMBLEE_API_DB_NAME="assemblee"
TRICOTEUSES_ASSEMBLEE_API_DB_USER="assemblee"
# Change value of TRICOTEUSES_ASSEMBLEE_API_DB_PASSWORD!
TRICOTEUSES_ASSEMBLEE_API_DB_PASSWORD="assemblee"
......@@ -1754,6 +1754,12 @@
"@types/node": "*"
}
},
"@types/command-line-args": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/@types/command-line-args/-/command-line-args-5.0.0.tgz",
"integrity": "sha512-4eOPXyn5DmP64MCMF8ePDvdlvlzt2a+F8ZaVjqmh2yFCpGjc1kI3kGnCFYX9SCsGTjQcWIyVZ86IHCEyjy/MNg==",
"dev": true
},
"@types/connect": {
"version": "3.4.32",
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.32.tgz",
......@@ -2081,6 +2087,11 @@
"safer-buffer": "~2.1.0"
}
},
"assert-options": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/assert-options/-/assert-options-0.4.0.tgz",
"integrity": "sha512-HukncCp5jpMpKE4vcxH/YrmoJUDTtjpITOdw2OR1yDjhbecUf6EJn/xRPN73ykuvDGRLmAD7OL92zhvMO3nnTA=="
},
"assert-plus": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
......@@ -2219,6 +2230,15 @@
}
}
},
"basic-auth": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
"dev": true,
"requires": {
"safe-buffer": "5.1.2"
}
},
"bcrypt-pbkdf": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
......@@ -2352,6 +2372,11 @@
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A=="
},
"buffer-writer": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz",
"integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw=="
},
"busboy": {
"version": "0.2.14",
"resolved": "https://registry.npmjs.org/busboy/-/busboy-0.2.14.tgz",
......@@ -2705,6 +2730,15 @@
"capture-stack-trace": "^1.0.0"
}
},
"cross-fetch": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.0.4.tgz",
"integrity": "sha512-MSHgpjQqgbT/94D4CyADeNoYh52zMkCX4pcJvPP5WqPsLFMKjr2TCMg381ox5qI0ii2dPwaLx/00477knXqXVw==",
"requires": {
"node-fetch": "2.6.0",
"whatwg-fetch": "3.0.0"
}
},
"cross-spawn": {
"version": "6.0.5",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz",
......@@ -2908,6 +2942,11 @@
"is-obj": "^1.0.0"
}
},
"dotenv": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz",
"integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg=="
},
"duplexer3": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz",
......@@ -5109,6 +5148,11 @@
"semver": "^5.6.0"
}
},
"manakin": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/manakin/-/manakin-0.5.2.tgz",
"integrity": "sha512-pfDSB7QYoVg0Io4KMV9hhPoXpj6p0uBscgtyUSKCOFZe8bqgbpStfgnKIbF/ulnr6U3ICu4OqdyxAqBgOhZwBQ=="
},
"map-cache": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/map-cache/-/map-cache-0.2.2.tgz",
......@@ -5229,6 +5273,19 @@
"minimist": "0.0.8"
}
},
"morgan": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz",
"integrity": "sha512-HQStPIV4y3afTiCYVxirakhlCfGkI161c76kKFca7Fk1JusM//Qeo1ej2XaMniiNeaZklMVrh3vTtIzpzwbpmA==",
"dev": true,
"requires": {
"basic-auth": "~2.0.0",
"debug": "2.6.9",
"depd": "~1.1.2",
"on-finished": "~2.3.0",
"on-headers": "~1.0.1"
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
......@@ -5482,6 +5539,12 @@
"ee-first": "1.1.1"
}
},
"on-headers": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==",
"dev": true
},
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
......@@ -5572,6 +5635,11 @@
"semver": "^5.1.0"
}
},
"packet-reader": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz",
"integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ=="
},
"parent-module": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz",
......@@ -5649,6 +5717,79 @@
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
"integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns="
},
"pg": {
"version": "7.12.0",
"resolved": "https://registry.npmjs.org/pg/-/pg-7.12.0.tgz",
"integrity": "sha512-q54Ic0oBXfDZMwheP8ALeUX32TUXvF7SNgAlZjyhkDuFCJkQCgcLBz0Be5uOrAj3ljSok/CI9lRbYzEko0z1Zw==",
"requires": {
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "0.1.3",
"pg-pool": "^2.0.4",
"pg-types": "~2.0.0",
"pgpass": "1.x",
"semver": "4.3.2"
},
"dependencies": {
"semver": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz",
"integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c="
}
}
},
"pg-connection-string": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz",
"integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc="
},
"pg-int8": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw=="
},
"pg-minify": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/pg-minify/-/pg-minify-1.4.1.tgz",
"integrity": "sha512-8aZ9xdx7Pe/ppFYVOqvU5KgmM6ttXjaBlsl9Y8yzrUH4xSNVucJKKOwm4Y4H+LCvzZGjZIm4Rkf2Ajt5ixtkBQ=="
},
"pg-pool": {
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.7.tgz",
"integrity": "sha512-UiJyO5B9zZpu32GSlP0tXy8J2NsJ9EFGFfz5v6PSbdz/1hBLX1rNiiy5+mAm5iJJYwfCv4A0EBcQLGWwjbpzZw=="
},
"pg-promise": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/pg-promise/-/pg-promise-9.0.0.tgz",
"integrity": "sha512-l8ZKzbxqBHSg+Z1Q1AXv+nPfK0OEpBiqOqIQyQdrfUPyjT2fHwkB+kQCzZ+4Phjh5Z2sAa8la4sRHvEke/P//g==",
"requires": {
"assert-options": "0.4.0",
"manakin": "0.5.2",
"pg": "7.12.0",
"pg-minify": "1.4.1",
"spex": "2.2.0"
}
},
"pg-types": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.0.1.tgz",
"integrity": "sha512-b7y6QM1VF5nOeX9ukMQ0h8a9z89mojrBHXfJeSug4mhL0YpxNBm83ot2TROyoAmX/ZOX3UbwVO4EbH7i1ZZNiw==",
"requires": {
"pg-int8": "1.0.1",
"postgres-array": "~2.0.0",
"postgres-bytea": "~1.0.0",
"postgres-date": "~1.0.4",
"postgres-interval": "^1.1.0"
}
},
"pgpass": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.2.tgz",
"integrity": "sha1-Knu0G2BltnkH6R2hsHwYR8h3swY=",
"requires": {
"split": "^1.0.0"
}
},
"pify": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz",
......@@ -5684,6 +5825,29 @@
"integrity": "sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=",
"dev": true
},
"postgres-array": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",
"integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA=="
},
"postgres-bytea": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz",
"integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU="
},
"postgres-date": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.4.tgz",
"integrity": "sha512-bESRvKVuTrjoBluEcpv2346+6kgB7UlnqWZsnbnCccTNq/pqfj1j6oBaN5+b/NrDXepYUT/HKadqv3iS9lJuVA=="
},
"postgres-interval": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz",
"integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==",
"requires": {
"xtend": "^4.0.0"
}
},
"prelude-ls": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz",
......@@ -6409,6 +6573,19 @@
"integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=",
"dev": true
},
"spex": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/spex/-/spex-2.2.0.tgz",
"integrity": "sha512-iwBxqKe4ZKD+P/i/WdzWw5qxmerHvzVb29wQm4zwYaDPuwsTKjS7nbqt8OyBSLAi2q0ZFUN3F2b4erX0UwF0fA=="
},
"split": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz",
"integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==",
"requires": {
"through": "2"
}
},
"split-string": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/split-string/-/split-string-3.1.0.tgz",
......@@ -6624,8 +6801,7 @@
"through": {
"version": "2.3.8",
"resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz",
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=",
"dev": true
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU="
},
"timed-out": {
"version": "4.0.1",
......@@ -6737,9 +6913,9 @@
}
},
"tslib": {
"version": "1.9.3",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz",
"integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ=="
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.10.0.tgz",
"integrity": "sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ=="
},
"tunnel-agent": {
"version": "0.6.0",
......@@ -7039,6 +7215,11 @@
"iconv-lite": "0.4.24"
}
},
"whatwg-fetch": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz",
"integrity": "sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q=="
},
"whatwg-mimetype": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz",
......@@ -7127,6 +7308,11 @@
"resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.1.1.tgz",
"integrity": "sha512-7hew1RPJ1iIuje/Y01bGD/mXokXxegAgVS+e+E0wSi2ILHQkYAH1+JXARwTjZSM4Z4Z+c73aKspEcqj+zPPL/w=="
},
"xtend": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="
},
"yallist": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz",
......
{
"name": "@tricoteuses/assemblee-api-server",
"version": "0.2.0",
"description": "GraphQL API based on open data of French Assemblée nationale",
"description": "REST & GraphQL API based on open data of French Assemblée nationale",
"keywords": [
"Assemblée nationale",
"France",
"open data",
"Parliament"
],
"main": "bundle.js",
"scripts": {
"build": "npx babel --out-file bundle.js src/index.ts",
"dev": "npx nodemon --watch 'graphql/*.gql' --watch 'src/**/*.ts' --exec 'npx babel-node --extensions \".ts\"' src/index.ts",
"prettier": "npx prettier --write \"src/**/*.ts\""
},
"author": "Emmanuel Raviart <emmanuel@raviart.com>",
"bugs": {
"url": "https://framagit.org/tricoteuses/tricoteuses-assemblee-api-server/issues"
......@@ -24,11 +18,29 @@
"type": "git",
"url": "https://framagit.org/tricoteuses/tricoteuses-assemblee-api-server.git"
},
"main": "dist/server.js",
"types": "dist/server.d.ts",
"scripts": {
"build": "npm run build:types && npm run build:js",
"build:js": "npx babel src --out-dir dist --extensions \".ts\" --source-maps inline",
"build:types": "tsc --emitDeclarationOnly",
"configure": "npx babel-node --extensions \".ts\" src/configure.ts",
"dev": "npx nodemon --watch 'graphql/*.gql' --watch 'src/**/*.ts' --exec 'npx babel-node --extensions \".ts\"' src/server.ts",
"prepare": "npm run build",
"prettier": "npx prettier --write \"src/*.ts\" \"src/**/*.ts\"",
"start": "npx babel-node src/server.js",
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch"
},
"dependencies": {
"@tricoteuses/assemblee": "^0.8.8",
"@tricoteuses/assemblee": "^0.8.9",
"command-line-args": "^5.1.1",
"cross-fetch": "^3.0.4",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"graphql": "^14.4.2",
"graphql-yoga": "^1.18.1"
"graphql-yoga": "^1.18.1",
"pg-promise": "^9.0.0"
},
"devDependencies": {
"@babel/cli": "^7.5.5",
......@@ -55,10 +67,16 @@
"@babel/preset-env": "^7.5.5",
"@babel/preset-typescript": "^7.3.3",
"@babel/register": "^7.5.5",
"@types/command-line-args": "^5.0.0",
"babel-eslint": "^10.0.2",
"eslint": "^6.1.0",
"morgan": "^1.9.1",
"nodemon": "^1.19.1",
"prettier": "^1.18.2",
"tslib": "^1.10.0",
"typescript": "^3.5.3"
},
"engines": {
"node": ">=8"
}
}
import assert from "assert"
import { db, versionNumber } from "./database"
async function configureDatabase() {
// Check that database exists.
await db.connect()
// Table: version
await db.none(
`
CREATE TABLE IF NOT EXISTS version(
number integer NOT NULL
)
`,
)
let version = await db.oneOrNone("SELECT * FROM version")
if (version === null) {
await db.none("INSERT INTO version(number) VALUES ($<number>)", {
number: versionNumber,
})
version = await db.one("SELECT * FROM version")
}
assert(
version.number <= versionNumber,
`Database is too recent for current version of application: ${version.number} > ${versionNumber}.`,
)
if (version.number < versionNumber) {
console.log(
`Upgrading database from version ${version.number} to ${versionNumber}...`,
)
}
// Table: acteurs
await db.none(
`
CREATE TABLE IF NOT EXISTS acteurs (
uid text NOT NULL PRIMARY KEY,
data jsonb
)
`,
)
for (const command of [
"COMMENT ON TABLE acteurs IS 'persons (députés, sénateurs, …)'",
"COMMENT ON COLUMN acteurs.uid IS 'unique ID of acteur (given by Assemblée)'",
"COMMENT ON COLUMN acteurs.data IS 'JSON data of acteur'",
]) {
await db.none(command)
}
// Table: organes
await db.none(
`
CREATE TABLE IF NOT EXISTS organes (
uid text NOT NULL PRIMARY KEY,
data jsonb
)
`,
)
for (const command of [
"COMMENT ON TABLE organes IS 'organizations (commissions, groupes, …)'",
"COMMENT ON COLUMN organes.uid IS 'unique ID of organe (given by Assemblée)'",
"COMMENT ON COLUMN organes.data IS 'JSON data of organe'",
]) {
await db.none(command)
}
const previousVersionNumber = version.number
version.number = versionNumber
assert(
version.number >= previousVersionNumber,
`Error in database upgrade script: Wrong version number: ${version.number} < ${previousVersionNumber}.`,
)
if (version.number !== previousVersionNumber) {
await db.none("UPDATE version SET number = $1", version.number)
console.log(
`Upgraded database from version ${previousVersionNumber} to ${version.number}.`,
)
}
}
configureDatabase()
.then(() => process.exit(0))
.catch(error => {
console.log(error.stack || error)
process.exit(1)
})
import assert from "assert"
import PgPromise from "pg-promise"
import serverConfig from "./server_config"
export const pgPromise = PgPromise()
export const db = pgPromise({
database: serverConfig.db.database,
host: serverConfig.db.host,
password: serverConfig.db.password,
port: serverConfig.db.port,
user: serverConfig.db.user,
})
export let dbSharedConnectionObject: PgPromise.IConnected<unknown> | null = null
export const versionNumber = 0
export async function checkDatabase() {
// Check that database exists.
dbSharedConnectionObject = await db.connect()
assert(
await existsTable("version"),
'Database is not initialized. Run "npm run configure" to configure it.',
)
let version = await db.one("SELECT * FROM version")