Skip to content

Commit 97b3495

Browse files
authored
fix(orama): fix various bugs in Orama-DB generator (#286)
1 parent c25ec86 commit 97b3495

File tree

1 file changed

+67
-56
lines changed

1 file changed

+67
-56
lines changed

src/generators/orama-db/index.mjs

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,55 @@
11
'use strict';
22

3-
import { create } from '@orama/orama';
3+
import { create, insert } from '@orama/orama';
44
import { persistToFile } from '@orama/plugin-data-persistence/server';
55

6+
import { enforceArray } from '../../utils/array.mjs';
67
import { groupNodesByModule } from '../../utils/generators.mjs';
78
import { createSectionBuilder } from '../legacy-json/utils/buildSection.mjs';
89

10+
/**
11+
* Schema definition for the Orama database
12+
*/
13+
const ORAMA_SCHEMA = {
14+
name: 'string',
15+
type: 'string',
16+
desc: 'string',
17+
stability: 'number',
18+
stabilityText: 'string',
19+
meta: {
20+
changes: 'string[]',
21+
added: 'string[]',
22+
napiVersion: 'string[]',
23+
deprecated: 'string[]',
24+
removed: 'string[]',
25+
},
26+
};
27+
28+
/**
29+
* Transforms a section into the format expected by Orama
30+
* @param {import('../legacy-json/types.d.ts').ModuleSection} node - The section to transform
31+
*/
32+
function transformSectionForOrama(node) {
33+
return {
34+
name: node.name,
35+
type: node.type,
36+
desc: node.desc,
37+
// Account for duplicate stability nodes
38+
stability: enforceArray(node.stability)[0],
39+
stabilityText: enforceArray(node.stabilityText)[0],
40+
meta: {
41+
changes:
42+
node.meta?.changes?.map(
43+
c => `${enforceArray(c.version).join(', ')}: ${c.description}`
44+
) ?? [],
45+
added: node.meta?.added ?? [],
46+
napiVersion: node.meta?.napiVersion ?? [],
47+
deprecated: node.meta?.deprecated ?? [],
48+
removed: node.meta?.removed ?? [],
49+
},
50+
};
51+
}
52+
953
/**
1054
* This generator is responsible for generating the Orama database for the
1155
* API docs. It is based on the legacy-json generator.
@@ -16,11 +60,8 @@ import { createSectionBuilder } from '../legacy-json/utils/buildSection.mjs';
1660
*/
1761
export default {
1862
name: 'orama-db',
19-
2063
version: '1.0.0',
21-
2264
description: 'Generates the Orama database for the API docs.',
23-
2465
dependsOn: 'ast',
2566

2667
/**
@@ -30,65 +71,35 @@ export default {
3071
* @param {Partial<GeneratorOptions>} options
3172
*/
3273
async generate(input, { output, version }) {
33-
const buildSection = createSectionBuilder();
74+
if (!input?.length) {
75+
throw new Error('Input data is required and must not be empty');
76+
}
3477

35-
// Create the Orama instance with the schema
36-
const db = create({
37-
schema: {
38-
name: 'string',
39-
type: 'string',
40-
desc: 'string',
41-
stability: 'number',
42-
stabilityText: 'string',
43-
meta: {
44-
changes: 'string[]',
45-
added: 'string[]',
46-
napiVersion: 'string[]',
47-
deprecated: 'string[]',
48-
removed: 'string[]',
49-
},
50-
},
51-
});
78+
if (!output || !version) {
79+
throw new Error('Output path and version are required');
80+
}
5281

82+
const db = create({ schema: ORAMA_SCHEMA });
83+
const buildSection = createSectionBuilder();
5384
const groupedModules = groupNodesByModule(input);
85+
const headNodes = input.filter(node => node.heading?.depth === 1);
5486

55-
// Gets the first nodes of each module, which is considered the "head"
56-
const headNodes = input.filter(node => node.heading.depth === 1);
57-
58-
/**
59-
* @param {ApiDocMetadataEntry} head
60-
* @returns {void}
61-
*/
62-
const processModuleNodes = head => {
63-
const nodes = groupedModules.get(head.api);
87+
// Process each head node and insert into database
88+
headNodes.forEach(headNode => {
89+
const nodes = groupedModules.get(headNode.api);
6490

65-
const section = buildSection(head, nodes);
91+
const section = buildSection(headNode, nodes);
92+
const node = (section.modules || section.globals || section.miscs)[0];
93+
if (!node) return;
6694

67-
// Insert data into the Orama instance
68-
db.insert({
69-
name: section.name,
70-
type: section.type,
71-
desc: section.desc,
72-
stability: section.stability,
73-
stabilityText: section.stabilityText,
74-
meta: {
75-
changes: section.meta.changes,
76-
added: section.meta.added,
77-
napiVersion: section.meta.napiVersion,
78-
deprecated: section.meta.deprecated,
79-
removed: section.meta.removed,
80-
},
81-
});
82-
83-
return section;
84-
};
95+
const oramaData = transformSectionForOrama(node);
96+
insert(db, oramaData);
97+
});
8598

86-
headNodes.map(processModuleNodes);
99+
// Generate output filename and persist database
100+
const sanitizedVersion = version.raw.replaceAll('.', '-');
101+
const outputFilename = `${output}/${sanitizedVersion}-orama-db.json`;
87102

88-
await persistToFile(
89-
db,
90-
'json',
91-
`${output}/${version.raw.replaceAll('.', '-')}-orama-db.json`
92-
);
103+
await persistToFile(db, 'json', outputFilename);
93104
},
94105
};

0 commit comments

Comments
 (0)