@isoftdata/svelte-store-crud
Advanced tools
| import type { ConditionalKeys } from 'type-fest'; | ||
| import type { CrudRuneStore, CrudStore } from './store.svelte'; | ||
| export type CrudByKey<T> = Partial<Record<EntityIdValue<T>, ArrayCrudMap<T>>>; | ||
| export type CrudMapKey = 'created' | 'updated' | 'deleted'; | ||
| export type CrudType = 'created' | 'updated' | 'deleted' | 'undelete'; | ||
| export type EntityIdKey<T> = ConditionalKeys<T, string | number>; | ||
| export type EntityIdValue<T> = T[EntityIdKey<T>] extends string | number ? T[EntityIdKey<T>] : never; | ||
| export type EntityMap<T> = Record<EntityIdValue<T>, T>; | ||
| export type CrudMap<T> = Record<CrudMapKey, EntityMap<T>>; | ||
| export type ArrayCrudMap<T> = Record<CrudMapKey, Array<T>>; | ||
| export type MapEntityMap<T> = Map<EntityIdValue<T>, T>; | ||
| export type MapCrudMap<T> = Record<CrudMapKey, MapEntityMap<T>>; | ||
| /** Group your CRUD by a common key. Useful for if you're saving the CRUD entities as a child of another entity */ | ||
| export declare function groupCrudByKey<T, K extends EntityIdKey<T>>(crud: CrudRuneStore<T, K> | CrudStore<T, K>, key: K): CrudByKey<T>; | ||
| export declare function newArrayCrudMap<T>(): ArrayCrudMap<T>; | ||
| /** Creates a new CRUD Map, without the rest of the store logic */ | ||
| export declare const newCrudMap: <T>() => CrudMap<T>; | ||
| /** Creates a new Entity Map, without the rest of the store logic */ | ||
| export declare const newEntityMap: <T>() => EntityMap<T>; | ||
| /** Handles all the logic of adding entities to the specified crud map, with some guard rails. */ | ||
| export declare function doCrud<T, C extends CrudMap<T>>(crud: C, crudType: CrudType, entity: T, idKey: EntityIdKey<T>): C; |
| /** Group your CRUD by a common key. Useful for if you're saving the CRUD entities as a child of another entity */ | ||
| export function groupCrudByKey(crud, key) { | ||
| const grouped = {}; | ||
| crud.createdValues.forEach(entity => { | ||
| const id = entity[key] ?? null; | ||
| if (typeof id === 'string' || typeof id === 'number') { | ||
| grouped[id] ??= newArrayCrudMap(); | ||
| grouped[id].created.push(entity); | ||
| } | ||
| }); | ||
| crud.updatedValues.forEach(entity => { | ||
| const id = entity[key] ?? null; | ||
| if (typeof id === 'string' || typeof id === 'number') { | ||
| grouped[id] ??= newArrayCrudMap(); | ||
| grouped[id].updated.push(entity); | ||
| } | ||
| }); | ||
| crud.deletedValues.forEach(entity => { | ||
| const id = entity[key] ?? null; | ||
| if (typeof id === 'string' || typeof id === 'number') { | ||
| grouped[id] ??= newArrayCrudMap(); | ||
| grouped[id].deleted.push(entity); | ||
| } | ||
| }); | ||
| return grouped; | ||
| } | ||
| export function newArrayCrudMap() { | ||
| return { created: [], updated: [], deleted: [] }; | ||
| } | ||
| // #region stuff for old crud store | ||
| /** Creates a new CRUD Map, without the rest of the store logic */ | ||
| export const newCrudMap = () => ({ | ||
| created: {}, | ||
| updated: {}, | ||
| deleted: {}, | ||
| }); | ||
| /** Creates a new Entity Map, without the rest of the store logic */ | ||
| export const newEntityMap = () => ({}); | ||
| /** Handles all the logic of adding entities to the specified crud map, with some guard rails. */ | ||
| export function doCrud(crud, crudType, entity, idKey) { | ||
| const key = entity[idKey]; | ||
| if (crudType === 'deleted') { | ||
| delete crud.created[key]; | ||
| delete crud.updated[key]; | ||
| crud.deleted[key] = entity; | ||
| } | ||
| else if (crudType === 'created') { | ||
| crud.created[key] = entity; | ||
| delete crud.deleted[key]; | ||
| } | ||
| else if (crudType === 'undelete') { | ||
| delete crud.deleted[key]; | ||
| } | ||
| else if (crudType === 'updated') { | ||
| delete crud.deleted[key]; | ||
| if (crud.created[key]) { | ||
| crud.created[key] = entity; | ||
| } | ||
| else { | ||
| crud.updated[key] = entity; | ||
| } | ||
| } | ||
| return crud; | ||
| } | ||
| // #endregion |
@@ -1,16 +0,4 @@ | ||
| import type { ConditionalKeys, Simplify } from 'type-fest'; | ||
| import type { Simplify } from 'type-fest'; | ||
| import { type Writable } from 'svelte/store'; | ||
| export type CrudMapKey = 'created' | 'updated' | 'deleted'; | ||
| export type CrudType = 'created' | 'updated' | 'deleted' | 'undelete'; | ||
| export type EntityIdKey<T> = ConditionalKeys<T, string | number>; | ||
| export type EntityIdValue<T> = T[EntityIdKey<T>] extends string | number ? T[EntityIdKey<T>] : never; | ||
| export type EntityMap<T> = Record<EntityIdValue<T>, T>; | ||
| export type CrudMap<T> = Record<CrudMapKey, EntityMap<T>>; | ||
| export type ArrayCrudMap<T> = Record<CrudMapKey, Array<T>>; | ||
| /** Creates a new CRUD Map, without the rest of the store logic */ | ||
| export declare const newCrudMap: <T>() => CrudMap<T>; | ||
| /** Creates a new Entity Map, without the rest of the store logic */ | ||
| export declare const newEntityMap: <T>() => EntityMap<T>; | ||
| /** Handles all the logic of adding entities to the specified crud map, with some guard rails. */ | ||
| export declare function doCrud<T, C extends CrudMap<T>>(crud: C, crudType: CrudType, entity: T, idKey: EntityIdKey<T>): C; | ||
| import { type ArrayCrudMap, type CrudByKey, type CrudMap, type CrudType, type EntityIdKey, type EntityIdValue, type EntityMap, type MapCrudMap } from './utility'; | ||
| /** This is a Svelte store for tracking entities that have been Created, Updated, and Deleted. | ||
@@ -68,2 +56,3 @@ * | ||
| clear(type?: 'create' | 'update' | 'delete'): void; | ||
| groupByKey(key: EntityIdKey<T>): CrudByKey<T>; | ||
| toJSON(): { | ||
@@ -76,3 +65,2 @@ hasChanges: boolean; | ||
| } | ||
| type MapEntityMap<T> = Map<EntityIdValue<T>, T>; | ||
| type MapCrudMap<T> = Record<CrudMapKey, MapEntityMap<T>>; | ||
| export * from './utility'; |
+5
-34
| import { writable } from 'svelte/store'; | ||
| import { SvelteMap } from 'svelte/reactivity'; | ||
| /** Creates a new CRUD Map, without the rest of the store logic */ | ||
| export const newCrudMap = () => ({ | ||
| created: {}, | ||
| updated: {}, | ||
| deleted: {}, | ||
| }); | ||
| /** Creates a new Entity Map, without the rest of the store logic */ | ||
| export const newEntityMap = () => ({}); | ||
| /** Handles all the logic of adding entities to the specified crud map, with some guard rails. */ | ||
| export function doCrud(crud, crudType, entity, idKey) { | ||
| const key = entity[idKey]; | ||
| if (crudType === 'deleted') { | ||
| delete crud.created[key]; | ||
| delete crud.updated[key]; | ||
| crud.deleted[key] = entity; | ||
| } | ||
| else if (crudType === 'created') { | ||
| crud.created[key] = entity; | ||
| delete crud.deleted[key]; | ||
| } | ||
| else if (crudType === 'undelete') { | ||
| delete crud.deleted[key]; | ||
| } | ||
| else if (crudType === 'updated') { | ||
| delete crud.deleted[key]; | ||
| if (crud.created[key]) { | ||
| crud.created[key] = entity; | ||
| } | ||
| else { | ||
| crud.updated[key] = entity; | ||
| } | ||
| } | ||
| return crud; | ||
| } | ||
| import { doCrud, groupCrudByKey, newCrudMap, newEntityMap, } from './utility'; | ||
| /** This is a Svelte store for tracking entities that have been Created, Updated, and Deleted. | ||
@@ -222,2 +189,5 @@ * | ||
| } | ||
| groupByKey(key) { | ||
| return groupCrudByKey(this, key); | ||
| } | ||
| #doCrud(crudType, entity) { | ||
@@ -258,1 +228,2 @@ const id = this.#getEntityId(entity); | ||
| } | ||
| export * from './utility'; |
+1
-1
| { | ||
| "name": "@isoftdata/svelte-store-crud", | ||
| "version": "2.7.0", | ||
| "version": "2.8.0-beta.0", | ||
| "files": [ | ||
@@ -5,0 +5,0 @@ "dist", |
+4
-0
@@ -120,2 +120,6 @@ # Svelte Store CRUD | ||
| #### 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 | ||
| ```ts | ||
@@ -122,0 +126,0 @@ import { CrudRuneStore } from '@isoftdata/svelte-store-crud' |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
21042
11.97%8
33.33%380
13.43%166
2.47%2
100%