UNPKG

@rattus-orm/react-mobx

Version:

ORM for your JS/TS apps: react-mobx bindings

140 lines (134 loc) 3.77 kB
// src/context/rattus-context.tsx import { contextBootstrap } from "@rattus-orm/core/utils/integrationsHelpers"; import { RattusReactContext } from "@rattus-orm/core/utils/reactIntegrationHelpers"; import React, { useMemo } from "react"; // src/data-provider/react-mobx-data-provider.ts import { DataProviderHelpers } from "@rattus-orm/core"; // src/mobx/mobx-store.ts import { action, computed, makeObservable, observable } from "mobx"; var MobxStore = class { constructor(initialState) { this.state = { data: {} }; makeObservable(this, { state: observable, save: action, flush: action, fresh: action, destroy: action, data: computed }); this.state = initialState ?? { data: {} }; } get data() { return this.state; } destroy(ids) { ids.forEach((id) => { delete this.state.data[id]; }); } save(records) { this.state.data = { ...this.state.data, ...records }; } flush() { this.state = { data: {} }; } fresh(records) { this.state = { data: records }; } }; // src/data-provider/react-mobx-data-provider.ts var ReactMobxDataProvider = class extends DataProviderHelpers { constructor() { super(...arguments); this.storesMap = /* @__PURE__ */ new Map(); } delete(module, ids) { this.getMobxStore(module).destroy(ids); } dump() { const result = {}; for (const storeId of this.storesMap.keys()) { const modulePath = storeId.split("/"); const [connection, moduleName] = modulePath; if (!result[connection]) { result[connection] = { [moduleName]: this.getModuleState(modulePath) }; continue; } result[connection][moduleName] = this.getModuleState(modulePath); } return result; } flush(module) { this.getMobxStore(module).flush(); } getModuleState(module) { return this.getMobxStore(module).data; } hasModule(module) { return this.storesMap.has(this.getModulePathAsString(module)); } insert(module, records) { this.getMobxStore(module).save(records); } registerConnection() { } registerModule(path, initialState) { const storeId = this.getModulePathAsString(path); if (this.storesMap.has(storeId)) { return; } const newStore = new MobxStore(initialState); this.storesMap.set(storeId, newStore); } replace(module, records) { this.getMobxStore(module).fresh(records); } save(module, records) { this.getMobxStore(module).save(records); } update(module, records) { this.getMobxStore(module).save(records); } getModulePathAsString(modulePath) { return modulePath.join("/"); } getMobxStore(modulePath) { const storeId = this.getModulePathAsString(modulePath); if (!this.storesMap.has(storeId)) { throw new Error(`[ReactMobxDataProvider] store "${storeId}" does not exists`); } return this.storesMap.get(storeId); } }; // src/context/rattus-context.tsx import { reactUseDatabase } from "@rattus-orm/core/utils/reactIntegrationHelpers"; function RattusProvider(props) { const createdDatabase = useMemo( () => contextBootstrap(props, new ReactMobxDataProvider()), [props, props.database, props.plugins, props.customRepositories, props.connection] ); return /* @__PURE__ */ React.createElement(RattusReactContext.Provider, { value: createdDatabase }, props.children); } // src/hooks/useRepository.ts import { useRepositoryForDynamicContext } from "@rattus-orm/core/utils/integrationsHelpers"; function useRepository(model, connection) { return useRepositoryForDynamicContext(model, connection); } export { RattusProvider, ReactMobxDataProvider, reactUseDatabase as useDatabase, useRepository };