text-to-map 1.0.7 → 1.1.0

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 (39) hide show
  1. package/dist/bin/clear-all.js +21 -4
  2. package/dist/bin/init-db.d.ts +1 -0
  3. package/dist/bin/init-db.js +18 -0
  4. package/dist/bin/parse-ordinance.js +56 -42
  5. package/dist/bin/schools.js +1 -1
  6. package/dist/db/address-points.d.ts +13 -13
  7. package/dist/db/address-points.js +93 -77
  8. package/dist/db/db.d.ts +19 -10
  9. package/dist/db/db.js +124 -50
  10. package/dist/db/founders.d.ts +7 -7
  11. package/dist/db/founders.js +187 -135
  12. package/dist/db/meta.d.ts +3 -4
  13. package/dist/db/meta.js +23 -16
  14. package/dist/db/migrations/20230704125715_initial.d.ts +3 -0
  15. package/dist/db/migrations/20230704125715_initial.js +251 -0
  16. package/dist/db/regions.d.ts +5 -5
  17. package/dist/db/regions.js +40 -28
  18. package/dist/db/schools.d.ts +3 -2
  19. package/dist/db/schools.js +30 -14
  20. package/dist/db/street-sync.d.ts +4 -4
  21. package/dist/db/street-sync.js +26 -17
  22. package/dist/index.d.ts +3 -2
  23. package/dist/index.js +4 -2
  24. package/dist/open-data-sync/address-points.d.ts +0 -1
  25. package/dist/open-data-sync/address-points.js +35 -25
  26. package/dist/open-data-sync/regions.js +13 -5
  27. package/dist/open-data-sync/schools.js +19 -15
  28. package/dist/open-data-sync/streets.js +46 -33
  29. package/dist/street-markdown/municipality.d.ts +2 -2
  30. package/dist/street-markdown/municipality.js +46 -14
  31. package/dist/street-markdown/smd-line-parser.d.ts +1 -1
  32. package/dist/street-markdown/smd-line-parser.js +21 -8
  33. package/dist/street-markdown/smd.d.ts +8 -5
  34. package/dist/street-markdown/smd.js +100 -86
  35. package/dist/street-markdown/token-definition.js +6 -6
  36. package/dist/street-markdown/types.d.ts +8 -3
  37. package/dist/utils/helpers.d.ts +0 -1
  38. package/dist/utils/helpers.js +1 -8
  39. package/package.json +9 -5
@@ -1,39 +1,36 @@
1
- import { MunicipalityType, } from "./types";
2
- import { generatePlaceholders, getDb, insertAutoincrementRow, insertMultipleRows, } from "./db";
3
- import { extractMunicipalityName, findClosestString, sanitizeMunicipalityName, } from "../utils/helpers";
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
4
10
  import distance from "@turf/distance";
11
+ import { wholeLineError } from "../street-markdown/smd";
12
+ import { extractMunicipalityName, findClosestString, sanitizeMunicipalityName, } from "../utils/helpers";
13
+ import { getKnexDb, insertAutoincrementRow, insertMultipleRows } from "./db";
14
+ import { MunicipalityType, } from "./types";
5
15
  const cityTypeCode = 261;
6
16
  const cityDistrictTypeCode = 263;
