@tricoteuses/senat 1.1.1 → 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.
Files changed (259) hide show
  1. package/README.md +53 -15
  2. package/lib/aggregates.d.ts +4 -6
  3. package/lib/aggregates.mjs +908 -780
  4. package/lib/aggregates.ts +8 -122
  5. package/lib/data/legislatures.json +38 -0
  6. package/lib/databases.d.ts +9 -0
  7. package/lib/databases.js +41 -11
  8. package/lib/databases.mjs +32 -1
  9. package/lib/databases.ts +40 -1
  10. package/lib/fields.mjs +64 -18
  11. package/lib/index.d.ts +5 -6
  12. package/lib/index.js +8 -99
  13. package/lib/index.mjs +4 -6
  14. package/lib/index.ts +9 -26
  15. package/lib/inserters.d.ts +0 -4
  16. package/lib/inserters.mjs +461 -337
  17. package/lib/inserters.ts +0 -24
  18. package/lib/legislatures.json +38 -0
  19. package/lib/loaders.d.ts +64 -0
  20. package/lib/loaders.js +682 -0
  21. package/lib/loaders.mjs +158 -0
  22. package/lib/loaders.ts +271 -0
  23. package/lib/model/ameli.d.ts +105 -4
  24. package/lib/model/ameli.js +20 -160
  25. package/lib/model/ameli.mjs +84 -57
  26. package/lib/model/ameli.ts +94 -80
  27. package/lib/model/debats.d.ts +0 -4
  28. package/lib/model/debats.js +1 -122
  29. package/lib/model/debats.mjs +1 -43
  30. package/lib/model/debats.ts +0 -68
  31. package/lib/model/dosleg.d.ts +227 -29
  32. package/lib/model/dosleg.js +92 -832
  33. package/lib/model/dosleg.mjs +196 -337
  34. package/lib/model/dosleg.ts +213 -531
  35. package/lib/model/index.d.ts +4 -5
  36. package/lib/model/index.js +14 -15
  37. package/lib/model/index.mjs +4 -5
  38. package/lib/model/index.ts +8 -5
  39. package/lib/model/questions.d.ts +88 -2
  40. package/lib/model/questions.js +40 -45
  41. package/lib/model/questions.mjs +71 -8
  42. package/lib/model/questions.ts +90 -11
  43. package/lib/model/sens.d.ts +524 -2
  44. package/lib/model/sens.js +172 -49
  45. package/lib/model/sens.mjs +415 -9
  46. package/lib/model/sens.ts +514 -16
  47. package/lib/model/texte.d.ts +7 -0
  48. package/lib/model/texte.js +321 -0
  49. package/lib/model/texte.mjs +208 -0
  50. package/lib/model/texte.ts +229 -0
  51. package/lib/model/util.d.ts +7 -1
  52. package/lib/model/util.js +37 -53
  53. package/lib/model/util.mjs +19 -10
  54. package/lib/model/util.ts +30 -14
  55. package/lib/raw_types/ameli.d.ts +912 -538
  56. package/lib/raw_types/ameli.js +1 -39
  57. package/lib/raw_types/ameli.mjs +4 -1
  58. package/lib/raw_types/ameli.ts +947 -597
  59. package/lib/raw_types/debats.d.ts +204 -124
  60. package/lib/raw_types/debats.js +1 -18
  61. package/lib/raw_types/debats.mjs +4 -1
  62. package/lib/raw_types/debats.ts +218 -141
  63. package/lib/raw_types/dosleg.d.ts +3533 -2024
  64. package/lib/raw_types/dosleg.js +1 -92
  65. package/lib/raw_types/dosleg.mjs +4 -1
  66. package/lib/raw_types/dosleg.ts +3621 -2189
  67. package/lib/raw_types/questions.d.ts +404 -220
  68. package/lib/raw_types/questions.js +1 -18
  69. package/lib/raw_types/questions.mjs +4 -1
  70. package/lib/raw_types/questions.ts +404 -226
  71. package/lib/raw_types/sens.d.ts +4391 -2706
  72. package/lib/raw_types/sens.js +1 -108
  73. package/lib/raw_types/sens.mjs +4 -1
  74. package/lib/raw_types/sens.ts +4495 -2903
  75. package/lib/raw_types_kysely/ameli.d.ts +6 -0
  76. package/lib/raw_types_kysely/ameli.mjs +7 -0
  77. package/lib/raw_types_kysely/ameli.ts +6 -0
  78. package/lib/raw_types_kysely/debats.d.ts +6 -0
  79. package/lib/raw_types_kysely/debats.mjs +7 -0
  80. package/lib/raw_types_kysely/debats.ts +6 -0
  81. package/lib/raw_types_kysely/dosleg.d.ts +6 -0
  82. package/lib/raw_types_kysely/dosleg.mjs +7 -0
  83. package/lib/raw_types_kysely/dosleg.ts +6 -0
  84. package/lib/raw_types_kysely/questions.d.ts +6 -0
  85. package/lib/raw_types_kysely/questions.mjs +7 -0
  86. package/lib/raw_types_kysely/questions.ts +6 -0
  87. package/lib/raw_types_kysely/sens.d.ts +6 -0
  88. package/lib/raw_types_kysely/sens.mjs +7 -0
  89. package/lib/raw_types_kysely/sens.ts +6 -0
  90. package/lib/raw_types_kysely/texte.d.ts +45 -0
  91. package/lib/raw_types_kysely/texte.mjs +7 -0
  92. package/lib/raw_types_kysely/texte.ts +53 -0
  93. package/lib/raw_types_schemats/ameli.d.ts +541 -0
  94. package/lib/raw_types_schemats/ameli.js +45 -0
  95. package/lib/raw_types_schemats/ameli.mjs +2 -0
  96. package/lib/raw_types_schemats/ameli.ts +601 -0
  97. package/lib/raw_types_schemats/debats.d.ts +127 -0
  98. package/lib/raw_types_schemats/debats.js +24 -0
  99. package/lib/raw_types_schemats/debats.mjs +2 -0
  100. package/lib/raw_types_schemats/debats.ts +145 -0
  101. package/lib/raw_types_schemats/dosleg.d.ts +2029 -0
  102. package/lib/raw_types_schemats/dosleg.js +98 -0
  103. package/lib/raw_types_schemats/dosleg.mjs +2 -0
  104. package/lib/raw_types_schemats/dosleg.ts +2195 -0
  105. package/lib/raw_types_schemats/questions.d.ts +233 -0
  106. package/lib/raw_types_schemats/questions.js +24 -0
  107. package/lib/raw_types_schemats/questions.mjs +2 -0
  108. package/lib/raw_types_schemats/questions.ts +251 -0
  109. package/lib/raw_types_schemats/sens.d.ts +2709 -0
  110. package/lib/raw_types_schemats/sens.js +114 -0
  111. package/lib/raw_types_schemats/sens.mjs +2 -0
  112. package/lib/raw_types_schemats/sens.ts +2907 -0
  113. package/lib/scripts/convert_data.js +574 -215
  114. package/lib/scripts/convert_data.mjs +134 -99
  115. package/lib/scripts/convert_data.ts +173 -112
  116. package/lib/scripts/datautil.d.ts +5 -0
  117. package/lib/scripts/datautil.js +64 -0
  118. package/lib/scripts/datautil.mjs +16 -0
  119. package/lib/scripts/datautil.ts +19 -0
  120. package/lib/scripts/parse_textes.js +132 -0
  121. package/lib/scripts/parse_textes.mjs +46 -0
  122. package/lib/scripts/parse_textes.ts +65 -0
  123. package/lib/scripts/retrieve_documents.d.ts +1 -0
  124. package/lib/scripts/retrieve_documents.js +521 -0
  125. package/lib/scripts/retrieve_documents.mjs +249 -0
  126. package/lib/scripts/retrieve_documents.ts +298 -0
  127. package/lib/scripts/retrieve_open_data.js +77 -69
  128. package/lib/scripts/retrieve_open_data.mjs +48 -49
  129. package/lib/scripts/retrieve_open_data.ts +74 -58
  130. package/lib/scripts/retrieve_senateurs_photos.js +45 -63
  131. package/lib/scripts/retrieve_senateurs_photos.mjs +4 -21
  132. package/lib/scripts/retrieve_senateurs_photos.ts +6 -29
  133. package/lib/scripts/retrieve_textes.mjs +325 -74
  134. package/lib/scripts/retrieve_textes.ts +111 -63
  135. package/lib/scripts/shared/cli_helpers.d.ts +44 -0
  136. package/lib/scripts/shared/cli_helpers.js +35 -0
  137. package/lib/scripts/shared/cli_helpers.ts +36 -0
  138. package/lib/scripts/shared/util.d.ts +3 -0
  139. package/lib/scripts/shared/util.js +102 -0
  140. package/lib/scripts/shared/util.ts +33 -0
  141. package/lib/src/aggregates.d.ts +52 -0
  142. package/lib/src/aggregates.mjs +726 -0
  143. package/lib/src/config.d.ts +2 -0
  144. package/lib/src/config.mjs +16 -0
  145. package/lib/src/databases.d.ts +18 -0
  146. package/lib/src/databases.mjs +55 -0
  147. package/lib/src/datasets.d.ts +28 -0
  148. package/lib/src/datasets.mjs +78 -0
  149. package/lib/src/fields.d.ts +10 -0
  150. package/lib/src/fields.mjs +22 -0
  151. package/lib/src/index.d.ts +8 -0
  152. package/lib/src/index.mjs +7 -0
  153. package/lib/src/inserters.d.ts +98 -0
  154. package/lib/src/inserters.mjs +360 -0
  155. package/lib/src/loaders.d.ts +36 -0
  156. package/lib/src/loaders.mjs +107 -0
  157. package/lib/src/model/ameli.d.ts +4 -0
  158. package/lib/src/model/ameli.js +57 -0
  159. package/lib/src/model/debats.d.ts +4 -0
  160. package/lib/src/model/debats.js +43 -0
  161. package/lib/src/model/dosleg.d.ts +197 -0
  162. package/lib/src/model/dosleg.js +169 -0
  163. package/lib/src/model/index.d.ts +4 -0
  164. package/lib/src/model/index.js +4 -0
  165. package/lib/src/model/questions.d.ts +89 -0
  166. package/lib/src/model/questions.js +76 -0
  167. package/lib/src/model/sens.d.ts +390 -0
  168. package/lib/src/model/sens.js +339 -0
  169. package/lib/src/model/texte.d.ts +7 -0
  170. package/lib/src/model/texte.js +183 -0
  171. package/lib/src/raw_types_kysely/ameli.d.ts +915 -0
  172. package/lib/src/raw_types_kysely/ameli.js +5 -0
  173. package/lib/src/raw_types_kysely/debats.d.ts +207 -0
  174. package/lib/src/raw_types_kysely/debats.js +5 -0
  175. package/lib/src/raw_types_kysely/dosleg.d.ts +3532 -0
  176. package/lib/src/raw_types_kysely/dosleg.js +5 -0
  177. package/lib/src/raw_types_kysely/questions.d.ts +414 -0
  178. package/lib/src/raw_types_kysely/questions.js +5 -0
  179. package/lib/src/raw_types_kysely/sens.d.ts +4394 -0
  180. package/lib/src/raw_types_kysely/sens.js +5 -0
  181. package/lib/src/raw_types_schemats/ameli.d.ts +541 -0
  182. package/lib/src/raw_types_schemats/ameli.js +2 -0
  183. package/lib/src/raw_types_schemats/debats.d.ts +127 -0
  184. package/lib/src/raw_types_schemats/debats.js +2 -0
  185. package/lib/src/raw_types_schemats/dosleg.d.ts +2027 -0
  186. package/lib/src/raw_types_schemats/dosleg.js +2 -0
  187. package/lib/src/raw_types_schemats/questions.d.ts +231 -0
  188. package/lib/src/raw_types_schemats/questions.js +2 -0
  189. package/lib/src/raw_types_schemats/sens.d.ts +2709 -0
  190. package/lib/src/raw_types_schemats/sens.js +2 -0
  191. package/lib/src/scripts/convert_data.d.ts +1 -0
  192. package/lib/src/scripts/convert_data.js +95 -0
  193. package/lib/src/scripts/datautil.d.ts +5 -0
  194. package/lib/src/scripts/datautil.js +16 -0
  195. package/lib/src/scripts/parse_textes.d.ts +1 -0
  196. package/lib/src/scripts/parse_textes.js +47 -0
  197. package/lib/src/scripts/retrieve_documents.d.ts +1 -0
  198. package/lib/src/scripts/retrieve_documents.js +258 -0
  199. package/lib/src/scripts/retrieve_open_data.d.ts +1 -0
  200. package/lib/src/scripts/retrieve_open_data.js +214 -0
  201. package/lib/src/scripts/retrieve_senateurs_photos.d.ts +1 -0
  202. package/lib/src/scripts/retrieve_senateurs_photos.js +147 -0
  203. package/lib/src/scripts/shared/cli_helpers.d.ts +44 -0
  204. package/lib/src/scripts/shared/cli_helpers.js +32 -0
  205. package/lib/src/scripts/shared/util.d.ts +3 -0
  206. package/lib/src/scripts/shared/util.js +28 -0
  207. package/lib/src/strings.d.ts +1 -0
  208. package/lib/src/strings.mjs +18 -0
  209. package/lib/src/types/ameli.d.ts +10 -0
  210. package/lib/src/types/ameli.js +13 -0
  211. package/lib/src/types/debats.d.ts +4 -0
  212. package/lib/src/types/debats.js +2 -0
  213. package/lib/src/types/dosleg.d.ts +98 -0
  214. package/lib/src/types/dosleg.js +151 -0
  215. package/lib/src/types/questions.d.ts +2 -0
  216. package/lib/src/types/questions.js +1 -0
  217. package/lib/src/types/sens.d.ts +10 -0
  218. package/lib/src/types/sens.js +1 -0
  219. package/lib/src/types/sessions.d.ts +42 -0
  220. package/lib/src/types/sessions.js +43 -0
  221. package/lib/src/types/texte.d.ts +61 -0
  222. package/lib/src/types/texte.js +16 -0
  223. package/lib/src/validators/config.d.ts +1 -0
  224. package/lib/src/validators/config.js +54 -0
  225. package/lib/src/validators/senat.d.ts +0 -0
  226. package/lib/src/validators/senat.js +24 -0
  227. package/lib/types/ameli.d.ts +1 -1
  228. package/lib/types/ameli.js +2 -2
  229. package/lib/types/ameli.ts +1 -1
  230. package/lib/types/debats.d.ts +1 -1
  231. package/lib/types/debats.js +2 -2
  232. package/lib/types/debats.ts +1 -1
  233. package/lib/types/dosleg.d.ts +2 -2
  234. package/lib/types/dosleg.js +2 -2
  235. package/lib/types/dosleg.ts +2 -2
  236. package/lib/types/questions.d.ts +1 -1
  237. package/lib/types/questions.js +2 -2
  238. package/lib/types/questions.ts +1 -1
  239. package/lib/types/sens.d.ts +1 -3
  240. package/lib/types/sens.js +2 -5
  241. package/lib/types/sens.mjs +1 -23
  242. package/lib/types/sens.ts +1 -25
  243. package/lib/types/sessions.d.ts +42 -0
  244. package/lib/types/sessions.js +51 -0
  245. package/lib/types/sessions.mjs +43 -0
  246. package/lib/types/sessions.ts +42 -0
  247. package/lib/types/texte.d.ts +65 -0
  248. package/lib/types/texte.js +24 -0
  249. package/lib/types/texte.mjs +16 -0
  250. package/lib/types/texte.ts +76 -0
  251. package/package.json +35 -10
  252. package/lib/aggregates.js +0 -1122
  253. package/lib/fields.js +0 -68
  254. package/lib/inserters.js +0 -518
  255. package/lib/scripts/fix_db.js +0 -144
  256. package/lib/scripts/fix_db.mjs +0 -64
  257. package/lib/scripts/fix_db.ts +0 -75
  258. package/lib/scripts/retrieve_textes.js +0 -179
  259. /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 '../datasets';
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, schema",
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/raw_types directory",
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: "repair-encoding",
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["repair-encoding"]) && dataset.repairEncoding) {
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(`psql -c "DROP DATABASE IF EXISTS ${dataset.database}"`, {
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
- // stdio: ["ignore", "ignore", "pipe"],
145
+ stdio: ["pipe", "ignore", "ignore"],
170
146
  });
171
147
  }
