-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
196 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
connectors | ||
integrations | ||
src/_connectors.ts | ||
dist | ||
node_modules | ||
.output |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,111 @@ | ||
import { readFile, readdir, writeFile } from "node:fs/promises"; | ||
import { join } from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
import { findTypeExports } from "mlly"; | ||
import { camelCase, upperFirst } from "scule"; | ||
|
||
const connectorsDir = fileURLToPath( | ||
new URL("../src/connectors", import.meta.url), | ||
); | ||
|
||
const connectorsMetaFile = fileURLToPath( | ||
new URL("../src/_connectors.ts", import.meta.url), | ||
); | ||
|
||
const aliases = { | ||
"better-sqlite3": ["sqlite"], | ||
"bun-sqlite": ["bun"], | ||
"libsql-node": ["libsql"], | ||
} as const; | ||
|
||
async function getConnectorFiles(dir: string): Promise<string[]> { | ||
const files: string[] = []; | ||
const entries = await readdir(dir, { withFileTypes: true }); | ||
|
||
for (const entry of entries) { | ||
if (entry.isDirectory()) { | ||
files.push(...(await getConnectorFiles(join(dir, entry.name)))); | ||
} else if (entry.isFile()) { | ||
files.push(join(dir, entry.name)); | ||
} | ||
} | ||
|
||
return files; | ||
} | ||
|
||
const connectorFiles = await getConnectorFiles(connectorsDir); | ||
const connectorEntries = connectorFiles.map((file) => | ||
file.replace(connectorsDir + "/", ""), | ||
); | ||
|
||
const connectors: { | ||
name: string; | ||
safeName: string; | ||
names: string[]; | ||
subpath: string; | ||
optionsTExport?: string; | ||
optionsTName?: string; | ||
}[] = []; | ||
|
||
for (const entry of connectorEntries) { | ||
const pathName = entry.replace(/\.ts$/, ""); | ||
const name = pathName.replace(/\/|\\/g, "-"); | ||
const subpath = `db0/connectors/${pathName}`; | ||
const fullPath = join(connectorsDir, `${pathName}.ts`); | ||
|
||
const contents = await readFile(fullPath, "utf8"); | ||
const optionsTExport = findTypeExports(contents).find((type) => | ||
type.name?.endsWith("Options"), | ||
)?.name; | ||
|
||
const safeName = camelCase(name).replace(/db/i, "DB").replace(/sql/i, "SQL"); | ||
|
||
const alternativeNames: string[] = aliases[name] || []; | ||
|
||
const names = [...new Set([name, ...alternativeNames])]; | ||
|
||
const optionsTName = upperFirst(safeName) + "Options"; | ||
|
||
connectors.push({ | ||
name, | ||
safeName, | ||
names, | ||
subpath, | ||
optionsTExport, | ||
optionsTName, | ||
}); | ||
} | ||
|
||
connectors.sort((a, b) => a.name.localeCompare(b.name)); | ||
|
||
const genCode = /* ts */ `// Auto-generated using scripts/gen-connectors. | ||
// Do not manually edit! | ||
${connectors | ||
.filter((d) => d.optionsTExport) | ||
.map( | ||
(d) => | ||
/* ts */ `import type { ${d.optionsTExport} as ${d.optionsTName} } from "${d.subpath}";`, | ||
) | ||
.join("\n")} | ||
export type BuiltinConnectorName = ${connectors.flatMap((d) => d.names.map((name) => `"${name}"`)).join(" | ")}; | ||
export type BuiltinConnectorOptions = { | ||
${connectors | ||
.filter((d) => d.optionsTExport) | ||
.flatMap((d) => | ||
d.names.map( | ||
(name, i) => | ||
`${i === 0 ? "" : `/** @deprecated Alias of ${d.name} */\n `}"${name}": ${d.optionsTName};`, | ||
), | ||
) | ||
.join("\n ")} | ||
}; | ||
export const builtinConnectors = Object.freeze({ | ||
${connectors.flatMap((d) => d.names.map((name, i) => `${i === 0 ? "" : `/** @deprecated Alias of ${d.name} */\n `}"${name}": "${d.subpath}"`)).join(",\n ")}, | ||
} as const); | ||
`; | ||
|
||
await writeFile(connectorsMetaFile, genCode, "utf8"); | ||
console.log("Generated connectors metadata file to", connectorsMetaFile); |
This file contains 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,55 @@ | ||
// Auto-generated using scripts/gen-connectors. | ||
// Do not manually edit! | ||
import type { ConnectorOptions as BetterSQLite3Options } from "db0/connectors/better-sqlite3"; | ||
import type { ConnectorOptions as BunSQLiteOptions } from "db0/connectors/bun-sqlite"; | ||
import type { ConnectorOptions as CloudflareD1Options } from "db0/connectors/cloudflare-d1"; | ||
import type { ConnectorOptions as LibSQLCoreOptions } from "db0/connectors/libsql/core"; | ||
import type { ConnectorOptions as LibSQLHttpOptions } from "db0/connectors/libsql/http"; | ||
import type { ConnectorOptions as LibSQLNodeOptions } from "db0/connectors/libsql/node"; | ||
import type { ConnectorOptions as LibSQLWebOptions } from "db0/connectors/libsql/web"; | ||
import type { ConnectorOptions as MySQL2Options } from "db0/connectors/mysql2"; | ||
import type { ConnectorOptions as PgliteOptions } from "db0/connectors/pglite"; | ||
import type { ConnectorOptions as PlanetscaleOptions } from "db0/connectors/planetscale"; | ||
import type { ConnectorOptions as PostgreSQLOptions } from "db0/connectors/postgresql"; | ||
|
||
export type BuiltinConnectorName = "better-sqlite3" | "sqlite" | "bun-sqlite" | "bun" | "cloudflare-d1" | "libsql-core" | "libsql-http" | "libsql-node" | "libsql" | "libsql-web" | "mysql2" | "pglite" | "planetscale" | "postgresql"; | ||
|
||
export type BuiltinConnectorOptions = { | ||
"better-sqlite3": BetterSQLite3Options; | ||
/** @deprecated Alias of better-sqlite3 */ | ||
"sqlite": BetterSQLite3Options; | ||
"bun-sqlite": BunSQLiteOptions; | ||
/** @deprecated Alias of bun-sqlite */ | ||
"bun": BunSQLiteOptions; | ||
"cloudflare-d1": CloudflareD1Options; | ||
"libsql-core": LibSQLCoreOptions; | ||
"libsql-http": LibSQLHttpOptions; | ||
"libsql-node": LibSQLNodeOptions; | ||
/** @deprecated Alias of libsql-node */ | ||
"libsql": LibSQLNodeOptions; | ||
"libsql-web": LibSQLWebOptions; | ||
"mysql2": MySQL2Options; | ||
"pglite": PgliteOptions; | ||
"planetscale": PlanetscaleOptions; | ||
"postgresql": PostgreSQLOptions; | ||
}; | ||
|
||
export const builtinConnectors = Object.freeze({ | ||
"better-sqlite3": "db0/connectors/better-sqlite3", | ||
/** @deprecated Alias of better-sqlite3 */ | ||
"sqlite": "db0/connectors/better-sqlite3", | ||
"bun-sqlite": "db0/connectors/bun-sqlite", | ||
/** @deprecated Alias of bun-sqlite */ | ||
"bun": "db0/connectors/bun-sqlite", | ||
"cloudflare-d1": "db0/connectors/cloudflare-d1", | ||
"libsql-core": "db0/connectors/libsql/core", | ||
"libsql-http": "db0/connectors/libsql/http", | ||
"libsql-node": "db0/connectors/libsql/node", | ||
/** @deprecated Alias of libsql-node */ | ||
"libsql": "db0/connectors/libsql/node", | ||
"libsql-web": "db0/connectors/libsql/web", | ||
"mysql2": "db0/connectors/mysql2", | ||
"pglite": "db0/connectors/pglite", | ||
"planetscale": "db0/connectors/planetscale", | ||
"postgresql": "db0/connectors/postgresql", | ||
} as const); |
This file contains 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 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 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 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