@isoftdata/svelte-store-crud
v2.8.0
Published
This is a Svelte store for tracking CRUD operations.
Maintainers
Keywords
Readme
Svelte Store CRUD
This is a Svelte store for tracking CRUD operations.
For example, if you have a map of entities and want to track when they are created, updated, and deleted without having to add a bunch of boolean flags to each entity, this store can help.
This store's value is an object containing three maps of objects, created, updated, and deleted.
Install
npm i @isoftdata/svelte-store-crudBreaking Changes
2.0.0
- Require Svelte 5
- Deprecate the old
makeCrudStorefunction. Use theCrudRuneStoreclass instead
API
CrudRuneStore<T, K>
const crudStore = new CrudRuneStore<Entity, 'id'>('id')Creates a CRUD store for tracking changes to objects of type T, tracked by a key of type K.
This has the same API as the Svelte 4 version powered by Svelte stores, except for the subscribe function as it is not needed. Since it is written with runes, all fields and functions should be reactive if referenced in an effect.
Properties on the store are described below:
create(entity: T): void
Add an entity to the created map.
update(entity: T): void
Add an entity to the updated map.
If you call this function with an entity whose id is in the created map, it will update it in that map instead of adding it to the updated map.
delete(entity: T): void
Add an entity to the deleted map.
If you call this function with an entity whose id is in the created map, that entity will be removed from the created map, and will be put in the deleted map.
If you call this function with an entity whose id is in the updated map, it will be removed from the updated map and added to the deleted map.
unDelete(entity: T): void
Removes an entity from the deleted map.
This does not add it back to the created or updated map if it was in one before
get createdValues(): T[]
Get a list of all values in the created map
get updatedValues(): T[]
Get a list of all values in the updated map
get deletedValues(): T[]
Get a list of all values in the deleted map
get createdEntries(): T[]
Get a list of all entries in the created map
get updatedEntries(): T[]
Get a list of all entries in the updated map
get deletedEntries(): T[]
Get a list of all entries in the deleted map
hasChanges(entity?: EntityOrId<T, K> | Array<EntityOrId<T, K>>): boolean
If entity is passed, returns true if that entity is created/updated deleted. Otherwise, returns true if there are any entity changes in the store
isCreated(entity: EntityOrId<T, K>): boolean
Returns true if the passed entity is in the created map
isUpdated(entity: EntityOrId<T, K>): boolean
Returns true if the passed entity is in the updated map
isDeleted(entity: EntityOrId<T, K>): boolean
Returns true if the passed entity is in the deleted map
getCreated(entity: EntityOrId<T, K>): T | undefined
Returns the entity if it is in the created map
getUpdated(entity: EntityOrId<T, K>): T | undefined
Returns the entity if it is in the updated map
getDeleted(entity: EntityOrId<T, K>): T | undefined
Returns the entity if it is in the deleted map
clear(type?: 'create' | 'update' | 'delete'): void
Clear the created, updated, and deleted maps. Or, if specified, clear just one.
groupByKey(key: EntityIdKJey<T>): CrudByKey<T>
Groups your CRUD by another key. Useful for if you're saving your CRUD as a child of another entity and want to group it by that entity's ID
import { CrudRuneStore } from '@isoftdata/svelte-store-crud'
type Entity = {
id: string
name: string
}
const crudStore = new CrudRuneStore<Entity, 'id'>('id')
// Create an entity with id = '1'
crudStore.create({
id: '1',
name: 'Entity 1'
})
// Update an entity with id = '1'
crudStore.update({
id: '1',
name: 'Entity 1 Updated'
})
// Delete an entity with id = '1'
crudStore.delete({
id: '1',
name: 'Entity 1 Updated'
})
// In a Svelte file, get the values reactively whenever the store is updated
$inspect(crudStore.createdValues)
$inspect(crudStore.updatedValues)
$inspect(crudStore.deletedValues)
$inspect(crudStore.hasChanges())
newCrudMap
Creates the map used internally in the CRUD store, but without all the store logic
doCrud
Does all the logic of adding, removing, and updating things in a "CRUD map" without all the store logic
