Skip to content

Commit 02a836c

Browse files
committed
seed properties from yaml files
1 parent 7ae5ca7 commit 02a836c

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

databases/catdat/scripts/seed.yaml.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import { get_client } from './shared'
44
import YAML from 'yaml'
5-
import type { CategoryYaml, ConfigYaml } from './yaml.types'
5+
import type {
6+
CategoryYaml,
7+
ConfigYaml,
8+
PropertyYaml as CategoryPropertyYaml,
9+
} from './yaml.types'
610

711
const db = get_client()
812

@@ -20,6 +24,16 @@ function seed() {
2024

2125
seed_config()
2226

27+
const category_properties = get_category_properties()
28+
29+
for (const property of category_properties) {
30+
seed_category_property(property)
31+
}
32+
33+
for (const property of category_properties) {
34+
seed_related_and_dual_properties(property)
35+
}
36+
2337
const categories = get_categories()
2438

2539
for (const category of categories) {
@@ -226,3 +240,54 @@ function seed_related_and_dual_categories(category: CategoryYaml) {
226240
).run(category.id, rel)
227241
}
228242
}
243+
244+
function get_category_properties() {
245+
const properties: CategoryPropertyYaml[] = []
246+
247+
const property_files = fs
248+
.readdirSync(path.join(data_folder, 'category-properties'))
249+
.filter((file) => file.endsWith('.yaml'))
250+
.sort()
251+
252+
for (const property_file of property_files) {
253+
const property_file_content = fs.readFileSync(
254+
path.join(data_folder, 'category-properties', property_file),
255+
'utf8',
256+
)
257+
258+
const property = YAML.parse(property_file_content)
259+
properties.push(property)
260+
}
261+
262+
return properties
263+
}
264+
265+
function seed_category_property(property: CategoryPropertyYaml) {
266+
db.prepare(
267+
`INSERT INTO category_properties
268+
(id, relation, description, nlab_link,
269+
dual_property_id, invariant_under_equivalences)
270+
VALUES (?, ?, ?, ?, ?, ?)`,
271+
).run([
272+
property.id,
273+
property.relation,
274+
property.description,
275+
property.nlab_link || null,
276+
null,
277+
Number(property.invariant_under_equivalences),
278+
])
279+
}
280+
281+
function seed_related_and_dual_properties(property: CategoryPropertyYaml) {
282+
if (property.dual_property_id) {
283+
db.prepare(
284+
`UPDATE category_properties SET dual_property_id = ? WHERE id = ?`,
285+
).run(property.dual_property_id, property.id)
286+
}
287+
288+
for (const rel of property.related_properties) {
289+
db.prepare(
290+
`INSERT INTO related_category_properties (property_id, related_property_id) VALUES (?, ?)`,
291+
).run(property.id, rel)
292+
}
293+
}

databases/catdat/scripts/yaml.types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,13 @@ type MorphismEntry = {
6262
description: string
6363
reason: string
6464
}
65+
66+
export type PropertyYaml = {
67+
id: string
68+
relation: string
69+
description: string
70+
nlab_link?: string | null
71+
dual_property_id?: string | null
72+
invariant_under_equivalences: boolean
73+
related_properties: string[]
74+
}

0 commit comments

Comments
 (0)