Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/publish-function-kysely-tailordb-codegen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Publish npm package function-kysely-tailordb-codegen

on:
workflow_dispatch:

permissions:
contents: read

defaults:
run:
shell: bash

jobs:
publish:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version-file: packages/kysely-tailordb-codegen/package.json
registry-url: https://registry.npmjs.org

- name: Update corepack
run: |
npm i -g corepack@latest

- name: Publish
working-directory: packages/kysely-tailordb-codegen
run: |
corepack enable
pnpm install
pnpm run build
pnpm publish --no-git-checks --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
43 changes: 43 additions & 0 deletions .github/workflows/test-function-kysely-tailordb-codegen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Test packages/kysely-tailordb-codegen

on: workflow_call

jobs:
test:
timeout-minutes: 10
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write

steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version-file: packages/kysely-tailordb/package.json
registry-url: https://registry.npmjs.org

- name: Setup pnpm
run: |
npm i -g corepack@latest
corepack enable

- name: Install deps
working-directory: packages/kysely-tailordb-codegen
run: |
pnpm install

- name: Lint
working-directory: packages/kysely-tailordb-codegen
run: |
pnpm run check

- name: Run type-check
working-directory: packages/kysely-tailordb-codegen
run: |
pnpm run type-check

- name: Build
working-directory: packages/kysely-tailordb-codegen
run: |
pnpm run build
12 changes: 12 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
action: ${{ steps.changes.outputs.action }}
renovate: ${{ steps.changes.outputs.renovate }}
kysely-tailordb: ${{ steps.changes.outputs.kysely-tailordb }}
kysely-tailordb-codegen: ${{ steps.changes.outputs.kysely-tailordb-codegen }}
types: ${{ steps.changes.outputs.types }}
runs-on: ubuntu-latest
permissions:
Expand All @@ -28,6 +29,8 @@ jobs:
- renovate.json
kysely-tailordb:
- packages/kysely-tailordb/**
kysely-tailordb-codegen:
- packages/kysely-tailordb-codegen/**
types:
- packages/types/**

Expand All @@ -38,6 +41,7 @@ jobs:
- action
- renovate
- kysely-tailordb
- kysely-tailordb-codegen
permissions: {}
if: failure()
steps:
Expand All @@ -64,3 +68,11 @@ jobs:
permissions:
contents: read
pull-requests: write

kysely-tailordb-codegen:
uses: ./.github/workflows/test-function-kysely-tailordb-codegen.yaml
needs: path-filter
if: needs.path-filter.outputs.kysely-tailordb-codegen == 'true'
permissions:
contents: read
pull-requests: write
28 changes: 28 additions & 0 deletions packages/kysely-tailordb-codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @tailor-platform/function-kysely-tailordb-codegen

Generate [Kysely](https://github.com/kysely-org/kysely) code for TailorDB

## Usage

```sh
npm install -D @tailor-platform/function-kysely-tailordb-codegen
npx kysely-tailordb-codegen -h
```

```
Usage: kysely-tailordb-codegen [options]

Generate Kysely code for TailorDB

Options:
-a, --app <string> App name
-n, --namespace <string> TailorDB namespace
-m, --machineuser <string> Machine user name
-o, --output <string> Output file name
-h, --help display help for command
```

## Requirements

This command uses `tailorctl` internally to run the code generation script on the Tailor Platform.
Therefore, please make sure `tailorctl` is installed and the correct workspace is selected before running the command.
31 changes: 31 additions & 0 deletions packages/kysely-tailordb-codegen/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 2,
"formatWithErrors": false
},
"javascript": {
"formatter": {
"quoteStyle": "double",
"semicolons": "always",
"trailingCommas": "all"
}
},
"linter": {
"enabled": true,
"ignore": ["**/node_modules/**", "**/dist/**"],
"rules": {
"recommended": true
}
},
"organizeImports": {
"enabled": true
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
}
}
107 changes: 107 additions & 0 deletions packages/kysely-tailordb-codegen/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { createRequire } from "node:module";
import path from "node:path";
import { build } from "esbuild";
import { nodeless } from "unenv";

const require = createRequire(import.meta.url);

// When importing the Node API (e.g., `import path from "node:path"`),
// the unenv package should be bundled to run even in the Function service.
// Functions that are difficult to execute in the Function service (e.g., `fs.writeFile`)
// will be bundled as mocks and will throw a "not implemented" error at runtime.
const unenvAlias = {
name: "unenv-alias",
setup(build) {
const alias = nodeless.alias;
const re = new RegExp(`^(${Object.keys(alias).join("|")})$`);

build.onResolve({ filter: re }, (args) => {
const resolved = require.resolve(alias[args.path]);
// Since require.resolve() always resolves to cjs,
// it needs to be converted to mjs when this callback is invoked via import.
const path =
args.kind === "require-call"
? resolved
: resolved.replace(/\.cjs$/, ".mjs");
return { path };
});
},
};

// Inject the unenv package to emulate the behavior of global variables (e.g., process)
// that do not exist within the Function service.
const unenvInject = {
name: "unenv-inject",
setup(build) {
const inject = nodeless.inject;
const re = /unenv-inject-([^.]+)\.js$/;
const prefix = path.join(import.meta.dirname, "unenv-inject-");

build.initialOptions.inject = [
...(build.initialOptions.inject ?? []),
...Object.keys(inject).map((globalName) => `${prefix}${globalName}.js`),
];

build.onResolve({ filter: re }, ({ path }) => ({ path }));

build.onLoad({ filter: re }, ({ path }) => {
const globalName = path.match(re)[1];
return {
contents: getInjectContent(globalName, inject[globalName]),
};
});
},
};

const getInjectContent = (globalName, globalInject) => {
if (typeof globalInject === "string") {
return `import globalVar from "${globalInject}"; globalThis.${globalName} = globalVar;`;
}
const [moduleSpecifier, exportName] = globalInject;
return `import { ${exportName} } from "${moduleSpecifier}"; globalThis.${globalName} = ${exportName};`;
};

// Due to the use of dynamic require, bundling `git-diff` is difficult.
// However, since it's not used in this use case, we can mock it instead.
const mockGitDiff = {
name: "mock-git-diff",
setup(build) {
build.onResolve({ filter: /^git-diff$/ }, (args) => {
return { path: args.path, namespace: "git-diff" };
});
build.onLoad({ filter: /.*/, namespace: "git-diff" }, () => {
return { contents: "export default null" };
});
},
};

