@tricoteuses/senat 1.1.2 → 1.3.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 +53 -15
- package/lib/aggregates.d.ts +4 -6
- package/lib/aggregates.mjs +908 -780
- package/lib/aggregates.ts +8 -122
- package/lib/data/legislatures.json +38 -0
- package/lib/databases.d.ts +9 -0
- package/lib/databases.js +41 -11
- package/lib/databases.mjs +32 -1
- package/lib/databases.ts +40 -1
- package/lib/fields.mjs +64 -18
- package/lib/index.d.ts +5 -7
- package/lib/index.js +8 -112
- package/lib/index.mjs +4 -7
- package/lib/index.ts +9 -30
- package/lib/inserters.d.ts +0 -4
- package/lib/inserters.mjs +461 -337
- package/lib/inserters.ts +0 -24
- package/lib/legislatures.json +38 -0
- package/lib/loaders.d.ts +64 -0
- package/lib/loaders.js +682 -0
- package/lib/loaders.mjs +158 -0
- package/lib/loaders.ts +271 -0
- package/lib/model/ameli.d.ts +105 -4
- package/lib/model/ameli.js +20 -160
- package/lib/model/ameli.mjs +84 -57
- package/lib/model/ameli.ts +94 -80
- package/lib/model/debats.d.ts +0 -4
- package/lib/model/debats.js +1 -122
- package/lib/model/debats.mjs +1 -43
- package/lib/model/debats.ts +0 -68
- package/lib/model/dosleg.d.ts +227 -29
- package/lib/model/dosleg.js +92 -832
- package/lib/model/dosleg.mjs +196 -337
- package/lib/model/dosleg.ts +213 -531
- package/lib/model/index.d.ts +4 -5
- package/lib/model/index.js +14 -15
- package/lib/model/index.mjs +4 -5
- package/lib/model/index.ts +8 -5
- package/lib/model/questions.d.ts +88 -2
- package/lib/model/questions.js +40 -45
- package/lib/model/questions.mjs +71 -8
- package/lib/model/questions.ts +90 -11
- package/lib/model/sens.d.ts +524 -2
- package/lib/model/sens.js +172 -49
- package/lib/model/sens.mjs +415 -9
- package/lib/model/sens.ts +514 -16
- package/lib/model/texte.d.ts +7 -0
- package/lib/model/texte.js +321 -0
- package/lib/model/texte.mjs +208 -0
- package/lib/model/texte.ts +229 -0
- package/lib/model/util.d.ts +7 -1
- package/lib/model/util.js +37 -53
- package/lib/model/util.mjs +19 -10
- package/lib/model/util.ts +30 -14
- package/lib/raw_types/ameli.d.ts +912 -538
- package/lib/raw_types/ameli.js +1 -39
- package/lib/raw_types/ameli.mjs +4 -1
- package/lib/raw_types/ameli.ts +947 -597
- package/lib/raw_types/debats.d.ts +204 -124
- package/lib/raw_types/debats.js +1 -18
- package/lib/raw_types/debats.mjs +4 -1
- package/lib/raw_types/debats.ts +218 -141
- package/lib/raw_types/dosleg.d.ts +3533 -2024
- package/lib/raw_types/dosleg.js +1 -92
- package/lib/raw_types/dosleg.mjs +4 -1
- package/lib/raw_types/dosleg.ts +3621 -2189
- package/lib/raw_types/questions.d.ts +404 -220
- package/lib/raw_types/questions.js +1 -18
- package/lib/raw_types/questions.mjs +4 -1
- package/lib/raw_types/questions.ts +404 -226
- package/lib/raw_types/sens.d.ts +4391 -2706
- package/lib/raw_types/sens.js +1 -108
- package/lib/raw_types/sens.mjs +4 -1
- package/lib/raw_types/sens.ts +4495 -2903
- package/lib/raw_types_kysely/ameli.d.ts +6 -0
- package/lib/raw_types_kysely/ameli.mjs +7 -0
- package/lib/raw_types_kysely/ameli.ts +6 -0
- package/lib/raw_types_kysely/debats.d.ts +6 -0
- package/lib/raw_types_kysely/debats.mjs +7 -0
- package/lib/raw_types_kysely/debats.ts +6 -0
- package/lib/raw_types_kysely/dosleg.d.ts +6 -0
- package/lib/raw_types_kysely/dosleg.mjs +7 -0
- package/lib/raw_types_kysely/dosleg.ts +6 -0
- package/lib/raw_types_kysely/questions.d.ts +6 -0
- package/lib/raw_types_kysely/questions.mjs +7 -0
- package/lib/raw_types_kysely/questions.ts +6 -0
- package/lib/raw_types_kysely/sens.d.ts +6 -0
- package/lib/raw_types_kysely/sens.mjs +7 -0
- package/lib/raw_types_kysely/sens.ts +6 -0
- package/lib/raw_types_kysely/texte.d.ts +45 -0
- package/lib/raw_types_kysely/texte.mjs +7 -0
- package/lib/raw_types_kysely/texte.ts +53 -0
- package/lib/raw_types_schemats/ameli.d.ts +541 -0
- package/lib/raw_types_schemats/ameli.js +45 -0
- package/lib/raw_types_schemats/ameli.mjs +2 -0
- package/lib/raw_types_schemats/ameli.ts +601 -0
- package/lib/raw_types_schemats/debats.d.ts +127 -0
- package/lib/raw_types_schemats/debats.js +24 -0
- package/lib/raw_types_schemats/debats.mjs +2 -0
- package/lib/raw_types_schemats/debats.ts +145 -0
- package/lib/raw_types_schemats/dosleg.d.ts +2029 -0
- package/lib/raw_types_schemats/dosleg.js +98 -0
- package/lib/raw_types_schemats/dosleg.mjs +2 -0
- package/lib/raw_types_schemats/dosleg.ts +2195 -0
- package/lib/raw_types_schemats/questions.d.ts +233 -0
- package/lib/raw_types_schemats/questions.js +24 -0
- package/lib/raw_types_schemats/questions.mjs +2 -0
- package/lib/raw_types_schemats/questions.ts +251 -0
- package/lib/raw_types_schemats/sens.d.ts +2709 -0
- package/lib/raw_types_schemats/sens.js +114 -0
- package/lib/raw_types_schemats/sens.mjs +2 -0
- package/lib/raw_types_schemats/sens.ts +2907 -0
- package/lib/scripts/convert_data.js +574 -215
- package/lib/scripts/convert_data.mjs +134 -99
- package/lib/scripts/convert_data.ts +173 -112
- package/lib/scripts/datautil.d.ts +5 -0
- package/lib/scripts/datautil.js +64 -0
- package/lib/scripts/datautil.mjs +16 -0
- package/lib/scripts/datautil.ts +19 -0
- package/lib/scripts/parse_textes.js +132 -0
- package/lib/scripts/parse_textes.mjs +46 -0
- package/lib/scripts/parse_textes.ts +65 -0
- package/lib/scripts/retrieve_documents.d.ts +1 -0
- package/lib/scripts/retrieve_documents.js +521 -0
- package/lib/scripts/retrieve_documents.mjs +249 -0
- package/lib/scripts/retrieve_documents.ts +298 -0
- package/lib/scripts/retrieve_open_data.js +77 -69
- package/lib/scripts/retrieve_open_data.mjs +48 -49
- package/lib/scripts/retrieve_open_data.ts +74 -58
- package/lib/scripts/retrieve_senateurs_photos.js +45 -63
- package/lib/scripts/retrieve_senateurs_photos.mjs +4 -21
- package/lib/scripts/retrieve_senateurs_photos.ts +6 -29
- package/lib/scripts/retrieve_textes.mjs +325 -74
- package/lib/scripts/retrieve_textes.ts +111 -63
- package/lib/scripts/shared/cli_helpers.d.ts +44 -0
- package/lib/scripts/shared/cli_helpers.js +35 -0
- package/lib/scripts/shared/cli_helpers.ts +36 -0
- package/lib/scripts/shared/util.d.ts +3 -0
- package/lib/scripts/shared/util.js +102 -0
- package/lib/scripts/shared/util.ts +33 -0
- package/lib/src/aggregates.d.ts +52 -0
- package/lib/src/aggregates.mjs +726 -0
- package/lib/src/config.d.ts +2 -0
- package/lib/src/config.mjs +16 -0
- package/lib/src/databases.d.ts +18 -0
- package/lib/src/databases.mjs +55 -0
- package/lib/src/datasets.d.ts +28 -0
- package/lib/src/datasets.mjs +78 -0
- package/lib/src/fields.d.ts +10 -0
- package/lib/src/fields.mjs +22 -0
- package/lib/src/index.d.ts +8 -0
- package/lib/src/index.mjs +7 -0
- package/lib/src/inserters.d.ts +98 -0
- package/lib/src/inserters.mjs +360 -0
- package/lib/src/loaders.d.ts +36 -0
- package/lib/src/loaders.mjs +107 -0
- package/lib/src/model/ameli.d.ts +4 -0
- package/lib/src/model/ameli.js +57 -0
- package/lib/src/model/debats.d.ts +4 -0
- package/lib/src/model/debats.js +43 -0
- package/lib/src/model/dosleg.d.ts +197 -0
- package/lib/src/model/dosleg.js +169 -0
- package/lib/src/model/index.d.ts +4 -0
- package/lib/src/model/index.js +4 -0
- package/lib/src/model/questions.d.ts +89 -0
- package/lib/src/model/questions.js +76 -0
- package/lib/src/model/sens.d.ts +390 -0
- package/lib/src/model/sens.js +339 -0
- package/lib/src/model/texte.d.ts +7 -0
- package/lib/src/model/texte.js +183 -0
- package/lib/src/raw_types_kysely/ameli.d.ts +915 -0
- package/lib/src/raw_types_kysely/ameli.js +5 -0
- package/lib/src/raw_types_kysely/debats.d.ts +207 -0
- package/lib/src/raw_types_kysely/debats.js +5 -0
- package/lib/src/raw_types_kysely/dosleg.d.ts +3532 -0
- package/lib/src/raw_types_kysely/dosleg.js +5 -0
- package/lib/src/raw_types_kysely/questions.d.ts +414 -0
- package/lib/src/raw_types_kysely/questions.js +5 -0
- package/lib/src/raw_types_kysely/sens.d.ts +4394 -0
- package/lib/src/raw_types_kysely/sens.js +5 -0
- package/lib/src/raw_types_schemats/ameli.d.ts +541 -0
- package/lib/src/raw_types_schemats/ameli.js +2 -0
- package/lib/src/raw_types_schemats/debats.d.ts +127 -0
- package/lib/src/raw_types_schemats/debats.js +2 -0
- package/lib/src/raw_types_schemats/dosleg.d.ts +2027 -0
- package/lib/src/raw_types_schemats/dosleg.js +2 -0
- package/lib/src/raw_types_schemats/questions.d.ts +231 -0
- package/lib/src/raw_types_schemats/questions.js +2 -0
- package/lib/src/raw_types_schemats/sens.d.ts +2709 -0
- package/lib/src/raw_types_schemats/sens.js +2 -0
- package/lib/src/scripts/convert_data.d.ts +1 -0
- package/lib/src/scripts/convert_data.js +95 -0
- package/lib/src/scripts/datautil.d.ts +5 -0
- package/lib/src/scripts/datautil.js +16 -0
- package/lib/src/scripts/parse_textes.d.ts +1 -0
- package/lib/src/scripts/parse_textes.js +47 -0
- package/lib/src/scripts/retrieve_documents.d.ts +1 -0
- package/lib/src/scripts/retrieve_documents.js +258 -0
- package/lib/src/scripts/retrieve_open_data.d.ts +1 -0
- package/lib/src/scripts/retrieve_open_data.js +214 -0
- package/lib/src/scripts/retrieve_senateurs_photos.d.ts +1 -0
- package/lib/src/scripts/retrieve_senateurs_photos.js +147 -0
- package/lib/src/scripts/shared/cli_helpers.d.ts +44 -0
- package/lib/src/scripts/shared/cli_helpers.js +32 -0
- package/lib/src/scripts/shared/util.d.ts +3 -0
- package/lib/src/scripts/shared/util.js +28 -0
- package/lib/src/strings.d.ts +1 -0
- package/lib/src/strings.mjs +18 -0
- package/lib/src/types/ameli.d.ts +10 -0
- package/lib/src/types/ameli.js +13 -0
- package/lib/src/types/debats.d.ts +4 -0
- package/lib/src/types/debats.js +2 -0
- package/lib/src/types/dosleg.d.ts +98 -0
- package/lib/src/types/dosleg.js +151 -0
- package/lib/src/types/questions.d.ts +2 -0
- package/lib/src/types/questions.js +1 -0
- package/lib/src/types/sens.d.ts +10 -0
- package/lib/src/types/sens.js +1 -0
- package/lib/src/types/sessions.d.ts +42 -0
- package/lib/src/types/sessions.js +43 -0
- package/lib/src/types/texte.d.ts +61 -0
- package/lib/src/types/texte.js +16 -0
- package/lib/src/validators/config.d.ts +1 -0
- package/lib/src/validators/config.js +54 -0
- package/lib/src/validators/senat.d.ts +0 -0
- package/lib/src/validators/senat.js +24 -0
- package/lib/types/ameli.d.ts +1 -1
- package/lib/types/ameli.js +2 -2
- package/lib/types/ameli.ts +1 -1
- package/lib/types/debats.d.ts +1 -1
- package/lib/types/debats.js +2 -2
- package/lib/types/debats.ts +1 -1
- package/lib/types/dosleg.d.ts +2 -2
- package/lib/types/dosleg.js +2 -2
- package/lib/types/dosleg.ts +2 -2
- package/lib/types/questions.d.ts +1 -1
- package/lib/types/questions.js +2 -2
- package/lib/types/questions.ts +1 -1
- package/lib/types/sens.d.ts +1 -3
- package/lib/types/sens.js +2 -5
- package/lib/types/sens.mjs +1 -23
- package/lib/types/sens.ts +1 -25
- package/lib/types/sessions.d.ts +42 -0
- package/lib/types/sessions.js +51 -0
- package/lib/types/sessions.mjs +43 -0
- package/lib/types/sessions.ts +42 -0
- package/lib/types/texte.d.ts +65 -0
- package/lib/types/texte.js +24 -0
- package/lib/types/texte.mjs +16 -0
- package/lib/types/texte.ts +76 -0
- package/package.json +35 -10
- package/lib/aggregates.js +0 -1122
- package/lib/fields.js +0 -68
- package/lib/inserters.js +0 -518
- package/lib/scripts/fix_db.js +0 -144
- package/lib/scripts/fix_db.mjs +0 -64
- package/lib/scripts/fix_db.ts +0 -75
- package/lib/scripts/retrieve_textes.js +0 -179
- /package/lib/scripts/{fix_db.d.ts → parse_textes.d.ts} +0 -0
|
@@ -10,33 +10,27 @@ import readline from "readline";
|
|
|
10
10
|
// import util from "util"
|
|
11
11
|
import windows1252 from "windows-1252";
|
|
12
12
|
import config from "../config";
|
|
13
|
-
import { getChosenFromEnabledDatasets } from
|
|
13
|
+
import { datasets, getChosenFromEnabledDatasets } from "../datasets";
|
|
14
|
+
import { commonOptions } from "./shared/cli_helpers";
|
|
14
15
|
const badWindows1252CharacterRegex = /[\u0080-\u009f]/g;
|
|
15
16
|
const optionsDefinitions = [
|
|
16
|
-
|
|
17
|
-
alias: 'k',
|
|
18
|
-
defaultValue: ['All'],
|
|
19
|
-
help: 'categories of datasets to reorganize',
|
|
20
|
-
multiple: true,
|
|
21
|
-
name: 'categories',
|
|
22
|
-
type: String,
|
|
23
|
-
},
|
|
17
|
+
...commonOptions,
|
|
24
18
|
{
|
|
25
19
|
alias: "a",
|
|
26
|
-
help: "all options: fetch, unzip, repair-encoding, import
|
|
20
|
+
help: "all options: fetch, unzip, repair-encoding, import",
|
|
27
21
|
name: "all",
|
|
28
22
|
type: Boolean,
|
|
29
23
|
},
|
|
30
24
|
{
|
|
31
25
|
alias: "c",
|
|
32
|
-
help: "create TypeScript interfaces from databases schemas into src/
|
|
26
|
+
help: "create TypeScript interfaces from databases schemas into src/raw_types_* directories",
|
|
33
27
|
name: "schema",
|
|
34
28
|
type: Boolean,
|
|
35
29
|
},
|
|
36
30
|
{
|
|
37
31
|
alias: "e",
|
|
38
32
|
help: "repair Windows CP 1252 encoding of SQL dumps",
|
|
39
|
-
name: "
|
|
33
|
+
name: "repairEncoding",
|
|
40
34
|
type: Boolean,
|
|
41
35
|
},
|
|
42
36
|
{
|
|
@@ -51,24 +45,18 @@ const optionsDefinitions = [
|
|
|
51
45
|
name: "import",
|
|
52
46
|
type: Boolean,
|
|
53
47
|
},
|
|
48
|
+
{
|
|
49
|
+
alias: "S",
|
|
50
|
+
help: "sudo psql commands with given user",
|
|
51
|
+
name: "sudo",
|
|
52
|
+
type: String,
|
|
53
|
+
},
|
|
54
54
|
{
|
|
55
55
|
alias: "z",
|
|
56
56
|
help: "unzip SQL files",
|
|
57
57
|
name: "unzip",
|
|
58
58
|
type: Boolean,
|
|
59
59
|
},
|
|
60
|
-
{
|
|
61
|
-
alias: "s",
|
|
62
|
-
help: "don't log anything",
|
|
63
|
-
name: "silent",
|
|
64
|
-
type: Boolean,
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
defaultOption: true,
|
|
68
|
-
help: "directory containing Sénat open data files",
|
|
69
|
-
name: "dataDir",
|
|
70
|
-
type: String,
|
|
71
|
-
},
|
|
72
60
|
];
|
|
73
61
|
const options = commandLineArgs(optionsDefinitions);
|
|
74
62
|
// const pipeline = util.promisify(stream.pipeline)
|
|
@@ -128,7 +116,7 @@ async function retrieveDataset(dataDir, dataset) {
|
|
|
128
116
|
dataset.repairZip(dataset, dataDir);
|
|
129
117
|
}
|
|
130
118
|
}
|
|
131
|
-
if ((options.all || options
|
|
119
|
+
if ((options.all || options.repairEncoding) && dataset.repairEncoding) {
|
|
132
120
|
if (!options.silent) {
|
|
133
121
|
console.log(`Repairing Windows CP1252 encoding of ${dataset.title}: ${sqlFilename}…`);
|
|
134
122
|
}
|
|
@@ -146,38 +134,26 @@ async function retrieveDataset(dataDir, dataset) {
|
|
|
146
134
|
repairedSqlWriter.end();
|
|
147
135
|
await fs.move(repairedSqlFilePath, sqlFilePath, { overwrite: true });
|
|
148
136
|
}
|
|
149
|
-
if (options.all || options.import) {
|
|
137
|
+
if (options.all || options.import || options.schema) {
|
|
150
138
|
if (!options.silent) {
|
|
151
139
|
console.log(`Importing ${dataset.title}: ${sqlFilename}…`);
|
|
152
140
|
}
|
|
153
|
-
execSync(`
|
|
154
|
-
cwd: dataDir,
|
|
155
|
-
env: process.env,
|
|
156
|
-
encoding: "utf-8",
|
|
157
|
-
// stdio: ["ignore", "ignore", "pipe"],
|
|
158
|
-
});
|
|
159
|
-
execSync(`psql -c "CREATE DATABASE ${dataset.database} WITH OWNER opendata"`, {
|
|
160
|
-
cwd: dataDir,
|
|
161
|
-
env: process.env,
|
|
162
|
-
encoding: "utf-8",
|
|
163
|
-
// stdio: ["ignore", "ignore", "pipe"],
|
|
164
|
-
});
|
|
165
|
-
execSync(`psql -f ${sqlFilename} ${dataset.database}`, {
|
|
141
|
+
execSync(`${options.sudo ? `sudo -u ${options.sudo} ` : ""}psql --quiet -d ${dataset.database} -f ${sqlFilename}`, {
|
|
166
142
|
cwd: dataDir,
|
|
167
143
|
env: process.env,
|
|
168
144
|
encoding: "utf-8",
|
|
169
|
-
|
|
145
|
+
stdio: ["pipe", "ignore", "ignore"],
|
|
170
146
|
});
|
|
171
147
|
}
|
|
172
148
|
if (options.schema) {
|
|
173
|
-
|
|
149
|
+
let definitionsDir = path.resolve("src", "raw_types_schemats");
|
|
174
150
|
assert(fs.statSync(definitionsDir).isDirectory());
|
|
175
151
|
if (!options.silent) {
|
|
176
152
|
console.log(`Creating TypeScript definitions from schema of database ${dataset.database}…`);
|
|
177
153
|
}
|
|
178
154
|
const dbConnectionString = `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@${process.env.PGHOST}:${process.env.PGPORT}/${dataset.database}`;
|
|
179
|
-
|
|
180
|
-
execSync(`npx schemats generate -c ${dbConnectionString} -s
|
|
155
|
+
let definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`);
|
|
156
|
+
execSync(`npx schemats generate -c ${dbConnectionString} -s ${dataset.schema} -o ${definitionFilePath}`, {
|
|
181
157
|
// cwd: dataDir,
|
|
182
158
|
env: process.env,
|
|
183
159
|
encoding: "utf-8",
|
|
@@ -188,6 +164,14 @@ async function retrieveDataset(dataDir, dataset) {
|
|
|
188
164
|
.replace(/\r\n/g, "\n")
|
|
189
165
|
.replace(/AUTO-GENERATED FILE @ \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, "AUTO-GENERATED FILE");
|
|
190
166
|
fs.writeFileSync(definitionFilePath, definitionRepaired);
|
|
167
|
+
definitionsDir = path.resolve("src", "raw_types");
|
|
168
|
+
definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`);
|
|
169
|
+
execSync(`kysely-codegen --url ${dbConnectionString} --default-schema=${dataset.schema} --out-file=${definitionFilePath}`, {
|
|
170
|
+
// cwd: dataDir,
|
|
171
|
+
env: process.env,
|
|
172
|
+
encoding: "utf-8",
|
|
173
|
+
// stdio: ["ignore", "ignore", "pipe"],
|
|
174
|
+
});
|
|
191
175
|
}
|
|
192
176
|
}
|
|
193
177
|
async function retrieveOpenData() {
|
|
@@ -198,17 +182,32 @@ async function retrieveOpenData() {
|
|
|
198
182
|
PGHOST: process.env.PGHOST || config.db.host,
|
|
199
183
|
PGPORT: process.env.PGPORT || config.db.port,
|
|
200
184
|
PGUSER: process.env.PGUSER || config.db.user,
|
|
201
|
-
PGPASSWORD: process.env.PGPASSWORD || config.db.password
|
|
185
|
+
PGPASSWORD: process.env.PGPASSWORD || config.db.password,
|
|
202
186
|
};
|
|
203
|
-
assert(process.env.PGHOST
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
187
|
+
assert(process.env.PGHOST &&
|
|
188
|
+
process.env.PGPORT &&
|
|
189
|
+
process.env.PGUSER &&
|
|
190
|
+
process.env.PGPASSWORD, "Missing database configuration: environment variables PGHOST, PGPORT, PGUSER and PGPASSWORD or TRICOTEUSES_SENAT_DB_* in .env file");
|
|
191
|
+
console.time("data extraction time");
|
|
192
|
+
for (const [, dataset] of Object.entries(datasets)) {
|
|
193
|
+
execSync(`${options.sudo ? `sudo -u ${options.sudo} ` : ""}psql --quiet -c "DROP DATABASE IF EXISTS ${dataset.database}"`, {
|
|
194
|
+
cwd: dataDir,
|
|
195
|
+
env: process.env,
|
|
196
|
+
encoding: "utf-8",
|
|
197
|
+
});
|
|
198
|
+
execSync(`${options.sudo ? `sudo -u ${options.sudo} ` : ""}psql --quiet -c "CREATE DATABASE ${dataset.database} WITH OWNER opendata"`, {
|
|
199
|
+
cwd: dataDir,
|
|
200
|
+
env: process.env,
|
|
201
|
+
encoding: "utf-8",
|
|
202
|
+
});
|
|
203
|
+
}
|
|
207
204
|
const choosenDatasets = getChosenFromEnabledDatasets(options.categories);
|
|
208
|
-
// await Promise.all(choosenDatasets.map(dataset => retrieveDataset(dataDir, dataset)))
|
|
209
205
|
for (const dataset of choosenDatasets) {
|
|
210
206
|
await retrieveDataset(dataDir, dataset);
|
|
211
207
|
}
|
|
208
|
+
if (!options.silent) {
|
|
209
|
+
console.timeEnd("data extraction time");
|
|
210
|
+
}
|
|
212
211
|
}
|
|
213
212
|
retrieveOpenData()
|
|
214
213
|
.then(() => process.exit(0))
|
|
@@ -11,38 +11,28 @@ import readline from "readline"
|
|
|
11
11
|
import windows1252 from "windows-1252"
|
|
12
12
|
|
|
13
13
|
import config from "../config"
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
getChosenFromEnabledDatasets
|
|
17
|
-
} from '../datasets'
|
|
14
|
+
import { Dataset, datasets, getChosenFromEnabledDatasets } from "../datasets"
|
|
15
|
+
import { commonOptions } from "./shared/cli_helpers"
|
|
18
16
|
|
|
19
17
|
const badWindows1252CharacterRegex = /[\u0080-\u009f]/g
|
|
20
18
|
const optionsDefinitions = [
|
|
21
|
-
|
|
22
|
-
alias: 'k',
|
|
23
|
-
defaultValue: ['All'],
|
|
24
|
-
help: 'categories of datasets to reorganize',
|
|
25
|
-
multiple: true,
|
|
26
|
-
name: 'categories',
|
|
27
|
-
type: String,
|
|
28
|
-
},
|
|
19
|
+
...commonOptions,
|
|
29
20
|
{
|
|
30
21
|
alias: "a",
|
|
31
|
-
help: "all options: fetch, unzip, repair-encoding, import
|
|
22
|
+
help: "all options: fetch, unzip, repair-encoding, import",
|
|
32
23
|
name: "all",
|
|
33
24
|
type: Boolean,
|
|
34
25
|
},
|
|
35
26
|
{
|
|
36
27
|
alias: "c",
|
|
37
|
-
help:
|
|
38
|
-
"create TypeScript interfaces from databases schemas into src/raw_types directory",
|
|
28
|
+
help: "create TypeScript interfaces from databases schemas into src/raw_types_* directories",
|
|
39
29
|
name: "schema",
|
|
40
30
|
type: Boolean,
|
|
41
31
|
},
|
|
42
32
|
{
|
|
43
33
|
alias: "e",
|
|
44
34
|
help: "repair Windows CP 1252 encoding of SQL dumps",
|
|
45
|
-
name: "
|
|
35
|
+
name: "repairEncoding",
|
|
46
36
|
type: Boolean,
|
|
47
37
|
},
|
|
48
38
|
{
|
|
@@ -57,24 +47,18 @@ const optionsDefinitions = [
|
|
|
57
47
|
name: "import",
|
|
58
48
|
type: Boolean,
|
|
59
49
|
},
|
|
50
|
+
{
|
|
51
|
+
alias: "S",
|
|
52
|
+
help: "sudo psql commands with given user",
|
|
53
|
+
name: "sudo",
|
|
54
|
+
type: String,
|
|
55
|
+
},
|
|
60
56
|
{
|
|
61
57
|
alias: "z",
|
|
62
58
|
help: "unzip SQL files",
|
|
63
59
|
name: "unzip",
|
|
64
60
|
type: Boolean,
|
|
65
61
|
},
|
|
66
|
-
{
|
|
67
|
-
alias: "s",
|
|
68
|
-
help: "don't log anything",
|
|
69
|
-
name: "silent",
|
|
70
|
-
type: Boolean,
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
defaultOption: true,
|
|
74
|
-
help: "directory containing Sénat open data files",
|
|
75
|
-
name: "dataDir",
|
|
76
|
-
type: String,
|
|
77
|
-
},
|
|
78
62
|
]
|
|
79
63
|
const options = commandLineArgs(optionsDefinitions)
|
|
80
64
|
|
|
@@ -140,7 +124,7 @@ async function retrieveDataset(
|
|
|
140
124
|
}
|
|
141
125
|
}
|
|
142
126
|
|
|
143
|
-
if ((options.all || options
|
|
127
|
+
if ((options.all || options.repairEncoding) && dataset.repairEncoding) {
|
|
144
128
|
if (!options.silent) {
|
|
145
129
|
console.log(
|
|
146
130
|
`Repairing Windows CP1252 encoding of ${dataset.title}: ${sqlFilename}…`,
|
|
@@ -165,35 +149,25 @@ async function retrieveDataset(
|
|
|
165
149
|
await fs.move(repairedSqlFilePath, sqlFilePath, { overwrite: true })
|
|
166
150
|
}
|
|
167
151
|
|
|
168
|
-
if (options.all || options.import) {
|
|
152
|
+
if (options.all || options.import || options.schema) {
|
|
169
153
|
if (!options.silent) {
|
|
170
154
|
console.log(`Importing ${dataset.title}: ${sqlFilename}…`)
|
|
171
155
|
}
|
|
172
|
-
execSync(`psql -c "DROP DATABASE IF EXISTS ${dataset.database}"`, {
|
|
173
|
-
cwd: dataDir,
|
|
174
|
-
env: process.env,
|
|
175
|
-
encoding: "utf-8",
|
|
176
|
-
// stdio: ["ignore", "ignore", "pipe"],
|
|
177
|
-
})
|
|
178
156
|
execSync(
|
|
179
|
-
`
|
|
157
|
+
`${options.sudo ? `sudo -u ${options.sudo} ` : ""}psql --quiet -d ${
|
|
158
|
+
dataset.database
|
|
159
|
+
} -f ${sqlFilename}`,
|
|
180
160
|
{
|
|
181
161
|
cwd: dataDir,
|
|
182
162
|
env: process.env,
|
|
183
163
|
encoding: "utf-8",
|
|
184
|
-
|
|
164
|
+
stdio: ["pipe", "ignore", "ignore"],
|
|
185
165
|
},
|
|
186
166
|
)
|
|
187
|
-
execSync(`psql -f ${sqlFilename} ${dataset.database}`, {
|
|
188
|
-
cwd: dataDir,
|
|
189
|
-
env: process.env,
|
|
190
|
-
encoding: "utf-8",
|
|
191
|
-
// stdio: ["ignore", "ignore", "pipe"],
|
|
192
|
-
})
|
|
193
167
|
}
|
|
194
168
|
|
|
195
169
|
if (options.schema) {
|
|
196
|
-
|
|
170
|
+
let definitionsDir = path.resolve("src", "raw_types_schemats")
|
|
197
171
|
assert(fs.statSync(definitionsDir).isDirectory())
|
|
198
172
|
if (!options.silent) {
|
|
199
173
|
console.log(
|
|
@@ -201,12 +175,9 @@ async function retrieveDataset(
|
|
|
201
175
|
)
|
|
202
176
|
}
|
|
203
177
|
const dbConnectionString = `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@${process.env.PGHOST}:${process.env.PGPORT}/${dataset.database}`
|
|
204
|
-
|
|
205
|
-
definitionsDir,
|
|
206
|
-
`${dataset.database}.ts`,
|
|
207
|
-
)
|
|
178
|
+
let definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`)
|
|
208
179
|
execSync(
|
|
209
|
-
`npx schemats generate -c ${dbConnectionString} -s
|
|
180
|
+
`npx schemats generate -c ${dbConnectionString} -s ${dataset.schema} -o ${definitionFilePath}`,
|
|
210
181
|
{
|
|
211
182
|
// cwd: dataDir,
|
|
212
183
|
env: process.env,
|
|
@@ -222,6 +193,18 @@ async function retrieveDataset(
|
|
|
222
193
|
"AUTO-GENERATED FILE",
|
|
223
194
|
)
|
|
224
195
|
fs.writeFileSync(definitionFilePath, definitionRepaired)
|
|
196
|
+
|
|
197
|
+
definitionsDir = path.resolve("src", "raw_types")
|
|
198
|
+
definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`)
|
|
199
|
+
execSync(
|
|
200
|
+
`kysely-codegen --url ${dbConnectionString} --default-schema=${dataset.schema} --out-file=${definitionFilePath}`,
|
|
201
|
+
{
|
|
202
|
+
// cwd: dataDir,
|
|
203
|
+
env: process.env,
|
|
204
|
+
encoding: "utf-8",
|
|
205
|
+
// stdio: ["ignore", "ignore", "pipe"],
|
|
206
|
+
},
|
|
207
|
+
)
|
|
225
208
|
}
|
|
226
209
|
}
|
|
227
210
|
|
|
@@ -234,20 +217,53 @@ async function retrieveOpenData(): Promise<void> {
|
|
|
234
217
|
PGHOST: process.env.PGHOST || config.db.host,
|
|
235
218
|
PGPORT: process.env.PGPORT || config.db.port,
|
|
236
219
|
PGUSER: process.env.PGUSER || config.db.user,
|
|
237
|
-
PGPASSWORD: process.env.PGPASSWORD || config.db.password
|
|
220
|
+
PGPASSWORD: process.env.PGPASSWORD || config.db.password,
|
|
238
221
|
}
|
|
239
|
-
assert(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
222
|
+
assert(
|
|
223
|
+
process.env.PGHOST &&
|
|
224
|
+
process.env.PGPORT &&
|
|
225
|
+
process.env.PGUSER &&
|
|
226
|
+
process.env.PGPASSWORD,
|
|
227
|
+
"Missing database configuration: environment variables PGHOST, PGPORT, PGUSER and PGPASSWORD or TRICOTEUSES_SENAT_DB_* in .env file",
|
|
244
228
|
)
|
|
245
229
|
|
|
246
|
-
|
|
247
|
-
|
|
230
|
+
console.time("data extraction time")
|
|
231
|
+
|
|
232
|
+
for (const [, dataset] of Object.entries(datasets)) {
|
|
233
|
+
execSync(
|
|
234
|
+
`${
|
|
235
|
+
options.sudo ? `sudo -u ${options.sudo} ` : ""
|
|
236
|
+
}psql --quiet -c "DROP DATABASE IF EXISTS ${dataset.database}"`,
|
|
237
|
+
{
|
|
238
|
+
cwd: dataDir,
|
|
239
|
+
env: process.env,
|
|
240
|
+
encoding: "utf-8",
|
|
241
|
+
},
|
|
242
|
+
)
|
|
243
|
+
execSync(
|
|
244
|
+
`${
|
|
245
|
+
options.sudo ? `sudo -u ${options.sudo} ` : ""
|
|
246
|
+
}psql --quiet -c "CREATE DATABASE ${
|
|
247
|
+
dataset.database
|
|
248
|
+
} WITH OWNER opendata"`,
|
|
249
|
+
{
|
|
250
|
+
cwd: dataDir,
|
|
251
|
+
env: process.env,
|
|
252
|
+
encoding: "utf-8",
|
|
253
|
+
},
|
|
254
|
+
)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const choosenDatasets: Dataset[] = getChosenFromEnabledDatasets(
|
|
258
|
+
options.categories,
|
|
259
|
+
)
|
|
248
260
|
for (const dataset of choosenDatasets) {
|
|
249
261
|
await retrieveDataset(dataDir, dataset)
|
|
250
262
|
}
|
|
263
|
+
|
|
264
|
+
if (!options.silent) {
|
|
265
|
+
console.timeEnd("data extraction time")
|
|
266
|
+
}
|
|
251
267
|
}
|
|
252
268
|
|
|
253
269
|
retrieveOpenData()
|