Repository navigation
Expand file tree
/
Copy pathrender-sql.mjs
More file actions
74 lines (67 loc) · 2.3 KB
/
Copy pathrender-sql.mjs
File metadata and controls
74 lines (67 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
/** Print SQL with edition.values.json applied (for pgAdmin). */
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { LAYER_IDS } from './geojson-export/exportConfig.mjs'
import {
NETWORK_SQL_DIR,
PARTNER_KEY_TO_LAYER_ID,
readNetworkSqlSection,
renderNetworkSqlFileForPgAdmin,
} from './networkSql.mjs'
const repoRoot = path.dirname(fileURLToPath(import.meta.url))
function resolveSqlPath(inputPath) {
const candidates = [
inputPath,
path.join(repoRoot, inputPath),
path.join(NETWORK_SQL_DIR, inputPath),
path.join(NETWORK_SQL_DIR, `${inputPath}.sql`),
path.join(repoRoot, 'sql', `${inputPath}.sql`),
]
for (const candidate of candidates) {
if (fs.existsSync(candidate)) return path.resolve(candidate)
}
throw new Error(
`SQL file not found: ${inputPath} (try sql/${inputPath}.sql or a layer id like vos)`,
)
}
/**
* @param {'all' | 'geojson' | 'partners'} mode
*/
function renderBatch(mode) {
const layerIds =
mode === 'partners' ?
[...new Set(Object.values(PARTNER_KEY_TO_LAYER_ID))]
: LAYER_IDS
for (const layerId of layerIds) {
const filePath = path.join(NETWORK_SQL_DIR, `${layerId}.sql`)
const label = path.relative(repoRoot, filePath)
process.stdout.write(`\n-- ===== ${label} =====\n\n`)
if (mode === 'geojson') {
process.stdout.write(`${readNetworkSqlSection(layerId, 'geojson')}\n`)
} else if (mode === 'partners') {
process.stdout.write(`${readNetworkSqlSection(layerId, 'partner')}\n`)
} else {
process.stdout.write(renderNetworkSqlFileForPgAdmin(filePath))
}
}
process.stderr.write(`Rendered ${layerIds.length} network file(s) (${mode}).\n`)
}
const arg = process.argv[2]
if (!arg) {
process.stderr.write(`Usage:
npm run render:sql -- sql/argo.sql
npm run render:sql -- vos # shorthand → sql/vos.sql
npm run render:sql -- all # @geojson + @partner (@where inlined)
npm run render:sql -- geojson # map queries only
npm run render:sql -- partners # partner count queries only
`)
process.exit(1)
}
if (arg === 'all' || arg === 'geojson' || arg === 'partners') {
renderBatch(arg)
} else {
const filePath = resolveSqlPath(arg)
process.stdout.write(`${renderNetworkSqlFileForPgAdmin(filePath)}\n`)
}