-
Notifications
You must be signed in to change notification settings - Fork 9
database type #104
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
base: main
Are you sure you want to change the base?
database type #104
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import type {Node} from "acorn"; | ||
| import type {Sourcemap} from "./sourcemap.js"; | ||
| import {getStringLiteralValue, isStringLiteral} from "./strings.js"; | ||
| import {simple} from "./walk.js"; | ||
| import {DatabaseConfig} from "../databases/index.js"; | ||
|
|
||
| export function rewriteDatabaseClient( | ||
| output: Sourcemap, | ||
| body: Node, | ||
| databases: Map<string, Partial<DatabaseConfig>> = new Map() | ||
| ): void { | ||
| simple(body, { | ||
| CallExpression(node) { | ||
| if (node.callee.type !== "Identifier" || node.callee.name !== "DatabaseClient") return; | ||
| if (!isStringLiteral(node.arguments[0])) | ||
| throw new Error("DatabaseClient name must be a string literal"); | ||
|
|
||
| // if options are passed, don't change them | ||
| if (node.arguments.length !== 1) return; | ||
|
Comment on lines
+18
to
+19
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should pass |
||
|
|
||
| const name = getStringLiteralValue(node.arguments[0]); | ||
| let type: string | undefined; | ||
| if (databases.has(name)) { | ||
| ({type} = databases.get(name)!); | ||
| } else { | ||
| // see defaults in databases/index.ts ; @todo: unify? | ||
| if (name === "postgres") type = "postgres"; | ||
| else if (name === "duckdb") type = "duckdb"; | ||
| else if (name === "sqlite") type = "sqlite"; | ||
| else if (/\.duckdb$/i.test(name)) type = "duckdb"; | ||
| else if (/\.db$/i.test(name)) type = "sqlite"; | ||
| else throw new Error(`database not found: ${name}`); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be a SyntaxError, not an internal Error. Ideally in the |
||
| } | ||
| output.insertRight(node.arguments[0].end, `, {type: ${JSON.stringify(type)}}`); | ||
| } | ||
| }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this redaction accomplishes anything, since any process can read the .observable/databases.json file directly. I guess if we want to call this
getDatabaseTypesthen we could have aMap<string, string>that tells you the database types… but it’s probably just fine to load the unredacted database configs, too.