7
- export const insertFounders = (founders) => {
8
- const db = getDb();
9
- const selectCityStatement = db.prepare(`SELECT c.name, c.code FROM school s
10
- JOIN school_location l ON s.izo = l.school_izo
11
- JOIN address_point a ON l.address_point_id = a.id
12
- JOIN city c ON a.city_code = c.code
13
- WHERE s.izo = ?
14
- LIMIT 1`);
15
- const selectDistrictStatement = db.prepare(`SELECT d.name, d.code FROM school s
16
- JOIN school_location l ON s.izo = l.school_izo
17
- JOIN address_point a ON l.address_point_id = a.id
18
- JOIN city_district d ON a.city_district_code = d.code
19
- WHERE s.izo = ?
20
- LIMIT 1`);
17
+ export const insertFounders = (founders) => __awaiter(void 0, void 0, void 0, function* () {
18
+ var _a;
21
19
  let insertedFounders = 0;
22
20
  const schoolFounderConnectionData = [];
23
- founders.forEach((founder) => {
24
- var _a;
21
+ for (const founder of founders) {
25
22
  if (founder.municipalityType !== MunicipalityType.City &&
26
23
  founder.municipalityType !== MunicipalityType.District) {
27
- return;
24
+ continue;
28
25
  }
29
26
  const extractedMunicipalityName = extractMunicipalityName(founder);
30
27
  // check if the extracted municipality name is the same as in all the schools' locations
31
28
  let differingSchools = [];
32
29
  let municipalityCode = -1;
33
- founder.schools.forEach((school) => {
34
- const result = (founder.municipalityType === MunicipalityType.City
35
- ? selectCityStatement
36
- : selectDistrictStatement).get(school.izo);
30
+ for (const school of founder.schools) {
31
+ const result = yield (founder.municipalityType === MunicipalityType.City
32
+ ? getCityOfSchool(school.izo)
33
+ : getDistrictOfSchool(school.izo));
37
34
  if (!result) {
38
35
  console.log(`izo: ${school.izo}, extracted: ${extractedMunicipalityName}, RUIAN: UNDEFINED`);
39
36
  differingSchools.push(school);
@@ -47,8 +44,8 @@ export const insertFounders = (founders) => {
47
44
  // store municipalityCode even if the names don't match, we will use it later
48
45
  municipalityCode = parseInt(code);
49
46
  }
50
- });
51
- municipalityCode = fixFounderProblems(founder, municipalityCode, differingSchools, extractedMunicipalityName);
47
+ }
48
+ municipalityCode = yield fixFounderProblems(founder, municipalityCode, differingSchools, extractedMunicipalityName);
52
49
  const cityDistrictCode = founder.municipalityType === MunicipalityType.District
53
50
  ? municipalityCode.toString()
54
51
  : null;
@@ -57,46 +54,80 @@ export const insertFounders = (founders) => {
57
54
  cityCode = (_a = municipalityCode === null || municipalityCode === void 0 ? void 0 : municipalityCode.toString()) !== null && _a !== void 0 ? _a : null;
58
55
  }
59
56
  else {
60
- cityCode = getCityCodeByDistrictCode(municipalityCode).toString();
57
+ cityCode = yield getCityCodeByDistrictCode(municipalityCode);
61
58
  }
62
- const founderId = insertAutoincrementRow([
63
- sanitizeMunicipalityName(founder.name),
64
- sanitizeMunicipalityName(extractedMunicipalityName),
65
- founder.ico,
66
- String(founder.originalType),
67
- cityCode,
68
- cityDistrictCode,
69
- ], "founder", ["name", "short_name", "ico", "founder_type_code", "city_code", "city_district_code"]);
70
- // founder table has unique (name, ico) with on conflict ignore, so possibly
71
- // the row has not been inserted
72
- if (founderId !== null) {
59
+ const existing = yield getKnexDb()
60
+ .select("*")
61
+ .from("founder")
62
+ .where({
63
+ name: sanitizeMunicipalityName(founder.name),
64
+ ico: founder.ico,
65
+ });
66
+ if (existing.length === 0) {
67
+ const founderId = yield insertAutoincrementRow([
68
+ sanitizeMunicipalityName(founder.name),
69
+ sanitizeMunicipalityName(extractedMunicipalityName),
70
+ founder.ico,
71
+ String(founder.originalType),
72
+ cityCode,
73
+ cityDistrictCode,
74
+ ], "founder", [
75
+ "name",
76
+ "short_name",
77
+ "ico",
78
+ "founder_type_code",
79
+ "city_code",
80
+ "city_district_code",
81
+ ]);
73
82
  insertedFounders++;
74
83
  founder.schools.forEach((school) => {
75
84
  schoolFounderConnectionData.push([school.izo, founderId]);
76
85
  });
77
86
  }
78
- });
79
- const insertedConnections = insertMultipleRows(schoolFounderConnectionData, "school_founder", ["school_izo", "founder_id"]);
87
+ }
88
+ const insertedConnections = yield insertMultipleRows(schoolFounderConnectionData, "school_founder", ["school_izo", "founder_id"]);
80
89
  return insertedFounders + insertedConnections;
81
- };
82
- const fixFounderProblems = (founder, municipalityCode, differingSchools, extractedMunicipalityName) => {
90
+ });
91
+ const getCityOfSchool = (izo) => __awaiter(void 0, void 0, void 0, function* () {
92
+ return yield getKnexDb()
93
+ .select("c.name", "c.code")
94
+ .from("school as s")
95
+ .join("school_location as l", "s.izo", "l.school_izo")
96
+ .join("address_point as a", "l.address_point_id", "a.id")
97
+ .join("city as c", "a.city_code", "c.code")
98
+ .where("s.izo", izo)
99
+ .limit(1)
100
+ .first();
101
+ });
102
+ const getDistrictOfSchool = (izo) => __awaiter(void 0, void 0, void 0, function* () {
103
+ return yield getKnexDb()
104
+ .select("d.name", "d.code")
105
+ .from("school as s")
106
+ .join("school_location as l", "s.izo", "l.school_izo")
107
+ .join("address_point as a", "l.address_point_id", "a.id")
108
+ .join("city_district as d", "a.city_district_code", "d.code")
109
+ .where("s.izo", izo)
110
+ .limit(1)
111
+ .first();
112
+ });
113
+ const fixFounderProblems = (founder, municipalityCode, differingSchools, extractedMunicipalityName) => __awaiter(void 0, void 0, void 0, function* () {
83
114
  if (differingSchools.length === 0 ||
84
115
  differingSchools.length < founder.schools.length) {
85
116
  return municipalityCode;
86
117
  }
87
118
  // either the school does not have a position (invalid RUIAN or missing building)
88
119
  // or the school is not in the same municipality as the founder
89
- const db = getDb();
90
120
  // find all cities and their position with the same name as municipalityName
91
- const municipalities = findMunicipalitiesAndPositionsByNameAndType(extractedMunicipalityName, founder.municipalityType);
121
+ const municipalities = yield findMunicipalitiesAndPositionsByNameAndType(extractedMunicipalityName, founder.municipalityType);
92
122
  // get one school position (if there are more schools, they should be close to each other)
93
- const schoolPosition = db
94
- .prepare(`SELECT a.wgs84_latitude, a.wgs84_longitude FROM school s
95
- JOIN school_location l ON s.izo = l.school_izo
96
- JOIN address_point a ON l.address_point_id = a.id
97
- WHERE s.izo IN (${generatePlaceholders(differingSchools.length)})
98
- LIMIT 1`)
99
- .get(...differingSchools.map((school) => school.izo));
123
+ const schoolPosition = yield getKnexDb()
124
+ .select("address_point.wgs84_latitude", "address_point.wgs84_longitude")
125
+ .from("school")
126
+ .join("school_location", "school.izo", "school_location.school_izo")
127
+ .join("address_point", "school_location.address_point_id", "address_point.id")
128
+ .whereIn("school.izo", differingSchools.map((school) => school.izo))
129
+ .limit(1)
130
+ .first();
100
131
  if (schoolPosition) {
101
132
  if (municipalities.length === 0) {
102
133
  if (municipalityCode === -1) {
@@ -141,91 +172,107 @@ const fixFounderProblems = (founder, municipalityCode, differingSchools, extract
141
172
  return null;
142
173
  }
143
174
  }
144
- };
145
- const getCityCodeByDistrictCode = (districtCode) => {
146
- const db = getDb();
147
- const result = db
148
- .prepare(`SELECT c.code FROM city_district d
149
- JOIN city c ON d.city_code = c.code
150
- WHERE d.code = ?
151
- LIMIT 1`)
152
- .get(districtCode);
153
- if (result) {
154
- return result.code;
155
- }
156
- else {
157
- return null;
158
- }
159
- };
160
- const findMunicipalitiesAndPositionsByNameAndType = (name, type) => {
161
- const db = getDb();
175
+ });
176
+ const getCityCodeByDistrictCode = (districtCode) => __awaiter(void 0, void 0, void 0, function* () {
177
+ var _b;
178
+ const result = yield getKnexDb()
179
+ .first("city.code")
180
+ .from("city_district")
181
+ .join("city", "city_district.city_code", "city.code")
182
+ .where("city_district.code", districtCode)
183
+ .limit(1);
184
+ return (_b = result === null || result === void 0 ? void 0 : result.code) !== null && _b !== void 0 ? _b : null;
185
+ });
186
+ const findMunicipalitiesAndPositionsByNameAndType = (name, type) => __awaiter(void 0, void 0, void 0, function* () {
187
+ const knex = getKnexDb();
162
188
  return (type === MunicipalityType.City
163
- ? db
164
- .prepare(`SELECT c.name, c.code, a.wgs84_latitude, a.wgs84_longitude FROM city c
165
- JOIN address_point a ON c.code = a.city_code
166
- WHERE c.name = ?
167
- GROUP BY c.code`)
168
- .all(name)
169
- : db
170
- .prepare(`SELECT d.name, d.code, a.wgs84_latitude, a.wgs84_longitude FROM city_district d
171
- JOIN address_point a ON d.code = a.city_district_code
172
- WHERE d.name = ?
173
- GROUP BY d.code`)
174
- .all(name)).map((row) => ({
189
+ ? yield knex
190
+ .select("city.name", "city.code", "address_point.wgs84_latitude", "address_point.wgs84_longitude")
191
+ .from("city")
192
+ .join("address_point", "city.code", "address_point.city_code")
193
+ .where("city.name", name)
194
+ .groupBy("city.code")
195
+ : yield knex
196
+ .select("city_district.name", "city_district.code", "address_point.wgs84_latitude", "address_point.wgs84_longitude")
197
+ .from("city_district")
198
+ .join("address_point", "city_district.code", "address_point.city_district_code")
199
+ .where("city_district.name", name)
200
+ .groupBy("city_district.code")).map((row) => ({
175
201
  code: row.code,
176
202
  type,
177
203
  lat: row.wgs84_latitude,
178
204
  lng: row.wgs84_longitude,
179
205
  }));
206
+ });
207
+ const extractFounderName = (line) => {
208
+ if (line[0] === "#") {
209
+ return line.substring(1).trim();
210
+ }
211
+ else {
212
+ return line.trim();
213
+ }
180
214
  };
181
- export const findFounder = (name) => {
215
+ export const findFounder = (nameWithHashtag) => __awaiter(void 0, void 0, void 0, function* () {
182
216
  const errors = [];
183
- const db = getDb();
184
- const exactMatchStatement = db.prepare(`SELECT f.id, f.name, f.ico, f.founder_type_code, f.city_code, f.city_district_code FROM founder f
185
- LEFT JOIN city c ON c.code = f.city_code
186
- LEFT JOIN city_district d ON d.code = f.city_district_code
187
- WHERE f.name = ? OR c.name = ? OR d.name = ?`);
188
- const result = exactMatchStatement.get(name, name, name);
217
+ const name = extractFounderName(nameWithHashtag);
218
+ const result = yield getKnexDb()
219
+ .select("f.id", "f.name", "f.ico", "f.founder_type_code", "f.city_code", "f.city_district_code")
220
+ .from("founder as f")
221
+ .leftJoin("city as c", "c.code", "f.city_code")
222
+ .leftJoin("city_district as d", "d.code", "f.city_district_code")
223
+ .where("f.name", name)
224
+ .orWhere("c.name", name)
225
+ .orWhere("d.name", name)
226
+ .first();
189
227
  if (result) {
190
- return { founder: resultToFounder(result), errors };
228
+ return { founder: yield resultToFounder(result), errors };
191
229
  }
192
230
  else {
193
- const allFounderNames = getAllFounderNames();
231
+ const allFounderNames = yield getAllFounderNames();
194
232
  const namesList = allFounderNames
195
233
  .map((row) => row.founderName)
196
234
  .concat(allFounderNames.map((row) => row.municipalityName));
197
235
  const bestMatch = findClosestString(name, namesList);
198
236
  const bestMatchRow = allFounderNames.find((foundersNames) => foundersNames.founderName === bestMatch ||
199
237
  foundersNames.municipalityName === bestMatch);
200
- errors.push(`No exact match for founder "${name}", using "${bestMatch}" instead.`);
201
- const findByIdStatement = db.prepare(`SELECT f.id, f.name, f.ico, f.founder_type_code, f.city_code, f.city_district_code FROM founder f
202
- WHERE f.id = ?`);
238
+ if (!bestMatchRow) {
239
+ errors.push(wholeLineError(`Nenašli jsme žádné zřizovatele, nejspíš jste zapomněli inicializovat databázi.`, nameWithHashtag));
240
+ return {
241
+ founder: null,
242
+ errors,
243
+ };
244
+ }
245
+ errors.push(wholeLineError(`Zřizovatel '${name}' neexistuje, mysleli jste '${bestMatch}'?`, nameWithHashtag));
246
+ const founder = yield getKnexDb()
247
+ .select("id", "name", "ico", "founder_type_code", "city_code", "city_district_code")
248
+ .from("founder")
249
+ .where("id", bestMatchRow.id)
250
+ .first();
203
251
  return {
204
- founder: resultToFounder(findByIdStatement.get(bestMatchRow.id)),
252
+ founder: yield resultToFounder(founder),
205
253
  errors,
206
254
  };
207
255
  }
208
- };
256
+ });
209
257
  let cachedCityCode = null;
210
258
  let cityCodeFounder = null;
211
- export const getFounderCityCode = (founder) => {
212
- const db = getDb();
259
+ export const getFounderCityCode = (founder) => __awaiter(void 0, void 0, void 0, function* () {
213
260
  if (founder.municipalityType === MunicipalityType.District) {
214
261
  if (cityCodeFounder !== founder) {
215
- const founderStatement = db.prepare(`
216
- SELECT city_code
217
- FROM city_district
218
- WHERE code = ?
219
- `);
220
- cachedCityCode = founderStatement.get(founder.cityOrDistrictCode).city_code;
262
+ cityCodeFounder = founder;
263
+ cachedCityCode = yield getKnexDb()
264
+ .pluck("city_code")
265
+ .from("city_district")
266
+ .where("code", founder.cityOrDistrictCode)
267
+ .first();
221
268
  }
222
269
  return cachedCityCode;
223
270
  }
224
271
  else {
225
272
  return founder.cityOrDistrictCode;
226
273
  }
227
- };
228
- const resultToFounder = (result) => {
274
+ });
275
+ const resultToFounder = (result) => __awaiter(void 0, void 0, void 0, function* () {
229
276
  return {
230
277
  name: result.name,
231
278
  ico: result.ico,
@@ -236,16 +283,17 @@ const resultToFounder = (result) => {
236
283
  cityOrDistrictCode: result.founder_type_code === cityTypeCode
237
284
  ? result.city_code
238
285
  : result.city_district_code,
239
- schools: getSchoolsByFounderId(parseInt(result.id)),
286
+ schools: yield getSchoolsByFounderId(parseInt(result.id)),
240
287
  };
241
- };
242
- const getSchoolsByFounderId = (founderId) => {
243
- const db = getDb();
244
- const statement = db.prepare(`SELECT s.izo, s.redizo, s.name, s.capacity, sl.address_point_id FROM school s
245
- JOIN school_founder sf ON s.izo = sf.school_izo
246
- JOIN school_location sl ON s.izo = sl.school_izo
247
- WHERE sf.founder_id = ?`);
248
- return statement.all(founderId).map((row) => ({
288
+ });
289
+ const getSchoolsByFounderId = (founderId) => __awaiter(void 0, void 0, void 0, function* () {
290
+ const result = yield getKnexDb()
291
+ .select("s.izo", "s.redizo", "s.name", "s.capacity", "sl.address_point_id")
292
+ .from("school as s")
293
+ .join("school_founder as sf", "s.izo", "sf.school_izo")
294
+ .join("school_location as sl", "s.izo", "sl.school_izo")
295
+ .where("sf.founder_id", founderId);
296
+ return result.map((row) => ({
249
297
  izo: String(row.izo),
250
298
  redizo: String(row.redizo),
251
299
  name: String(row.name),
@@ -256,45 +304,49 @@ const getSchoolsByFounderId = (founderId) => {
256
304
  },
257
305
  ],
258
306
  }));
259
- };
260
- const getAllFounderNames = () => {
261
- const db = getDb();
262
- const statement = db.prepare(`SELECT f.id, f.name AS founder_name, c.name AS city_name, d.name AS city_district_name FROM founder f
263
- LEFT JOIN city c ON c.code = f.city_code
264
- LEFT JOIN city_district d ON d.code = f.city_district_code`);
265
- const result = statement.all();
307
+ });
308
+ const getAllFounderNames = () => __awaiter(void 0, void 0, void 0, function* () {
309
+ const result = yield getKnexDb()
310
+ .select("f.id", "f.name as founder_name", "c.name as city_name", "d.name as city_district_name")
311
+ .from("founder as f")
312
+ .leftJoin("city as c", "c.code", "f.city_code")
313
+ .leftJoin("city_district as d", "d.code", "f.city_district_code");
266
314
  return result.map((row) => ({
267
315
  id: parseInt(row.id),
268
316
  founderName: String(row.founder_name),
269
317
  municipalityName: String(row.city_name ? row.city_name : row.city_district_name),
270
318
  }));
271
- };
272
- export const findMunicipalityByNameAndType = (name, type) => {
319
+ });
320
+ export const findMunicipalityByNameAndType = (name, type) => __awaiter(void 0, void 0, void 0, function* () {
273
321
  const errors = [];
274
- const db = getDb();
275
- const exactMatchStatement = db.prepare(`SELECT code FROM ${type === MunicipalityType.City ? "city" : "city_district"} WHERE name = ?`);
276
- const result = exactMatchStatement.get(name);
322
+ const result = yield getKnexDb()
323
+ .first("code")
324
+ .from(type === MunicipalityType.City ? "city" : "city_district")
325
+ .where("name", name);
277
326
  if (result) {
278
327
  return { municipality: { code: result.code, type }, errors };
279
328
  }
280
329
  else {
281
- const allNames = getAllMunicipalityNames(type);
330
+ const allNames = yield getAllMunicipalityNames(type);
282
331
  const namesList = allNames.map((row) => row.name);
283
332
  const bestMatch = findClosestString(name, namesList);
284
333
  const bestMatchRow = allNames.find((municipality) => municipality.name === bestMatch);
285
- errors.push(`No exact match for municipality "${name}", using "${bestMatch}" instead.`);
334
+ errors.push({
335
+ message: `Obec ani městská část '${name}' neexistuje, mysleli jste '${bestMatch}'?`,
336
+ startOffset: 0,
337
+ endOffset: 0,
338
+ });
286
339
  return {
287
340
  municipality: { code: bestMatchRow.code, type },
288
341
  errors,
289
342
  };
290
343
  }
291
- };
292
- const getAllMunicipalityNames = (type) => {
293
- const db = getDb();
294
- const statement = db.prepare(`SELECT name, code FROM ${type === MunicipalityType.City ? "city" : "city_district"}`);
295
- const result = statement.all();
296
- return result.map((row) => ({
344
+ });
345
+ const getAllMunicipalityNames = (type) => __awaiter(void 0, void 0, void 0, function* () {
346
+ return (yield getKnexDb()
347
+ .select("name", "code")
348
+ .from(type === MunicipalityType.City ? "city" : "city_district")).map((row) => ({
297
349
  name: row.name,
298
350
  code: row.code,
299
351
  }));
300
- };
352
+ });
package/dist/db/meta.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- /// <reference types="better-sqlite3" />
2
- export declare const getMetaValue: (key: string) => string | undefined;
3
- export declare const setMetaValue: (key: string, value: any) => import("better-sqlite3").RunResult;
1
+ export declare const getMetaValue: (key: string) => Promise<string | undefined>;
2
+ export declare const setMetaValue: (key: string, value: any) => Promise<void>;
4
3
  export declare const setCurrentDatetimeMetaValue: (key: string) => void;
5
- export declare const getDatetimeMetaValue: (key: string) => Date | undefined;
4
+ export declare const getDatetimeMetaValue: (key: string) => Promise<Date | undefined>;
package/dist/db/meta.js CHANGED
@@ -1,27 +1,34 @@
1
- import { getDb } from "./db";
2
- export const getMetaValue = (key) => {
3
- const db = getDb();
4
- const statement = db.prepare("SELECT value FROM meta WHERE key = ?");
5
- return statement.pluck().get(key);
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
6
9
  };
7
- export const setMetaValue = (key, value) => {
8
- const db = getDb();
9
- if (getMetaValue(key) === undefined) {
10
- const statement = db.prepare("INSERT INTO meta (key, value) VALUES (?, ?)");
11
- return statement.run(key, value.toString());
10
+ import { getKnexDb } from "./db";
11
+ export const getMetaValue = (key) => __awaiter(void 0, void 0, void 0, function* () {
12
+ const knex = getKnexDb();
13
+ const result = yield knex.pluck("value").from("meta").where({ key });
14
+ return result.length > 0 ? result[0] : undefined;
15
+ });
16
+ export const setMetaValue = (key, value) => __awaiter(void 0, void 0, void 0, function* () {
17
+ const knex = getKnexDb();
18
+ if ((yield getMetaValue(key)) === undefined) {
19
+ yield knex.from("meta").insert({ key, value });
12
20
  }
13
21
  else {
14
- const statement = db.prepare("UPDATE meta SET value = ? WHERE key = ?");
15
- return statement.run(value.toString(), key);
22
+ yield knex.from("meta").update({ value }).where({ key });
16
23
  }
17
- };
24
+ });
18
25
  export const setCurrentDatetimeMetaValue = (key) => {
19
26
  setMetaValue(key, new Date().toISOString());
20
27
  };
21
- export const getDatetimeMetaValue = (key) => {
22
- const value = getMetaValue(key);
28
+ export const getDatetimeMetaValue = (key) => __awaiter(void 0, void 0, void 0, function* () {
29
+ const value = yield getMetaValue(key);
23
30
  if (value === undefined) {
24
31
  return undefined;
25
32
  }
26
33
  return new Date(value);
27
- };
34
+ });
@@ -0,0 +1,3 @@
1
+ import { Knex } from "knex";
2
+ export declare function up(knex: Knex): Promise<void>;
3
+ export declare function down(knex: Knex): Promise<void>;