Skip to content

Commit 0ee5242

Browse files
committed
feat: init function-kysely-tailordb-codegen
1 parent f278473 commit 0ee5242

7 files changed

Lines changed: 1169 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @tailor-platform/function-kysely-tailordb-codegen
2+
3+
Generate [Kysely](https://github.com/kysely-org/kysely) code for TailorDB
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { build } from "esbuild";
2+
import { createRequire } from "node:module";
3+
import path from "node:path";
4+
import { nodeless } from "unenv";
5+
6+
const require = createRequire(import.meta.url);
7+
8+
// When importing the Node API (e.g., `import path from "node:path"`),
9+
// the unenv package should be bundled to run even in the Function service.
10+
// Functions that are difficult to execute in the Function service (e.g., `fs.writeFile`)
11+
// will be bundled as mocks and will throw a "not implemented" error at runtime.
12+
const unenvAlias = {
13+
name: "unenv-alias",
14+
setup(build) {
15+
const alias = nodeless.alias;
16+
const re = new RegExp(`^(${Object.keys(alias).join("|")})$`);
17+
18+
build.onResolve({ filter: re }, (args) => {
19+
const resolved = require.resolve(alias[args.path]);
20+
// Since require.resolve() always resolves to cjs,
21+
// it needs to be converted to mjs when this callback is invoked via import.
22+
const path =
23+
args.kind === "require-call"
24+
? resolved
25+
: resolved.replace(/\.cjs$/, ".mjs");
26+
return { path };
27+
});
28+
},
29+
};
30+
31+
// Inject the unenv package to emulate the behavior of global variables (e.g., process)
32+
// that do not exist within the Function service.
33+
const unenvInject = {
34+
name: "unenv-inject",
35+
setup(build) {
36+
const inject = nodeless.inject;
37+
const re = /unenv-inject-([^.]+)\.js$/;
38+
const prefix = path.join(import.meta.dirname, "unenv-inject-");
39+
40+
build.initialOptions.inject = [
41+
...(build.initialOptions.inject ?? []),
42+
...Object.keys(inject).map((globalName) => `${prefix}${globalName}.js`),
43+
];
44+
45+
build.onResolve({ filter: re }, ({ path }) => ({ path }));
46+
47+
build.onLoad({ filter: re }, ({ path }) => {
48+
const globalName = path.match(re)[1];
49+
return {
50+
contents: getInjectContent(globalName, inject[globalName]),
51+
};
52+
});
53+
},
54+
};
55+
56+
const getInjectContent = (globalName, globalInject) => {
57+
if (typeof globalInject === "string") {
58+
return `import globalVar from "${globalInject}"; globalThis.${globalName} = globalVar;`;
59+
}
60+
const [moduleSpecifier, exportName] = globalInject;
61+
return `import { ${exportName} } from "${moduleSpecifier}"; globalThis.${globalName} = ${exportName};`;
62+
};
63+
64+
// Due to the use of dynamic require, bundling `git-diff` is difficult.
65+
// However, since it's not used in this use case, we can mock it instead.
66+
const mockGitDiff = {
67+
name: "mock-git-diff",
68+
setup(build) {
69+
build.onResolve({ filter: /^git-diff$/ }, (args) => {
70+
return { path: args.path, namespace: "git-diff" };
71+
});
72+
build.onLoad({ filter: /.*/, namespace: "git-diff" }, () => {
73+
return { contents: "export default null" };
74+
});
75+
},
76+
};
77+
78+
build({
79+
entryPoints: ["src/function.ts"],
80+
outfile: "dist/function.js",
81+
format: "esm",
82+
bundle: true,
83+
minify: true,
84+
define: {
85+
global: "globalThis",
86+
},
87+
plugins: [unenvAlias, unenvInject, mockGitDiff],
88+
// Unused drivers are left unbundled as-is.
89+
// Note that future updates to `kysely-codegen` may introduce new drivers.
90+
external: [
91+
"@libsql/kysely-libsql",
92+
"@tediousjs/connection-string",
93+
"better-sqlite3",
94+
"bun:sqlite",
95+
"kysely-bun-sqlite",
96+
"mysql2",
97+
"pg",
98+
"tarn",
99+
"tedious",
100+
],
101+
});
102+
103+
build({
104+
entryPoints: ["src/cli.ts"],
105+
outfile: "dist/cli.js",
106+
format: "esm",
107+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@tailor-platform/function-kysely-tailordb-codegen",
3+
"version": "0.1.0",
4+
"description": "Generate Kysely code for TailorDB",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/tailor-platform/function",
8+
"directory": "packages/kysely-tailordb-codegen"
9+
},
10+
"type": "module",
11+
"bin": {
12+
"kysely-tailordb-codegen": "./dist/cli.js"
13+
},
14+
"files": [
15+
"dist"
16+
],
17+
"scripts": {
18+
"build": "node ./build.mjs",
19+
"type-check": "tsc"
20+
},
21+
"dependencies": {
22+
"commander": "^14.0.0",
23+
"zx": "^8.5.4"
24+
},
25+
"devDependencies": {
26+
"@tailor-platform/function-kysely-tailordb": "^0.1.2",
27+
"@tailor-platform/function-types": "^0.2.0",
28+
"@tsconfig/recommended": "^1.0.8",
29+
"@types/fs-extra": "^11.0.4",
30+
"@types/node": "^22.15.24",
31+
"esbuild": "^0.25.5",
32+
"kysely": "^0.27.5",
33+
"kysely-codegen": "^0.18.5",
34+
"typescript": "^5.8.3",
35+
"unenv": "^1.10.0"
36+
},
37+
"packageManager": "pnpm@10.11.0"
38+
}

0 commit comments

Comments
 (0)