mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-02 16:16:02 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e81636ea95 | |||
| 37c36e5d07 |
@@ -9,21 +9,6 @@ jobs:
|
|||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
services:
|
|
||||||
postgres:
|
|
||||||
image: postgres:17
|
|
||||||
env:
|
|
||||||
POSTGRES_PASSWORD: postgres
|
|
||||||
POSTGRES_USER: postgres
|
|
||||||
POSTGRES_DB: bknd
|
|
||||||
ports:
|
|
||||||
- 5430:5432
|
|
||||||
options: >-
|
|
||||||
--health-cmd pg_isready
|
|
||||||
--health-interval 10s
|
|
||||||
--health-timeout 5s
|
|
||||||
--health-retries 5
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
@@ -35,11 +20,11 @@ jobs:
|
|||||||
- name: Setup Bun
|
- name: Setup Bun
|
||||||
uses: oven-sh/setup-bun@v1
|
uses: oven-sh/setup-bun@v1
|
||||||
with:
|
with:
|
||||||
bun-version: "1.3.3"
|
bun-version: "1.2.22"
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
working-directory: ./app
|
working-directory: ./app
|
||||||
run: bun install #--linker=hoisted
|
run: bun install
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
working-directory: ./app
|
working-directory: ./app
|
||||||
|
|||||||
+1
-2
@@ -27,8 +27,7 @@ packages/media/.env
|
|||||||
.npmrc
|
.npmrc
|
||||||
/.verdaccio
|
/.verdaccio
|
||||||
.idea
|
.idea
|
||||||
.vscode/*
|
.vscode
|
||||||
!.vscode/settings.json
|
|
||||||
.git_old
|
.git_old
|
||||||
docker/tmp
|
docker/tmp
|
||||||
.debug
|
.debug
|
||||||
|
|||||||
Vendored
-19
@@ -1,19 +0,0 @@
|
|||||||
{
|
|
||||||
"typescript.tsdk": "node_modules/typescript/lib",
|
|
||||||
"biome.enabled": true,
|
|
||||||
"editor.defaultFormatter": "biomejs.biome",
|
|
||||||
"editor.codeActionsOnSave": {
|
|
||||||
//"source.organizeImports.biome": "explicit",
|
|
||||||
"source.fixAll.biome": "explicit"
|
|
||||||
},
|
|
||||||
"typescript.preferences.importModuleSpecifier": "non-relative",
|
|
||||||
"typescript.preferences.autoImportFileExcludePatterns": [
|
|
||||||
"**/dist/**",
|
|
||||||
"**/node_modules/**/dist/**",
|
|
||||||
"**/node_modules/**/!(src|lib|esm)/**" // optional, stricter
|
|
||||||
],
|
|
||||||
"typescript.preferences.includePackageJsonAutoImports": "on",
|
|
||||||
"typescript.tsserver.watchOptions": {
|
|
||||||
"excludeDirectories": ["**/dist", "**/node_modules/**/dist"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -17,7 +17,7 @@ It's designed to avoid vendor lock-in and architectural limitations. Built exclu
|
|||||||
* SQLite: LibSQL, Node SQLite, Bun SQLite, Cloudflare D1, Cloudflare Durable Objects SQLite, SQLocal
|
* SQLite: LibSQL, Node SQLite, Bun SQLite, Cloudflare D1, Cloudflare Durable Objects SQLite, SQLocal
|
||||||
* Postgres: Vanilla Postgres, Supabase, Neon, Xata
|
* Postgres: Vanilla Postgres, Supabase, Neon, Xata
|
||||||
* **Frameworks**: React, Next.js, React Router, Astro, Vite, Waku
|
* **Frameworks**: React, Next.js, React Router, Astro, Vite, Waku
|
||||||
* **Storage**: AWS S3, S3-compatible (Tigris, R2, Minio, etc.), Cloudflare R2 (binding), Cloudinary, Filesystem, Origin Private File System (OPFS)
|
* **Storage**: AWS S3, S3-compatible (Tigris, R2, Minio, etc.), Cloudflare R2 (binding), Cloudinary, Filesystem
|
||||||
* **Deployment**: Standalone, Docker, Cloudflare Workers, Vercel, Netlify, Deno Deploy, AWS Lambda, Valtown etc.
|
* **Deployment**: Standalone, Docker, Cloudflare Workers, Vercel, Netlify, Deno Deploy, AWS Lambda, Valtown etc.
|
||||||
|
|
||||||
**For documentation and examples, please visit https://docs.bknd.io.**
|
**For documentation and examples, please visit https://docs.bknd.io.**
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ VITE_SHOW_ROUTES=
|
|||||||
|
|
||||||
# ===== Test Credentials =====
|
# ===== Test Credentials =====
|
||||||
RESEND_API_KEY=
|
RESEND_API_KEY=
|
||||||
PLUNK_API_KEY=
|
|
||||||
R2_TOKEN=
|
R2_TOKEN=
|
||||||
|
|
||||||
R2_ACCESS_KEY=
|
R2_ACCESS_KEY=
|
||||||
|
|||||||
Vendored
-12
@@ -1,12 +0,0 @@
|
|||||||
{
|
|
||||||
"typescript.preferences.includePackageJsonAutoImports": "off",
|
|
||||||
"typescript.suggest.autoImports": true,
|
|
||||||
"typescript.preferences.importModuleSpecifier": "relative",
|
|
||||||
"search.exclude": {
|
|
||||||
"**/dist/**": true,
|
|
||||||
"**/node_modules/**": true
|
|
||||||
},
|
|
||||||
"files.exclude": {
|
|
||||||
"**/dist/**": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -108,7 +108,7 @@ describe("App tests", async () => {
|
|||||||
expect(Array.from(app.plugins.keys())).toEqual(["test"]);
|
expect(Array.from(app.plugins.keys())).toEqual(["test"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("drivers", async () => {
|
test.only("drivers", async () => {
|
||||||
const called: string[] = [];
|
const called: string[] = [];
|
||||||
const app = new App(dummyConnection, undefined, {
|
const app = new App(dummyConnection, undefined, {
|
||||||
drivers: {
|
drivers: {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const mockedBackend = new Hono()
|
|||||||
.get("/file/:name", async (c) => {
|
.get("/file/:name", async (c) => {
|
||||||
const { name } = c.req.param();
|
const { name } = c.req.param();
|
||||||
const file = Bun.file(`${assetsPath}/${name}`);
|
const file = Bun.file(`${assetsPath}/${name}`);
|
||||||
return new Response(new File([await file.bytes()], name, { type: file.type }), {
|
return new Response(file, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": file.type,
|
"Content-Type": file.type,
|
||||||
"Content-Length": file.size.toString(),
|
"Content-Length": file.size.toString(),
|
||||||
@@ -67,7 +67,7 @@ describe("MediaApi", () => {
|
|||||||
const res = await mockedBackend.request("/api/media/file/" + name);
|
const res = await mockedBackend.request("/api/media/file/" + name);
|
||||||
await Bun.write(path, res);
|
await Bun.write(path, res);
|
||||||
|
|
||||||
const file = Bun.file(path);
|
const file = await Bun.file(path);
|
||||||
expect(file.size).toBeGreaterThan(0);
|
expect(file.size).toBeGreaterThan(0);
|
||||||
expect(file.type).toBe("image/png");
|
expect(file.type).toBe("image/png");
|
||||||
await file.delete();
|
await file.delete();
|
||||||
@@ -154,12 +154,15 @@ describe("MediaApi", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// upload via readable from bun
|
// upload via readable from bun
|
||||||
await matches(api.upload(file.stream(), { filename: "readable.png" }), "readable.png");
|
await matches(await api.upload(file.stream(), { filename: "readable.png" }), "readable.png");
|
||||||
|
|
||||||
// upload via readable from response
|
// upload via readable from response
|
||||||
{
|
{
|
||||||
const response = (await mockedBackend.request(url)) as Response;
|
const response = (await mockedBackend.request(url)) as Response;
|
||||||
await matches(api.upload(response.body!, { filename: "readable.png" }), "readable.png");
|
await matches(
|
||||||
|
await api.upload(response.body!, { filename: "readable.png" }),
|
||||||
|
"readable.png",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import { describe, expect, mock, test, beforeAll, afterAll } from "bun:test";
|
import { describe, expect, mock, test } from "bun:test";
|
||||||
import { createApp as internalCreateApp, type CreateAppConfig } from "bknd";
|
import { createApp as internalCreateApp, type CreateAppConfig } from "bknd";
|
||||||
import { getDummyConnection } from "../../__test__/helper";
|
import { getDummyConnection } from "../../__test__/helper";
|
||||||
import { ModuleManager } from "modules/ModuleManager";
|
import { ModuleManager } from "modules/ModuleManager";
|
||||||
import { em, entity, text } from "data/prototype";
|
import { em, entity, text } from "data/prototype";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
|
||||||
|
|
||||||
beforeAll(disableConsoleLog);
|
|
||||||
afterAll(enableConsoleLog);
|
|
||||||
|
|
||||||
async function createApp(config: CreateAppConfig = {}) {
|
async function createApp(config: CreateAppConfig = {}) {
|
||||||
const app = internalCreateApp({
|
const app = internalCreateApp({
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
import { AppEvents } from "App";
|
import { AppEvents } from "App";
|
||||||
import { describe, test, expect, beforeAll, mock, afterAll } from "bun:test";
|
import { describe, test, expect, beforeAll, mock } from "bun:test";
|
||||||
import { type App, createApp, createMcpToolCaller } from "core/test/utils";
|
import { type App, createApp, createMcpToolCaller } from "core/test/utils";
|
||||||
import type { McpServer } from "bknd/utils";
|
import type { McpServer } from "bknd/utils";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
|
||||||
|
|
||||||
beforeAll(disableConsoleLog);
|
|
||||||
afterAll(enableConsoleLog);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* - [x] system_config
|
* - [x] system_config
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
import { describe, expect, test } from "bun:test";
|
|
||||||
import { code, hybrid } from "modes";
|
|
||||||
|
|
||||||
describe("modes", () => {
|
|
||||||
describe("code", () => {
|
|
||||||
test("verify base configuration", async () => {
|
|
||||||
const c = code({}) as any;
|
|
||||||
const config = await c.app?.({} as any);
|
|
||||||
expect(Object.keys(config)).toEqual(["options"]);
|
|
||||||
expect(config.options.mode).toEqual("code");
|
|
||||||
expect(config.options.plugins).toEqual([]);
|
|
||||||
expect(config.options.manager.skipValidation).toEqual(false);
|
|
||||||
expect(config.options.manager.onModulesBuilt).toBeDefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("keeps overrides", async () => {
|
|
||||||
const c = code({
|
|
||||||
connection: {
|
|
||||||
url: ":memory:",
|
|
||||||
},
|
|
||||||
}) as any;
|
|
||||||
const config = await c.app?.({} as any);
|
|
||||||
expect(config.connection.url).toEqual(":memory:");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("hybrid", () => {
|
|
||||||
test("fails if no reader is provided", () => {
|
|
||||||
// @ts-ignore
|
|
||||||
expect(hybrid({} as any).app?.({} as any)).rejects.toThrow(/reader/);
|
|
||||||
});
|
|
||||||
test("verify base configuration", async () => {
|
|
||||||
const c = hybrid({ reader: async () => ({}) }) as any;
|
|
||||||
const config = await c.app?.({} as any);
|
|
||||||
expect(Object.keys(config)).toEqual(["reader", "beforeBuild", "config", "options"]);
|
|
||||||
expect(config.options.mode).toEqual("db");
|
|
||||||
expect(config.options.plugins).toEqual([]);
|
|
||||||
expect(config.options.manager.skipValidation).toEqual(false);
|
|
||||||
expect(config.options.manager.onModulesBuilt).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -76,7 +76,7 @@ describe("repros", async () => {
|
|||||||
expect(app.em.entities.map((e) => e.name)).toEqual(["media", "test"]);
|
expect(app.em.entities.map((e) => e.name)).toEqual(["media", "test"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("verify inversedBy", async () => {
|
test.only("verify inversedBy", async () => {
|
||||||
const schema = proto.em(
|
const schema = proto.em(
|
||||||
{
|
{
|
||||||
products: proto.entity("products", {
|
products: proto.entity("products", {
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import { describe, expect, test, beforeAll, afterAll } from "bun:test";
|
import { describe, expect, test } from "bun:test";
|
||||||
import { Guard, type GuardConfig } from "auth/authorize/Guard";
|
import { Guard, type GuardConfig } from "auth/authorize/Guard";
|
||||||
import { Permission } from "auth/authorize/Permission";
|
import { Permission } from "auth/authorize/Permission";
|
||||||
import { Role, type RoleSchema } from "auth/authorize/Role";
|
import { Role, type RoleSchema } from "auth/authorize/Role";
|
||||||
import { objectTransform, s } from "bknd/utils";
|
import { objectTransform, s } from "bknd/utils";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
|
||||||
|
|
||||||
beforeAll(disableConsoleLog);
|
|
||||||
afterAll(enableConsoleLog);
|
|
||||||
|
|
||||||
function createGuard(
|
function createGuard(
|
||||||
permissionNames: string[],
|
permissionNames: string[],
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import type { App, DB } from "bknd";
|
|||||||
import type { CreateUserPayload } from "auth/AppAuth";
|
import type { CreateUserPayload } from "auth/AppAuth";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
||||||
|
|
||||||
beforeAll(disableConsoleLog);
|
beforeAll(() => disableConsoleLog());
|
||||||
afterAll(enableConsoleLog);
|
afterAll(() => enableConsoleLog());
|
||||||
|
|
||||||
async function makeApp(config: Partial<CreateAppConfig["config"]> = {}) {
|
async function makeApp(config: Partial<CreateAppConfig["config"]> = {}) {
|
||||||
const app = createApp({
|
const app = createApp({
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
import { describe, test, expect, beforeAll, afterAll } from "bun:test";
|
|
||||||
import { createAuthTestApp } from "./shared";
|
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
|
||||||
import { em, entity, text } from "data/prototype";
|
|
||||||
|
|
||||||
beforeAll(disableConsoleLog);
|
|
||||||
afterAll(enableConsoleLog);
|
|
||||||
|
|
||||||
const schema = em(
|
|
||||||
{
|
|
||||||
posts: entity("posts", {
|
|
||||||
title: text(),
|
|
||||||
content: text(),
|
|
||||||
}),
|
|
||||||
comments: entity("comments", {
|
|
||||||
content: text(),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
({ relation }, { posts, comments }) => {
|
|
||||||
relation(posts).manyToOne(comments);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
describe("DataController (auth)", () => {
|
|
||||||
test("reading schema.json", async () => {
|
|
||||||
const { request } = await createAuthTestApp(
|
|
||||||
{
|
|
||||||
permission: ["system.access.api", "data.entity.read", "system.schema.read"],
|
|
||||||
request: new Request("http://localhost/api/data/schema.json"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
config: { data: schema.toJSON() },
|
|
||||||
},
|
|
||||||
);
|
|
||||||
expect((await request.guest()).status).toBe(403);
|
|
||||||
expect((await request.member()).status).toBe(403);
|
|
||||||
expect((await request.authorized()).status).toBe(200);
|
|
||||||
expect((await request.admin()).status).toBe(200);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { describe, it, expect } from "bun:test";
|
||||||
|
import { SystemController } from "modules/server/SystemController";
|
||||||
|
import { createApp } from "core/test/utils";
|
||||||
|
import type { CreateAppConfig } from "App";
|
||||||
|
import { getPermissionRoutes } from "auth/middlewares/permission.middleware";
|
||||||
|
|
||||||
|
async function makeApp(config: Partial<CreateAppConfig> = {}) {
|
||||||
|
const app = createApp(config);
|
||||||
|
await app.build();
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe.skip("SystemController", () => {
|
||||||
|
it("...", async () => {
|
||||||
|
const app = await makeApp();
|
||||||
|
const controller = new SystemController(app);
|
||||||
|
const hono = controller.getController();
|
||||||
|
console.log(getPermissionRoutes(hono));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
import { describe, test, expect, beforeAll, afterAll } from "bun:test";
|
|
||||||
import { createAuthTestApp } from "./shared";
|
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
|
||||||
|
|
||||||
beforeAll(disableConsoleLog);
|
|
||||||
afterAll(enableConsoleLog);
|
|
||||||
|
|
||||||
describe("SystemController (auth)", () => {
|
|
||||||
test("reading info", async () => {
|
|
||||||
const { request } = await createAuthTestApp({
|
|
||||||
permission: ["system.access.api", "system.info"],
|
|
||||||
request: new Request("http://localhost/api/system/info"),
|
|
||||||
});
|
|
||||||
expect((await request.guest()).status).toBe(403);
|
|
||||||
expect((await request.member()).status).toBe(403);
|
|
||||||
expect((await request.authorized()).status).toBe(200);
|
|
||||||
expect((await request.admin()).status).toBe(200);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("reading permissions", async () => {
|
|
||||||
const { request } = await createAuthTestApp({
|
|
||||||
permission: ["system.access.api", "system.schema.read"],
|
|
||||||
request: new Request("http://localhost/api/system/permissions"),
|
|
||||||
});
|
|
||||||
expect((await request.guest()).status).toBe(403);
|
|
||||||
expect((await request.member()).status).toBe(403);
|
|
||||||
expect((await request.authorized()).status).toBe(200);
|
|
||||||
expect((await request.admin()).status).toBe(200);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("access openapi", async () => {
|
|
||||||
const { request } = await createAuthTestApp({
|
|
||||||
permission: ["system.access.api", "system.openapi"],
|
|
||||||
request: new Request("http://localhost/api/system/openapi.json"),
|
|
||||||
});
|
|
||||||
expect((await request.guest()).status).toBe(403);
|
|
||||||
expect((await request.member()).status).toBe(403);
|
|
||||||
expect((await request.authorized()).status).toBe(200);
|
|
||||||
expect((await request.admin()).status).toBe(200);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,171 +0,0 @@
|
|||||||
import { createApp } from "core/test/utils";
|
|
||||||
import type { CreateAppConfig } from "App";
|
|
||||||
import type { RoleSchema } from "auth/authorize/Role";
|
|
||||||
import { isPlainObject } from "core/utils";
|
|
||||||
|
|
||||||
export type AuthTestConfig = {
|
|
||||||
guest?: RoleSchema;
|
|
||||||
member?: RoleSchema;
|
|
||||||
authorized?: RoleSchema;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function createAuthTestApp(
|
|
||||||
testConfig: {
|
|
||||||
permission: AuthTestConfig | string | string[];
|
|
||||||
request: Request;
|
|
||||||
},
|
|
||||||
config: Partial<CreateAppConfig> = {},
|
|
||||||
) {
|
|
||||||
let member: RoleSchema | undefined;
|
|
||||||
let authorized: RoleSchema | undefined;
|
|
||||||
let guest: RoleSchema | undefined;
|
|
||||||
if (isPlainObject(testConfig.permission)) {
|
|
||||||
if (testConfig.permission.guest)
|
|
||||||
guest = {
|
|
||||||
...testConfig.permission.guest,
|
|
||||||
is_default: true,
|
|
||||||
};
|
|
||||||
if (testConfig.permission.member) member = testConfig.permission.member;
|
|
||||||
if (testConfig.permission.authorized) authorized = testConfig.permission.authorized;
|
|
||||||
} else {
|
|
||||||
member = {
|
|
||||||
permissions: [],
|
|
||||||
};
|
|
||||||
authorized = {
|
|
||||||
permissions: Array.isArray(testConfig.permission)
|
|
||||||
? testConfig.permission
|
|
||||||
: [testConfig.permission],
|
|
||||||
};
|
|
||||||
guest = {
|
|
||||||
permissions: [],
|
|
||||||
is_default: true,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("authorized", authorized);
|
|
||||||
|
|
||||||
const app = createApp({
|
|
||||||
...config,
|
|
||||||
config: {
|
|
||||||
...config.config,
|
|
||||||
auth: {
|
|
||||||
...config.config?.auth,
|
|
||||||
enabled: true,
|
|
||||||
guard: {
|
|
||||||
enabled: true,
|
|
||||||
...config.config?.auth?.guard,
|
|
||||||
},
|
|
||||||
jwt: {
|
|
||||||
...config.config?.auth?.jwt,
|
|
||||||
secret: "secret",
|
|
||||||
},
|
|
||||||
roles: {
|
|
||||||
...config.config?.auth?.roles,
|
|
||||||
guest,
|
|
||||||
member,
|
|
||||||
authorized,
|
|
||||||
admin: {
|
|
||||||
implicit_allow: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
await app.build();
|
|
||||||
|
|
||||||
const users = {
|
|
||||||
guest: null,
|
|
||||||
member: await app.createUser({
|
|
||||||
email: "member@test.com",
|
|
||||||
password: "12345678",
|
|
||||||
role: "member",
|
|
||||||
}),
|
|
||||||
authorized: await app.createUser({
|
|
||||||
email: "authorized@test.com",
|
|
||||||
password: "12345678",
|
|
||||||
role: "authorized",
|
|
||||||
}),
|
|
||||||
admin: await app.createUser({
|
|
||||||
email: "admin@test.com",
|
|
||||||
password: "12345678",
|
|
||||||
role: "admin",
|
|
||||||
}),
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
const tokens = {} as Record<keyof typeof users, string>;
|
|
||||||
for (const [key, user] of Object.entries(users)) {
|
|
||||||
if (user) {
|
|
||||||
tokens[key as keyof typeof users] = await app.module.auth.authenticator.jwt(user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function makeRequest(user: keyof typeof users, input: string, init: RequestInit = {}) {
|
|
||||||
const headers = new Headers(init.headers ?? {});
|
|
||||||
if (user in tokens) {
|
|
||||||
headers.set("Authorization", `Bearer ${tokens[user as keyof typeof tokens]}`);
|
|
||||||
}
|
|
||||||
const res = await app.server.request(input, {
|
|
||||||
...init,
|
|
||||||
headers,
|
|
||||||
});
|
|
||||||
|
|
||||||
let data: any;
|
|
||||||
if (res.headers.get("Content-Type")?.startsWith("application/json")) {
|
|
||||||
data = await res.json();
|
|
||||||
} else if (res.headers.get("Content-Type")?.startsWith("text/")) {
|
|
||||||
data = await res.text();
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: res.status,
|
|
||||||
ok: res.ok,
|
|
||||||
headers: Object.fromEntries(res.headers.entries()),
|
|
||||||
data,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const requestFn = new Proxy(
|
|
||||||
{},
|
|
||||||
{
|
|
||||||
get(_, prop: keyof typeof users) {
|
|
||||||
return async (input: string, init: RequestInit = {}) => {
|
|
||||||
return makeRequest(prop, input, init);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
},
|
|
||||||
) as {
|
|
||||||
[K in keyof typeof users]: (
|
|
||||||
input: string,
|
|
||||||
init?: RequestInit,
|
|
||||||
) => Promise<{
|
|
||||||
status: number;
|
|
||||||
ok: boolean;
|
|
||||||
headers: Record<string, string>;
|
|
||||||
data: any;
|
|
||||||
}>;
|
|
||||||
};
|
|
||||||
|
|
||||||
const request = new Proxy(
|
|
||||||
{},
|
|
||||||
{
|
|
||||||
get(_, prop: keyof typeof users) {
|
|
||||||
return async () => {
|
|
||||||
return makeRequest(prop, testConfig.request.url, {
|
|
||||||
headers: testConfig.request.headers,
|
|
||||||
method: testConfig.request.method,
|
|
||||||
body: testConfig.request.body,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
},
|
|
||||||
},
|
|
||||||
) as {
|
|
||||||
[K in keyof typeof users]: () => Promise<{
|
|
||||||
status: number;
|
|
||||||
ok: boolean;
|
|
||||||
headers: Record<string, string>;
|
|
||||||
data: any;
|
|
||||||
}>;
|
|
||||||
};
|
|
||||||
|
|
||||||
return { app, users, request, requestFn };
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
import { describe, beforeAll, afterAll, test } from "bun:test";
|
|
||||||
import type { PostgresConnection } from "data/connection/postgres/PostgresConnection";
|
|
||||||
import { pg, postgresJs } from "bknd";
|
|
||||||
import { Pool } from "pg";
|
|
||||||
import postgres from "postgres";
|
|
||||||
import { disableConsoleLog, enableConsoleLog, $waitUntil } from "bknd/utils";
|
|
||||||
import { $ } from "bun";
|
|
||||||
import { connectionTestSuite } from "data/connection/connection-test-suite";
|
|
||||||
import { bunTestRunner } from "adapter/bun/test";
|
|
||||||
|
|
||||||
const credentials = {
|
|
||||||
host: "localhost",
|
|
||||||
port: 5430,
|
|
||||||
user: "postgres",
|
|
||||||
password: "postgres",
|
|
||||||
database: "bknd",
|
|
||||||
};
|
|
||||||
|
|
||||||
async function cleanDatabase(connection: InstanceType<typeof PostgresConnection>) {
|
|
||||||
const kysely = connection.kysely;
|
|
||||||
|
|
||||||
// drop all tables+indexes & create new schema
|
|
||||||
await kysely.schema.dropSchema("public").ifExists().cascade().execute();
|
|
||||||
await kysely.schema.dropIndex("public").ifExists().cascade().execute();
|
|
||||||
await kysely.schema.createSchema("public").execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function isPostgresRunning() {
|
|
||||||
try {
|
|
||||||
// Try to actually connect to PostgreSQL
|
|
||||||
const conn = pg({ pool: new Pool(credentials) });
|
|
||||||
await conn.ping();
|
|
||||||
await conn.close();
|
|
||||||
return true;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("postgres", () => {
|
|
||||||
beforeAll(async () => {
|
|
||||||
if (!(await isPostgresRunning())) {
|
|
||||||
await $`docker run --rm --name bknd-test-postgres -d -e POSTGRES_PASSWORD=${credentials.password} -e POSTGRES_USER=${credentials.user} -e POSTGRES_DB=${credentials.database} -p ${credentials.port}:5432 postgres:17`;
|
|
||||||
await $waitUntil("Postgres is running", isPostgresRunning);
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
||||||
}
|
|
||||||
|
|
||||||
disableConsoleLog();
|
|
||||||
});
|
|
||||||
afterAll(async () => {
|
|
||||||
if (await isPostgresRunning()) {
|
|
||||||
try {
|
|
||||||
await $`docker stop bknd-test-postgres`;
|
|
||||||
} catch (e) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
enableConsoleLog();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe.serial.each([
|
|
||||||
["pg", () => pg({ pool: new Pool(credentials) })],
|
|
||||||
["postgresjs", () => postgresJs({ postgres: postgres(credentials) })],
|
|
||||||
])("%s", (name, createConnection) => {
|
|
||||||
connectionTestSuite(
|
|
||||||
{
|
|
||||||
...bunTestRunner,
|
|
||||||
test: test.serial,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
makeConnection: () => {
|
|
||||||
const connection = createConnection();
|
|
||||||
return {
|
|
||||||
connection,
|
|
||||||
dispose: async () => {
|
|
||||||
await cleanDatabase(connection);
|
|
||||||
await connection.close();
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
rawDialectDetails: [],
|
|
||||||
disableConsoleLog: false,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -124,81 +124,6 @@ describe("[Repository]", async () => {
|
|||||||
.then((r) => [r.count, r.total]),
|
.then((r) => [r.count, r.total]),
|
||||||
).resolves.toEqual([undefined, undefined]);
|
).resolves.toEqual([undefined, undefined]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("auto join", async () => {
|
|
||||||
const schema = $em(
|
|
||||||
{
|
|
||||||
posts: $entity("posts", {
|
|
||||||
title: $text(),
|
|
||||||
content: $text(),
|
|
||||||
}),
|
|
||||||
comments: $entity("comments", {
|
|
||||||
content: $text(),
|
|
||||||
}),
|
|
||||||
another: $entity("another", {
|
|
||||||
title: $text(),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
({ relation }, { posts, comments }) => {
|
|
||||||
relation(comments).manyToOne(posts);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
const em = schema.proto.withConnection(getDummyConnection().dummyConnection);
|
|
||||||
await em.schema().sync({ force: true });
|
|
||||||
|
|
||||||
await em.mutator("posts").insertOne({ title: "post1", content: "content1" });
|
|
||||||
await em
|
|
||||||
.mutator("comments")
|
|
||||||
.insertMany([{ content: "comment1", posts_id: 1 }, { content: "comment2" }] as any);
|
|
||||||
|
|
||||||
const res = await em.repo("comments").findMany({
|
|
||||||
where: {
|
|
||||||
"posts.title": "post1",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expect(res.data as any).toEqual([
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
content: "comment1",
|
|
||||||
posts_id: 1,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
{
|
|
||||||
// manual join should still work
|
|
||||||
const res = await em.repo("comments").findMany({
|
|
||||||
join: ["posts"],
|
|
||||||
where: {
|
|
||||||
"posts.title": "post1",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expect(res.data as any).toEqual([
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
content: "comment1",
|
|
||||||
posts_id: 1,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// inexistent should be detected and thrown
|
|
||||||
expect(
|
|
||||||
em.repo("comments").findMany({
|
|
||||||
where: {
|
|
||||||
"random.title": "post1",
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
).rejects.toThrow(/Invalid where field/);
|
|
||||||
|
|
||||||
// existing alias, but not a relation should throw
|
|
||||||
expect(
|
|
||||||
em.repo("comments").findMany({
|
|
||||||
where: {
|
|
||||||
"another.title": "post1",
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
).rejects.toThrow(/Invalid where field/);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("[data] Repository (Events)", async () => {
|
describe("[data] Repository (Events)", async () => {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ describe("SqliteIntrospector", () => {
|
|||||||
dataType: "INTEGER",
|
dataType: "INTEGER",
|
||||||
isNullable: false,
|
isNullable: false,
|
||||||
isAutoIncrementing: true,
|
isAutoIncrementing: true,
|
||||||
hasDefaultValue: true,
|
hasDefaultValue: false,
|
||||||
comment: undefined,
|
comment: undefined,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -89,7 +89,7 @@ describe("SqliteIntrospector", () => {
|
|||||||
dataType: "INTEGER",
|
dataType: "INTEGER",
|
||||||
isNullable: false,
|
isNullable: false,
|
||||||
isAutoIncrementing: true,
|
isAutoIncrementing: true,
|
||||||
hasDefaultValue: true,
|
hasDefaultValue: false,
|
||||||
comment: undefined,
|
comment: undefined,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { assetsPath, assetsTmpPath } from "../helper";
|
|||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
disableConsoleLog();
|
//disableConsoleLog();
|
||||||
registries.media.register("local", StorageLocalAdapter);
|
registries.media.register("local", StorageLocalAdapter);
|
||||||
});
|
});
|
||||||
afterAll(enableConsoleLog);
|
afterAll(enableConsoleLog);
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ beforeAll(disableConsoleLog);
|
|||||||
afterAll(enableConsoleLog);
|
afterAll(enableConsoleLog);
|
||||||
|
|
||||||
describe("AppAuth", () => {
|
describe("AppAuth", () => {
|
||||||
|
test.skip("...", () => {
|
||||||
|
const auth = new AppAuth({});
|
||||||
|
console.log(auth.toJSON());
|
||||||
|
console.log(auth.config);
|
||||||
|
});
|
||||||
|
|
||||||
moduleTestSuite(AppAuth);
|
moduleTestSuite(AppAuth);
|
||||||
|
|
||||||
let ctx: ModuleBuildContext;
|
let ctx: ModuleBuildContext;
|
||||||
@@ -33,9 +39,11 @@ describe("AppAuth", () => {
|
|||||||
await auth.build();
|
await auth.build();
|
||||||
|
|
||||||
const oldConfig = auth.toJSON(true);
|
const oldConfig = auth.toJSON(true);
|
||||||
|
//console.log(oldConfig);
|
||||||
await auth.schema().patch("enabled", true);
|
await auth.schema().patch("enabled", true);
|
||||||
await auth.build();
|
await auth.build();
|
||||||
const newConfig = auth.toJSON(true);
|
const newConfig = auth.toJSON(true);
|
||||||
|
//console.log(newConfig);
|
||||||
expect(newConfig.jwt.secret).not.toBe(oldConfig.jwt.secret);
|
expect(newConfig.jwt.secret).not.toBe(oldConfig.jwt.secret);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -61,6 +69,7 @@ describe("AppAuth", () => {
|
|||||||
const app = new AuthController(auth).getController();
|
const app = new AuthController(auth).getController();
|
||||||
|
|
||||||
{
|
{
|
||||||
|
disableConsoleLog();
|
||||||
const res = await app.request("/password/register", {
|
const res = await app.request("/password/register", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -71,6 +80,7 @@ describe("AppAuth", () => {
|
|||||||
password: "12345678",
|
password: "12345678",
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
enableConsoleLog();
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
const { data: users } = await ctx.em.repository("users").findMany();
|
const { data: users } = await ctx.em.repository("users").findMany();
|
||||||
@@ -109,6 +119,7 @@ describe("AppAuth", () => {
|
|||||||
const app = new AuthController(auth).getController();
|
const app = new AuthController(auth).getController();
|
||||||
|
|
||||||
{
|
{
|
||||||
|
disableConsoleLog();
|
||||||
const res = await app.request("/password/register", {
|
const res = await app.request("/password/register", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -119,6 +130,7 @@ describe("AppAuth", () => {
|
|||||||
password: "12345678",
|
password: "12345678",
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
enableConsoleLog();
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(200);
|
||||||
|
|
||||||
const { data: users } = await ctx.em.repository("users").findMany();
|
const { data: users } = await ctx.em.repository("users").findMany();
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
import { describe, expect, test, beforeAll, afterAll } from "bun:test";
|
import { describe, expect, test } from "bun:test";
|
||||||
import { createApp } from "core/test/utils";
|
import { createApp } from "core/test/utils";
|
||||||
import { em, entity, text } from "data/prototype";
|
import { em, entity, text } from "data/prototype";
|
||||||
import { registries } from "modules/registries";
|
import { registries } from "modules/registries";
|
||||||
import { StorageLocalAdapter } from "adapter/node/storage/StorageLocalAdapter";
|
import { StorageLocalAdapter } from "adapter/node/storage/StorageLocalAdapter";
|
||||||
import { AppMedia } from "../../src/media/AppMedia";
|
import { AppMedia } from "../../src/media/AppMedia";
|
||||||
import { moduleTestSuite } from "./module-test-suite";
|
import { moduleTestSuite } from "./module-test-suite";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
|
||||||
|
|
||||||
beforeAll(disableConsoleLog);
|
|
||||||
afterAll(enableConsoleLog);
|
|
||||||
|
|
||||||
describe("AppMedia", () => {
|
describe("AppMedia", () => {
|
||||||
test.skip("...", () => {
|
test.skip("...", () => {
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
import { it, expect, describe, beforeAll, afterAll } from "bun:test";
|
import { it, expect, describe } from "bun:test";
|
||||||
import { DbModuleManager } from "modules/db/DbModuleManager";
|
import { DbModuleManager } from "modules/db/DbModuleManager";
|
||||||
import { getDummyConnection } from "../helper";
|
import { getDummyConnection } from "../helper";
|
||||||
import { TABLE_NAME } from "modules/db/migrations";
|
import { TABLE_NAME } from "modules/db/migrations";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
|
||||||
|
|
||||||
beforeAll(disableConsoleLog);
|
|
||||||
afterAll(enableConsoleLog);
|
|
||||||
|
|
||||||
describe("DbModuleManager", () => {
|
describe("DbModuleManager", () => {
|
||||||
it("should extract secrets", async () => {
|
it("should extract secrets", async () => {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import { s, stripMark } from "core/utils/schema";
|
|||||||
import { Connection } from "data/connection/Connection";
|
import { Connection } from "data/connection/Connection";
|
||||||
import { entity, text } from "data/prototype";
|
import { entity, text } from "data/prototype";
|
||||||
|
|
||||||
beforeAll(() => disableConsoleLog());
|
beforeAll(disableConsoleLog);
|
||||||
afterAll(enableConsoleLog);
|
afterAll(enableConsoleLog);
|
||||||
|
|
||||||
describe("ModuleManager", async () => {
|
describe("ModuleManager", async () => {
|
||||||
@@ -82,6 +82,7 @@ describe("ModuleManager", async () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
} as any;
|
} as any;
|
||||||
|
//const { version, ...json } = mm.toJSON() as any;
|
||||||
|
|
||||||
const { dummyConnection } = getDummyConnection();
|
const { dummyConnection } = getDummyConnection();
|
||||||
const db = dummyConnection.kysely;
|
const db = dummyConnection.kysely;
|
||||||
@@ -96,6 +97,10 @@ describe("ModuleManager", async () => {
|
|||||||
|
|
||||||
await mm2.build();
|
await mm2.build();
|
||||||
|
|
||||||
|
/* console.log({
|
||||||
|
json,
|
||||||
|
configs: mm2.configs(),
|
||||||
|
}); */
|
||||||
//expect(stripMark(json)).toEqual(stripMark(mm2.configs()));
|
//expect(stripMark(json)).toEqual(stripMark(mm2.configs()));
|
||||||
expect(mm2.configs().data.entities?.test).toBeDefined();
|
expect(mm2.configs().data.entities?.test).toBeDefined();
|
||||||
expect(mm2.configs().data.entities?.test?.fields?.content).toBeDefined();
|
expect(mm2.configs().data.entities?.test?.fields?.content).toBeDefined();
|
||||||
@@ -223,6 +228,8 @@ describe("ModuleManager", async () => {
|
|||||||
const c = getDummyConnection();
|
const c = getDummyConnection();
|
||||||
const mm = new ModuleManager(c.dummyConnection);
|
const mm = new ModuleManager(c.dummyConnection);
|
||||||
await mm.build();
|
await mm.build();
|
||||||
|
console.log("==".repeat(30));
|
||||||
|
console.log("");
|
||||||
const json = mm.configs();
|
const json = mm.configs();
|
||||||
|
|
||||||
const c2 = getDummyConnection();
|
const c2 = getDummyConnection();
|
||||||
@@ -268,6 +275,7 @@ describe("ModuleManager", async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override async build() {
|
override async build() {
|
||||||
|
//console.log("building FailingModule", this.config);
|
||||||
if (this.config.value && this.config.value < 0) {
|
if (this.config.value && this.config.value < 0) {
|
||||||
throw new Error("value must be positive, given: " + this.config.value);
|
throw new Error("value must be positive, given: " + this.config.value);
|
||||||
}
|
}
|
||||||
@@ -288,6 +296,9 @@ describe("ModuleManager", async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
beforeEach(() => disableConsoleLog(["log", "warn", "error"]));
|
||||||
|
afterEach(enableConsoleLog);
|
||||||
|
|
||||||
test("it builds", async () => {
|
test("it builds", async () => {
|
||||||
const { dummyConnection } = getDummyConnection();
|
const { dummyConnection } = getDummyConnection();
|
||||||
const mm = new TestModuleManager(dummyConnection);
|
const mm = new TestModuleManager(dummyConnection);
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ import v7 from "./samples/v7.json";
|
|||||||
import v8 from "./samples/v8.json";
|
import v8 from "./samples/v8.json";
|
||||||
import v8_2 from "./samples/v8-2.json";
|
import v8_2 from "./samples/v8-2.json";
|
||||||
import v9 from "./samples/v9.json";
|
import v9 from "./samples/v9.json";
|
||||||
|
import v10 from "./samples/v10.json";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
||||||
|
import { CURRENT_VERSION } from "modules/db/migrations";
|
||||||
|
|
||||||
beforeAll(() => disableConsoleLog());
|
beforeAll(() => disableConsoleLog());
|
||||||
afterAll(enableConsoleLog);
|
afterAll(enableConsoleLog);
|
||||||
@@ -61,7 +63,7 @@ async function getRawConfig(
|
|||||||
return await db
|
return await db
|
||||||
.selectFrom("__bknd")
|
.selectFrom("__bknd")
|
||||||
.selectAll()
|
.selectAll()
|
||||||
.$if(!!opts?.version, (qb) => qb.where("version", "=", opts?.version))
|
.where("version", "=", opts?.version ?? CURRENT_VERSION)
|
||||||
.$if((opts?.types?.length ?? 0) > 0, (qb) => qb.where("type", "in", opts?.types))
|
.$if((opts?.types?.length ?? 0) > 0, (qb) => qb.where("type", "in", opts?.types))
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
@@ -115,7 +117,6 @@ describe("Migrations", () => {
|
|||||||
"^^s3.secret_access_key^^",
|
"^^s3.secret_access_key^^",
|
||||||
);
|
);
|
||||||
const [config, secrets] = (await getRawConfig(app, {
|
const [config, secrets] = (await getRawConfig(app, {
|
||||||
version: 10,
|
|
||||||
types: ["config", "secrets"],
|
types: ["config", "secrets"],
|
||||||
})) as any;
|
})) as any;
|
||||||
|
|
||||||
@@ -129,4 +130,15 @@ describe("Migrations", () => {
|
|||||||
"^^s3.secret_access_key^^",
|
"^^s3.secret_access_key^^",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("migration from 10 to 11", async () => {
|
||||||
|
expect(v10.version).toBe(10);
|
||||||
|
expect(v10.data.entities.test.fields.title.config.fillable).toEqual(["read", "update"]);
|
||||||
|
|
||||||
|
const app = await createVersionedApp(v10);
|
||||||
|
|
||||||
|
expect(app.version()).toBeGreaterThan(10);
|
||||||
|
const [config] = (await getRawConfig(app, { types: ["config"] })) as any;
|
||||||
|
expect(config.json.data.entities.test.fields.title.config.fillable).toEqual(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,270 @@
|
|||||||
|
{
|
||||||
|
"version": 10,
|
||||||
|
"server": {
|
||||||
|
"cors": {
|
||||||
|
"origin": "*",
|
||||||
|
"allow_methods": ["GET", "POST", "PATCH", "PUT", "DELETE"],
|
||||||
|
"allow_headers": [
|
||||||
|
"Content-Type",
|
||||||
|
"Content-Length",
|
||||||
|
"Authorization",
|
||||||
|
"Accept"
|
||||||
|
],
|
||||||
|
"allow_credentials": true
|
||||||
|
},
|
||||||
|
"mcp": {
|
||||||
|
"enabled": true,
|
||||||
|
"path": "/api/system/mcp",
|
||||||
|
"logLevel": "warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"basepath": "/api/data",
|
||||||
|
"default_primary_format": "integer",
|
||||||
|
"entities": {
|
||||||
|
"test": {
|
||||||
|
"type": "regular",
|
||||||
|
"fields": {
|
||||||
|
"id": {
|
||||||
|
"type": "primary",
|
||||||
|
"config": {
|
||||||
|
"format": "integer",
|
||||||
|
"fillable": false,
|
||||||
|
"required": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"title": {
|
||||||
|
"type": "text",
|
||||||
|
"config": {
|
||||||
|
"required": false,
|
||||||
|
"fillable": ["read", "update"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
"type": "enum",
|
||||||
|
"config": {
|
||||||
|
"default_value": "INACTIVE",
|
||||||
|
"options": {
|
||||||
|
"type": "strings",
|
||||||
|
"values": ["INACTIVE", "SUBSCRIBED", "UNSUBSCRIBED"]
|
||||||
|
},
|
||||||
|
"required": true,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
"type": "date",
|
||||||
|
"config": {
|
||||||
|
"type": "datetime",
|
||||||
|
"required": true,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"schema": {
|
||||||
|
"type": "jsonschema",
|
||||||
|
"config": {
|
||||||
|
"default_from_schema": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"one": {
|
||||||
|
"type": "number",
|
||||||
|
"default": 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": true,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"text": {
|
||||||
|
"type": "text",
|
||||||
|
"config": {
|
||||||
|
"required": false,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"sort_field": "id",
|
||||||
|
"sort_dir": "asc"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"items": {
|
||||||
|
"type": "regular",
|
||||||
|
"fields": {
|
||||||
|
"id": {
|
||||||
|
"type": "primary",
|
||||||
|
"config": {
|
||||||
|
"format": "integer",
|
||||||
|
"fillable": false,
|
||||||
|
"required": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"title": {
|
||||||
|
"type": "text",
|
||||||
|
"config": {
|
||||||
|
"required": false,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"sort_field": "id",
|
||||||
|
"sort_dir": "asc"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"media": {
|
||||||
|
"type": "system",
|
||||||
|
"fields": {
|
||||||
|
"id": {
|
||||||
|
"type": "primary",
|
||||||
|
"config": {
|
||||||
|
"format": "integer",
|
||||||
|
"fillable": false,
|
||||||
|
"required": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"path": {
|
||||||
|
"type": "text",
|
||||||
|
"config": {
|
||||||
|
"required": true,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"folder": {
|
||||||
|
"type": "boolean",
|
||||||
|
"config": {
|
||||||
|
"default_value": false,
|
||||||
|
"hidden": true,
|
||||||
|
"fillable": ["create"],
|
||||||
|
"required": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mime_type": {
|
||||||
|
"type": "text",
|
||||||
|
"config": {
|
||||||
|
"required": false,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"size": {
|
||||||
|
"type": "number",
|
||||||
|
"config": {
|
||||||
|
"required": false,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"scope": {
|
||||||
|
"type": "text",
|
||||||
|
"config": {
|
||||||
|
"hidden": true,
|
||||||
|
"fillable": ["create"],
|
||||||
|
"required": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"etag": {
|
||||||
|
"type": "text",
|
||||||
|
"config": {
|
||||||
|
"required": false,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"modified_at": {
|
||||||
|
"type": "date",
|
||||||
|
"config": {
|
||||||
|
"type": "datetime",
|
||||||
|
"required": false,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"reference": {
|
||||||
|
"type": "text",
|
||||||
|
"config": {
|
||||||
|
"required": false,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"entity_id": {
|
||||||
|
"type": "text",
|
||||||
|
"config": {
|
||||||
|
"required": false,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"type": "json",
|
||||||
|
"config": {
|
||||||
|
"required": false,
|
||||||
|
"fillable": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"sort_field": "id",
|
||||||
|
"sort_dir": "asc"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"relations": {},
|
||||||
|
"indices": {
|
||||||
|
"idx_unique_media_path": {
|
||||||
|
"entity": "media",
|
||||||
|
"fields": ["path"],
|
||||||
|
"unique": true
|
||||||
|
},
|
||||||
|
"idx_media_reference": {
|
||||||
|
"entity": "media",
|
||||||
|
"fields": ["reference"],
|
||||||
|
"unique": false
|
||||||
|
},
|
||||||
|
"idx_media_entity_id": {
|
||||||
|
"entity": "media",
|
||||||
|
"fields": ["entity_id"],
|
||||||
|
"unique": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"auth": {
|
||||||
|
"enabled": false,
|
||||||
|
"basepath": "/api/auth",
|
||||||
|
"entity_name": "users",
|
||||||
|
"allow_register": true,
|
||||||
|
"jwt": {
|
||||||
|
"secret": "",
|
||||||
|
"alg": "HS256",
|
||||||
|
"fields": ["id", "email", "role"]
|
||||||
|
},
|
||||||
|
"cookie": {
|
||||||
|
"path": "/",
|
||||||
|
"sameSite": "lax",
|
||||||
|
"secure": true,
|
||||||
|
"httpOnly": true,
|
||||||
|
"expires": 604800,
|
||||||
|
"partitioned": false,
|
||||||
|
"renew": true,
|
||||||
|
"pathSuccess": "/",
|
||||||
|
"pathLoggedOut": "/"
|
||||||
|
},
|
||||||
|
"strategies": {
|
||||||
|
"password": {
|
||||||
|
"type": "password",
|
||||||
|
"enabled": true,
|
||||||
|
"config": {
|
||||||
|
"hashing": "sha256"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"guard": {
|
||||||
|
"enabled": false
|
||||||
|
},
|
||||||
|
"roles": {}
|
||||||
|
},
|
||||||
|
"media": {
|
||||||
|
"enabled": false
|
||||||
|
},
|
||||||
|
"flows": {
|
||||||
|
"basepath": "/api/flows",
|
||||||
|
"flows": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
+21
-12
@@ -1,19 +1,29 @@
|
|||||||
import pkg from "./package.json" with { type: "json" };
|
import pkg from "./package.json" with { type: "json" };
|
||||||
import c from "picocolors";
|
import c from "picocolors";
|
||||||
import { formatNumber } from "bknd/utils";
|
import { formatNumber } from "bknd/utils";
|
||||||
|
import * as esbuild from "esbuild";
|
||||||
|
|
||||||
const deps = Object.keys(pkg.dependencies);
|
const deps = Object.keys(pkg.dependencies);
|
||||||
const external = [
|
const external = ["jsonv-ts/*", "wrangler", "bknd", "bknd/*", ...deps];
|
||||||
"jsonv-ts/*",
|
|
||||||
"wrangler",
|
if (process.env.DEBUG) {
|
||||||
"bknd",
|
const result = await esbuild.build({
|
||||||
"bknd/*",
|
entryPoints: ["./src/cli/index.ts"],
|
||||||
"@vitejs/plugin-react",
|
outdir: "./dist/cli",
|
||||||
"vite",
|
platform: "node",
|
||||||
"@tailwindcss/vite",
|
minify: true,
|
||||||
"@cloudflare/vite-plugin",
|
format: "esm",
|
||||||
...deps,
|
metafile: true,
|
||||||
];
|
bundle: true,
|
||||||
|
external,
|
||||||
|
define: {
|
||||||
|
__isDev: "0",
|
||||||
|
__version: JSON.stringify(pkg.version),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await Bun.write("./dist/cli/metafile-esm.json", JSON.stringify(result.metafile, null, 2));
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
const result = await Bun.build({
|
const result = await Bun.build({
|
||||||
entrypoints: ["./src/cli/index.ts"],
|
entrypoints: ["./src/cli/index.ts"],
|
||||||
@@ -21,7 +31,6 @@ const result = await Bun.build({
|
|||||||
outdir: "./dist/cli",
|
outdir: "./dist/cli",
|
||||||
env: "PUBLIC_*",
|
env: "PUBLIC_*",
|
||||||
minify: true,
|
minify: true,
|
||||||
banner: `const __originalLog=console.log;console.log=(...o)=>{const n=o[0];"string"==typeof n&&n.includes("[dotenv@")||__originalLog.apply(console,o)};`,
|
|
||||||
external,
|
external,
|
||||||
define: {
|
define: {
|
||||||
__isDev: "0",
|
__isDev: "0",
|
||||||
|
|||||||
@@ -96,7 +96,6 @@ async function buildApi() {
|
|||||||
metafile: true,
|
metafile: true,
|
||||||
target: "esnext",
|
target: "esnext",
|
||||||
platform: "browser",
|
platform: "browser",
|
||||||
removeNodeProtocol: false,
|
|
||||||
format: ["esm"],
|
format: ["esm"],
|
||||||
splitting: false,
|
splitting: false,
|
||||||
loader: {
|
loader: {
|
||||||
@@ -186,9 +185,6 @@ async function buildUiElements() {
|
|||||||
outDir: "dist/ui/elements",
|
outDir: "dist/ui/elements",
|
||||||
external: [
|
external: [
|
||||||
"ui/client",
|
"ui/client",
|
||||||
"bknd",
|
|
||||||
/^bknd\/.*/,
|
|
||||||
"wouter",
|
|
||||||
"react",
|
"react",
|
||||||
"react-dom",
|
"react-dom",
|
||||||
"react/jsx-runtime",
|
"react/jsx-runtime",
|
||||||
@@ -232,7 +228,6 @@ function baseConfig(adapter: string, overrides: Partial<tsup.Options> = {}): tsu
|
|||||||
outDir: `dist/adapter/${adapter}`,
|
outDir: `dist/adapter/${adapter}`,
|
||||||
metafile: true,
|
metafile: true,
|
||||||
splitting: false,
|
splitting: false,
|
||||||
removeNodeProtocol: false,
|
|
||||||
onSuccess: async () => {
|
onSuccess: async () => {
|
||||||
delayTypes();
|
delayTypes();
|
||||||
oldConsole.log(c.cyan("[Adapter]"), adapter || "base", c.green("built"));
|
oldConsole.log(c.cyan("[Adapter]"), adapter || "base", c.green("built"));
|
||||||
@@ -268,11 +263,6 @@ async function buildAdapters() {
|
|||||||
|
|
||||||
// specific adatpers
|
// specific adatpers
|
||||||
tsup.build(baseConfig("react-router")),
|
tsup.build(baseConfig("react-router")),
|
||||||
tsup.build(
|
|
||||||
baseConfig("browser", {
|
|
||||||
external: [/^sqlocal\/?.*?/, "wouter"],
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
tsup.build(
|
tsup.build(
|
||||||
baseConfig("bun", {
|
baseConfig("bun", {
|
||||||
external: [/^bun\:.*/],
|
external: [/^bun\:.*/],
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
[install]
|
||||||
|
#registry = "http://localhost:4873"
|
||||||
|
|
||||||
|
[test]
|
||||||
|
coverageSkipTestFiles = true
|
||||||
|
console.depth = 10
|
||||||
+50
-66
@@ -3,7 +3,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"sideEffects": false,
|
"sideEffects": false,
|
||||||
"bin": "./dist/cli/index.js",
|
"bin": "./dist/cli/index.js",
|
||||||
"version": "0.20.0-rc.1",
|
"version": "0.19.0",
|
||||||
"description": "Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.",
|
"description": "Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.",
|
||||||
"homepage": "https://bknd.io",
|
"homepage": "https://bknd.io",
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/bknd-io/bknd/issues"
|
"url": "https://github.com/bknd-io/bknd/issues"
|
||||||
},
|
},
|
||||||
"packageManager": "bun@1.3.3",
|
"packageManager": "bun@1.2.22",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=22.13"
|
"node": ">=22.13"
|
||||||
},
|
},
|
||||||
@@ -49,104 +49,93 @@
|
|||||||
"license": "FSL-1.1-MIT",
|
"license": "FSL-1.1-MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cfworker/json-schema": "^4.1.1",
|
"@cfworker/json-schema": "^4.1.1",
|
||||||
"@codemirror/lang-html": "^6.4.11",
|
"@codemirror/lang-html": "^6.4.9",
|
||||||
"@codemirror/lang-json": "^6.0.2",
|
"@codemirror/lang-json": "^6.0.1",
|
||||||
"@hello-pangea/dnd": "^18.0.1",
|
"@hello-pangea/dnd": "^18.0.1",
|
||||||
"@hono/swagger-ui": "^0.5.2",
|
"@hono/swagger-ui": "^0.5.1",
|
||||||
"@mantine/core": "^7.17.1",
|
"@mantine/core": "^7.17.1",
|
||||||
"@mantine/hooks": "^7.17.1",
|
"@mantine/hooks": "^7.17.1",
|
||||||
"@tanstack/react-form": "^1.0.5",
|
"@tanstack/react-form": "^1.0.5",
|
||||||
"@uiw/react-codemirror": "^4.25.2",
|
"@uiw/react-codemirror": "^4.23.10",
|
||||||
"@xyflow/react": "^12.9.2",
|
"@xyflow/react": "^12.4.4",
|
||||||
"aws4fetch": "^1.0.20",
|
"aws4fetch": "^1.0.20",
|
||||||
"bcryptjs": "^3.0.3",
|
"bcryptjs": "^3.0.2",
|
||||||
"dayjs": "^1.11.19",
|
"dayjs": "^1.11.13",
|
||||||
"fast-xml-parser": "^5.3.1",
|
"fast-xml-parser": "^5.0.8",
|
||||||
"hono": "4.10.4",
|
"hono": "4.8.3",
|
||||||
"json-schema-library": "10.0.0-rc7",
|
"json-schema-library": "10.0.0-rc7",
|
||||||
"json-schema-to-ts": "^3.1.1",
|
"json-schema-to-ts": "^3.1.1",
|
||||||
"jsonv-ts": "^0.10.1",
|
"jsonv-ts": "0.9.1",
|
||||||
"kysely": "0.28.8",
|
"kysely": "0.27.6",
|
||||||
"lodash-es": "^4.17.21",
|
"lodash-es": "^4.17.21",
|
||||||
"oauth4webapi": "^2.11.1",
|
"oauth4webapi": "^2.11.1",
|
||||||
"object-path-immutable": "^4.1.2",
|
"object-path-immutable": "^4.1.2",
|
||||||
"picocolors": "^1.1.1",
|
|
||||||
"radix-ui": "^1.1.3",
|
"radix-ui": "^1.1.3",
|
||||||
"swr": "^2.3.6",
|
"picocolors": "^1.1.1",
|
||||||
"use-sync-external-store": "^1.6.0",
|
"swr": "^2.3.3"
|
||||||
"zustand": "^4"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@aws-sdk/client-s3": "^3.922.0",
|
"@aws-sdk/client-s3": "^3.758.0",
|
||||||
"@bluwy/giget-core": "^0.1.6",
|
"@bluwy/giget-core": "^0.1.2",
|
||||||
"@clack/prompts": "^0.11.0",
|
"@clack/prompts": "^0.11.0",
|
||||||
"@cloudflare/vite-plugin": "^1.15.3",
|
"@cloudflare/vitest-pool-workers": "^0.9.3",
|
||||||
"@cloudflare/vitest-pool-workers": "^0.10.4",
|
"@cloudflare/workers-types": "^4.20250606.0",
|
||||||
"@cloudflare/workers-types": "^4.20251014.0",
|
|
||||||
"@dagrejs/dagre": "^1.1.4",
|
"@dagrejs/dagre": "^1.1.4",
|
||||||
"@hono/vite-dev-server": "^0.23.0",
|
"@hono/vite-dev-server": "^0.21.0",
|
||||||
"@hookform/resolvers": "^5.2.2",
|
"@hookform/resolvers": "^4.1.3",
|
||||||
"@libsql/client": "^0.15.15",
|
"@libsql/client": "^0.15.9",
|
||||||
"@mantine/modals": "^7.17.1",
|
"@mantine/modals": "^7.17.1",
|
||||||
"@mantine/notifications": "^7.17.1",
|
"@mantine/notifications": "^7.17.1",
|
||||||
"@playwright/test": "^1.56.1",
|
"@playwright/test": "^1.51.1",
|
||||||
"@rjsf/core": "5.22.2",
|
"@rjsf/core": "5.22.2",
|
||||||
"@rjsf/utils": "5.22.0",
|
|
||||||
"@standard-schema/spec": "^1.0.0",
|
"@standard-schema/spec": "^1.0.0",
|
||||||
"@tabler/icons-react": "3.35.0",
|
"@tabler/icons-react": "3.18.0",
|
||||||
"@tailwindcss/postcss": "^4.1.16",
|
"@tailwindcss/postcss": "^4.0.12",
|
||||||
"@tailwindcss/vite": "^4.1.16",
|
"@tailwindcss/vite": "^4.0.12",
|
||||||
"@tanstack/react-store": "^0.8.0",
|
|
||||||
"@testing-library/jest-dom": "^6.6.3",
|
"@testing-library/jest-dom": "^6.6.3",
|
||||||
"@testing-library/react": "^16.2.0",
|
"@testing-library/react": "^16.2.0",
|
||||||
"@types/node": "^24.10.0",
|
"@types/node": "^22.13.10",
|
||||||
"@types/pg": "^8.15.6",
|
|
||||||
"@types/react": "^19.0.10",
|
"@types/react": "^19.0.10",
|
||||||
"@types/react-dom": "^19.0.4",
|
"@types/react-dom": "^19.0.4",
|
||||||
"@vitejs/plugin-react": "^5.1.0",
|
"@vitejs/plugin-react": "^4.3.4",
|
||||||
"@vitest/coverage-v8": "3.0.9",
|
"@vitest/coverage-v8": "^3.0.9",
|
||||||
"autoprefixer": "^10.4.21",
|
"autoprefixer": "^10.4.21",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
"commander": "^14.0.2",
|
"dotenv": "^16.4.7",
|
||||||
"dotenv": "^17.2.3",
|
|
||||||
"jotai": "^2.12.2",
|
"jotai": "^2.12.2",
|
||||||
"jsdom": "^26.1.0",
|
"jsdom": "^26.0.0",
|
||||||
|
"kysely-d1": "^0.3.0",
|
||||||
"kysely-generic-sqlite": "^1.2.1",
|
"kysely-generic-sqlite": "^1.2.1",
|
||||||
"kysely-postgres-js": "^2.0.0",
|
|
||||||
"libsql": "^0.5.22",
|
|
||||||
"libsql-stateless-easy": "^1.8.0",
|
"libsql-stateless-easy": "^1.8.0",
|
||||||
"miniflare": "^4.20251011.2",
|
"open": "^10.1.0",
|
||||||
"open": "^10.2.0",
|
|
||||||
"openapi-types": "^12.1.3",
|
"openapi-types": "^12.1.3",
|
||||||
"pg": "^8.16.3",
|
|
||||||
"postcss": "^8.5.3",
|
"postcss": "^8.5.3",
|
||||||
"postcss-preset-mantine": "^1.18.0",
|
"postcss-preset-mantine": "^1.17.0",
|
||||||
"postcss-simple-vars": "^7.0.1",
|
"postcss-simple-vars": "^7.0.1",
|
||||||
"postgres": "^3.4.7",
|
"posthog-js-lite": "^3.4.2",
|
||||||
"posthog-js-lite": "^3.6.0",
|
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
"react-dom": "^19.0.0",
|
"react-dom": "^19.0.0",
|
||||||
"react-hook-form": "^7.66.0",
|
"react-hook-form": "^7.54.2",
|
||||||
"react-icons": "5.5.0",
|
"react-icons": "5.2.1",
|
||||||
"react-json-view-lite": "^2.5.0",
|
"react-json-view-lite": "^2.4.1",
|
||||||
"sql-formatter": "^15.6.10",
|
"sql-formatter": "^15.4.11",
|
||||||
"sqlocal": "^0.16.0",
|
|
||||||
"tailwind-merge": "^3.0.2",
|
"tailwind-merge": "^3.0.2",
|
||||||
"tailwindcss": "^4.1.16",
|
"tailwindcss": "^4.0.12",
|
||||||
"tailwindcss-animate": "^1.0.7",
|
"tailwindcss-animate": "^1.0.7",
|
||||||
"tsc-alias": "^1.8.16",
|
"tsc-alias": "^1.8.11",
|
||||||
"tsup": "^8.5.0",
|
"tsup": "^8.4.0",
|
||||||
"tsx": "^4.20.6",
|
"tsx": "^4.19.3",
|
||||||
"uuid": "^13.0.0",
|
"uuid": "^11.1.0",
|
||||||
"vite": "^7.1.12",
|
"vite": "^6.3.5",
|
||||||
"vite-plugin-circular-dependency": "^0.5.0",
|
"vite-plugin-circular-dependency": "^0.5.0",
|
||||||
"vite-tsconfig-paths": "^5.1.4",
|
"vite-tsconfig-paths": "^5.1.4",
|
||||||
"vitest": "3.0.9",
|
"vitest": "^3.0.9",
|
||||||
"wouter": "^3.7.1",
|
"wouter": "^3.6.0",
|
||||||
"wrangler": "^4.52.1"
|
"wrangler": "^4.37.1",
|
||||||
|
"miniflare": "^4.20250913.0"
|
||||||
},
|
},
|
||||||
"optionalDependencies": {
|
"optionalDependencies": {
|
||||||
"@hono/node-server": "^1.19.6"
|
"@hono/node-server": "^1.14.3"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react": ">=19",
|
"react": ">=19",
|
||||||
@@ -259,11 +248,6 @@
|
|||||||
"import": "./dist/adapter/aws/index.js",
|
"import": "./dist/adapter/aws/index.js",
|
||||||
"require": "./dist/adapter/aws/index.js"
|
"require": "./dist/adapter/aws/index.js"
|
||||||
},
|
},
|
||||||
"./adapter/browser": {
|
|
||||||
"types": "./dist/types/adapter/browser/index.d.ts",
|
|
||||||
"import": "./dist/adapter/browser/index.js",
|
|
||||||
"require": "./dist/adapter/browser/index.js"
|
|
||||||
},
|
|
||||||
"./dist/main.css": "./dist/ui/main.css",
|
"./dist/main.css": "./dist/ui/main.css",
|
||||||
"./dist/styles.css": "./dist/ui/styles.css",
|
"./dist/styles.css": "./dist/ui/styles.css",
|
||||||
"./dist/manifest.json": "./dist/static/.vite/manifest.json",
|
"./dist/manifest.json": "./dist/static/.vite/manifest.json",
|
||||||
|
|||||||
+1
-1
@@ -61,7 +61,7 @@ export class Api {
|
|||||||
private token?: string;
|
private token?: string;
|
||||||
private user?: TApiUser;
|
private user?: TApiUser;
|
||||||
private verified = false;
|
private verified = false;
|
||||||
public token_transport: "header" | "cookie" | "none" = "header";
|
private token_transport: "header" | "cookie" | "none" = "header";
|
||||||
|
|
||||||
public system!: SystemApi;
|
public system!: SystemApi;
|
||||||
public data!: DataApi;
|
public data!: DataApi;
|
||||||
|
|||||||
+2
-3
@@ -5,6 +5,7 @@ import type { em as prototypeEm } from "data/prototype";
|
|||||||
import { Connection } from "data/connection/Connection";
|
import { Connection } from "data/connection/Connection";
|
||||||
import type { Hono } from "hono";
|
import type { Hono } from "hono";
|
||||||
import {
|
import {
|
||||||
|
type InitialModuleConfigs,
|
||||||
type ModuleConfigs,
|
type ModuleConfigs,
|
||||||
type Modules,
|
type Modules,
|
||||||
ModuleManager,
|
ModuleManager,
|
||||||
@@ -380,10 +381,8 @@ export class App<
|
|||||||
if (results.length > 0) {
|
if (results.length > 0) {
|
||||||
for (const { name, result } of results) {
|
for (const { name, result } of results) {
|
||||||
if (result) {
|
if (result) {
|
||||||
|
$console.log(`[Plugin:${name}] schema`);
|
||||||
ctx.helper.ensureSchema(result);
|
ctx.helper.ensureSchema(result);
|
||||||
if (ctx.flags.sync_required) {
|
|
||||||
$console.log(`[Plugin:${name}] schema, sync required`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,153 +0,0 @@
|
|||||||
import {
|
|
||||||
createContext,
|
|
||||||
lazy,
|
|
||||||
Suspense,
|
|
||||||
useContext,
|
|
||||||
useEffect,
|
|
||||||
useState,
|
|
||||||
type ReactNode,
|
|
||||||
} from "react";
|
|
||||||
import { checksum } from "bknd/utils";
|
|
||||||
import { App, registries, sqlocal, type BkndConfig } from "bknd";
|
|
||||||
import { Route, Router, Switch } from "wouter";
|
|
||||||
import { ClientProvider } from "bknd/client";
|
|
||||||
import { SQLocalKysely } from "sqlocal/kysely";
|
|
||||||
import type { ClientConfig, DatabasePath } from "sqlocal";
|
|
||||||
import { OpfsStorageAdapter } from "bknd/adapter/browser";
|
|
||||||
import type { BkndAdminConfig } from "bknd/ui";
|
|
||||||
|
|
||||||
const Admin = lazy(() =>
|
|
||||||
Promise.all([
|
|
||||||
import("bknd/ui"),
|
|
||||||
// @ts-ignore
|
|
||||||
import("bknd/dist/styles.css"),
|
|
||||||
]).then(([mod]) => ({
|
|
||||||
default: mod.Admin,
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
|
|
||||||
function safeViewTransition(fn: () => void) {
|
|
||||||
if (document.startViewTransition) {
|
|
||||||
document.startViewTransition(fn);
|
|
||||||
} else {
|
|
||||||
fn();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export type BrowserBkndConfig<Args = ImportMetaEnv> = Omit<
|
|
||||||
BkndConfig<Args>,
|
|
||||||
"connection" | "app"
|
|
||||||
> & {
|
|
||||||
adminConfig?: BkndAdminConfig;
|
|
||||||
connection?: ClientConfig | DatabasePath;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type BkndBrowserAppProps = {
|
|
||||||
children: ReactNode;
|
|
||||||
header?: ReactNode;
|
|
||||||
loading?: ReactNode;
|
|
||||||
notFound?: ReactNode;
|
|
||||||
} & BrowserBkndConfig;
|
|
||||||
|
|
||||||
const BkndBrowserAppContext = createContext<{
|
|
||||||
app: App;
|
|
||||||
hash: string;
|
|
||||||
}>(undefined!);
|
|
||||||
|
|
||||||
export function BkndBrowserApp({
|
|
||||||
children,
|
|
||||||
adminConfig,
|
|
||||||
header,
|
|
||||||
loading,
|
|
||||||
notFound,
|
|
||||||
...config
|
|
||||||
}: BkndBrowserAppProps) {
|
|
||||||
const [app, setApp] = useState<App | undefined>(undefined);
|
|
||||||
const [hash, setHash] = useState<string>("");
|
|
||||||
const adminRoutePath = (adminConfig?.basepath ?? "") + "/*?";
|
|
||||||
|
|
||||||
async function onBuilt(app: App) {
|
|
||||||
safeViewTransition(async () => {
|
|
||||||
setApp(app);
|
|
||||||
setHash(await checksum(app.toJSON()));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setup({ ...config, adminConfig })
|
|
||||||
.then((app) => onBuilt(app as any))
|
|
||||||
.catch(console.error);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (!app) {
|
|
||||||
return (
|
|
||||||
loading ?? (
|
|
||||||
<Center>
|
|
||||||
<span style={{ opacity: 0.2 }}>Loading...</span>
|
|
||||||
</Center>
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<BkndBrowserAppContext.Provider value={{ app, hash }}>
|
|
||||||
<ClientProvider storage={window.localStorage} fetcher={app.server.request}>
|
|
||||||
{header}
|
|
||||||
<Router key={hash}>
|
|
||||||
<Switch>
|
|
||||||
{children}
|
|
||||||
|
|
||||||
<Route path={adminRoutePath}>
|
|
||||||
<Suspense>
|
|
||||||
<Admin config={adminConfig} />
|
|
||||||
</Suspense>
|
|
||||||
</Route>
|
|
||||||
<Route path="*">
|
|
||||||
{notFound ?? (
|
|
||||||
<Center style={{ fontSize: "48px", fontFamily: "monospace" }}>404</Center>
|
|
||||||
)}
|
|
||||||
</Route>
|
|
||||||
</Switch>
|
|
||||||
</Router>
|
|
||||||
</ClientProvider>
|
|
||||||
</BkndBrowserAppContext.Provider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useApp() {
|
|
||||||
return useContext(BkndBrowserAppContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
const Center = (props: React.HTMLAttributes<HTMLDivElement>) => (
|
|
||||||
<div
|
|
||||||
{...props}
|
|
||||||
style={{
|
|
||||||
width: "100%",
|
|
||||||
minHeight: "100vh",
|
|
||||||
display: "flex",
|
|
||||||
justifyContent: "center",
|
|
||||||
alignItems: "center",
|
|
||||||
...(props.style ?? {}),
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
let initialized = false;
|
|
||||||
async function setup(config: BrowserBkndConfig = {}) {
|
|
||||||
if (initialized) return;
|
|
||||||
initialized = true;
|
|
||||||
|
|
||||||
registries.media.register("opfs", OpfsStorageAdapter);
|
|
||||||
|
|
||||||
const app = App.create({
|
|
||||||
...config,
|
|
||||||
// @ts-ignore
|
|
||||||
connection: sqlocal(new SQLocalKysely(config.connection ?? ":localStorage:")),
|
|
||||||
});
|
|
||||||
|
|
||||||
await config.beforeBuild?.(app);
|
|
||||||
await app.build({ sync: true });
|
|
||||||
await config.onBuilt?.(app);
|
|
||||||
|
|
||||||
return app;
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import { describe, beforeAll, vi, afterAll, spyOn } from "bun:test";
|
|
||||||
import { OpfsStorageAdapter } from "./OpfsStorageAdapter";
|
|
||||||
// @ts-ignore
|
|
||||||
import { assetsPath } from "../../../__test__/helper";
|
|
||||||
import { adapterTestSuite } from "media/storage/adapters/adapter-test-suite";
|
|
||||||
import { bunTestRunner } from "adapter/bun/test";
|
|
||||||
import { MockFileSystemDirectoryHandle } from "adapter/browser/mock";
|
|
||||||
|
|
||||||
describe("OpfsStorageAdapter", async () => {
|
|
||||||
let mockRoot: MockFileSystemDirectoryHandle;
|
|
||||||
let testSuiteAdapter: OpfsStorageAdapter;
|
|
||||||
|
|
||||||
const _mock = spyOn(global, "navigator");
|
|
||||||
|
|
||||||
beforeAll(() => {
|
|
||||||
// mock navigator.storage.getDirectory()
|
|
||||||
mockRoot = new MockFileSystemDirectoryHandle("opfs-root");
|
|
||||||
const mockNavigator = {
|
|
||||||
storage: {
|
|
||||||
getDirectory: vi.fn().mockResolvedValue(mockRoot),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
// @ts-ignore
|
|
||||||
_mock.mockReturnValue(mockNavigator);
|
|
||||||
testSuiteAdapter = new OpfsStorageAdapter();
|
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
_mock.mockRestore();
|
|
||||||
});
|
|
||||||
|
|
||||||
const file = Bun.file(`${assetsPath}/image.png`);
|
|
||||||
await adapterTestSuite(bunTestRunner, () => testSuiteAdapter, file);
|
|
||||||
});
|
|
||||||
@@ -1,265 +0,0 @@
|
|||||||
import type { FileBody, FileListObject, FileMeta, FileUploadPayload } from "bknd";
|
|
||||||
import { StorageAdapter, guessMimeType } from "bknd";
|
|
||||||
import { parse, s, isFile, isBlob } from "bknd/utils";
|
|
||||||
|
|
||||||
export const opfsAdapterConfig = s.object(
|
|
||||||
{
|
|
||||||
root: s.string({ default: "" }).optional(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: "OPFS",
|
|
||||||
description: "Origin Private File System storage",
|
|
||||||
additionalProperties: false,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
export type OpfsAdapterConfig = s.Static<typeof opfsAdapterConfig>;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Storage adapter for OPFS (Origin Private File System)
|
|
||||||
* Provides browser-based file storage using the File System Access API
|
|
||||||
*/
|
|
||||||
export class OpfsStorageAdapter extends StorageAdapter {
|
|
||||||
private config: OpfsAdapterConfig;
|
|
||||||
private rootPromise: Promise<FileSystemDirectoryHandle>;
|
|
||||||
|
|
||||||
constructor(config: Partial<OpfsAdapterConfig> = {}) {
|
|
||||||
super();
|
|
||||||
this.config = parse(opfsAdapterConfig, config);
|
|
||||||
this.rootPromise = this.initializeRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async initializeRoot(): Promise<FileSystemDirectoryHandle> {
|
|
||||||
const opfsRoot = await navigator.storage.getDirectory();
|
|
||||||
if (!this.config.root) {
|
|
||||||
return opfsRoot;
|
|
||||||
}
|
|
||||||
|
|
||||||
// navigate to or create nested directory structure
|
|
||||||
const parts = this.config.root.split("/").filter(Boolean);
|
|
||||||
let current = opfsRoot;
|
|
||||||
for (const part of parts) {
|
|
||||||
current = await current.getDirectoryHandle(part, { create: true });
|
|
||||||
}
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
||||||
getSchema() {
|
|
||||||
return opfsAdapterConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
getName(): string {
|
|
||||||
return "opfs";
|
|
||||||
}
|
|
||||||
|
|
||||||
async listObjects(prefix?: string): Promise<FileListObject[]> {
|
|
||||||
const root = await this.rootPromise;
|
|
||||||
const files: FileListObject[] = [];
|
|
||||||
|
|
||||||
for await (const [name, handle] of root.entries()) {
|
|
||||||
if (handle.kind === "file") {
|
|
||||||
if (!prefix || name.startsWith(prefix)) {
|
|
||||||
const file = await (handle as FileSystemFileHandle).getFile();
|
|
||||||
files.push({
|
|
||||||
key: name,
|
|
||||||
last_modified: new Date(file.lastModified),
|
|
||||||
size: file.size,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return files;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async computeEtagFromArrayBuffer(buffer: ArrayBuffer): Promise<string> {
|
|
||||||
const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
|
|
||||||
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
||||||
const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
||||||
|
|
||||||
// wrap the hex string in quotes for ETag format
|
|
||||||
return `"${hashHex}"`;
|
|
||||||
}
|
|
||||||
|
|
||||||
async putObject(key: string, body: FileBody): Promise<string | FileUploadPayload> {
|
|
||||||
if (body === null) {
|
|
||||||
throw new Error("Body is empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
const root = await this.rootPromise;
|
|
||||||
const fileHandle = await root.getFileHandle(key, { create: true });
|
|
||||||
const writable = await fileHandle.createWritable();
|
|
||||||
|
|
||||||
try {
|
|
||||||
let contentBuffer: ArrayBuffer;
|
|
||||||
|
|
||||||
if (isFile(body)) {
|
|
||||||
contentBuffer = await body.arrayBuffer();
|
|
||||||
await writable.write(contentBuffer);
|
|
||||||
} else if (body instanceof ReadableStream) {
|
|
||||||
const chunks: Uint8Array[] = [];
|
|
||||||
const reader = body.getReader();
|
|
||||||
try {
|
|
||||||
while (true) {
|
|
||||||
const { done, value } = await reader.read();
|
|
||||||
if (done) break;
|
|
||||||
chunks.push(value);
|
|
||||||
await writable.write(value);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
reader.releaseLock();
|
|
||||||
}
|
|
||||||
// compute total size and combine chunks for etag
|
|
||||||
const totalSize = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
||||||
const combined = new Uint8Array(totalSize);
|
|
||||||
let offset = 0;
|
|
||||||
for (const chunk of chunks) {
|
|
||||||
combined.set(chunk, offset);
|
|
||||||
offset += chunk.length;
|
|
||||||
}
|
|
||||||
contentBuffer = combined.buffer;
|
|
||||||
} else if (isBlob(body)) {
|
|
||||||
contentBuffer = await (body as Blob).arrayBuffer();
|
|
||||||
await writable.write(contentBuffer);
|
|
||||||
} else {
|
|
||||||
// body is ArrayBuffer or ArrayBufferView
|
|
||||||
if (ArrayBuffer.isView(body)) {
|
|
||||||
const view = body as ArrayBufferView;
|
|
||||||
contentBuffer = view.buffer.slice(
|
|
||||||
view.byteOffset,
|
|
||||||
view.byteOffset + view.byteLength,
|
|
||||||
) as ArrayBuffer;
|
|
||||||
} else {
|
|
||||||
contentBuffer = body as ArrayBuffer;
|
|
||||||
}
|
|
||||||
await writable.write(body);
|
|
||||||
}
|
|
||||||
|
|
||||||
await writable.close();
|
|
||||||
return await this.computeEtagFromArrayBuffer(contentBuffer);
|
|
||||||
} catch (error) {
|
|
||||||
await writable.abort();
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async deleteObject(key: string): Promise<void> {
|
|
||||||
try {
|
|
||||||
const root = await this.rootPromise;
|
|
||||||
await root.removeEntry(key);
|
|
||||||
} catch {
|
|
||||||
// file doesn't exist, which is fine
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async objectExists(key: string): Promise<boolean> {
|
|
||||||
try {
|
|
||||||
const root = await this.rootPromise;
|
|
||||||
await root.getFileHandle(key);
|
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private parseRangeHeader(
|
|
||||||
rangeHeader: string,
|
|
||||||
fileSize: number,
|
|
||||||
): { start: number; end: number } | null {
|
|
||||||
// parse "bytes=start-end" format
|
|
||||||
const match = rangeHeader.match(/^bytes=(\d*)-(\d*)$/);
|
|
||||||
if (!match) return null;
|
|
||||||
|
|
||||||
const [, startStr, endStr] = match;
|
|
||||||
let start = startStr ? Number.parseInt(startStr, 10) : 0;
|
|
||||||
let end = endStr ? Number.parseInt(endStr, 10) : fileSize - 1;
|
|
||||||
|
|
||||||
// handle suffix-byte-range-spec (e.g., "bytes=-500")
|
|
||||||
if (!startStr && endStr) {
|
|
||||||
start = Math.max(0, fileSize - Number.parseInt(endStr, 10));
|
|
||||||
end = fileSize - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// validate range
|
|
||||||
if (start < 0 || end >= fileSize || start > end) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return { start, end };
|
|
||||||
}
|
|
||||||
|
|
||||||
async getObject(key: string, headers: Headers): Promise<Response> {
|
|
||||||
try {
|
|
||||||
const root = await this.rootPromise;
|
|
||||||
const fileHandle = await root.getFileHandle(key);
|
|
||||||
const file = await fileHandle.getFile();
|
|
||||||
const fileSize = file.size;
|
|
||||||
const mimeType = guessMimeType(key);
|
|
||||||
|
|
||||||
const responseHeaders = new Headers({
|
|
||||||
"Accept-Ranges": "bytes",
|
|
||||||
"Content-Type": mimeType || "application/octet-stream",
|
|
||||||
});
|
|
||||||
|
|
||||||
const rangeHeader = headers.get("range");
|
|
||||||
|
|
||||||
if (rangeHeader) {
|
|
||||||
const range = this.parseRangeHeader(rangeHeader, fileSize);
|
|
||||||
|
|
||||||
if (!range) {
|
|
||||||
// invalid range - return 416 Range Not Satisfiable
|
|
||||||
responseHeaders.set("Content-Range", `bytes */${fileSize}`);
|
|
||||||
return new Response("", {
|
|
||||||
status: 416,
|
|
||||||
headers: responseHeaders,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const { start, end } = range;
|
|
||||||
const arrayBuffer = await file.arrayBuffer();
|
|
||||||
const chunk = arrayBuffer.slice(start, end + 1);
|
|
||||||
|
|
||||||
responseHeaders.set("Content-Range", `bytes ${start}-${end}/${fileSize}`);
|
|
||||||
responseHeaders.set("Content-Length", chunk.byteLength.toString());
|
|
||||||
|
|
||||||
return new Response(chunk, {
|
|
||||||
status: 206, // Partial Content
|
|
||||||
headers: responseHeaders,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// normal request - return entire file
|
|
||||||
const content = await file.arrayBuffer();
|
|
||||||
responseHeaders.set("Content-Length", content.byteLength.toString());
|
|
||||||
|
|
||||||
return new Response(content, {
|
|
||||||
status: 200,
|
|
||||||
headers: responseHeaders,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// handle file reading errors
|
|
||||||
return new Response("", { status: 404 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getObjectUrl(_key: string): string {
|
|
||||||
throw new Error("Method not implemented.");
|
|
||||||
}
|
|
||||||
|
|
||||||
async getObjectMeta(key: string): Promise<FileMeta> {
|
|
||||||
const root = await this.rootPromise;
|
|
||||||
const fileHandle = await root.getFileHandle(key);
|
|
||||||
const file = await fileHandle.getFile();
|
|
||||||
|
|
||||||
return {
|
|
||||||
type: guessMimeType(key) || "application/octet-stream",
|
|
||||||
size: file.size,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
toJSON(_secrets?: boolean) {
|
|
||||||
return {
|
|
||||||
type: this.getName(),
|
|
||||||
config: this.config,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
export * from "./OpfsStorageAdapter";
|
|
||||||
export * from "./BkndBrowserApp";
|
|
||||||
@@ -1,136 +0,0 @@
|
|||||||
// mock OPFS API for testing
|
|
||||||
class MockFileSystemFileHandle {
|
|
||||||
kind: "file" = "file";
|
|
||||||
name: string;
|
|
||||||
private content: ArrayBuffer;
|
|
||||||
private lastModified: number;
|
|
||||||
|
|
||||||
constructor(name: string, content: ArrayBuffer = new ArrayBuffer(0)) {
|
|
||||||
this.name = name;
|
|
||||||
this.content = content;
|
|
||||||
this.lastModified = Date.now();
|
|
||||||
}
|
|
||||||
|
|
||||||
async getFile(): Promise<File> {
|
|
||||||
return new File([this.content], this.name, {
|
|
||||||
lastModified: this.lastModified,
|
|
||||||
type: this.guessMimeType(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async createWritable(): Promise<FileSystemWritableFileStream> {
|
|
||||||
const handle = this;
|
|
||||||
return {
|
|
||||||
async write(data: any) {
|
|
||||||
if (data instanceof ArrayBuffer) {
|
|
||||||
handle.content = data;
|
|
||||||
} else if (ArrayBuffer.isView(data)) {
|
|
||||||
handle.content = data.buffer.slice(
|
|
||||||
data.byteOffset,
|
|
||||||
data.byteOffset + data.byteLength,
|
|
||||||
) as ArrayBuffer;
|
|
||||||
} else if (data instanceof Blob) {
|
|
||||||
handle.content = await data.arrayBuffer();
|
|
||||||
}
|
|
||||||
handle.lastModified = Date.now();
|
|
||||||
},
|
|
||||||
async close() {},
|
|
||||||
async abort() {},
|
|
||||||
async seek(_position: number) {},
|
|
||||||
async truncate(_size: number) {},
|
|
||||||
} as FileSystemWritableFileStream;
|
|
||||||
}
|
|
||||||
|
|
||||||
private guessMimeType(): string {
|
|
||||||
const ext = this.name.split(".").pop()?.toLowerCase();
|
|
||||||
const mimeTypes: Record<string, string> = {
|
|
||||||
png: "image/png",
|
|
||||||
jpg: "image/jpeg",
|
|
||||||
jpeg: "image/jpeg",
|
|
||||||
gif: "image/gif",
|
|
||||||
webp: "image/webp",
|
|
||||||
svg: "image/svg+xml",
|
|
||||||
txt: "text/plain",
|
|
||||||
json: "application/json",
|
|
||||||
pdf: "application/pdf",
|
|
||||||
};
|
|
||||||
return mimeTypes[ext || ""] || "application/octet-stream";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class MockFileSystemDirectoryHandle {
|
|
||||||
kind: "directory" = "directory";
|
|
||||||
name: string;
|
|
||||||
private files: Map<string, MockFileSystemFileHandle> = new Map();
|
|
||||||
private directories: Map<string, MockFileSystemDirectoryHandle> = new Map();
|
|
||||||
|
|
||||||
constructor(name: string = "root") {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getFileHandle(
|
|
||||||
name: string,
|
|
||||||
options?: FileSystemGetFileOptions,
|
|
||||||
): Promise<FileSystemFileHandle> {
|
|
||||||
if (this.files.has(name)) {
|
|
||||||
return this.files.get(name) as any;
|
|
||||||
}
|
|
||||||
if (options?.create) {
|
|
||||||
const handle = new MockFileSystemFileHandle(name);
|
|
||||||
this.files.set(name, handle);
|
|
||||||
return handle as any;
|
|
||||||
}
|
|
||||||
throw new Error(`File not found: ${name}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getDirectoryHandle(
|
|
||||||
name: string,
|
|
||||||
options?: FileSystemGetDirectoryOptions,
|
|
||||||
): Promise<FileSystemDirectoryHandle> {
|
|
||||||
if (this.directories.has(name)) {
|
|
||||||
return this.directories.get(name) as any;
|
|
||||||
}
|
|
||||||
if (options?.create) {
|
|
||||||
const handle = new MockFileSystemDirectoryHandle(name);
|
|
||||||
this.directories.set(name, handle);
|
|
||||||
return handle as any;
|
|
||||||
}
|
|
||||||
throw new Error(`Directory not found: ${name}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
async removeEntry(name: string, _options?: FileSystemRemoveOptions): Promise<void> {
|
|
||||||
this.files.delete(name);
|
|
||||||
this.directories.delete(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
async *entries(): AsyncIterableIterator<[string, FileSystemHandle]> {
|
|
||||||
for (const [name, handle] of this.files) {
|
|
||||||
yield [name, handle as any];
|
|
||||||
}
|
|
||||||
for (const [name, handle] of this.directories) {
|
|
||||||
yield [name, handle as any];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async *keys(): AsyncIterableIterator<string> {
|
|
||||||
for (const name of this.files.keys()) {
|
|
||||||
yield name;
|
|
||||||
}
|
|
||||||
for (const name of this.directories.keys()) {
|
|
||||||
yield name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async *values(): AsyncIterableIterator<FileSystemHandle> {
|
|
||||||
for (const handle of this.files.values()) {
|
|
||||||
yield handle as any;
|
|
||||||
}
|
|
||||||
for (const handle of this.directories.values()) {
|
|
||||||
yield handle as any;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Symbol.asyncIterator](): AsyncIterableIterator<[string, FileSystemHandle]> {
|
|
||||||
return this.entries();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
|
/// <reference types="bun-types" />
|
||||||
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { type RuntimeBkndConfig, createRuntimeApp } from "bknd/adapter";
|
import { type RuntimeBkndConfig, createRuntimeApp } from "bknd/adapter";
|
||||||
import { registerLocalMediaAdapter } from ".";
|
import { registerLocalMediaAdapter } from ".";
|
||||||
import { config, type App } from "bknd";
|
import { config, type App } from "bknd";
|
||||||
|
import type { ServeOptions } from "bun";
|
||||||
import { serveStatic } from "hono/bun";
|
import { serveStatic } from "hono/bun";
|
||||||
|
|
||||||
type BunEnv = Bun.Env;
|
type BunEnv = Bun.Env;
|
||||||
export type BunBkndConfig<Env = BunEnv> = RuntimeBkndConfig<Env> &
|
export type BunBkndConfig<Env = BunEnv> = RuntimeBkndConfig<Env> & Omit<ServeOptions, "fetch">;
|
||||||
Omit<Bun.Serve.Options<undefined, string>, "fetch">;
|
|
||||||
|
|
||||||
export async function createApp<Env = BunEnv>(
|
export async function createApp<Env = BunEnv>(
|
||||||
{ distPath, serveStatic: _serveStatic, ...config }: BunBkndConfig<Env> = {},
|
{ distPath, serveStatic: _serveStatic, ...config }: BunBkndConfig<Env> = {},
|
||||||
@@ -43,7 +45,6 @@ export function createHandler<Env = BunEnv>(
|
|||||||
|
|
||||||
export function serve<Env = BunEnv>(
|
export function serve<Env = BunEnv>(
|
||||||
{
|
{
|
||||||
app,
|
|
||||||
distPath,
|
distPath,
|
||||||
connection,
|
connection,
|
||||||
config: _config,
|
config: _config,
|
||||||
@@ -59,11 +60,10 @@ export function serve<Env = BunEnv>(
|
|||||||
args: Env = Bun.env as Env,
|
args: Env = Bun.env as Env,
|
||||||
) {
|
) {
|
||||||
Bun.serve({
|
Bun.serve({
|
||||||
...(serveOptions as any),
|
...serveOptions,
|
||||||
port,
|
port,
|
||||||
fetch: createHandler(
|
fetch: createHandler(
|
||||||
{
|
{
|
||||||
app,
|
|
||||||
connection,
|
connection,
|
||||||
config: _config,
|
config: _config,
|
||||||
options,
|
options,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import type { RuntimeBkndConfig } from "bknd/adapter";
|
import type { RuntimeBkndConfig } from "bknd/adapter";
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { serveStatic } from "hono/cloudflare-workers";
|
import { serveStatic } from "hono/cloudflare-workers";
|
||||||
import type { App, MaybePromise } from "bknd";
|
import type { MaybePromise } from "bknd";
|
||||||
import { $console } from "bknd/utils";
|
import { $console } from "bknd/utils";
|
||||||
import { createRuntimeApp } from "bknd/adapter";
|
import { createRuntimeApp } from "bknd/adapter";
|
||||||
import { registerAsyncsExecutionContext, makeConfig, type CloudflareContext } from "./config";
|
import { registerAsyncsExecutionContext, makeConfig, type CloudflareContext } from "./config";
|
||||||
@@ -55,12 +55,8 @@ export async function createApp<Env extends CloudflareEnv = CloudflareEnv>(
|
|||||||
// compatiblity
|
// compatiblity
|
||||||
export const getFresh = createApp;
|
export const getFresh = createApp;
|
||||||
|
|
||||||
let app: App | undefined;
|
|
||||||
export function serve<Env extends CloudflareEnv = CloudflareEnv>(
|
export function serve<Env extends CloudflareEnv = CloudflareEnv>(
|
||||||
config: CloudflareBkndConfig<Env> = {},
|
config: CloudflareBkndConfig<Env> = {},
|
||||||
serveOptions?: (args: Env) => {
|
|
||||||
warm?: boolean;
|
|
||||||
},
|
|
||||||
) {
|
) {
|
||||||
return {
|
return {
|
||||||
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
||||||
@@ -96,11 +92,8 @@ export function serve<Env extends CloudflareEnv = CloudflareEnv>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const { warm } = serveOptions?.(env) ?? {};
|
const context = { request, env, ctx } as CloudflareContext<Env>;
|
||||||
if (!app || warm !== true) {
|
const app = await createApp(config, context);
|
||||||
const context = { request, env, ctx } as CloudflareContext<Env>;
|
|
||||||
app = await createApp(config, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
return app.fetch(request, env, ctx);
|
return app.fetch(request, env, ctx);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -65,31 +65,37 @@ export function withPlatformProxy<Env extends CloudflareEnv>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
...config,
|
||||||
|
beforeBuild: async (app, registries) => {
|
||||||
|
if (!use_proxy) return;
|
||||||
|
const env = await getEnv();
|
||||||
|
registerMedia(env, registries as any);
|
||||||
|
await config?.beforeBuild?.(app, registries);
|
||||||
|
},
|
||||||
|
bindings: async (env) => {
|
||||||
|
return (await config?.bindings?.(await getEnv(env))) || {};
|
||||||
|
},
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
app: async (_env) => {
|
app: async (_env) => {
|
||||||
const env = await getEnv(_env);
|
const env = await getEnv(_env);
|
||||||
const binding = use_proxy ? getBinding(env, "D1Database") : undefined;
|
const binding = use_proxy ? getBinding(env, "D1Database") : undefined;
|
||||||
const appConfig = typeof config.app === "function" ? await config.app(env) : config;
|
|
||||||
const connection =
|
|
||||||
use_proxy && binding
|
|
||||||
? d1Sqlite({
|
|
||||||
binding: binding.value as any,
|
|
||||||
})
|
|
||||||
: appConfig.connection;
|
|
||||||
|
|
||||||
return {
|
if (config?.app === undefined && use_proxy && binding) {
|
||||||
...appConfig,
|
return {
|
||||||
beforeBuild: async (app, registries) => {
|
connection: d1Sqlite({
|
||||||
if (!use_proxy) return;
|
binding: binding.value,
|
||||||
const env = await getEnv();
|
}),
|
||||||
registerMedia(env, registries as any);
|
};
|
||||||
await config?.beforeBuild?.(app, registries);
|
} else if (typeof config?.app === "function") {
|
||||||
},
|
const appConfig = await config?.app(env);
|
||||||
bindings: async (env) => {
|
if (binding) {
|
||||||
return (await config?.bindings?.(await getEnv(env))) || {};
|
appConfig.connection = d1Sqlite({
|
||||||
},
|
binding: binding.value,
|
||||||
connection,
|
}) as any;
|
||||||
};
|
}
|
||||||
|
return appConfig;
|
||||||
|
}
|
||||||
|
return config?.app || {};
|
||||||
},
|
},
|
||||||
} satisfies CloudflareBkndConfig<Env>;
|
} satisfies CloudflareBkndConfig<Env>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,15 +14,14 @@ import type { AdminControllerOptions } from "modules/server/AdminController";
|
|||||||
import type { Manifest } from "vite";
|
import type { Manifest } from "vite";
|
||||||
|
|
||||||
export type BkndConfig<Args = any, Additional = {}> = Merge<
|
export type BkndConfig<Args = any, Additional = {}> = Merge<
|
||||||
CreateAppConfig &
|
CreateAppConfig & {
|
||||||
Omit<Additional, "app"> & {
|
app?:
|
||||||
app?:
|
| Merge<Omit<BkndConfig, "app"> & Additional>
|
||||||
| Omit<BkndConfig<Args, Additional>, "app">
|
| ((args: Args) => MaybePromise<Merge<Omit<BkndConfig<Args>, "app"> & Additional>>);
|
||||||
| ((args: Args) => MaybePromise<Omit<BkndConfig<Args, Additional>, "app">>);
|
onBuilt?: (app: App) => MaybePromise<void>;
|
||||||
onBuilt?: (app: App) => MaybePromise<void>;
|
beforeBuild?: (app?: App, registries?: typeof $registries) => MaybePromise<void>;
|
||||||
beforeBuild?: (app?: App, registries?: typeof $registries) => MaybePromise<void>;
|
buildConfig?: Parameters<App["build"]>[0];
|
||||||
buildConfig?: Parameters<App["build"]>[0];
|
} & Additional
|
||||||
}
|
|
||||||
>;
|
>;
|
||||||
|
|
||||||
export type FrameworkBkndConfig<Args = any> = BkndConfig<Args>;
|
export type FrameworkBkndConfig<Args = any> = BkndConfig<Args>;
|
||||||
@@ -69,11 +68,6 @@ export async function createAdapterApp<Config extends BkndConfig = BkndConfig, A
|
|||||||
if (Connection.isConnection(config.connection)) {
|
if (Connection.isConnection(config.connection)) {
|
||||||
connection = config.connection;
|
connection = config.connection;
|
||||||
} else {
|
} else {
|
||||||
if (connection) {
|
|
||||||
$console.warn(
|
|
||||||
"Connection is not a valid connection object, using default SQLite connection",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const sqlite = (await import("bknd/adapter/sqlite")).sqlite;
|
const sqlite = (await import("bknd/adapter/sqlite")).sqlite;
|
||||||
const conf = appConfig.connection ?? { url: "file:data.db" };
|
const conf = appConfig.connection ?? { url: "file:data.db" };
|
||||||
connection = sqlite(conf) as any;
|
connection = sqlite(conf) as any;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { createFrameworkApp, type FrameworkBkndConfig } from "bknd/adapter";
|
import { createFrameworkApp, type FrameworkBkndConfig } from "bknd/adapter";
|
||||||
import { isNode } from "bknd/utils";
|
import { isNode } from "bknd/utils";
|
||||||
// @ts-expect-error next is not installed
|
|
||||||
import type { NextApiRequest } from "next";
|
import type { NextApiRequest } from "next";
|
||||||
|
|
||||||
type NextjsEnv = NextApiRequest["env"];
|
type NextjsEnv = NextApiRequest["env"];
|
||||||
@@ -19,9 +18,7 @@ function getCleanRequest(req: Request, cleanRequest: NextjsBkndConfig["cleanRequ
|
|||||||
if (!cleanRequest) return req;
|
if (!cleanRequest) return req;
|
||||||
|
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
cleanRequest?.searchParams?.forEach((k) => {
|
cleanRequest?.searchParams?.forEach((k) => url.searchParams.delete(k));
|
||||||
url.searchParams.delete(k);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (isNode()) {
|
if (isNode()) {
|
||||||
return new Request(url.toString(), {
|
return new Request(url.toString(), {
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export async function createApp<Env = NodeEnv>(
|
|||||||
path.resolve(distPath ?? relativeDistPath ?? "./node_modules/bknd/dist", "static"),
|
path.resolve(distPath ?? relativeDistPath ?? "./node_modules/bknd/dist", "static"),
|
||||||
);
|
);
|
||||||
if (relativeDistPath) {
|
if (relativeDistPath) {
|
||||||
$console.warn("relativeDistPath is deprecated, please use distPath instead");
|
console.warn("relativeDistPath is deprecated, please use distPath instead");
|
||||||
}
|
}
|
||||||
|
|
||||||
registerLocalMediaAdapter();
|
registerLocalMediaAdapter();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { describe } from "vitest";
|
import { describe, beforeAll, afterAll } from "vitest";
|
||||||
import * as node from "./node.adapter";
|
import * as node from "./node.adapter";
|
||||||
import { adapterTestSuite } from "adapter/adapter-test-suite";
|
import { adapterTestSuite } from "adapter/adapter-test-suite";
|
||||||
import { viTestRunner } from "adapter/node/vitest";
|
import { viTestRunner } from "adapter/node/vitest";
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
import type { CliCommand } from "cli/types";
|
|
||||||
import { Option } from "commander";
|
|
||||||
import { withConfigOptions, type WithConfigOptions } from "cli/utils/options";
|
|
||||||
import * as utils from "./utils";
|
|
||||||
import { $console } from "core/utils";
|
|
||||||
import { Project } from "./lib/Project";
|
|
||||||
|
|
||||||
export const dev: CliCommand = (program) =>
|
|
||||||
withConfigOptions(program.command("dev"))
|
|
||||||
.description("dev server")
|
|
||||||
.addOption(new Option("--build", "build the project"))
|
|
||||||
.action(action);
|
|
||||||
|
|
||||||
async function action(options: WithConfigOptions<{ build?: boolean }>) {
|
|
||||||
console.log("options", options);
|
|
||||||
const project = new Project({
|
|
||||||
templatePath: utils.TEMPLATE_PATH,
|
|
||||||
});
|
|
||||||
|
|
||||||
await project.init();
|
|
||||||
|
|
||||||
if (options.build) {
|
|
||||||
await project.build();
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
await project.listen();
|
|
||||||
}
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
import { RelativeFS } from "./RelativeFS";
|
|
||||||
//import { $console } from "bknd/utils";
|
|
||||||
import { type ViteDevServer, createServer, type UserConfig, createBuilder } from "vite";
|
|
||||||
import { readdir, copyFile, stat } from "node:fs/promises";
|
|
||||||
import path from "node:path";
|
|
||||||
import react from "@vitejs/plugin-react";
|
|
||||||
import tailwindcss from "@tailwindcss/vite";
|
|
||||||
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
||||||
import { getRelativeDistPath } from "cli/utils/sys";
|
|
||||||
import { injectMain } from "./vite/inject-main";
|
|
||||||
import { devFsVitePlugin } from "adapter/cloudflare";
|
|
||||||
|
|
||||||
export type ProjectOptions = {
|
|
||||||
userPath?: string;
|
|
||||||
templatePath?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
// @todo: add multiple public dirs
|
|
||||||
// @todo: add npm install (first time)
|
|
||||||
// @todo: add package.json
|
|
||||||
export class Project {
|
|
||||||
public userFs: RelativeFS;
|
|
||||||
public templateFs: RelativeFS;
|
|
||||||
private _server?: ViteDevServer;
|
|
||||||
|
|
||||||
constructor(public options: ProjectOptions) {
|
|
||||||
this.userFs = new RelativeFS(options.userPath ?? process.cwd());
|
|
||||||
this.templateFs = new RelativeFS(options.templatePath ?? "src/cli/commands/dev/template");
|
|
||||||
}
|
|
||||||
|
|
||||||
get server() {
|
|
||||||
if (!this._server) {
|
|
||||||
throw new Error("Server not initialized");
|
|
||||||
}
|
|
||||||
return this._server!;
|
|
||||||
}
|
|
||||||
|
|
||||||
async init() {
|
|
||||||
const source = this.templateFs.root;
|
|
||||||
const destination = this.userFs.root;
|
|
||||||
|
|
||||||
// recursively copy all files and directories from source to dest
|
|
||||||
const copyRecursive = async (src: string, dst: string) => {
|
|
||||||
const entries = await readdir(src, { withFileTypes: true });
|
|
||||||
|
|
||||||
for (const entry of entries) {
|
|
||||||
const srcPath = path.join(src, entry.name);
|
|
||||||
const dstPath = path.join(dst, entry.name);
|
|
||||||
|
|
||||||
if (entry.isDirectory()) {
|
|
||||||
await this.userFs.makeDir(path.relative(destination, dstPath));
|
|
||||||
await copyRecursive(srcPath, dstPath);
|
|
||||||
} else if (entry.isFile()) {
|
|
||||||
const exists = await stat(dstPath)
|
|
||||||
.then((s) => s.isFile())
|
|
||||||
.catch(() => null);
|
|
||||||
|
|
||||||
// only copy everything from ".bknd" with override
|
|
||||||
if (!exists || (dstPath.includes(".bknd") && !dstPath.includes("bknd-types.d.ts"))) {
|
|
||||||
await copyFile(srcPath, dstPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
await copyRecursive(source, destination);
|
|
||||||
this._server = await createServer(this.getViteConfig());
|
|
||||||
}
|
|
||||||
|
|
||||||
private getViteConfig(): UserConfig {
|
|
||||||
return {
|
|
||||||
clearScreen: false,
|
|
||||||
publicDir: getRelativeDistPath() + "/static",
|
|
||||||
plugins: [
|
|
||||||
react(),
|
|
||||||
tailwindcss(),
|
|
||||||
devFsVitePlugin({ configFile: ".bknd/bknd.config.ts" }) as any,
|
|
||||||
cloudflare({
|
|
||||||
configPath: this.userFs.path(".bknd/wrangler.json"),
|
|
||||||
persistState: {
|
|
||||||
path: this.userFs.path(".bknd/state"),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
injectMain({
|
|
||||||
appPath: "./src/App.tsx",
|
|
||||||
rootId: "root",
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
build: {
|
|
||||||
minify: true,
|
|
||||||
},
|
|
||||||
resolve: {
|
|
||||||
dedupe: ["react", "react-dom"],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async build() {
|
|
||||||
const builder = await createBuilder(this.getViteConfig());
|
|
||||||
await builder.buildApp();
|
|
||||||
}
|
|
||||||
|
|
||||||
async listen() {
|
|
||||||
await this.server.listen();
|
|
||||||
this.server.printUrls();
|
|
||||||
this.server.bindCLIShortcuts({ print: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
import {
|
|
||||||
mkdir,
|
|
||||||
stat,
|
|
||||||
readFile as nodeReadFile,
|
|
||||||
writeFile as nodeWriteFile,
|
|
||||||
} from "node:fs/promises";
|
|
||||||
import { getRootPath } from "cli/utils/sys";
|
|
||||||
import path from "node:path";
|
|
||||||
|
|
||||||
export class RelativeFS {
|
|
||||||
public root: string;
|
|
||||||
|
|
||||||
constructor(p: string) {
|
|
||||||
this.root = path.resolve(getRootPath(), p);
|
|
||||||
}
|
|
||||||
|
|
||||||
path(p: string) {
|
|
||||||
return path.join(this.root, p);
|
|
||||||
}
|
|
||||||
|
|
||||||
async hasFile(path: string) {
|
|
||||||
try {
|
|
||||||
const s = await stat(this.path(path));
|
|
||||||
return s.isFile();
|
|
||||||
} catch (_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async hasDir(path: string) {
|
|
||||||
try {
|
|
||||||
const s = await stat(this.path(path));
|
|
||||||
return s.isDirectory();
|
|
||||||
} catch (_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async readFile(p: string) {
|
|
||||||
return await nodeReadFile(this.path(p), "utf-8");
|
|
||||||
}
|
|
||||||
|
|
||||||
async writeFile(p: string, content: string) {
|
|
||||||
return await nodeWriteFile(this.path(p), content);
|
|
||||||
}
|
|
||||||
|
|
||||||
async makeDir(p: string) {
|
|
||||||
return await mkdir(this.path(p), { recursive: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
import type { PluginOption } from "vite";
|
|
||||||
|
|
||||||
export function injectMain(options?: { appPath?: string; rootId?: string }): PluginOption {
|
|
||||||
const appPath = options?.appPath ?? "/App.tsx";
|
|
||||||
const rootId = options?.rootId ?? "root";
|
|
||||||
const publicId = "/@virtual/react-main.js";
|
|
||||||
const internalId = "\0virtual-react-main.js";
|
|
||||||
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
name: "bknd-virtual-react-entry",
|
|
||||||
transformIndexHtml(html) {
|
|
||||||
return {
|
|
||||||
html,
|
|
||||||
tags: [
|
|
||||||
{
|
|
||||||
tag: "script",
|
|
||||||
injectTo: "body",
|
|
||||||
attrs: {
|
|
||||||
type: "module",
|
|
||||||
src: publicId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
},
|
|
||||||
resolveId(id) {
|
|
||||||
if (id === publicId) {
|
|
||||||
return internalId;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
load(id) {
|
|
||||||
if (id === internalId) {
|
|
||||||
return `
|
|
||||||
import React from "react";
|
|
||||||
import ReactDOM from "react-dom/client";
|
|
||||||
import App from "${appPath}";
|
|
||||||
import { ClientProvider } from "bknd/client";
|
|
||||||
|
|
||||||
const container = document.getElementById("${rootId}");
|
|
||||||
if (!container) {
|
|
||||||
throw new Error("Cannot find #${rootId} element");
|
|
||||||
}
|
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(container);
|
|
||||||
root.render(
|
|
||||||
React.createElement(
|
|
||||||
React.StrictMode,
|
|
||||||
null,
|
|
||||||
React.createElement(ClientProvider, null, React.createElement(App, null))
|
|
||||||
)
|
|
||||||
);
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import type { DB } from "bknd";
|
|
||||||
import type { Insertable, Selectable, Updateable } from "kysely";
|
|
||||||
|
|
||||||
declare global {
|
|
||||||
type BkndEntity<T extends keyof DB> = Selectable<DB[T]>;
|
|
||||||
type BkndEntityCreate<T extends keyof DB> = Insertable<DB[T]>;
|
|
||||||
type BkndEntityUpdate<T extends keyof DB> = Updateable<DB[T]>;
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "ES2022",
|
|
||||||
"useDefineForClassFields": true,
|
|
||||||
"lib": ["ES2023", "DOM", "DOM.Iterable"],
|
|
||||||
"module": "ESNext",
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"moduleResolution": "bundler",
|
|
||||||
"allowImportingTsExtensions": true,
|
|
||||||
"isolatedModules": true,
|
|
||||||
"moduleDetection": "force",
|
|
||||||
"noEmit": true,
|
|
||||||
"jsx": "react-jsx",
|
|
||||||
"strict": true,
|
|
||||||
"noUnusedLocals": true,
|
|
||||||
"noUnusedParameters": true,
|
|
||||||
"noFallthroughCasesInSwitch": true,
|
|
||||||
"noUncheckedSideEffectImports": true,
|
|
||||||
"types": ["vite/client"]
|
|
||||||
},
|
|
||||||
"include": ["bknd-types.d.ts"]
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
import { serve } from "bknd/adapter/cloudflare";
|
|
||||||
import config from "./bknd.config";
|
|
||||||
|
|
||||||
export default serve(config);
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "bknd-dev",
|
|
||||||
"main": "./worker.ts",
|
|
||||||
"compatibility_date": "2025-10-08",
|
|
||||||
"compatibility_flags": ["nodejs_compat"],
|
|
||||||
"observability": {
|
|
||||||
"enabled": true
|
|
||||||
},
|
|
||||||
"assets": {
|
|
||||||
"binding": "ASSETS",
|
|
||||||
"directory": "../dist/client",
|
|
||||||
"not_found_handling": "single-page-application",
|
|
||||||
"run_worker_first": ["!/", "/admin*", "/api*", "!/assets/*"]
|
|
||||||
},
|
|
||||||
"vars": {
|
|
||||||
"ENVIRONMENT": "development"
|
|
||||||
},
|
|
||||||
"d1_databases": [
|
|
||||||
{
|
|
||||||
"binding": "DB"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"r2_buckets": [
|
|
||||||
{
|
|
||||||
"binding": "BUCKET"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
||||||
<title>bknd + Vite + Cloudflare + React + TS</title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<div id="root"></div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import "./styles.css";
|
|
||||||
|
|
||||||
export default function App() {
|
|
||||||
return <div>Hello World</div>;
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import { Hono } from "hono";
|
|
||||||
import type { ServerEnv } from "bknd";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add custom routes to the API here. Base path is `/api`.
|
|
||||||
*/
|
|
||||||
export default new Hono<ServerEnv>().get("/", (c) => {
|
|
||||||
// const app = c.var.app;
|
|
||||||
// const api = app.getApi();
|
|
||||||
return c.json({ message: "Hello, world!" });
|
|
||||||
});
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
import { AppEvents, DatabaseEvents, type EventManager } from "bknd";
|
|
||||||
|
|
||||||
export default function (emgr: EventManager) {
|
|
||||||
// emgr.onEvent(AppEvents.AppRequest, async (event) => {
|
|
||||||
// console.log("Request received", event.params.request.url);
|
|
||||||
// });
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import { em, entity, text, boolean, number, datetime, json, jsonSchema, enumm } from "bknd";
|
|
||||||
|
|
||||||
export default em({
|
|
||||||
// todos: entity("todos", {
|
|
||||||
// title: text(),
|
|
||||||
// done: boolean(),
|
|
||||||
// }),
|
|
||||||
});
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
@import "tailwindcss";
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "./.bknd/tsconfig.json",
|
|
||||||
"include": ["src"]
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
import path from "node:path";
|
|
||||||
import {
|
|
||||||
mkdir,
|
|
||||||
stat,
|
|
||||||
readFile as nodeReadFile,
|
|
||||||
writeFile as nodeWriteFile,
|
|
||||||
} from "node:fs/promises";
|
|
||||||
import { getRootPath } from "cli/utils/sys";
|
|
||||||
|
|
||||||
export const TEMPLATE_PATH = "src/cli/commands/dev/template";
|
|
||||||
export const currentDir = process.cwd();
|
|
||||||
|
|
||||||
export const fs = (dir: string) => {
|
|
||||||
const PATH = path.resolve(getRootPath(), dir);
|
|
||||||
return {
|
|
||||||
PATH,
|
|
||||||
path: (_path: string) => path.join(PATH, _path),
|
|
||||||
hasFile: async (file: string) => {
|
|
||||||
try {
|
|
||||||
const s = await stat(path.join(PATH, file));
|
|
||||||
return s.isFile();
|
|
||||||
} catch (_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
hasDir: async (_dir: string) => {
|
|
||||||
try {
|
|
||||||
const s = await stat(path.join(PATH, _dir));
|
|
||||||
return s.isDirectory();
|
|
||||||
} catch (_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
readFile: (file: string) => nodeReadFile(path.join(PATH, file), "utf-8"),
|
|
||||||
readJsonFile: async (file: string) =>
|
|
||||||
JSON.parse(await nodeReadFile(path.join(PATH, file), "utf-8")),
|
|
||||||
writeFile: (file: string, content: string) => nodeWriteFile(path.join(PATH, file), content),
|
|
||||||
makeDir: (_newDir: string) => mkdir(path.join(PATH, _newDir)),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@@ -9,4 +9,3 @@ export { types } from "./types";
|
|||||||
export { mcp } from "./mcp/mcp";
|
export { mcp } from "./mcp/mcp";
|
||||||
export { sync } from "./sync";
|
export { sync } from "./sync";
|
||||||
export { secrets } from "./secrets";
|
export { secrets } from "./secrets";
|
||||||
export { dev } from "./dev/dev";
|
|
||||||
|
|||||||
@@ -67,10 +67,7 @@ export async function startServer(
|
|||||||
$console.info("Server listening on", url);
|
$console.info("Server listening on", url);
|
||||||
|
|
||||||
if (options.open) {
|
if (options.open) {
|
||||||
const p = await open(url, { wait: false });
|
await open(url);
|
||||||
p.on("error", () => {
|
|
||||||
$console.warn("Couldn't open url in browser");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
import color from "picocolors";
|
import color from "picocolors";
|
||||||
import * as commands from "./commands";
|
import * as commands from "./commands";
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
import { describe, it, expect } from "bun:test";
|
|
||||||
import { plunkEmail } from "./plunk";
|
|
||||||
|
|
||||||
const ALL_TESTS = !!process.env.ALL_TESTS;
|
|
||||||
|
|
||||||
describe.skipIf(ALL_TESTS)("plunk", () => {
|
|
||||||
it("should throw on failed", async () => {
|
|
||||||
const driver = plunkEmail({ apiKey: "invalid" });
|
|
||||||
expect(driver.send("foo@bar.com", "Test", "Test")).rejects.toThrow();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should send an email", async () => {
|
|
||||||
const driver = plunkEmail({
|
|
||||||
apiKey: process.env.PLUNK_API_KEY!,
|
|
||||||
from: undefined, // Default to what Plunk sets
|
|
||||||
});
|
|
||||||
const response = await driver.send(
|
|
||||||
"help@bknd.io",
|
|
||||||
"Test Email from Plunk",
|
|
||||||
"This is a test email",
|
|
||||||
);
|
|
||||||
expect(response).toBeDefined();
|
|
||||||
expect(response.success).toBe(true);
|
|
||||||
expect(response.emails).toBeDefined();
|
|
||||||
expect(response.timestamp).toBeDefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should send HTML email", async () => {
|
|
||||||
const driver = plunkEmail({
|
|
||||||
apiKey: process.env.PLUNK_API_KEY!,
|
|
||||||
from: undefined,
|
|
||||||
});
|
|
||||||
const htmlBody = "<h1>Test Email</h1><p>This is a test email</p>";
|
|
||||||
const response = await driver.send(
|
|
||||||
"help@bknd.io",
|
|
||||||
"HTML Test",
|
|
||||||
htmlBody,
|
|
||||||
);
|
|
||||||
expect(response).toBeDefined();
|
|
||||||
expect(response.success).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should send with text and html", async () => {
|
|
||||||
const driver = plunkEmail({
|
|
||||||
apiKey: process.env.PLUNK_API_KEY!,
|
|
||||||
from: undefined,
|
|
||||||
});
|
|
||||||
const response = await driver.send("test@example.com", "Test Email", {
|
|
||||||
text: "help@bknd.io",
|
|
||||||
html: "<p>This is HTML</p>",
|
|
||||||
});
|
|
||||||
expect(response).toBeDefined();
|
|
||||||
expect(response.success).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
import type { IEmailDriver } from "./index";
|
|
||||||
|
|
||||||
export type PlunkEmailOptions = {
|
|
||||||
apiKey: string;
|
|
||||||
host?: string;
|
|
||||||
from?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type PlunkEmailSendOptions = {
|
|
||||||
subscribed?: boolean;
|
|
||||||
name?: string;
|
|
||||||
from?: string;
|
|
||||||
reply?: string;
|
|
||||||
headers?: Record<string, string>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type PlunkEmailResponse = {
|
|
||||||
success: boolean;
|
|
||||||
emails: Array<{
|
|
||||||
contact: {
|
|
||||||
id: string;
|
|
||||||
email: string;
|
|
||||||
};
|
|
||||||
email: string;
|
|
||||||
}>;
|
|
||||||
timestamp: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const plunkEmail = (
|
|
||||||
config: PlunkEmailOptions,
|
|
||||||
): IEmailDriver<PlunkEmailResponse, PlunkEmailSendOptions> => {
|
|
||||||
const host = config.host ?? "https://api.useplunk.com/v1/send";
|
|
||||||
const from = config.from;
|
|
||||||
|
|
||||||
return {
|
|
||||||
send: async (
|
|
||||||
to: string,
|
|
||||||
subject: string,
|
|
||||||
body: string | { text: string; html: string },
|
|
||||||
options?: PlunkEmailSendOptions,
|
|
||||||
) => {
|
|
||||||
const payload: any = {
|
|
||||||
from,
|
|
||||||
to,
|
|
||||||
subject,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (typeof body === "string") {
|
|
||||||
payload.body = body;
|
|
||||||
} else {
|
|
||||||
payload.body = body.html;
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await fetch(host, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: `Bearer ${config.apiKey}`,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ ...payload, ...options }),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error(`Plunk API error: ${await res.text()}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (await res.json()) as PlunkEmailResponse;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@@ -4,7 +4,7 @@ import { resendEmail } from "./resend";
|
|||||||
const ALL_TESTS = !!process.env.ALL_TESTS;
|
const ALL_TESTS = !!process.env.ALL_TESTS;
|
||||||
|
|
||||||
describe.skipIf(ALL_TESTS)("resend", () => {
|
describe.skipIf(ALL_TESTS)("resend", () => {
|
||||||
it("should throw on failed", async () => {
|
it.only("should throw on failed", async () => {
|
||||||
const driver = resendEmail({ apiKey: "invalid" } as any);
|
const driver = resendEmail({ apiKey: "invalid" } as any);
|
||||||
expect(driver.send("foo@bar.com", "Test", "Test")).rejects.toThrow();
|
expect(driver.send("foo@bar.com", "Test", "Test")).rejects.toThrow();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,4 +5,3 @@ export type { IEmailDriver } from "./email";
|
|||||||
export { resendEmail } from "./email/resend";
|
export { resendEmail } from "./email/resend";
|
||||||
export { sesEmail } from "./email/ses";
|
export { sesEmail } from "./email/ses";
|
||||||
export { mailchannelsEmail } from "./email/mailchannels";
|
export { mailchannelsEmail } from "./email/mailchannels";
|
||||||
export { plunkEmail } from "./email/plunk";
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import type { MaybePromise } from "bknd";
|
|
||||||
import type { Event } from "./Event";
|
import type { Event } from "./Event";
|
||||||
import type { EventClass } from "./EventManager";
|
import type { EventClass } from "./EventManager";
|
||||||
|
|
||||||
@@ -8,7 +7,7 @@ export type ListenerMode = (typeof ListenerModes)[number];
|
|||||||
export type ListenerHandler<E extends Event<any, any>> = (
|
export type ListenerHandler<E extends Event<any, any>> = (
|
||||||
event: E,
|
event: E,
|
||||||
slug: string,
|
slug: string,
|
||||||
) => E extends Event<any, infer R> ? MaybePromise<R | void> : never;
|
) => E extends Event<any, infer R> ? R | Promise<R | void> : never;
|
||||||
|
|
||||||
export class EventListener<E extends Event = Event> {
|
export class EventListener<E extends Event = Event> {
|
||||||
mode: ListenerMode = "async";
|
mode: ListenerMode = "async";
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ export function getFlashMessage(
|
|||||||
): { type: FlashMessageType; message: string } | undefined {
|
): { type: FlashMessageType; message: string } | undefined {
|
||||||
const flash = getCookieValue(flash_key);
|
const flash = getCookieValue(flash_key);
|
||||||
if (flash && clear) {
|
if (flash && clear) {
|
||||||
// biome-ignore lint/suspicious/noDocumentCookie: .
|
|
||||||
document.cookie = `${flash_key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
|
document.cookie = `${flash_key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
|
||||||
}
|
}
|
||||||
return flash ? JSON.parse(flash) : undefined;
|
return flash ? JSON.parse(flash) : undefined;
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ export function isObject(value: unknown): value is Record<string, unknown> {
|
|||||||
|
|
||||||
export function omitKeys<T extends object, K extends keyof T>(
|
export function omitKeys<T extends object, K extends keyof T>(
|
||||||
obj: T,
|
obj: T,
|
||||||
keys_: readonly K[] | K[] | string[],
|
keys_: readonly K[],
|
||||||
): Omit<T, Extract<K, keyof T>> {
|
): Omit<T, Extract<K, keyof T>> {
|
||||||
const keys = new Set(keys_ as readonly K[]);
|
const keys = new Set(keys_);
|
||||||
const result = {} as Omit<T, Extract<K, keyof T>>;
|
const result = {} as Omit<T, Extract<K, keyof T>>;
|
||||||
for (const [key, value] of Object.entries(obj) as [keyof T, T[keyof T]][]) {
|
for (const [key, value] of Object.entries(obj) as [keyof T, T[keyof T]][]) {
|
||||||
if (!keys.has(key as K)) {
|
if (!keys.has(key as K)) {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import type { MaybePromise } from "core/types";
|
|
||||||
import { getRuntimeKey as honoGetRuntimeKey } from "hono/adapter";
|
import { getRuntimeKey as honoGetRuntimeKey } from "hono/adapter";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,37 +77,3 @@ export function threw(fn: () => any, instance?: new (...args: any[]) => Error) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function threwAsync(fn: Promise<any>, instance?: new (...args: any[]) => Error) {
|
|
||||||
try {
|
|
||||||
await fn;
|
|
||||||
return false;
|
|
||||||
} catch (e) {
|
|
||||||
if (instance) {
|
|
||||||
if (e instanceof instance) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// if instance given but not what expected, throw
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function $waitUntil(
|
|
||||||
message: string,
|
|
||||||
condition: () => MaybePromise<boolean>,
|
|
||||||
delay = 100,
|
|
||||||
maxAttempts = 10,
|
|
||||||
) {
|
|
||||||
let attempts = 0;
|
|
||||||
while (attempts < maxAttempts) {
|
|
||||||
if (await condition()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
||||||
attempts++;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`$waitUntil: "${message}" failed after ${maxAttempts} attempts`);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -120,14 +120,17 @@ export function patternMatch(target: string, pattern: RegExp | string): boolean
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function slugify(str: string): string {
|
export function slugify(str: string): string {
|
||||||
return String(str)
|
return (
|
||||||
.normalize("NFKD") // split accented characters into their base characters and diacritical marks
|
String(str)
|
||||||
.replace(/[\u0300-\u036f]/g, "") // remove all the accents, which happen to be all in the \u03xx UNICODE block.
|
.normalize("NFKD") // split accented characters into their base characters and diacritical marks
|
||||||
.trim() // trim leading or trailing whitespace
|
// biome-ignore lint/suspicious/noMisleadingCharacterClass: <explanation>
|
||||||
.toLowerCase() // convert to lowercase
|
.replace(/[\u0300-\u036f]/g, "") // remove all the accents, which happen to be all in the \u03xx UNICODE block.
|
||||||
.replace(/[^a-z0-9 -]/g, "") // remove non-alphanumeric characters
|
.trim() // trim leading or trailing whitespace
|
||||||
.replace(/\s+/g, "-") // replace spaces with hyphens
|
.toLowerCase() // convert to lowercase
|
||||||
.replace(/-+/g, "-"); // remove consecutive hyphens
|
.replace(/[^a-z0-9 -]/g, "") // remove non-alphanumeric characters
|
||||||
|
.replace(/\s+/g, "-") // replace spaces with hyphens
|
||||||
|
.replace(/-+/g, "-") // remove consecutive hyphens
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function truncate(str: string, length = 50, end = "..."): string {
|
export function truncate(str: string, length = 50, end = "..."): string {
|
||||||
|
|||||||
@@ -96,9 +96,6 @@ export class DataController extends Controller {
|
|||||||
// read entity schema
|
// read entity schema
|
||||||
hono.get(
|
hono.get(
|
||||||
"/schema.json",
|
"/schema.json",
|
||||||
permission(SystemPermissions.schemaRead, {
|
|
||||||
context: (_c) => ({ module: "data" }),
|
|
||||||
}),
|
|
||||||
permission(DataPermissions.entityRead, {
|
permission(DataPermissions.entityRead, {
|
||||||
context: (c) => ({ entity: c.req.param("entity") }),
|
context: (c) => ({ entity: c.req.param("entity") }),
|
||||||
}),
|
}),
|
||||||
@@ -127,9 +124,6 @@ export class DataController extends Controller {
|
|||||||
// read schema
|
// read schema
|
||||||
hono.get(
|
hono.get(
|
||||||
"/schemas/:entity/:context?",
|
"/schemas/:entity/:context?",
|
||||||
permission(SystemPermissions.schemaRead, {
|
|
||||||
context: (_c) => ({ module: "data" }),
|
|
||||||
}),
|
|
||||||
permission(DataPermissions.entityRead, {
|
permission(DataPermissions.entityRead, {
|
||||||
context: (c) => ({ entity: c.req.param("entity") }),
|
context: (c) => ({ entity: c.req.param("entity") }),
|
||||||
}),
|
}),
|
||||||
@@ -167,7 +161,7 @@ export class DataController extends Controller {
|
|||||||
hono.get(
|
hono.get(
|
||||||
"/types",
|
"/types",
|
||||||
permission(SystemPermissions.schemaRead, {
|
permission(SystemPermissions.schemaRead, {
|
||||||
context: (_c) => ({ module: "data" }),
|
context: (c) => ({ module: "data" }),
|
||||||
}),
|
}),
|
||||||
describeRoute({
|
describeRoute({
|
||||||
summary: "Retrieve data typescript definitions",
|
summary: "Retrieve data typescript definitions",
|
||||||
@@ -188,9 +182,6 @@ export class DataController extends Controller {
|
|||||||
*/
|
*/
|
||||||
hono.get(
|
hono.get(
|
||||||
"/info/:entity",
|
"/info/:entity",
|
||||||
permission(SystemPermissions.schemaRead, {
|
|
||||||
context: (_c) => ({ module: "data" }),
|
|
||||||
}),
|
|
||||||
permission(DataPermissions.entityRead, {
|
permission(DataPermissions.entityRead, {
|
||||||
context: (c) => ({ entity: c.req.param("entity") }),
|
context: (c) => ({ entity: c.req.param("entity") }),
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -6,15 +6,17 @@ import {
|
|||||||
type CompiledQuery,
|
type CompiledQuery,
|
||||||
type DatabaseIntrospector,
|
type DatabaseIntrospector,
|
||||||
type Dialect,
|
type Dialect,
|
||||||
|
type Expression,
|
||||||
type Kysely,
|
type Kysely,
|
||||||
type KyselyPlugin,
|
type KyselyPlugin,
|
||||||
type OnModifyForeignAction,
|
type OnModifyForeignAction,
|
||||||
type QueryResult,
|
type QueryResult,
|
||||||
|
type RawBuilder,
|
||||||
type SelectQueryBuilder,
|
type SelectQueryBuilder,
|
||||||
type SelectQueryNode,
|
type SelectQueryNode,
|
||||||
|
type Simplify,
|
||||||
sql,
|
sql,
|
||||||
} from "kysely";
|
} from "kysely";
|
||||||
import type { jsonArrayFrom, jsonBuildObject, jsonObjectFrom } from "kysely/helpers/sqlite";
|
|
||||||
import type { BaseIntrospector, BaseIntrospectorConfig } from "./BaseIntrospector";
|
import type { BaseIntrospector, BaseIntrospectorConfig } from "./BaseIntrospector";
|
||||||
import type { DB } from "bknd";
|
import type { DB } from "bknd";
|
||||||
import type { Constructor } from "core/registry/Registry";
|
import type { Constructor } from "core/registry/Registry";
|
||||||
@@ -68,9 +70,15 @@ export type IndexSpec = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type DbFunctions = {
|
export type DbFunctions = {
|
||||||
jsonObjectFrom: typeof jsonObjectFrom;
|
jsonObjectFrom<O>(expr: SelectQueryBuilderExpression<O>): RawBuilder<Simplify<O> | null>;
|
||||||
jsonArrayFrom: typeof jsonArrayFrom;
|
jsonArrayFrom<O>(expr: SelectQueryBuilderExpression<O>): RawBuilder<Simplify<O>[]>;
|
||||||
jsonBuildObject: typeof jsonBuildObject;
|
jsonBuildObject<O extends Record<string, Expression<unknown>>>(
|
||||||
|
obj: O,
|
||||||
|
): RawBuilder<
|
||||||
|
Simplify<{
|
||||||
|
[K in keyof O]: O[K] extends Expression<infer V> ? V : never;
|
||||||
|
}>
|
||||||
|
>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ConnQuery = CompiledQuery | Compilable;
|
export type ConnQuery = CompiledQuery | Compilable;
|
||||||
|
|||||||
@@ -14,31 +14,27 @@ export function connectionTestSuite(
|
|||||||
{
|
{
|
||||||
makeConnection,
|
makeConnection,
|
||||||
rawDialectDetails,
|
rawDialectDetails,
|
||||||
disableConsoleLog: _disableConsoleLog = true,
|
|
||||||
}: {
|
}: {
|
||||||
makeConnection: () => MaybePromise<{
|
makeConnection: () => MaybePromise<{
|
||||||
connection: Connection;
|
connection: Connection;
|
||||||
dispose: () => MaybePromise<void>;
|
dispose: () => MaybePromise<void>;
|
||||||
}>;
|
}>;
|
||||||
rawDialectDetails: string[];
|
rawDialectDetails: string[];
|
||||||
disableConsoleLog?: boolean;
|
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const { test, expect, describe, beforeEach, afterEach, afterAll, beforeAll } = testRunner;
|
const { test, expect, describe, beforeEach, afterEach, afterAll, beforeAll } = testRunner;
|
||||||
if (_disableConsoleLog) {
|
beforeAll(() => disableConsoleLog());
|
||||||
beforeAll(() => disableConsoleLog());
|
afterAll(() => enableConsoleLog());
|
||||||
afterAll(() => enableConsoleLog());
|
|
||||||
}
|
|
||||||
|
|
||||||
let ctx: Awaited<ReturnType<typeof makeConnection>>;
|
describe("base", () => {
|
||||||
beforeEach(async () => {
|
let ctx: Awaited<ReturnType<typeof makeConnection>>;
|
||||||
ctx = await makeConnection();
|
beforeEach(async () => {
|
||||||
});
|
ctx = await makeConnection();
|
||||||
afterEach(async () => {
|
});
|
||||||
await ctx.dispose();
|
afterEach(async () => {
|
||||||
});
|
await ctx.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
describe("base", async () => {
|
|
||||||
test("pings", async () => {
|
test("pings", async () => {
|
||||||
const res = await ctx.connection.ping();
|
const res = await ctx.connection.ping();
|
||||||
expect(res).toBe(true);
|
expect(res).toBe(true);
|
||||||
@@ -102,54 +98,52 @@ export function connectionTestSuite(
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("schema", async () => {
|
describe("schema", async () => {
|
||||||
const makeSchema = async () => {
|
const { connection, dispose } = await makeConnection();
|
||||||
const fields = [
|
afterAll(async () => {
|
||||||
{
|
await dispose();
|
||||||
type: "integer",
|
});
|
||||||
name: "id",
|
|
||||||
primary: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
name: "text",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "json",
|
|
||||||
name: "json",
|
|
||||||
},
|
|
||||||
] as const satisfies FieldSpec[];
|
|
||||||
|
|
||||||
let b = ctx.connection.kysely.schema.createTable("test");
|
const fields = [
|
||||||
for (const field of fields) {
|
{
|
||||||
// @ts-expect-error
|
type: "integer",
|
||||||
b = b.addColumn(...ctx.connection.getFieldSchema(field));
|
name: "id",
|
||||||
}
|
primary: true,
|
||||||
await b.execute();
|
},
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
name: "text",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "json",
|
||||||
|
name: "json",
|
||||||
|
},
|
||||||
|
] as const satisfies FieldSpec[];
|
||||||
|
|
||||||
// add index
|
let b = connection.kysely.schema.createTable("test");
|
||||||
await ctx.connection.kysely.schema
|
for (const field of fields) {
|
||||||
.createIndex("test_index")
|
// @ts-expect-error
|
||||||
.on("test")
|
b = b.addColumn(...connection.getFieldSchema(field));
|
||||||
.columns(["id"])
|
}
|
||||||
.execute();
|
await b.execute();
|
||||||
};
|
|
||||||
|
// add index
|
||||||
|
await connection.kysely.schema.createIndex("test_index").on("test").columns(["id"]).execute();
|
||||||
|
|
||||||
test("executes query", async () => {
|
test("executes query", async () => {
|
||||||
await makeSchema();
|
await connection.kysely
|
||||||
await ctx.connection.kysely
|
|
||||||
.insertInto("test")
|
.insertInto("test")
|
||||||
.values({ id: 1, text: "test", json: JSON.stringify({ a: 1 }) })
|
.values({ id: 1, text: "test", json: JSON.stringify({ a: 1 }) })
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
const expected = { id: 1, text: "test", json: { a: 1 } };
|
const expected = { id: 1, text: "test", json: { a: 1 } };
|
||||||
|
|
||||||
const qb = ctx.connection.kysely.selectFrom("test").selectAll();
|
const qb = connection.kysely.selectFrom("test").selectAll();
|
||||||
const res = await ctx.connection.executeQuery(qb);
|
const res = await connection.executeQuery(qb);
|
||||||
expect(res.rows).toEqual([expected]);
|
expect(res.rows).toEqual([expected]);
|
||||||
expect(rawDialectDetails.every((detail) => getPath(res, detail) !== undefined)).toBe(true);
|
expect(rawDialectDetails.every((detail) => getPath(res, detail) !== undefined)).toBe(true);
|
||||||
|
|
||||||
{
|
{
|
||||||
const res = await ctx.connection.executeQueries(qb, qb);
|
const res = await connection.executeQueries(qb, qb);
|
||||||
expect(res.length).toBe(2);
|
expect(res.length).toBe(2);
|
||||||
res.map((r) => {
|
res.map((r) => {
|
||||||
expect(r.rows).toEqual([expected]);
|
expect(r.rows).toEqual([expected]);
|
||||||
@@ -161,21 +155,15 @@ export function connectionTestSuite(
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("introspects", async () => {
|
test("introspects", async () => {
|
||||||
await makeSchema();
|
const tables = await connection.getIntrospector().getTables({
|
||||||
const tables = await ctx.connection.getIntrospector().getTables({
|
|
||||||
withInternalKyselyTables: false,
|
withInternalKyselyTables: false,
|
||||||
});
|
});
|
||||||
const clean = tables.map((t) => ({
|
const clean = tables.map((t) => ({
|
||||||
...t,
|
...t,
|
||||||
columns: t.columns
|
columns: t.columns.map((c) => ({
|
||||||
.map((c) => ({
|
...c,
|
||||||
...c,
|
dataType: undefined,
|
||||||
// ignore data type
|
})),
|
||||||
dataType: undefined,
|
|
||||||
// ignore default value if "id"
|
|
||||||
hasDefaultValue: c.name !== "id" ? c.hasDefaultValue : undefined,
|
|
||||||
}))
|
|
||||||
.sort((a, b) => a.name.localeCompare(b.name)),
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
expect(clean).toEqual([
|
expect(clean).toEqual([
|
||||||
@@ -188,16 +176,7 @@ export function connectionTestSuite(
|
|||||||
dataType: undefined,
|
dataType: undefined,
|
||||||
isNullable: false,
|
isNullable: false,
|
||||||
isAutoIncrementing: true,
|
isAutoIncrementing: true,
|
||||||
hasDefaultValue: undefined,
|
|
||||||
comment: undefined,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "json",
|
|
||||||
dataType: undefined,
|
|
||||||
isNullable: true,
|
|
||||||
isAutoIncrementing: false,
|
|
||||||
hasDefaultValue: false,
|
hasDefaultValue: false,
|
||||||
comment: undefined,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "text",
|
name: "text",
|
||||||
@@ -205,26 +184,32 @@ export function connectionTestSuite(
|
|||||||
isNullable: true,
|
isNullable: true,
|
||||||
isAutoIncrementing: false,
|
isAutoIncrementing: false,
|
||||||
hasDefaultValue: false,
|
hasDefaultValue: false,
|
||||||
comment: undefined,
|
|
||||||
},
|
},
|
||||||
],
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect(await ctx.connection.getIntrospector().getIndices()).toEqual([
|
|
||||||
{
|
|
||||||
name: "test_index",
|
|
||||||
table: "test",
|
|
||||||
isUnique: false,
|
|
||||||
columns: [
|
|
||||||
{
|
{
|
||||||
name: "id",
|
name: "json",
|
||||||
order: 0,
|
dataType: undefined,
|
||||||
|
isNullable: true,
|
||||||
|
isAutoIncrementing: false,
|
||||||
|
hasDefaultValue: false,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
expect(await connection.getIntrospector().getIndices()).toEqual([
|
||||||
|
{
|
||||||
|
name: "test_index",
|
||||||
|
table: "test",
|
||||||
|
isUnique: false,
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
name: "id",
|
||||||
|
order: 0,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("integration", async () => {
|
describe("integration", async () => {
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
import { Kysely, PostgresDialect, type PostgresDialectConfig as KyselyPostgresDialectConfig } from "kysely";
|
|
||||||
import { PostgresIntrospector } from "./PostgresIntrospector";
|
|
||||||
import { PostgresConnection, plugins } from "./PostgresConnection";
|
|
||||||
import { customIntrospector } from "../Connection";
|
|
||||||
import type { Pool } from "pg";
|
|
||||||
|
|
||||||
export type PostgresDialectConfig = Omit<KyselyPostgresDialectConfig, "pool"> & {
|
|
||||||
pool: Pool;
|
|
||||||
};
|
|
||||||
|
|
||||||
export class PgPostgresConnection extends PostgresConnection<Pool> {
|
|
||||||
override name = "pg";
|
|
||||||
|
|
||||||
constructor(config: PostgresDialectConfig) {
|
|
||||||
const kysely = new Kysely({
|
|
||||||
dialect: customIntrospector(PostgresDialect, PostgresIntrospector, {
|
|
||||||
excludeTables: [],
|
|
||||||
}).create(config),
|
|
||||||
plugins,
|
|
||||||
});
|
|
||||||
|
|
||||||
super(kysely);
|
|
||||||
this.client = config.pool;
|
|
||||||
}
|
|
||||||
|
|
||||||
override async close(): Promise<void> {
|
|
||||||
await this.client.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function pg(config: PostgresDialectConfig): PgPostgresConnection {
|
|
||||||
return new PgPostgresConnection(config);
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
import { Kysely } from "kysely";
|
|
||||||
import { PostgresIntrospector } from "./PostgresIntrospector";
|
|
||||||
import { PostgresConnection, plugins } from "./PostgresConnection";
|
|
||||||
import { customIntrospector } from "../Connection";
|
|
||||||
import { PostgresJSDialect, type PostgresJSDialectConfig } from "kysely-postgres-js";
|
|
||||||
|
|
||||||
export class PostgresJsConnection extends PostgresConnection<PostgresJSDialectConfig["postgres"]> {
|
|
||||||
override name = "postgres-js";
|
|
||||||
|
|
||||||
constructor(config: PostgresJSDialectConfig) {
|
|
||||||
const kysely = new Kysely({
|
|
||||||
dialect: customIntrospector(PostgresJSDialect, PostgresIntrospector, {
|
|
||||||
excludeTables: [],
|
|
||||||
}).create(config),
|
|
||||||
plugins,
|
|
||||||
});
|
|
||||||
|
|
||||||
super(kysely);
|
|
||||||
this.client = config.postgres;
|
|
||||||
}
|
|
||||||
|
|
||||||
override async close(): Promise<void> {
|
|
||||||
await this.client.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function postgresJs(
|
|
||||||
config: PostgresJSDialectConfig,
|
|
||||||
): PostgresJsConnection {
|
|
||||||
return new PostgresJsConnection(config);
|
|
||||||
}
|
|
||||||
@@ -13,43 +13,31 @@ import { customIntrospector } from "../Connection";
|
|||||||
import { SqliteIntrospector } from "./SqliteIntrospector";
|
import { SqliteIntrospector } from "./SqliteIntrospector";
|
||||||
import type { Field } from "data/fields/Field";
|
import type { Field } from "data/fields/Field";
|
||||||
|
|
||||||
|
// @todo: add pragmas
|
||||||
export type SqliteConnectionConfig<
|
export type SqliteConnectionConfig<
|
||||||
CustomDialect extends Constructor<Dialect> = Constructor<Dialect>,
|
CustomDialect extends Constructor<Dialect> = Constructor<Dialect>,
|
||||||
> = {
|
> = {
|
||||||
excludeTables?: string[];
|
excludeTables?: string[];
|
||||||
|
dialect: CustomDialect;
|
||||||
|
dialectArgs?: ConstructorParameters<CustomDialect>;
|
||||||
additionalPlugins?: KyselyPlugin[];
|
additionalPlugins?: KyselyPlugin[];
|
||||||
customFn?: Partial<DbFunctions>;
|
customFn?: Partial<DbFunctions>;
|
||||||
} & (
|
};
|
||||||
| {
|
|
||||||
dialect: CustomDialect;
|
|
||||||
dialectArgs?: ConstructorParameters<CustomDialect>;
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
kysely: Kysely<any>;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
export abstract class SqliteConnection<Client = unknown> extends Connection<Client> {
|
export abstract class SqliteConnection<Client = unknown> extends Connection<Client> {
|
||||||
override name = "sqlite";
|
override name = "sqlite";
|
||||||
|
|
||||||
constructor(config: SqliteConnectionConfig) {
|
constructor(config: SqliteConnectionConfig) {
|
||||||
const { excludeTables, additionalPlugins } = config;
|
const { excludeTables, dialect, dialectArgs = [], additionalPlugins } = config;
|
||||||
const plugins = [new ParseJSONResultsPlugin(), ...(additionalPlugins ?? [])];
|
const plugins = [new ParseJSONResultsPlugin(), ...(additionalPlugins ?? [])];
|
||||||
|
|
||||||
let kysely: Kysely<any>;
|
const kysely = new Kysely({
|
||||||
if ("dialect" in config) {
|
dialect: customIntrospector(dialect, SqliteIntrospector, {
|
||||||
kysely = new Kysely({
|
excludeTables,
|
||||||
dialect: customIntrospector(config.dialect, SqliteIntrospector, {
|
|
||||||
excludeTables,
|
|
||||||
plugins,
|
|
||||||
}).create(...(config.dialectArgs ?? [])),
|
|
||||||
plugins,
|
plugins,
|
||||||
});
|
}).create(...dialectArgs),
|
||||||
} else if ("kysely" in config) {
|
plugins,
|
||||||
kysely = config.kysely;
|
});
|
||||||
} else {
|
|
||||||
throw new Error("Either dialect or kysely must be provided");
|
|
||||||
}
|
|
||||||
|
|
||||||
super(
|
super(
|
||||||
kysely,
|
kysely,
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ export class SqliteIntrospector extends BaseIntrospector {
|
|||||||
dataType: col.type,
|
dataType: col.type,
|
||||||
isNullable: !col.notnull,
|
isNullable: !col.notnull,
|
||||||
isAutoIncrementing: col.name === autoIncrementCol,
|
isAutoIncrementing: col.name === autoIncrementCol,
|
||||||
hasDefaultValue: col.name === autoIncrementCol ? true : col.dflt_value != null,
|
hasDefaultValue: col.dflt_value != null,
|
||||||
comment: undefined,
|
comment: undefined,
|
||||||
};
|
};
|
||||||
}) ?? [],
|
}) ?? [],
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export type LibsqlClientFns = {
|
|||||||
function getClient(clientOrCredentials: Client | LibSqlCredentials | LibsqlClientFns): Client {
|
function getClient(clientOrCredentials: Client | LibSqlCredentials | LibsqlClientFns): Client {
|
||||||
if (clientOrCredentials && "url" in clientOrCredentials) {
|
if (clientOrCredentials && "url" in clientOrCredentials) {
|
||||||
const { url, authToken } = clientOrCredentials;
|
const { url, authToken } = clientOrCredentials;
|
||||||
return createClient({ url, authToken }) as unknown as Client;
|
return createClient({ url, authToken });
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientOrCredentials as Client;
|
return clientOrCredentials as Client;
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
import { describe } from "bun:test";
|
|
||||||
import { SQLocalConnection } from "./SQLocalConnection";
|
|
||||||
import { connectionTestSuite } from "data/connection/connection-test-suite";
|
|
||||||
import { bunTestRunner } from "adapter/bun/test";
|
|
||||||
import { SQLocalKysely } from "sqlocal/kysely";
|
|
||||||
|
|
||||||
describe("SQLocalConnection", () => {
|
|
||||||
connectionTestSuite(bunTestRunner, {
|
|
||||||
makeConnection: () => ({
|
|
||||||
connection: new SQLocalConnection(new SQLocalKysely({ databasePath: ":memory:" })),
|
|
||||||
dispose: async () => {},
|
|
||||||
}),
|
|
||||||
rawDialectDetails: [],
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
import { Kysely, ParseJSONResultsPlugin } from "kysely";
|
|
||||||
import { SqliteConnection } from "../SqliteConnection";
|
|
||||||
import { SqliteIntrospector } from "../SqliteIntrospector";
|
|
||||||
import type { DB } from "bknd";
|
|
||||||
import type { SQLocalKysely } from "sqlocal/kysely";
|
|
||||||
|
|
||||||
const plugins = [new ParseJSONResultsPlugin()];
|
|
||||||
|
|
||||||
export class SQLocalConnection extends SqliteConnection<SQLocalKysely> {
|
|
||||||
private connected: boolean = false;
|
|
||||||
|
|
||||||
constructor(client: SQLocalKysely) {
|
|
||||||
// @ts-expect-error - config is protected
|
|
||||||
client.config.onConnect = () => {
|
|
||||||
// we need to listen for the connection, it will be awaited in init()
|
|
||||||
this.connected = true;
|
|
||||||
};
|
|
||||||
super({
|
|
||||||
kysely: new Kysely<any>({
|
|
||||||
dialect: {
|
|
||||||
...client.dialect,
|
|
||||||
createIntrospector: (db: Kysely<DB>) => {
|
|
||||||
return new SqliteIntrospector(db as any, {
|
|
||||||
plugins,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
plugins,
|
|
||||||
}) as any,
|
|
||||||
});
|
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
override async init() {
|
|
||||||
if (this.initialized) return;
|
|
||||||
let tries = 0;
|
|
||||||
while (!this.connected && tries < 100) {
|
|
||||||
tries++;
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
||||||
}
|
|
||||||
if (!this.connected) {
|
|
||||||
throw new Error("Failed to connect to SQLite database");
|
|
||||||
}
|
|
||||||
this.initialized = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function sqlocal(instance: InstanceType<typeof SQLocalKysely>): SQLocalConnection {
|
|
||||||
return new SQLocalConnection(instance);
|
|
||||||
}
|
|
||||||
@@ -136,8 +136,10 @@ export class Entity<
|
|||||||
.map((field) => (alias ? `${alias}.${field.name} as ${field.name}` : field.name));
|
.map((field) => (alias ? `${alias}.${field.name} as ${field.name}` : field.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
getFillableFields(context?: TActionContext, include_virtual?: boolean): Field[] {
|
getFillableFields(context?: "create" | "update", include_virtual?: boolean): Field[] {
|
||||||
return this.getFields(include_virtual).filter((field) => field.isFillable(context));
|
return this.getFields({ virtual: include_virtual }).filter((field) =>
|
||||||
|
field.isFillable(context),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getRequiredFields(): Field[] {
|
getRequiredFields(): Field[] {
|
||||||
@@ -189,9 +191,15 @@ export class Entity<
|
|||||||
return this.fields.findIndex((field) => field.name === name) !== -1;
|
return this.fields.findIndex((field) => field.name === name) !== -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
getFields(include_virtual: boolean = false): Field[] {
|
getFields({
|
||||||
if (include_virtual) return this.fields;
|
virtual = false,
|
||||||
return this.fields.filter((f) => !f.isVirtual());
|
primary = true,
|
||||||
|
}: { virtual?: boolean; primary?: boolean } = {}): Field[] {
|
||||||
|
return this.fields.filter((f) => {
|
||||||
|
if (!virtual && f.isVirtual()) return false;
|
||||||
|
if (!primary && f instanceof PrimaryField) return false;
|
||||||
|
return true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
addField(field: Field) {
|
addField(field: Field) {
|
||||||
@@ -231,7 +239,7 @@ export class Entity<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fields = this.getFillableFields(context, false);
|
const fields = this.getFillableFields(context as any, false);
|
||||||
|
|
||||||
if (options?.ignoreUnknown !== true) {
|
if (options?.ignoreUnknown !== true) {
|
||||||
const field_names = fields.map((f) => f.name);
|
const field_names = fields.map((f) => f.name);
|
||||||
@@ -275,7 +283,7 @@ export class Entity<
|
|||||||
fields = this.getFillableFields(options.context);
|
fields = this.getFillableFields(options.context);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fields = this.getFields(true);
|
fields = this.getFields({ virtual: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
const _fields = Object.fromEntries(fields.map((field) => [field.name, field]));
|
const _fields = Object.fromEntries(fields.map((field) => [field.name, field]));
|
||||||
|
|||||||
@@ -83,8 +83,10 @@ export class Mutator<
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we should never get here, but just to be sure (why?)
|
// we should never get here, but just to be sure (why?)
|
||||||
if (!field.isFillable(context)) {
|
if (!field.isFillable(context as any)) {
|
||||||
throw new Error(`Field "${key}" is not fillable on entity "${entity.name}"`);
|
throw new Error(
|
||||||
|
`Field "${key}" of entity "${entity.name}" is not fillable on context "${context}"`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// transform from field
|
// transform from field
|
||||||
|
|||||||
@@ -103,7 +103,6 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
|||||||
validated.with = options.with;
|
validated.with = options.with;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add explicit joins. Implicit joins are added in `where` builder
|
|
||||||
if (options.join && options.join.length > 0) {
|
if (options.join && options.join.length > 0) {
|
||||||
for (const entry of options.join) {
|
for (const entry of options.join) {
|
||||||
const related = this.em.relationOf(entity.name, entry);
|
const related = this.em.relationOf(entity.name, entry);
|
||||||
@@ -128,28 +127,12 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
|||||||
const invalid = WhereBuilder.getPropertyNames(options.where).filter((field) => {
|
const invalid = WhereBuilder.getPropertyNames(options.where).filter((field) => {
|
||||||
if (field.includes(".")) {
|
if (field.includes(".")) {
|
||||||
const [alias, prop] = field.split(".") as [string, string];
|
const [alias, prop] = field.split(".") as [string, string];
|
||||||
// check aliases first (added joins)
|
if (!aliases.includes(alias)) {
|
||||||
if (aliases.includes(alias)) {
|
|
||||||
this.checkIndex(alias, prop, "where");
|
|
||||||
return !this.em.entity(alias).getField(prop);
|
|
||||||
}
|
|
||||||
// check if alias (entity) exists
|
|
||||||
if (!this.em.hasEntity(alias)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// check related fields for auto join
|
|
||||||
const related = this.em.relationOf(entity.name, alias);
|
|
||||||
if (related) {
|
|
||||||
const other = related.other(entity);
|
|
||||||
if (other.entity.getField(prop)) {
|
|
||||||
// if related field is found, add join to validated options
|
|
||||||
validated.join?.push(alias);
|
|
||||||
this.checkIndex(alias, prop, "where");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
this.checkIndex(alias, prop, "where");
|
||||||
|
return !this.em.entity(alias).getField(prop);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.checkIndex(entity.name, field, "where");
|
this.checkIndex(entity.name, field, "where");
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import type { KyselyJsonFrom } from "data/relations/EntityRelation";
|
|||||||
import type { RepoQuery } from "data/server/query";
|
import type { RepoQuery } from "data/server/query";
|
||||||
import { InvalidSearchParamsException } from "data/errors";
|
import { InvalidSearchParamsException } from "data/errors";
|
||||||
import type { Entity, EntityManager, RepositoryQB } from "data/entities";
|
import type { Entity, EntityManager, RepositoryQB } from "data/entities";
|
||||||
import { $console } from "bknd/utils";
|
|
||||||
|
|
||||||
export class WithBuilder {
|
export class WithBuilder {
|
||||||
static addClause(
|
static addClause(
|
||||||
@@ -14,7 +13,7 @@ export class WithBuilder {
|
|||||||
withs: RepoQuery["with"],
|
withs: RepoQuery["with"],
|
||||||
) {
|
) {
|
||||||
if (!withs || !isObject(withs)) {
|
if (!withs || !isObject(withs)) {
|
||||||
$console.warn(`'withs' undefined or invalid, given: ${JSON.stringify(withs)}`);
|
console.warn(`'withs' undefined or invalid, given: ${JSON.stringify(withs)}`);
|
||||||
return qb;
|
return qb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +37,9 @@ export class WithBuilder {
|
|||||||
let subQuery = relation.buildWith(entity, ref)(eb);
|
let subQuery = relation.buildWith(entity, ref)(eb);
|
||||||
if (query) {
|
if (query) {
|
||||||
subQuery = em.repo(other.entity).addOptionsToQueryBuilder(subQuery, query as any, {
|
subQuery = em.repo(other.entity).addOptionsToQueryBuilder(subQuery, query as any, {
|
||||||
ignore: ["with", cardinality === 1 ? "limit" : undefined].filter(Boolean) as any,
|
ignore: ["with", "join", cardinality === 1 ? "limit" : undefined].filter(
|
||||||
|
Boolean,
|
||||||
|
) as any,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ export class WithBuilder {
|
|||||||
static validateWiths(em: EntityManager<any>, entity: string, withs: RepoQuery["with"]) {
|
static validateWiths(em: EntityManager<any>, entity: string, withs: RepoQuery["with"]) {
|
||||||
let depth = 0;
|
let depth = 0;
|
||||||
if (!withs || !isObject(withs)) {
|
if (!withs || !isObject(withs)) {
|
||||||
withs && $console.warn(`'withs' invalid, given: ${JSON.stringify(withs)}`);
|
withs && console.warn(`'withs' invalid, given: ${JSON.stringify(withs)}`);
|
||||||
return depth;
|
return depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,11 +26,19 @@ export const baseFieldConfigSchema = s
|
|||||||
.strictObject({
|
.strictObject({
|
||||||
label: s.string(),
|
label: s.string(),
|
||||||
description: s.string(),
|
description: s.string(),
|
||||||
required: s.boolean({ default: false }),
|
required: s.boolean({ default: DEFAULT_REQUIRED }),
|
||||||
fillable: s.anyOf([
|
fillable: s.anyOf(
|
||||||
s.boolean({ title: "Boolean" }),
|
[
|
||||||
s.array(s.string({ enum: ActionContext }), { title: "Context", uniqueItems: true }),
|
s.boolean({ title: "Boolean" }),
|
||||||
]),
|
s.array(s.string({ enum: ["create", "update"] }), {
|
||||||
|
title: "Context",
|
||||||
|
uniqueItems: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
{
|
||||||
|
default: DEFAULT_FILLABLE,
|
||||||
|
},
|
||||||
|
),
|
||||||
hidden: s.anyOf([
|
hidden: s.anyOf([
|
||||||
s.boolean({ title: "Boolean" }),
|
s.boolean({ title: "Boolean" }),
|
||||||
// @todo: tmp workaround
|
// @todo: tmp workaround
|
||||||
@@ -103,7 +111,7 @@ export abstract class Field<
|
|||||||
return this.config?.default_value;
|
return this.config?.default_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
isFillable(context?: TActionContext): boolean {
|
isFillable(context?: "create" | "update"): boolean {
|
||||||
if (Array.isArray(this.config.fillable)) {
|
if (Array.isArray(this.config.fillable)) {
|
||||||
return context ? this.config.fillable.includes(context) : DEFAULT_FILLABLE;
|
return context ? this.config.fillable.includes(context) : DEFAULT_FILLABLE;
|
||||||
}
|
}
|
||||||
@@ -165,7 +173,7 @@ export abstract class Field<
|
|||||||
// @todo: add field level validation
|
// @todo: add field level validation
|
||||||
isValid(value: any, context: TActionContext): boolean {
|
isValid(value: any, context: TActionContext): boolean {
|
||||||
if (typeof value !== "undefined") {
|
if (typeof value !== "undefined") {
|
||||||
return this.isFillable(context);
|
return this.isFillable(context as any);
|
||||||
} else if (context === "create") {
|
} else if (context === "create") {
|
||||||
return !this.isRequired();
|
return !this.isRequired();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,12 +26,7 @@ export class JsonSchemaField<
|
|||||||
|
|
||||||
constructor(name: string, config: Partial<JsonSchemaFieldConfig>) {
|
constructor(name: string, config: Partial<JsonSchemaFieldConfig>) {
|
||||||
super(name, config);
|
super(name, config);
|
||||||
|
this.validator = new Validator({ ...this.getJsonSchema() });
|
||||||
// make sure to hand over clean json
|
|
||||||
const schema = this.getJsonSchema();
|
|
||||||
this.validator = new Validator(
|
|
||||||
typeof schema === "object" ? JSON.parse(JSON.stringify(schema)) : {},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getSchema() {
|
protected getSchema() {
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ export class NumberField<Required extends true | false = false> extends Field<
|
|||||||
|
|
||||||
switch (context) {
|
switch (context) {
|
||||||
case "submit":
|
case "submit":
|
||||||
return Number.parseInt(value, 10);
|
return Number.parseInt(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ export function fieldTestSuite(
|
|||||||
const _config = {
|
const _config = {
|
||||||
..._requiredConfig,
|
..._requiredConfig,
|
||||||
required: false,
|
required: false,
|
||||||
|
fillable: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
function fieldJson(field: Field) {
|
function fieldJson(field: Field) {
|
||||||
@@ -116,10 +117,7 @@ export function fieldTestSuite(
|
|||||||
|
|
||||||
expect(fieldJson(fillable)).toEqual({
|
expect(fieldJson(fillable)).toEqual({
|
||||||
type: noConfigField.type,
|
type: noConfigField.type,
|
||||||
config: {
|
config: _config,
|
||||||
..._config,
|
|
||||||
fillable: true,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(fieldJson(required)).toEqual({
|
expect(fieldJson(required)).toEqual({
|
||||||
@@ -150,7 +148,6 @@ export function fieldTestSuite(
|
|||||||
type: requiredAndDefault.type,
|
type: requiredAndDefault.type,
|
||||||
config: {
|
config: {
|
||||||
..._config,
|
..._config,
|
||||||
fillable: true,
|
|
||||||
required: true,
|
required: true,
|
||||||
default_value: config.defaultValue,
|
default_value: config.defaultValue,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export function getChangeSet(
|
|||||||
const value = _value === "" ? null : _value;
|
const value = _value === "" ? null : _value;
|
||||||
|
|
||||||
// normalize to null if undefined
|
// normalize to null if undefined
|
||||||
const newValue = field.getValue(value, "submit") ?? null;
|
const newValue = field.getValue(value, "submit") || null;
|
||||||
// @todo: add typing for "action"
|
// @todo: add typing for "action"
|
||||||
if (action === "create" || newValue !== data[key]) {
|
if (action === "create" || newValue !== data[key]) {
|
||||||
acc[key] = newValue;
|
acc[key] = newValue;
|
||||||
|
|||||||
@@ -289,7 +289,7 @@ class EntityManagerPrototype<Entities extends Record<string, Entity>> extends En
|
|||||||
super(Object.values(__entities), new DummyConnection(), relations, indices);
|
super(Object.values(__entities), new DummyConnection(), relations, indices);
|
||||||
}
|
}
|
||||||
|
|
||||||
withConnection(connection: Connection): EntityManager<Schemas<Entities>> {
|
withConnection(connection: Connection): EntityManager<Schema<Entities>> {
|
||||||
return new EntityManager(this.entities, connection, this.relations.all, this.indices);
|
return new EntityManager(this.entities, connection, this.relations.all, this.indices);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ export class SchemaManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getIntrospectionFromEntity(entity: Entity): IntrospectedTable {
|
getIntrospectionFromEntity(entity: Entity): IntrospectedTable {
|
||||||
const fields = entity.getFields(false);
|
const fields = entity.getFields({ virtual: false });
|
||||||
const indices = this.em.getIndicesOf(entity);
|
const indices = this.em.getIndicesOf(entity);
|
||||||
|
|
||||||
// this is intentionally setting values to defaults, like "nullable" and "default"
|
// this is intentionally setting values to defaults, like "nullable" and "default"
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { test, describe, expect, beforeAll, afterAll } from "bun:test";
|
import { test, describe, expect } from "bun:test";
|
||||||
import * as q from "./query";
|
import * as q from "./query";
|
||||||
import { parse as $parse, type ParseOptions } from "bknd/utils";
|
import { parse as $parse, type ParseOptions } from "bknd/utils";
|
||||||
import type { PrimaryFieldType } from "modules";
|
import type { PrimaryFieldType } from "modules";
|
||||||
import type { Generated } from "kysely";
|
import type { Generated } from "kysely";
|
||||||
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
|
|
||||||
|
|
||||||
const parse = (v: unknown, o: ParseOptions = {}) =>
|
const parse = (v: unknown, o: ParseOptions = {}) =>
|
||||||
$parse(q.repoQuery, v, {
|
$parse(q.repoQuery, v, {
|
||||||
@@ -16,9 +15,6 @@ const decode = (input: any, output: any) => {
|
|||||||
expect(parse(input)).toEqual(output);
|
expect(parse(input)).toEqual(output);
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeAll(() => disableConsoleLog());
|
|
||||||
afterAll(() => enableConsoleLog());
|
|
||||||
|
|
||||||
describe("server/query", () => {
|
describe("server/query", () => {
|
||||||
test("limit & offset", () => {
|
test("limit & offset", () => {
|
||||||
//expect(() => parse({ limit: false })).toThrow();
|
//expect(() => parse({ limit: false })).toThrow();
|
||||||
|
|||||||
@@ -132,8 +132,6 @@ export type * from "data/entities/Entity";
|
|||||||
export type { EntityManager } from "data/entities/EntityManager";
|
export type { EntityManager } from "data/entities/EntityManager";
|
||||||
export type { SchemaManager } from "data/schema/SchemaManager";
|
export type { SchemaManager } from "data/schema/SchemaManager";
|
||||||
export type * from "data/entities";
|
export type * from "data/entities";
|
||||||
|
|
||||||
// data connection
|
|
||||||
export {
|
export {
|
||||||
BaseIntrospector,
|
BaseIntrospector,
|
||||||
Connection,
|
Connection,
|
||||||
@@ -146,32 +144,9 @@ export {
|
|||||||
type ConnQuery,
|
type ConnQuery,
|
||||||
type ConnQueryResults,
|
type ConnQueryResults,
|
||||||
} from "data/connection";
|
} from "data/connection";
|
||||||
|
|
||||||
// data sqlite
|
|
||||||
export { SqliteConnection } from "data/connection/sqlite/SqliteConnection";
|
export { SqliteConnection } from "data/connection/sqlite/SqliteConnection";
|
||||||
export { SqliteIntrospector } from "data/connection/sqlite/SqliteIntrospector";
|
export { SqliteIntrospector } from "data/connection/sqlite/SqliteIntrospector";
|
||||||
export { SqliteLocalConnection } from "data/connection/sqlite/SqliteLocalConnection";
|
export { SqliteLocalConnection } from "data/connection/sqlite/SqliteLocalConnection";
|
||||||
|
|
||||||
// data sqlocal
|
|
||||||
export { SQLocalConnection, sqlocal } from "data/connection/sqlite/sqlocal/SQLocalConnection";
|
|
||||||
|
|
||||||
// data postgres
|
|
||||||
export {
|
|
||||||
pg,
|
|
||||||
PgPostgresConnection,
|
|
||||||
} from "data/connection/postgres/PgPostgresConnection";
|
|
||||||
export { PostgresIntrospector } from "data/connection/postgres/PostgresIntrospector";
|
|
||||||
export { PostgresConnection } from "data/connection/postgres/PostgresConnection";
|
|
||||||
export {
|
|
||||||
postgresJs,
|
|
||||||
PostgresJsConnection,
|
|
||||||
} from "data/connection/postgres/PostgresJsConnection";
|
|
||||||
export {
|
|
||||||
createCustomPostgresConnection,
|
|
||||||
type CustomPostgresConnection,
|
|
||||||
} from "data/connection/postgres/custom";
|
|
||||||
|
|
||||||
// data prototype
|
|
||||||
export {
|
export {
|
||||||
text,
|
text,
|
||||||
number,
|
number,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import type { BunFile } from "bun";
|
|||||||
|
|
||||||
export async function adapterTestSuite(
|
export async function adapterTestSuite(
|
||||||
testRunner: TestRunner,
|
testRunner: TestRunner,
|
||||||
_adapter: StorageAdapter | (() => StorageAdapter),
|
adapter: StorageAdapter,
|
||||||
file: File | BunFile,
|
file: File | BunFile,
|
||||||
opts?: {
|
opts?: {
|
||||||
retries?: number;
|
retries?: number;
|
||||||
@@ -25,12 +25,7 @@ export async function adapterTestSuite(
|
|||||||
const _filename = randomString(10);
|
const _filename = randomString(10);
|
||||||
const filename = `${_filename}.png`;
|
const filename = `${_filename}.png`;
|
||||||
|
|
||||||
const getAdapter = (
|
|
||||||
typeof _adapter === "function" ? _adapter : () => _adapter
|
|
||||||
) as () => StorageAdapter;
|
|
||||||
|
|
||||||
await test("puts an object", async () => {
|
await test("puts an object", async () => {
|
||||||
const adapter = getAdapter();
|
|
||||||
objects = (await adapter.listObjects()).length;
|
objects = (await adapter.listObjects()).length;
|
||||||
const result = await adapter.putObject(filename, file as unknown as File);
|
const result = await adapter.putObject(filename, file as unknown as File);
|
||||||
expect(result).toBeDefined();
|
expect(result).toBeDefined();
|
||||||
@@ -43,7 +38,6 @@ export async function adapterTestSuite(
|
|||||||
});
|
});
|
||||||
|
|
||||||
await test("lists objects", async () => {
|
await test("lists objects", async () => {
|
||||||
const adapter = getAdapter();
|
|
||||||
const length = await retry(
|
const length = await retry(
|
||||||
() => adapter.listObjects().then((res) => res.length),
|
() => adapter.listObjects().then((res) => res.length),
|
||||||
(length) => length > objects,
|
(length) => length > objects,
|
||||||
@@ -55,12 +49,10 @@ export async function adapterTestSuite(
|
|||||||
});
|
});
|
||||||
|
|
||||||
await test("file exists", async () => {
|
await test("file exists", async () => {
|
||||||
const adapter = getAdapter();
|
|
||||||
expect(await adapter.objectExists(filename)).toBe(true);
|
expect(await adapter.objectExists(filename)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
await test("gets an object", async () => {
|
await test("gets an object", async () => {
|
||||||
const adapter = getAdapter();
|
|
||||||
const res = await adapter.getObject(filename, new Headers());
|
const res = await adapter.getObject(filename, new Headers());
|
||||||
expect(res.ok).toBe(true);
|
expect(res.ok).toBe(true);
|
||||||
expect(res.headers.get("Accept-Ranges")).toBe("bytes");
|
expect(res.headers.get("Accept-Ranges")).toBe("bytes");
|
||||||
@@ -70,7 +62,6 @@ export async function adapterTestSuite(
|
|||||||
if (options.testRange) {
|
if (options.testRange) {
|
||||||
await test("handles range request - partial content", async () => {
|
await test("handles range request - partial content", async () => {
|
||||||
const headers = new Headers({ Range: "bytes=0-99" });
|
const headers = new Headers({ Range: "bytes=0-99" });
|
||||||
const adapter = getAdapter();
|
|
||||||
const res = await adapter.getObject(filename, headers);
|
const res = await adapter.getObject(filename, headers);
|
||||||
expect(res.status).toBe(206); // Partial Content
|
expect(res.status).toBe(206); // Partial Content
|
||||||
expect(/^bytes 0-99\/\d+$/.test(res.headers.get("Content-Range")!)).toBe(true);
|
expect(/^bytes 0-99\/\d+$/.test(res.headers.get("Content-Range")!)).toBe(true);
|
||||||
@@ -79,7 +70,6 @@ export async function adapterTestSuite(
|
|||||||
|
|
||||||
await test("handles range request - suffix range", async () => {
|
await test("handles range request - suffix range", async () => {
|
||||||
const headers = new Headers({ Range: "bytes=-100" });
|
const headers = new Headers({ Range: "bytes=-100" });
|
||||||
const adapter = getAdapter();
|
|
||||||
const res = await adapter.getObject(filename, headers);
|
const res = await adapter.getObject(filename, headers);
|
||||||
expect(res.status).toBe(206); // Partial Content
|
expect(res.status).toBe(206); // Partial Content
|
||||||
expect(/^bytes \d+-\d+\/\d+$/.test(res.headers.get("Content-Range")!)).toBe(true);
|
expect(/^bytes \d+-\d+\/\d+$/.test(res.headers.get("Content-Range")!)).toBe(true);
|
||||||
@@ -87,7 +77,6 @@ export async function adapterTestSuite(
|
|||||||
|
|
||||||
await test("handles invalid range request", async () => {
|
await test("handles invalid range request", async () => {
|
||||||
const headers = new Headers({ Range: "bytes=invalid" });
|
const headers = new Headers({ Range: "bytes=invalid" });
|
||||||
const adapter = getAdapter();
|
|
||||||
const res = await adapter.getObject(filename, headers);
|
const res = await adapter.getObject(filename, headers);
|
||||||
expect(res.status).toBe(416); // Range Not Satisfiable
|
expect(res.status).toBe(416); // Range Not Satisfiable
|
||||||
expect(/^bytes \*\/\d+$/.test(res.headers.get("Content-Range")!)).toBe(true);
|
expect(/^bytes \*\/\d+$/.test(res.headers.get("Content-Range")!)).toBe(true);
|
||||||
@@ -95,7 +84,6 @@ export async function adapterTestSuite(
|
|||||||
}
|
}
|
||||||
|
|
||||||
await test("gets object meta", async () => {
|
await test("gets object meta", async () => {
|
||||||
const adapter = getAdapter();
|
|
||||||
expect(await adapter.getObjectMeta(filename)).toEqual({
|
expect(await adapter.getObjectMeta(filename)).toEqual({
|
||||||
type: file.type, // image/png
|
type: file.type, // image/png
|
||||||
size: file.size,
|
size: file.size,
|
||||||
@@ -103,7 +91,6 @@ export async function adapterTestSuite(
|
|||||||
});
|
});
|
||||||
|
|
||||||
await test("deletes an object", async () => {
|
await test("deletes an object", async () => {
|
||||||
const adapter = getAdapter();
|
|
||||||
expect(await adapter.deleteObject(filename)).toBeUndefined();
|
expect(await adapter.deleteObject(filename)).toBeUndefined();
|
||||||
|
|
||||||
if (opts?.skipExistsAfterDelete !== true) {
|
if (opts?.skipExistsAfterDelete !== true) {
|
||||||
|
|||||||
@@ -10,19 +10,16 @@ export type CodeMode<AdapterConfig extends BkndConfig> = AdapterConfig extends B
|
|||||||
? BkndModeConfig<Args, AdapterConfig>
|
? BkndModeConfig<Args, AdapterConfig>
|
||||||
: never;
|
: never;
|
||||||
|
|
||||||
export function code<
|
export function code<Args>(config: BkndCodeModeConfig<Args>): BkndConfig<Args> {
|
||||||
Config extends BkndConfig,
|
|
||||||
Args = Config extends BkndConfig<infer A> ? A : unknown,
|
|
||||||
>(codeConfig: CodeMode<Config>): BkndConfig<Args> {
|
|
||||||
return {
|
return {
|
||||||
...codeConfig,
|
...config,
|
||||||
app: async (args) => {
|
app: async (args) => {
|
||||||
const {
|
const {
|
||||||
config: appConfig,
|
config: appConfig,
|
||||||
plugins,
|
plugins,
|
||||||
isProd,
|
isProd,
|
||||||
syncSchemaOptions,
|
syncSchemaOptions,
|
||||||
} = await makeModeConfig(codeConfig, args);
|
} = await makeModeConfig(config, args);
|
||||||
|
|
||||||
if (appConfig?.options?.mode && appConfig?.options?.mode !== "code") {
|
if (appConfig?.options?.mode && appConfig?.options?.mode !== "code") {
|
||||||
$console.warn("You should not set a different mode than `db` when using code mode");
|
$console.warn("You should not set a different mode than `db` when using code mode");
|
||||||
|
|||||||
+23
-24
@@ -1,6 +1,6 @@
|
|||||||
import type { BkndConfig } from "bknd/adapter";
|
import type { BkndConfig } from "bknd/adapter";
|
||||||
import { makeModeConfig, type BkndModeConfig } from "./shared";
|
import { makeModeConfig, type BkndModeConfig } from "./shared";
|
||||||
import { getDefaultConfig, type MaybePromise, type Merge } from "bknd";
|
import { getDefaultConfig, type MaybePromise, type ModuleConfigs, type Merge } from "bknd";
|
||||||
import type { DbModuleManager } from "modules/db/DbModuleManager";
|
import type { DbModuleManager } from "modules/db/DbModuleManager";
|
||||||
import { invariant, $console } from "bknd/utils";
|
import { invariant, $console } from "bknd/utils";
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ export type BkndHybridModeOptions = {
|
|||||||
* Reader function to read the configuration from the file system.
|
* Reader function to read the configuration from the file system.
|
||||||
* This is required for hybrid mode to work.
|
* This is required for hybrid mode to work.
|
||||||
*/
|
*/
|
||||||
reader?: (path: string) => MaybePromise<string | object>;
|
reader?: (path: string) => MaybePromise<string>;
|
||||||
/**
|
/**
|
||||||
* Provided secrets to be merged into the configuration
|
* Provided secrets to be merged into the configuration
|
||||||
*/
|
*/
|
||||||
@@ -23,36 +23,42 @@ export type HybridMode<AdapterConfig extends BkndConfig> = AdapterConfig extends
|
|||||||
? BkndModeConfig<Args, Merge<BkndHybridModeOptions & AdapterConfig>>
|
? BkndModeConfig<Args, Merge<BkndHybridModeOptions & AdapterConfig>>
|
||||||
: never;
|
: never;
|
||||||
|
|
||||||
export function hybrid<
|
export function hybrid<Args>({
|
||||||
Config extends BkndConfig,
|
configFilePath = "bknd-config.json",
|
||||||
Args = Config extends BkndConfig<infer A> ? A : unknown,
|
...rest
|
||||||
>(hybridConfig: HybridMode<Config>): BkndConfig<Args> {
|
}: HybridBkndConfig<Args>): BkndConfig<Args> {
|
||||||
return {
|
return {
|
||||||
...hybridConfig,
|
...rest,
|
||||||
|
config: undefined,
|
||||||
app: async (args) => {
|
app: async (args) => {
|
||||||
const {
|
const {
|
||||||
config: appConfig,
|
config: appConfig,
|
||||||
isProd,
|
isProd,
|
||||||
plugins,
|
plugins,
|
||||||
syncSchemaOptions,
|
syncSchemaOptions,
|
||||||
} = await makeModeConfig(hybridConfig, args);
|
} = await makeModeConfig(
|
||||||
|
{
|
||||||
const configFilePath = appConfig.configFilePath ?? "bknd-config.json";
|
...rest,
|
||||||
|
configFilePath,
|
||||||
|
},
|
||||||
|
args,
|
||||||
|
);
|
||||||
|
|
||||||
if (appConfig?.options?.mode && appConfig?.options?.mode !== "db") {
|
if (appConfig?.options?.mode && appConfig?.options?.mode !== "db") {
|
||||||
$console.warn("You should not set a different mode than `db` when using hybrid mode");
|
$console.warn("You should not set a different mode than `db` when using hybrid mode");
|
||||||
}
|
}
|
||||||
invariant(
|
invariant(
|
||||||
typeof appConfig.reader === "function",
|
typeof appConfig.reader === "function",
|
||||||
"You must set a `reader` option when using hybrid mode",
|
"You must set the `reader` option when using hybrid mode",
|
||||||
);
|
);
|
||||||
|
|
||||||
const fileContent = await appConfig.reader?.(configFilePath);
|
let fileConfig: ModuleConfigs;
|
||||||
let fileConfig = typeof fileContent === "string" ? JSON.parse(fileContent) : fileContent;
|
try {
|
||||||
if (!fileConfig) {
|
fileConfig = JSON.parse(await appConfig.reader!(configFilePath)) as ModuleConfigs;
|
||||||
$console.warn("No config found, using default config");
|
} catch (e) {
|
||||||
fileConfig = getDefaultConfig();
|
const defaultConfig = (appConfig.config ?? getDefaultConfig()) as ModuleConfigs;
|
||||||
await appConfig.writer?.(configFilePath, JSON.stringify(fileConfig, null, 2));
|
await appConfig.writer!(configFilePath, JSON.stringify(defaultConfig, null, 2));
|
||||||
|
fileConfig = defaultConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -74,13 +80,6 @@ export function hybrid<
|
|||||||
skipValidation: isProd,
|
skipValidation: isProd,
|
||||||
// secrets are required for hybrid mode
|
// secrets are required for hybrid mode
|
||||||
secrets: appConfig.secrets,
|
secrets: appConfig.secrets,
|
||||||
onModulesBuilt: async (ctx) => {
|
|
||||||
if (ctx.flags.sync_required && !isProd && syncSchemaOptions.force) {
|
|
||||||
$console.log("[hybrid] syncing schema");
|
|
||||||
await ctx.em.schema().sync(syncSchemaOptions);
|
|
||||||
}
|
|
||||||
await appConfig?.options?.manager?.onModulesBuilt?.(ctx);
|
|
||||||
},
|
|
||||||
...appConfig?.options?.manager,
|
...appConfig?.options?.manager,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user