feat!: sanitize database names, augment 'harper', and scope codegen by database - #44
Merged
Merged
Conversation
…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>
There was a problem hiding this comment.
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.
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>
|
🎉 This PR is included in version 2.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-githubproduced identifiers such asmetrics-github_Repository— which TypeScript parses as the subtractionmetrics - 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.utils/dbTypePrefix.jsruns the database name throughtoIdentifier.generateInterface,generateJSDoc,generateTS,generateJSDocFromTables) now route through it, so the table-name and database-name axes can't drift apart again.databases['metrics-github'].Repository) stay verbatim and are quoted bysafeKey.Before → after for a
metrics-githubdatabase:Issue 1 — augment
harper, notharperdbgenerateTablesDTSnow emitsdeclare module 'harper'(and importsTablefrom it) by default for Harper 5.x, overridable via a newmoduleconfig option.TableMetanow carries the rawtableName, which also let me delete the fragile prefix-stripping string surgery in the.d.tsgenerator.Issue 3 — scope codegen to the app's own databases
New
utils/databaseFilter.jsplusincludeDatabases/excludeDatabasesconfig options (with*wildcard support, e.g.metrics-*) keep a shared local instance from leaking other projects' databases into the output. Default behavior is unchanged.The generated module augmentation now targets
harperby default instead ofharperdb. Harper 4.x apps that import fromharperdbmust setmodule: harperdbin their schema-codegen config. Released as a major version by semantic-release.Config options (documented in the README)
moduleharperharperdbfor Harper 4.x)includeDatabases*wildcardsexcludeDatabases*wildcardsTests
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