@harperfast/schema-codegen 1.0.10 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harperfast/schema-codegen",
3
- "version": "1.0.10",
3
+ "version": "1.1.0",
4
4
  "description": "Generate useful types from your Harper database schemas",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/harperfast",
@@ -35,8 +35,8 @@
35
35
  "test:watch": "vitest"
36
36
  },
37
37
  "devDependencies": {
38
- "@commitlint/cli": "^20.0.0",
39
- "@commitlint/config-conventional": "^20.0.0",
38
+ "@commitlint/cli": "^21.0.0",
39
+ "@commitlint/config-conventional": "^21.0.0",
40
40
  "@semantic-release/commit-analyzer": "^13.0.1",
41
41
  "@semantic-release/git": "^10.0.1",
42
42
  "@semantic-release/github": "^12.0.2",
@@ -46,8 +46,7 @@
46
46
  "@types/react": "^19.2.10",
47
47
  "@vitest/coverage-v8": "^4.0.18",
48
48
  "conventional-changelog-conventionalcommits": "^9.1.0",
49
- "harperdb": "^4.7.20",
50
- "oxfmt": "^0.36.0",
49
+ "oxfmt": "^0.49.0",
51
50
  "oxlint": "^1.51.0",
52
51
  "semantic-release": "^25.0.2",
53
52
  "vitest": "^4.0.18"
@@ -2,13 +2,14 @@
2
2
  import { isNullable } from './isNullable.js';
3
3
  import { mapType } from './mapType.js';
4
4
  import { singularize } from './singularize.js';
5
+ import { toIdentifier } from './toIdentifier.js';
5
6
 
6
7
  /**
7
8
  * @param {Table & { databaseName?: string }} table
8
9
  */
9
10
  export function generateInterface(table) {
10
- const pluralRaw = table.tableName;
11
- const singularRaw = singularize(pluralRaw);
11
+ const pluralRaw = toIdentifier(table.tableName);
12
+ const singularRaw = toIdentifier(singularize(table.tableName));
12
13
  const dbPrefix =
13
14
  table.databaseName && table.databaseName !== 'data' ? `${table.databaseName}_` : '';
14
15
  const plural = `${dbPrefix}${pluralRaw}`;
@@ -2,6 +2,7 @@
2
2
  import { isNullable } from './isNullable.js';
3
3
  import { mapType } from './mapType.js';
4
4
  import { singularize } from './singularize.js';
5
+ import { toIdentifier } from './toIdentifier.js';
5
6
 
6
7
  /**
7
8
  * Generates JSDoc types for a given HarperDB table.
@@ -17,8 +18,8 @@ import { singularize } from './singularize.js';
17
18
  * @param {Table & { databaseName?: string }} table
18
19
  */
19
20
  export function generateJSDoc(table) {
20
- const pluralRaw = table.tableName;
21
- const singularRaw = singularize(pluralRaw);
21
+ const pluralRaw = toIdentifier(table.tableName);
22
+ const singularRaw = toIdentifier(singularize(table.tableName));
22
23
  const dbPrefix =
23
24
  table.databaseName && table.databaseName !== 'data' ? `${table.databaseName}_` : '';
24
25
  const plural = `${dbPrefix}${pluralRaw}`;
@@ -2,6 +2,7 @@
2
2
  /** @import { TableMeta } from './tableMeta.js' */
3
3
  import { generateInterface } from './generateInterface.js';
4
4
  import { singularize } from './singularize.js';
5
+ import { toIdentifier } from './toIdentifier.js';
5
6
 
6
7
  /**
7
8
  * @param {(Table & { databaseName: string })[]} tablesInput
@@ -22,7 +23,7 @@ export function generateTSFromTables(tablesInput, label = 'HarperDB schemas') {
22
23
  const dbPrefix =
23
24
  table.databaseName && table.databaseName !== 'data' ? `${table.databaseName}_` : '';
24
25
  const plural = `${dbPrefix}${table.tableName}`;
25
- const singular = `${dbPrefix}${singularize(table.tableName)}`;
26
+ const singular = `${dbPrefix}${toIdentifier(singularize(table.tableName))}`;
26
27
  tables.push({ plural, singular, databaseName: table.databaseName });
27
28
  }
28
29
 
@@ -8,6 +8,15 @@ import { getLogger } from './logger.js';
8
8
  * @param {string} schemaTypesPath
9
9
  * @param {TableMeta[]} tables
10
10
  */
11
+ /**
12
+ * Wraps a property name in quotes if it contains characters that are not
13
+ * valid in an unquoted TypeScript identifier (anything other than word chars).
14
+ * @param {string} name
15
+ */
16
+ function safeKey(name) {
17
+ return /[^\w]/.test(name) ? `'${name}'` : name;
18
+ }
19
+
11
20
  export function generateTablesDTS(globalTypesPath, schemaTypesPath, tables) {
12
21
  let content = `/**
13
22
  Generated from your schema files
@@ -45,19 +54,19 @@ export function generateTablesDTS(globalTypesPath, schemaTypesPath, tables) {
45
54
  const dataTables = dbMap.get('data') || [];
46
55
  content += `\texport const tables: {\n`;
47
56
  for (const table of dataTables) {
48
- content += `\t\t${table.plural}: { new(...args: any[]): Table<${table.singular}> };\n`;
57
+ content += `\t\t${safeKey(table.plural)}: { new(...args: any[]): Table<${table.singular}> };\n`;
49
58
  }
50
59
  content += `\t};\n\n`;
51
60
 
52
61
  // Export namespaced databases
53
62
  content += `\texport const databases: {\n`;
54
63
  for (const [dbName, dbTables] of dbMap.entries()) {
55
- content += `\t\t${dbName}: {\n`;
64
+ content += `\t\t${safeKey(dbName)}: {\n`;
56
65
  for (const table of dbTables) {
57
66
  const pluralRaw = table.plural.startsWith(`${dbName}_`)
58
67
  ? table.plural.slice(dbName.length + 1)
59
68
  : table.plural;
60
- content += `\t\t\t${pluralRaw}: { new(...args: any[]): Table<${table.singular}> };\n`;
69
+ content += `\t\t\t${safeKey(pluralRaw)}: { new(...args: any[]): Table<${table.singular}> };\n`;
61
70
  }
62
71
  content += `\t\t};\n`;
63
72
  }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Converts a name that may contain dashes into a valid TypeScript identifier
3
+ * by converting kebab-case to camelCase.
4
+ * e.g. "blog-posts" → "blogPosts", "my-table-name" → "myTableName"
5
+ * Names without dashes are returned unchanged.
6
+ * @param {string} name
7
+ * @returns {string}
8
+ */
9
+ export function toIdentifier(name) {
10
+ return name.replace(/-([a-zA-Z0-9])/g, (_, c) => c.toUpperCase());
11
+ }