@carbonorm/carbonreact

中文文档
6.0.5发布于 2026/10/21MITTypeScriptESM + CJS构建来源已验证近 30 天下载 5199

npm License Size ![Documentation](https://img.shields.io/we

原始使用文档
文档目录4 节
  1. Installation
  2. Generate Models
  3. QuickStart Implementation
  4. Websocket Updates (Optional)

npm License Size Documentation Monthly Downloads All Downloads Star

CarbonReact

CarbonReact is a part of the CarbonORM series. It is a React MySQL ORM that is designed to generate all your boilerplate code.

Installation

CarbonReact is available on NPM. You'll need to have NodeJS installed which comes prepackaged with npm (node package manager).

npm install @carbonorm/carbonreact

Generate Models

The command below generates a single C6.ts file containing your tables, types, and REST bindings. Keep the output in version control and import from it on both server and client. All arguments are optional. If you do not provide them the defaults will be used. The example arguments below are the defaults.

npx generateRestBindings --user root --pass password --host 127.0.0.1 --port 3306 --dbname carbonPHP --prefix carbon_ --output ./shared/rest/C6.ts

The generated file exports C6, GLOBAL_REST_PARAMETERS, and per-table bindings (e.g. Users):

import { C6, GLOBAL_REST_PARAMETERS, Users } from "./shared/rest/C6";

You can view the generator source in CarbonNode. We use Handlebars templates to generate the code.

For more information on CarbonNode and the generations please see the CarbonNode.

QuickStart Implementation

CarbonReact is designed to be the Bootstrap of your application. It is in charge of managing the state of your application. ideally once it has mounted it never gets unmounted. In application where this is not possible, you can provide the shouldStatePersist property to the CarbonReact React Component. This will allow you to persist the state of your application even if the component is unmounted. The behavior of accessing or updating state while the component is unknown (undefined) and should be avoided. The example below shows a simple implementation of CarbonReact. Our user defined component is in CarbonORM which is written to extend the CarbonReact class. Your implementation must also extend CarbonReact. The name CarbonORM is arbitrary and can be changed to your application's name.

index.tsx


import 'react-toastify/dist/ReactToastify.min.css'; // This is required for alerts to work and not break styling

const container = document.getElementById('root');

const root = createRoot(container!);

root.render(
    <React.StrictMode>
        <CarbonORM />
    </React.StrictMode>
);

CarbonReact should be loaded as soon as the page loads. There are plans to allow other alerting systems to be used, but for now we use React Toastify. It must be required in one of your files and typically can be done the root of your project.

CarbonORM.tsx



export const initialCarbonORMState: typeof initialRestfulObjectsState
    & typeof initialRequiredCarbonORMState
    & iAuthenticate
    & iVersions
    & iUi
    & {} = {
    ...initialVersionsState,
    ...initialRestfulObjectsState,
    ...initialRequiredCarbonORMState,
    ...initialAuthenticateState,
    ...initialUiState,
}

export default class CarbonORM extends CarbonReact<{ browserRouter?: boolean }, typeof initialCarbonORMState> {

    static instance: CarbonORM;

    state = initialCarbonORMState;

    constructor(props) {
        super(props);
        CarbonORM.instance = this;
        CarbonReact.instance = this;
    }

    componentDidMount() {
        Carbons.Get()
        authenticateUser()
    }

    render() {
        console.log("CarbonORM TSX RENDER");

        const {isLoaded, backendThrowable} = this.state;


        if (backendThrowable.length > 0) {

            return <BackendThrowable />

        }

        const reactRouterContext = (children: any) => {

            if (isTest) {

                return <MemoryRouter initialEntries={['/']}>{children}</MemoryRouter>

            }

            return <HashRouter>{children}</HashRouter>

        }

        return reactRouterContext(<>
            <Routes>
                <Route path={UI + "*"}>
                    <Route path={MATERIAL_DASHBOARD + "*"} element={ppr(Dashboard, {})}>
                        <Route path={DASHBOARD + '*'} element={ppr(DashboardPage, {})}/>
                        <Route path={USER_PROFILE + '*'} element={ppr(UserProfile, {})}/>
                        <Route path={TABLES + '*'} element={ppr(TableList, {})}/>
                        <Route path={TYPOGRAPHY + '*'} element={ppr(Typography, {})}/>
                        <Route path={ICONS + '*'} element={ppr(Icons, {})}/>
                        <Route path={MAPS + '*'} element={ppr(Maps, {})}/>
                        <Route path={NOTIFICATIONS + '*'} element={ppr(Notifications, {})}/>
                        <Route path={UPGRADE_TO_PRO + '*'} element={ppr(UpgradeToPro, {})}/>
                        <Route path={'*'} element={<Navigate to={'/' + UI + MATERIAL_DASHBOARD + DASHBOARD}/>}/>
                    </Route>
                    <Route path={MATERIAL_KIT + "*"} element={ppr(MaterialKit, {})}>
                        <Route path={SECTION_NAVBARS + '*'} element={ppr(SectionNavbars, {})}/>
                        <Route path={SECTION_BASICS + '*'} element={ppr(SectionBasics, {})}/>
                        <Route path={SECTION_TABS + '*'} element={ppr(SectionTabs, {})}/>
                        <Route path={SECTION_PILLS + '*'} element={ppr(SectionPills, {})}/>
                        <Route path={SECTION_NOTIFICATIONS + '*'} element={ppr(SectionNotifications, {})}/>
                        <Route path={SECTION_TYPOGRAPHY + '*'} element={ppr(SectionTypography, {})}/>
                        <Route path={SECTION_JAVASCRIPT + '*'} element={ppr(SectionJavascript, {})}/>
                        <Route path={SECTION_COMPLETED_EXAMPLES + '*'} element={ppr(SectionCompletedExamples, {})}/>
                        <Route path={SECTION_LOGIN + '*'} element={ppr(SectionLogin, {})}/>
                        <Route path={LANDING_PAGE + '*'} element={ppr(LandingPage, {})}/>
                        <Route path={SECTION_DOWNLOAD + '*'} element={ppr(SectionDownload, {})}/>
                        <Route path={'*'} element={<Navigate to={'/' + UI + MATERIAL_KIT + SECTION_NAVBARS}/>}/>
                    </Route>
                    <Route path={'*'} element={<Navigate to={'/' + UI + MATERIAL_DASHBOARD}/>}/>
                </Route>
                <Route path={DOCUMENTATION + '*'} element={ppr(Documentation, {})}>
                    <Route path={CARBON_ORM_INTRODUCTION + '*'} element={ppr(CarbonORMIntroduction, {})}/>
                    <Route path={SUPPORT + '*'} element={ppr(Support, {})}/>
                    <Route path={CARBONPHP + '*'} element={ppr(CarbonPHP, {})}/>
                    <Route path={DEPENDENCIES + '*'} element={ppr(Dependencies, {})}/>
                    <Route path={CHANGELOG + "*"} element={ppr(Changelog, {})}/>
                    <Route path={IMPLEMENTATIONS + "*"} element={ppr(Implementations, {})}/>
                    <Route path={LICENSE + "*"} element={ppr(License, {})}/>
                    <Route path={'*'} element={<Navigate to={'/' + DOCUMENTATION + CARBON_ORM_INTRODUCTION}/>}/>
                </Route>
                <Route path="/landing-page" element={ppr(LandingPage, {})}/>
                <Route path={'*'} element={<Navigate to={'/documentation'}/>}/>
            </Routes>
            <ToastContainer
                autoClose={3000}
                draggable={false}
                position="top-right"
                hideProgressBar={false}
                newestOnTop
                closeOnClick
                rtl={false}
                pauseOnHover
            />
        </>)

    }
}

Websocket Updates (Optional)

Backend: broadcast the payload generated by CarbonNode on every write (POST/PUT/DELETE).

import { GLOBAL_REST_PARAMETERS } from "./shared/rest/C6";

GLOBAL_REST_PARAMETERS.websocketBroadcast = (payload) => {
    wsServer.broadcast(JSON.stringify(payload));
};

Frontend: pass C6 into CarbonReact so the websocket client can normalize updates and only apply them when the row is already in local state. Debug logs only appear when VERBOSE is enabled.

import { C6 } from "./shared/rest/C6";

root.render(
    <React.StrictMode>
        <CarbonORM websocket={{ C6, url: "ws://localhost:8888/carbonorm/websocket" }} />
    </React.StrictMode>
);

Our testing tool Jest requires React Router Dom to be in MemoryRouter mode. This is because Jest does not have a DOM and therefore cannot render the HashRouter or BrowserRouter component. CarbonReact does not require React Router Dom to be installed. You can use any router you like. The example above uses HashRouter which is necessary as this website hosted for free using GitHub Pages.

A folder named state in the root of your project src/state/ should contain all your state files. These files should be written with the intention of being imported into your CarbonReact extended component. The example below shows a simple state file that is implemented above. Helper functions are also included in the state files. These functions are designed to be used in your React Components. React stateful operations must be wrapped in a function and thus must not be run during the initial page load.

Updating state is as simple as calling CarbonORM.instance.setState({}). The class name CarbonORM can be replaced with any name of your liking. Typically, you will want to use the name of your project.

ui.tsx

import CarbonORM from "CarbonORM";


export interface iUi {
    pureWordpressPluginConfigured?: boolean,
    documentationVersionURI: string,
    isLoaded: boolean,
    darkMode: boolean,
}

export const initialUiState: iUi = {
    pureWordpressPluginConfigured: false,
    documentationVersionURI: '0.0.0',
    isLoaded: false,
    darkMode: true,
}


export const switchDarkAndLightTheme = () => {
    CarbonORM.instance.setState({
        darkMode: !CarbonORM.instance.state.darkMode
    });
};

版本与发布记录

@carbonorm/carbonreact 共发布 120 个版本,首次发布于 2023/07/28;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.0.5latest2026/10/2118 天前
6.0.42026/02/148 个月前
6.0.32026/02/029 个月前
6.0.22026/01/309 个月前
6.0.12026/01/259 个月前
6.0.02026/01/249 个月前

关于 @carbonorm/carbonreact 的常见问题

@carbonorm/carbonreact 是什么?

![Documentation](https://img.shields.io/we

如何安装 @carbonorm/carbonreact?

在项目目录运行 npm install @carbonorm/carbonreact,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^19.1.0)、react-dom(^19.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 compileValidSQL,可以用 npx compileValidSQL 直接运行。

@carbonorm/carbonreact 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@carbonorm/carbonreact 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@carbonorm/carbonreact 可以商用吗?

@carbonorm/carbonreact 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@carbonorm/carbonreact 的下载量有多少?

根据 npm 官方下载统计,@carbonorm/carbonreact 在 2026/10/09 – 2026/11/07 这 30 天内被下载 5,199 次,日均约 173 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@carbonorm/carbonreact 还在维护吗?

最新版本 6.0.5 发布于 2026/10/21(18 天前);至今共发布 120 个版本,其中过去 12 个月发布了 7 个;首次发布于 2023/07/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。