Function: createObjectStore()
Call Signature
createObjectStore<
Context,T,CacheDisabled>(options,context):IObjectStore<T,CacheDisabled> &ContextReturn<Context>
Defined in: store/src/createObjectStore.ts:96
Creates a IObjectStore instance initialized from a plain object.
This factory method automatically configures parse and stringify logic to treat the underlying storage as a serialized object, while providing a type-safe Map-like interface for individual properties.
This default behavior can be overridden by providing custom parse and stringify implementations in options.
Type Parameters
Context
Context extends object | ((store) => object)
The type of the context object or factory function.
T
T extends object = Record<string, unknown>
(optional) The structure of the object being stored. Can auto-infer from options.initialValue.
CacheDisabled
CacheDisabled extends boolean = false
Whether store data caching is disabled.
Parameters
options
ObjectStore_Options<T, CacheDisabled> | null | undefined
(optional) Configuration options for the storage instance. See ObjectStore_Options for more.
context
Context & ContextValidate<Context, IObjectStore<T, CacheDisabled>>
(optional) A plain object or a factory function that returns an object.
Purpose: Use context to encapsulate domain-specific business logic, helper methods, or non-reactive state directly into the store instance.
Behavior: See createStore.
Returns
IObjectStore<T, CacheDisabled> & ContextReturn<Context>
A new Store instance mapped to the object's keys and values.
Examples
Basic property-based access
import { createObjectStore } from '@superutils/store'
const userStore = createObjectStore('user', {
initialValue: {
age: 99,
name: 'Ninety Nine'
}
})
// Keys are strictly inferred from the interface
const name = userStore.get('name') // Type: string | undefined
console.log(name) // Prints: 'Ninety Nine'
// Update properties with type safety
userStore.set('age', 100) // Only numbers are accepted for 'age'
// Export the underlying data back to a plain object
const user = userStore.toObject()
console.log(user) // { age: 100, name: 'Ninety Nine' }Usage with context
import { createObjectStore } from '@superutils/store'
const getContext = store => ({
isAdmin: () => !!store.get('roles')?.includes('admin'),
promoteToAdmin() {
if (this.isAdmin()) return
// Use functional updates to safely modify the roles array
store.set('roles', (prev = []) => [...prev, 'admin'])
}
}
// Functional context allows you to attach business logic directly to the store
const userStore = createObjectStore(
'user',
{
initialValue: {
age: 25,
name: 'Jane Doe',
roles: ['guest']
},
},
getContext
)
// Logic is encapsulated and easy to invoke
userStore.promoteToAdmin()
console.log(userStore.get('roles')) // ['guest', 'admin']Call Signature
createObjectStore<
T,CacheDisabled>(options?):IObjectStore<T,CacheDisabled>
Defined in: store/src/createObjectStore.ts:108
Creates a IObjectStore instance initialized from a plain object.
This factory method automatically configures parse and stringify logic to treat the underlying storage as a serialized object, while providing a type-safe Map-like interface for individual properties.
This default behavior can be overridden by providing custom parse and stringify implementations in options.
Type Parameters
T
T extends object = Record<string, unknown>
(optional) The structure of the object being stored. Can auto-infer from options.initialValue.
CacheDisabled
CacheDisabled extends boolean = false
Whether store data caching is disabled.
Parameters
options?
ObjectStore_Options<T, CacheDisabled>
(optional) Configuration options for the storage instance. See ObjectStore_Options for more.
Returns
IObjectStore<T, CacheDisabled>
A new Store instance mapped to the object's keys and values.
Examples
Basic property-based access
import { createObjectStore } from '@superutils/store'
const userStore = createObjectStore('user', {
initialValue: {
age: 99,
name: 'Ninety Nine'
}
})
// Keys are strictly inferred from the interface
const name = userStore.get('name') // Type: string | undefined
console.log(name) // Prints: 'Ninety Nine'
// Update properties with type safety
userStore.set('age', 100) // Only numbers are accepted for 'age'
// Export the underlying data back to a plain object
const user = userStore.toObject()
console.log(user) // { age: 100, name: 'Ninety Nine' }Usage with context
import { createObjectStore } from '@superutils/store'
const getContext = store => ({
isAdmin: () => !!store.get('roles')?.includes('admin'),
promoteToAdmin() {
if (this.isAdmin()) return
// Use functional updates to safely modify the roles array
store.set('roles', (prev = []) => [...prev, 'admin'])
}
}
// Functional context allows you to attach business logic directly to the store
const userStore = createObjectStore(
'user',
{
initialValue: {
age: 25,
name: 'Jane Doe',
roles: ['guest']
},
},
getContext
)
// Logic is encapsulated and easy to invoke
userStore.promoteToAdmin()
console.log(userStore.get('roles')) // ['guest', 'admin']