@sqlrooms/room-store
Low-level state management primitives for SQLRooms, built on Zustand.
Use this package when you want to build custom room state from scratch.
If you want DuckDB + layout + room shell out of the box, use @sqlrooms/room-shell.
Installation
bash
npm install @sqlrooms/room-storeWhat this package provides
createRoomStore()andcreateRoomStoreCreator()- base lifecycle slice:
createBaseRoomSlice() - generic slice helper:
createSlice() - React context/hooks:
RoomStateProvider,useBaseRoomStore,useRoomStoreApi - persistence helpers:
persistSliceConfigs(),createPersistHelpers()
Quick start
tsx
import {
BaseRoomStoreState,
createBaseRoomSlice,
createRoomStore,
createSlice,
type StateCreator,
} from '@sqlrooms/room-store';
type CounterSliceState = {
counter: {
value: number;
increment: () => void;
};
};
function createCounterSlice(): StateCreator<CounterSliceState> {
return createSlice<CounterSliceState>((set, get) => ({
counter: {
value: 0,
increment: () =>
set((state) => ({
counter: {
...state.counter,
value: get().counter.value + 1,
},
})),
},
}));
}
type RoomState = BaseRoomStoreState & CounterSliceState;
export const {roomStore, useRoomStore} = createRoomStore<RoomState>(
(set, get, store) => ({
...createBaseRoomSlice()(set, get, store),
...createCounterSlice()(set, get, store),
}),
);React integration
tsx
import {RoomStateProvider} from '@sqlrooms/room-store';
import {roomStore} from './store';
export function App() {
return (
<RoomStateProvider roomStore={roomStore}>
<Dashboard />
</RoomStateProvider>
);
}tsx
import {useRoomStore} from './store';
import {Button} from '@sqlrooms/ui';
function Dashboard() {
const value = useRoomStore((state) => state.counter.value);
const increment = useRoomStore((state) => state.counter.increment);
return <Button onClick={increment}>Count: {value}</Button>;
}Imperative access
Use roomStore.getState() for non-reactive code (events, timers, async jobs).
ts
import {roomStore} from './store';
export function incrementLater() {
setTimeout(() => {
roomStore.getState().counter.increment();
}, 500);
}Inside components, useRoomStoreApi() gives you the raw store API:
tsx
import {useRoomStoreApi} from '@sqlrooms/room-store';
import {Button} from '@sqlrooms/ui';
function ResetButton() {
const store = useRoomStoreApi();
return (
<Button
onClick={() => {
// Example: imperative read from store
const current = store.getState().room.initialized;
console.log('initialized', current);
}}
>
Inspect store
</Button>
);
}Interfaces
Type Aliases
- LayoutTypes
- MosaicLayoutDirection
- MosaicLayoutParent
- MosaicLayoutNodeKey
- MosaicLayoutNode
- MosaicLayoutConfig
- LayoutConfig
- BaseRoomConfig
- DataSourceTypes
- BaseDataSource
- FileDataSource
- UrlDataSource
- SqlQueryDataSource
- DataSource
- LoadFile
- StandardLoadOptions
- SpatialLoadOptions
- SpatialLoadFileOptions
- StandardLoadFileOptions
- LoadFileOptions
- BaseRoomStoreState
- BaseRoomStore
- UseRoomStore
- CreateBaseRoomSliceProps
- CommandCliAdapterOptions
- CommandCliAdapter
- CommandMcpToolDescriptor
- CommandMcpAdapterOptions
- CommandMcpAdapter
- RoomCommandSurface
- RoomCommandInvocation
- RoomCommandInvocationOptions
- RoomCommandExecutionContext
- RoomCommandPredicate
- RoomCommandInputComponentProps
- RoomCommandInputComponent
- RoomCommandRiskLevel
- RoomCommandKeystrokes
- RoomCommandPolicyMetadata
- RoomCommandUiMetadata
- RoomCommandResult
- RoomCommandExecuteOutput
- RoomCommandMiddlewareNext
- RoomCommandMiddleware
- RoomCommandInvokeStartEvent
- RoomCommandInvokeSuccessEvent
- RoomCommandInvokeFailureEvent
- RoomCommandInvokeErrorEvent
- CreateCommandSliceProps
- RoomCommand
- RegisteredRoomCommand
- RoomCommandDescriptor
- RoomCommandListOptions
- CommandSliceState
- RoomCommandPortableSchema
- RoomStateProviderProps
- StateCreator
Variables
- MAIN_VIEW
- LayoutTypes
DEFAULT_MOSAIC_LAYOUT- MosaicLayoutDirection
- MosaicLayoutParent
- MosaicLayoutNodeKey
- MosaicLayoutNode
- MosaicLayoutConfig
- LayoutConfig
- DEFAULT_ROOM_TITLE
- BaseRoomConfig
- DataSourceTypes
- BaseDataSource
- FileDataSource
- UrlDataSource
- SqlQueryDataSource
- DataSource
- LoadFile
- StandardLoadOptions
- SpatialLoadOptions
- SpatialLoadFileOptions
- isSpatialLoadFileOptions
- StandardLoadFileOptions
- LoadFileOptions
createRoomSlicecreateBaseSlice- RoomStateContext
Functions
- createDefaultMosaicLayout
- isMosaicLayoutParent
- createDefaultBaseRoomConfig
- isFileDataSource
- isUrlDataSource
- isSqlQueryDataSource
- createBaseRoomSlice
- createSlice
- createRoomStore
- createRoomStoreCreator
- isRoomSliceWithInitialize
- isRoomSliceWithDestroy
- createCommandCliAdapter
- createCommandMcpAdapter
- createCommandSlice
- createRoomCommandExecutionContext
- hasCommandSliceState
- registerCommandsForOwner
- unregisterCommandsForOwner
- listCommandsFromStore
- invokeCommandFromStore
- validateCommandInput
- doesCommandRequireInput
- getCommandShortcut
- getCommandKeystrokes
- getCommandInputComponent
- resolveCommandPolicyMetadata
- exportCommandInputSchema
- RoomStateProvider
- useRoomStoreApi
- useBaseRoomStore
- createPersistHelpers
- persistSliceConfigs
