Skip to content

feat!: sanitize database names, augment 'harper', and scope codegen by database - #44

Merged
dawsontoth merged 2 commits into
mainfrom
claude/generated-name-safety-056689
Jul 28, 2026
Merged

feat!: sanitize database names, augment 'harper', and scope codegen by database#44
dawsontoth merged 2 commits into
mainfrom
claude/generated-name-safety-056689

Conversation

@dawsontoth

Copy link
Copy Markdown
Contributor

Closes #29.

Generating types for a Harper 5.x app with hyphenated, multi-database schemas produced invalid TypeScript and augmented the wrong module. This fixes all three problems from the issue.

Issue 2 — invalid identifiers for hyphenated databases (the "name safety" bug)

The database name was concatenated raw into generated type names, so a database like metrics-github produced identifiers such as metrics-github_Repository — which TypeScript parses as the subtraction metrics - github_Repository. This is the same class of defect as the earlier table-name sanitization, just on the database-name axis, which was never covered.

  • New shared helper utils/dbTypePrefix.js runs the database name through toIdentifier.
  • All four generators (generateInterface, generateJSDoc, generateTS, generateJSDocFromTables) now route through it, so the table-name and database-name axes can't drift apart again.
  • Runtime object keys (databases['metrics-github'].Repository) stay verbatim and are quoted by safeKey.

Before → after for a metrics-github database:

export interface metrics-github_Repository {  }   // ❌ before — parses as subtraction
export interface metricsGithub_Repository {  }    // ✅ after

Issue 1 — augment harper, not harperdb

generateTablesDTS now emits declare module 'harper' (and imports Table from it) by default for Harper 5.x, overridable via a new module config option. TableMeta now carries the raw tableName, which also let me delete the fragile prefix-stripping string surgery in the .d.ts generator.

Issue 3 — scope codegen to the app's own databases

New utils/databaseFilter.js plus includeDatabases / excludeDatabases config options (with * wildcard support, e.g. metrics-*) keep a shared local instance from leaking other projects' databases into the output. Default behavior is unchanged.

⚠️ Breaking change

The generated module augmentation now targets harper by default instead of harperdb. Harper 4.x apps that import from harperdb must set module: harperdb in their schema-codegen config. Released as a major version by semantic-release.

Config options (documented in the README)

Option Default Purpose
module harper Runtime package to augment (harperdb for Harper 4.x)
includeDatabases (all) Allowlist of databases; supports * wildcards
excludeDatabases (none) Denylist of databases; supports * wildcards

Tests

56 → 77 tests, all green (plus lint and format clean). New coverage: hyphenated database names at every layer (interface / JSDoc / .ts / .d.ts), the configurable module, and the database filter.

🤖 Generated with Claude Code

…y database

Fixes three problems generating types for Harper 5.x apps with hyphenated,
multi-database schemas (issue #29).

Issue 2 (name safety): the database name was concatenated raw into generated
type names, so a database like "metrics-github" produced invalid identifiers
such as `metrics-github_Repository`, which TypeScript parses as a subtraction.
This is the same class of bug as the earlier table-name sanitization work, on
the database-name axis. A new shared `dbTypePrefix` helper runs the database
name through `toIdentifier`, and all four generators now route through it so the
two axes cannot drift apart again. Runtime object keys stay verbatim and are
quoted by `safeKey`.

Issue 1 (module): the augmentation now targets `harper` by default (Harper 5.x)
instead of `harperdb`, and is overridable via a new `module` config option so
Harper 4.x apps can set `module: harperdb`. TableMeta now carries the raw
`tableName`, which also lets generateTablesDTS drop its fragile prefix-stripping.

Issue 3 (scoping): new `includeDatabases`/`excludeDatabases` config options
(supporting `*` wildcards) scope generation to an application's own databases,
so a shared instance no longer leaks other projects' databases into the output.

BREAKING CHANGE: the generated module augmentation now targets `harper` by
default instead of `harperdb`. Harper 4.x applications that import from
`harperdb` must set `module: harperdb` in their schema-codegen config.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces database filtering options (includeDatabases and excludeDatabases) and module customization (module) to the schema codegen. It also improves type-name sanitization for databases with non-identifier characters (like hyphens). The review feedback correctly identifies a potential runtime crash in the database filtering logic if a pattern is parsed as a number from YAML, and provides a robust fix to cast patterns to strings.

Comment thread utils/databaseFilter.js
A purely numeric, unquoted database name in YAML config (e.g. `101`) is parsed
as a number, so matchesDatabase would throw `pattern.replace is not a function`
and crash codegen. Coerce each pattern with String() before matching.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@dawsontoth
dawsontoth requested a review from kylebernhardy July 27, 2026 16:57
@dawsontoth
dawsontoth merged commit d152a54 into main Jul 28, 2026
9 checks passed
@dawsontoth
dawsontoth deleted the claude/generated-name-safety-056689 branch July 28, 2026 14:59
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harper 5.x: generated types invalid for hyphenated database names + augments 'harperdb' instead of 'harper'

1 participant