build({
entryPoints: ["src/function.ts"],
outfile: "dist/function.js",
format: "esm",
bundle: true,
minify: true,
define: {
global: "globalThis",
},
plugins: [unenvAlias, unenvInject, mockGitDiff],
// Unused drivers are left unbundled as-is.
// Note that future updates to `kysely-codegen` may introduce new drivers.
external: [
"@libsql/kysely-libsql",
"@tediousjs/connection-string",
"better-sqlite3",
"bun:sqlite",
"kysely-bun-sqlite",
"mysql2",
"pg",
"tarn",
"tedious",
],
});

build({
entryPoints: ["src/cli.ts"],
outfile: "dist/cli.js",
format: "esm",
});
44 changes: 44 additions & 0 deletions packages/kysely-tailordb-codegen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@tailor-platform/function-kysely-tailordb-codegen",
"version": "0.1.0",
"description": "Generate Kysely code for TailorDB",
"repository": {
"type": "git",
"url": "https://github.com/tailor-platform/function",
"directory": "packages/kysely-tailordb-codegen"
},
"type": "module",
"bin": {
"kysely-tailordb-codegen": "./dist/cli.js"
},
"files": ["dist"],
"scripts": {
"build": "node ./build.js",
"check": "biome check .",
"check-write": "biome check --write .",
"type-check": "tsc"
},
"dependencies": {
"commander": "^14.0.0",
"zx": "^8.5.4"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@tailor-platform/function-kysely-tailordb": "^0.1.2",
"@tailor-platform/function-types": "^0.2.0",
"@types/fs-extra": "^11.0.4",
"@types/node": "^22.15.24",
"esbuild": "^0.25.5",
"kysely": "^0.27.5",
"kysely-codegen": "^0.18.5",
"typescript": "^5.8.3",
"unenv": "^1.10.0"
},
"engines": {
"node": "22.x"
},
"packageManager": "pnpm@10.11.0",
"pnpm": {
"onlyBuiltDependencies": ["@biomejs/biome", "esbuild"]
}
}
Loading