Show HN:Bknd——可嵌入任何React栈的Firebase替代方案
Show HN: Bknd – Firebase alternative that embeds into any React stack

原始链接: https://github.com/bknd-io/bknd

bknd 通过提供完整的数据库管理、身份验证、媒体处理和工作流后端解决方案,简化了应用程序开发。它基于 Web 标准构建,轻量级,几乎可以在任何地方部署,包括您现有的框架内。 bknd 分为四个部分:带有 API 和适配器的后端、管理 UI 组件、带有 React hook 的 TypeScript SDK 以及用于身份验证和媒体的 React 元素。这种模块化设计提供了灵活性,允许开发人员仅使用他们需要的组件。 它提供 REST API 和 TypeScript SDK 以方便数据访问。React 开发人员可以受益于用于常见任务(如用户身份验证和媒体上传)的预构建 hook 和元素,从而简化开发流程。管理 UI 提供了一个图形界面,用于管理数据和配置后端。bknd 旨在成为一个通用的解决方案,可以与各种 JavaScript 环境和框架无缝集成。

Bknd 是一个全新的 Firebase 替代方案,旨在通过直接嵌入你的 React 前端来简化 Web 开发。开发者 dswbx 创建它来解决在每个新项目中集成各种 API(如身份验证、存储和数据库)的难题。Bknd 消除了单独后端部署的需要,并能与 Next.js、Remix 和 Astro 等框架无缝协作。它也支持 Cloudflare Workers 和 AWS Lambda 等无服务器环境。 Bknd 拥有即时 API、多策略身份验证、媒体处理和内置 Admin UI 等功能。它支持多种数据库选项,包括 Postgres、LibSQL (Turso)、D1 和 SQLite,并使用适配器进行存储。 一位评论者提出了一个关键问题:Bknd 如何在前端运行时保持 Postgres 凭据安全?开发者正在寻求反馈,并很好奇其他人会用它来构建什么。

原文

npm version npm downloads

bknd

⭐ Live Demo

bknd simplifies app development by providing a fully functional backend for database management, authentication, media and workflows. Being lightweight and built on Web Standards, it can be deployed nearly anywhere, including running inside your framework of choice. No more deploying multiple separate services!

For documentation and examples, please visit https://docs.bknd.io.

Warning

Please keep in mind that bknd is still under active development and therefore full backward compatibility is not guaranteed before reaching v1.0.0.

gzipped size of bknd gzipped size of bknd/client gzipped size of bknd/elements gzipped size of bknd/ui

The size on npm is misleading, as the bknd package includes the backend, the ui components as well as the whole backend bundled into the cli including static assets.

Depending on what you use, the size can be higher as additional dependencies are getting pulled in. The minimal size of a full bknd app as an API is around 212 kB gzipped (e.g. deployed as Cloudflare Worker).

Creating digital products always requires developing both the backend (the logic) and the frontend (the appearance). Building a backend from scratch demands deep knowledge in areas such as authentication and database management. Using a backend framework can speed up initial development, but it still requires ongoing effort to work within its constraints (e.g., "how to do X with Y?"), which can quickly slow you down. Choosing a backend system is a tough decision, as you might not be aware of its limitations until you encounter them.

The solution: A backend system that only assumes and implements primitive details, integrates into multiple environments, and adheres to industry standards.

  • ⚡ Instant backend with full REST API:
    • Data: Define, query, and control your data with ease.
    • Auth: Easily implement reliable authentication strategies.
    • Media: Effortlessly manage and serve all your media files.
    • Flows: Design and run workflows with seamless automation. (UI integration coming soon!)
  • 🌐 Built on Web Standards for maximum compatibility
  • 🏃‍♂️ Multiple run modes
    • standalone using the CLI
    • using a JavaScript runtime (Node, Bun, workerd)
    • using a React framework (Next.js, React Router, Astro)
  • 📦 Official API and React SDK with type-safety
  • ⚛️ React elements for auto-configured authentication and media components

The package is mainly split into 4 parts, each serving a specific purpose:

Import Purpose
bknd
bknd/adapter/*
Backend including APIs and adapters
bknd/ui Admin UI components for react frameworks
bknd/client TypeScript SDK and React hooks for the API endpoints
bknd/elements React components for authentication and media

Serve the backend as an API for any JS runtime or framework. The latter is especially handy, as it allows you to deploy your frontend and backend bundled together. Furthermore it allows adding additional logic in a way you're already familar with. Just add another route and you're good to go.

Here is an example of serving the API using node:

import { serve } from "bknd/adapter/node"
serve();

Integrated admin UI (bknd/ui)

The admin UI allows to manage your data including full configuration of your backend using a graphical user interface. Using vite, your admin route looks like this:

import { Admin } from "bknd/ui"
import "bknd/dist/styles.css";

export default function AdminPage() {
   return <Admin />
}

Using the REST API or TypeScript SDK (bknd/client)

If you're not using a JavaScript environment, you can still access any endpoint using the REST API:

curl -XGET <your-endpoint>/api/data/entity/<entity>
{
  "data": [
    { "id": 1, ... },
    { "id": 2, ... }
  ],
  "meta": { /* ... */ }
}

In a JavaScript environment, you can use the TypeScript SDK with type-safety. The above example would look like this:

import { Api } from "bknd/client";

const api = new Api({ host: "<endpoint>" });
const { data } = await api.data.readMany("<entity>");

If you're using React, there are 2 hooks exposed (useApi, useEntity), as well as an swr wrapper around each (useApiQuery, useEntityQuery). The swr wrapped hooks automatically handled query invalidation:

import { useState } from "react";
import { useEntityQuery } from "bknd/client";

export default function App() {
   const { data } = useEntityQuery("todos");   
   return <ul>
      {data?.map(todo => (
         <li key={todo.id}>{todo.name}</li>
      ))}
   </ul>
}

React elements (bknd/elements)

You don't have to figure out API details to include media uploads to your app. For an user avatar upload, this is all you need:

import { Media } from "bknd/elements"
import "bknd/dist/main.css"

export function UserAvatar() {
   return <Media.Dropzone
     entity={{ name: "users", id: 1, field: "avatar" }}
     maxItems={1}
     overwrite
   />
}

The import path also exports components for login and registration forms which are automatically pointed to the bknd defaults.

To quickly spin up an instance, run:

联系我们 contact @ memedata.com