-
Notifications
You must be signed in to change notification settings - Fork 0
feat!: sanitize database names, augment 'harper', and scope codegen by database #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * Matches a database name against a list of patterns. Each pattern is either an | ||
| * exact name or a glob using `*` as a wildcard for any run of characters, e.g. | ||
| * `metrics-*` matches both `metrics-github` and `metrics-jira`. Matching is | ||
| * case-sensitive and anchored (the whole name must match). | ||
| * @param {string} name | ||
| * @param {(string | number)[]} patterns Patterns come from YAML config, so a | ||
| * purely numeric, unquoted name (e.g. `101`) arrives as a number; each pattern | ||
| * is coerced to a string before matching. | ||
| * @returns {boolean} | ||
| */ | ||
| export function matchesDatabase(name, patterns) { | ||
| return patterns.some((pattern) => { | ||
| // escape regex metacharacters, then turn the wildcard back into `.*` | ||
| const source = String(pattern) | ||
| .replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
| .replace(/\\\*/g, '.*'); | ||
| return new RegExp(`^${source}$`).test(name); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Decides whether a database should be included in codegen given optional | ||
| * include/exclude lists. An empty or absent `include` list includes every | ||
| * database; a non-empty `include` list restricts output to matching databases. | ||
| * `exclude` then removes any matching database. Exclude wins over include. | ||
| * @param {string} name | ||
| * @param {{ include?: string[], exclude?: string[] }} [options] | ||
| * @returns {boolean} | ||
| */ | ||
| export function shouldIncludeDatabase(name, options = {}) { | ||
| const { include, exclude } = options; | ||
| if (Array.isArray(include) && include.length > 0 && !matchesDatabase(name, include)) { | ||
| return false; | ||
| } | ||
| if (Array.isArray(exclude) && exclude.length > 0 && matchesDatabase(name, exclude)) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { matchesDatabase, shouldIncludeDatabase } from './databaseFilter.js'; | ||
|
|
||
| describe('matchesDatabase', () => { | ||
| it('matches exact names', () => { | ||
| expect(matchesDatabase('data', ['data'])).toBe(true); | ||
| expect(matchesDatabase('data', ['blog'])).toBe(false); | ||
| }); | ||
|
|
||
| it('is anchored (no partial matches)', () => { | ||
| expect(matchesDatabase('metrics-github', ['metrics'])).toBe(false); | ||
| expect(matchesDatabase('metrics-github', ['github'])).toBe(false); | ||
| }); | ||
|
|
||
| it('supports the * wildcard', () => { | ||
| expect(matchesDatabase('metrics-github', ['metrics-*'])).toBe(true); | ||
| expect(matchesDatabase('metrics-jira', ['metrics-*'])).toBe(true); | ||
| expect(matchesDatabase('billing', ['metrics-*'])).toBe(false); | ||
| expect(matchesDatabase('anything', ['*'])).toBe(true); | ||
| }); | ||
|
|
||
| it('does not treat other regex metacharacters specially', () => { | ||
| // the dot must be a literal, not "any character" | ||
| expect(matchesDatabase('myXdb', ['my.db'])).toBe(false); | ||
| expect(matchesDatabase('my.db', ['my.db'])).toBe(true); | ||
| }); | ||
|
|
||
| it('coerces numeric patterns from unquoted YAML without crashing', () => { | ||
| // an unquoted numeric database name in YAML parses as a number | ||
| expect(matchesDatabase('101', [101])).toBe(true); | ||
| expect(matchesDatabase('202', [101])).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('shouldIncludeDatabase', () => { | ||
| it('includes everything when no options are given', () => { | ||
| expect(shouldIncludeDatabase('anything')).toBe(true); | ||
| expect(shouldIncludeDatabase('anything', {})).toBe(true); | ||
| }); | ||
|
|
||
| it('restricts to the include list when provided', () => { | ||
| expect(shouldIncludeDatabase('data', { include: ['data', 'blog'] })).toBe(true); | ||
| expect(shouldIncludeDatabase('other', { include: ['data', 'blog'] })).toBe(false); | ||
| }); | ||
|
|
||
| it('treats an empty include list as "include everything"', () => { | ||
| expect(shouldIncludeDatabase('anything', { include: [] })).toBe(true); | ||
| }); | ||
|
|
||
| it('removes databases in the exclude list', () => { | ||
| expect(shouldIncludeDatabase('system', { exclude: ['system'] })).toBe(false); | ||
| expect(shouldIncludeDatabase('data', { exclude: ['system'] })).toBe(true); | ||
| }); | ||
|
|
||
| it('lets exclude win over include', () => { | ||
| expect( | ||
| shouldIncludeDatabase('metrics-internal', { | ||
| include: ['metrics-*'], | ||
| exclude: ['metrics-internal'], | ||
| }), | ||
| ).toBe(false); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { toIdentifier } from './toIdentifier.js'; | ||
|
|
||
| /** | ||
| * Builds the prefix applied to generated *type names* for tables that live in a | ||
| * non-default database. Tables in the default 'data' database are not | ||
| * namespaced, so they receive no prefix. | ||
| * | ||
| * The database name is passed through {@link toIdentifier} so that databases | ||
| * whose names are not valid identifiers still produce valid TypeScript type | ||
| * names, e.g. a "metrics-github" database yields the prefix "metricsGithub_" | ||
| * (and thus the type "metricsGithub_Repository" rather than the invalid | ||
| * "metrics-github_Repository", which TypeScript parses as a subtraction). | ||
| * | ||
| * This is for type-name positions only. Runtime object keys (the actual | ||
| * database and table names on the `databases` object) must be preserved | ||
| * verbatim — quote those with {@link safeKey} instead. | ||
| * @param {string | undefined} databaseName | ||
| * @returns {string} | ||
| */ | ||
| export function dbTypePrefix(databaseName) { | ||
| return databaseName && databaseName !== 'data' ? `${toIdentifier(databaseName)}_` : ''; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { dbTypePrefix } from './dbTypePrefix.js'; | ||
|
|
||
| describe('dbTypePrefix', () => { | ||
| it('returns an empty prefix for the default data database', () => { | ||
| expect(dbTypePrefix('data')).toBe(''); | ||
| }); | ||
|
|
||
| it('returns an empty prefix when the database name is missing', () => { | ||
| expect(dbTypePrefix(undefined)).toBe(''); | ||
| expect(dbTypePrefix('')).toBe(''); | ||
| }); | ||
|
|
||
| it('namespaces a valid database name unchanged', () => { | ||
| expect(dbTypePrefix('blog')).toBe('blog_'); | ||
| }); | ||
|
|
||
| it('sanitizes a hyphenated database name into a valid identifier prefix', () => { | ||
| expect(dbTypePrefix('metrics-github')).toBe('metricsGithub_'); | ||
| }); | ||
|
|
||
| it('sanitizes a database name that starts with a digit', () => { | ||
| expect(dbTypePrefix('9lives')).toBe('_9lives_'); | ||
| }); | ||
|
|
||
| it('always produces an identifier-safe prefix', () => { | ||
| const names = ['metrics-github', 'my.db', 'weird name', '9lives']; | ||
| for (const name of names) { | ||
| expect(dbTypePrefix(name)).toMatch(/^[a-zA-Z_$][a-zA-Z0-9_$]*_$/); | ||
| } | ||
| }); | ||
| }); |
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.