172
148
  if (options.schema) {
173
- const definitionsDir = path.resolve("src", "raw_types");
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
- const definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`);
180
- execSync(`npx schemats generate -c ${dbConnectionString} -s public -o ${definitionFilePath}`, {
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
- && process.env.PGPORT
205
- && process.env.PGUSER
206
- && process.env.PGPASSWORD, 'Missing database configuration: environment variables PGHOST, PGPORT, PGUSER and PGPASSWORD or TRICOTEUSES_SENAT_DB_* in .env file');
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
- Dataset,
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, schema",
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: "repair-encoding",
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["repair-encoding"]) && dataset.repairEncoding) {
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
- `psql -c "CREATE DATABASE ${dataset.database} WITH OWNER opendata"`,
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
- // stdio: ["ignore", "ignore", "pipe"],
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
- const definitionsDir = path.resolve("src", "raw_types")
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
- const definitionFilePath = path.join(
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 public -o ${definitionFilePath}`,
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(process.env.PGHOST
240
- && process.env.PGPORT
241
- && process.env.PGUSER
242
- && process.env.PGPASSWORD,
243
- 'Missing database configuration: environment variables PGHOST, PGPORT, PGUSER and PGPASSWORD or TRICOTEUSES_SENAT_DB_* in .env file'
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
- const choosenDatasets: Dataset[] = getChosenFromEnabledDatasets(options.categories)
247
- // await Promise.all(choosenDatasets.map(dataset => retrieveDataset(dataDir, dataset)))
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()