@craft-ng/core 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fesm2022/craft-ng-core.mjs +159 -1
- package/fesm2022/craft-ng-core.mjs.map +1 -1
- package/package.json +1 -1
- package/types/craft-ng-core.d.ts +491 -366
|
@@ -10387,9 +10387,167 @@ function insertFormSubmit(submitCraftResource, config) {
|
|
|
10387
10387
|
};
|
|
10388
10388
|
}
|
|
10389
10389
|
|
|
10390
|
+
/**
|
|
10391
|
+
* Creates an insertion that adds entity collection management methods to state, query, or queryParam primitives.
|
|
10392
|
+
*
|
|
10393
|
+
* Provides type-safe manipulation of arrays of entities with operations like add, remove, update, and upsert.
|
|
10394
|
+
* Supports nested properties via dot notation paths and custom entity identifiers.
|
|
10395
|
+
*
|
|
10396
|
+
* @template State - The state type (array or object containing arrays)
|
|
10397
|
+
* @template K - The type of entity identifiers (string or number)
|
|
10398
|
+
* @template PreviousInsertionsOutputs - Combined outputs from previous insertions
|
|
10399
|
+
* @template EntityHelperFns - Tuple type of entity utility functions to expose
|
|
10400
|
+
* @template StateIdentifier - Type of identifier function for parallel queries
|
|
10401
|
+
* @template Path - Dot-notation path to nested array (inferred from state structure)
|
|
10402
|
+
*
|
|
10403
|
+
* @param config - Configuration object
|
|
10404
|
+
* @param config.methods - Array of entity utility functions (addOne, removeOne, updateOne, etc.) to expose as methods
|
|
10405
|
+
* @param config.identifier - Optional custom function to extract unique ID from entities.
|
|
10406
|
+
* Defaults to `entity.id` for objects or `entity` for primitives
|
|
10407
|
+
* @param config.path - Optional dot-notation path to nested array property (e.g., 'catalog.products').
|
|
10408
|
+
* Method names are prefixed with camelCase path when provided
|
|
10409
|
+
*
|
|
10410
|
+
* @returns Insertion function that adds entity management methods to the primitive
|
|
10411
|
+
*
|
|
10412
|
+
* @example
|
|
10413
|
+
* // Basic usage with primitives
|
|
10414
|
+
* const tags = state(
|
|
10415
|
+
* [] as string[],
|
|
10416
|
+
* insertEntities({
|
|
10417
|
+
* methods: [addOne, addMany, removeOne],
|
|
10418
|
+
* })
|
|
10419
|
+
* );
|
|
10420
|
+
* tags.addOne({ entity: 'typescript' });
|
|
10421
|
+
* tags.addMany({ newEntities: ['angular', 'signals'] });
|
|
10422
|
+
*
|
|
10423
|
+
* @example
|
|
10424
|
+
* // With objects having default id property
|
|
10425
|
+
* interface Product {
|
|
10426
|
+
* id: string;
|
|
10427
|
+
* name: string;
|
|
10428
|
+
* price: number;
|
|
10429
|
+
* }
|
|
10430
|
+
* const products = state(
|
|
10431
|
+
* [] as Product[],
|
|
10432
|
+
* insertEntities({
|
|
10433
|
+
* methods: [addOne, setOne, removeOne],
|
|
10434
|
+
* })
|
|
10435
|
+
* );
|
|
10436
|
+
* products.addOne({ entity: { id: '1', name: 'Laptop', price: 999 } });
|
|
10437
|
+
*
|
|
10438
|
+
* @example
|
|
10439
|
+
* // With custom identifier
|
|
10440
|
+
* interface User {
|
|
10441
|
+
* uuid: string;
|
|
10442
|
+
* name: string;
|
|
10443
|
+
* }
|
|
10444
|
+
* const users = state(
|
|
10445
|
+
* [] as User[],
|
|
10446
|
+
* insertEntities({
|
|
10447
|
+
* methods: [setOne, removeOne],
|
|
10448
|
+
* identifier: (user) => user.uuid,
|
|
10449
|
+
* })
|
|
10450
|
+
* );
|
|
10451
|
+
*
|
|
10452
|
+
* @example
|
|
10453
|
+
* // With nested path
|
|
10454
|
+
* interface Catalog {
|
|
10455
|
+
* total: number;
|
|
10456
|
+
* products: Array<{ id: string; name: string }>;
|
|
10457
|
+
* }
|
|
10458
|
+
* const catalog = state(
|
|
10459
|
+
* { total: 0, products: [] } as Catalog,
|
|
10460
|
+
* insertEntities({
|
|
10461
|
+
* methods: [addMany, removeOne],
|
|
10462
|
+
* path: 'products',
|
|
10463
|
+
* })
|
|
10464
|
+
* );
|
|
10465
|
+
* catalog.productsAddMany({ newEntities: [{ id: '1', name: 'Item' }] });
|
|
10466
|
+
*
|
|
10467
|
+
* @example
|
|
10468
|
+
* // With parallel queries
|
|
10469
|
+
* const userQuery = query(
|
|
10470
|
+
* {
|
|
10471
|
+
* params: () => 'userId',
|
|
10472
|
+
* identifier: (params) => params,
|
|
10473
|
+
* loader: async ({ params }) => fetchUserPosts(params),
|
|
10474
|
+
* },
|
|
10475
|
+
* insertEntities({
|
|
10476
|
+
* methods: [addOne],
|
|
10477
|
+
* })
|
|
10478
|
+
* );
|
|
10479
|
+
* userQuery.addOne({
|
|
10480
|
+
* select: 'user-123', // Target specific query instance
|
|
10481
|
+
* entity: { id: 'post-1', title: 'New Post' },
|
|
10482
|
+
* });
|
|
10483
|
+
*
|
|
10484
|
+
* @see {@link https://github.com/ng-angular-stack/ng-craft/blob/main/apps/docs/insertions/insert-entities.md | insertEntities Documentation}
|
|
10485
|
+
*/
|
|
10486
|
+
function insertEntities(config) {
|
|
10487
|
+
return (context) => {
|
|
10488
|
+
const methods = {};
|
|
10489
|
+
const hasPath = 'path' in config;
|
|
10490
|
+
const path = hasPath ? config.path : undefined;
|
|
10491
|
+
const pathKeys = path ? path.split('.') : undefined;
|
|
10492
|
+
const pathMethodPrefix = pathKeys
|
|
10493
|
+
? pathKeys.reduce((acc, key, index) => index === 0 ? key : `${acc}${key[0].toUpperCase()}${key.slice(1)}`, '')
|
|
10494
|
+
: undefined;
|
|
10495
|
+
for (const helperFn of config.methods) {
|
|
10496
|
+
const helperName = helperFn.name;
|
|
10497
|
+
if (!helperName) {
|
|
10498
|
+
continue;
|
|
10499
|
+
}
|
|
10500
|
+
const methodName = hasPath
|
|
10501
|
+
? `${pathMethodPrefix}${helperName[0].toUpperCase()}${helperName.slice(1)}`
|
|
10502
|
+
: helperName;
|
|
10503
|
+
methods[methodName] = (payload) => {
|
|
10504
|
+
context.update((state) => {
|
|
10505
|
+
const hasSelect = 'select' in payload && payload.select;
|
|
10506
|
+
const targetState = hasSelect
|
|
10507
|
+
? state[payload.select]
|
|
10508
|
+
: state;
|
|
10509
|
+
const entities = pathKeys
|
|
10510
|
+
? getNestedStateValue({
|
|
10511
|
+
state: targetState,
|
|
10512
|
+
keysPath: pathKeys,
|
|
10513
|
+
})
|
|
10514
|
+
: targetState;
|
|
10515
|
+
const updatedEntities = helperFn({
|
|
10516
|
+
...payload,
|
|
10517
|
+
entities,
|
|
10518
|
+
identifier: config.identifier,
|
|
10519
|
+
});
|
|
10520
|
+
if (hasSelect) {
|
|
10521
|
+
const updatedTargetState = pathKeys
|
|
10522
|
+
? createNestedStateUpdate({
|
|
10523
|
+
state: targetState,
|
|
10524
|
+
keysPath: pathKeys,
|
|
10525
|
+
value: updatedEntities,
|
|
10526
|
+
})
|
|
10527
|
+
: updatedEntities;
|
|
10528
|
+
return {
|
|
10529
|
+
...state,
|
|
10530
|
+
[payload.select]: updatedTargetState,
|
|
10531
|
+
};
|
|
10532
|
+
}
|
|
10533
|
+
if (pathKeys) {
|
|
10534
|
+
return createNestedStateUpdate({
|
|
10535
|
+
state,
|
|
10536
|
+
keysPath: pathKeys,
|
|
10537
|
+
value: updatedEntities,
|
|
10538
|
+
});
|
|
10539
|
+
}
|
|
10540
|
+
return updatedEntities;
|
|
10541
|
+
});
|
|
10542
|
+
};
|
|
10543
|
+
}
|
|
10544
|
+
return methods;
|
|
10545
|
+
};
|
|
10546
|
+
}
|
|
10547
|
+
|
|
10390
10548
|
/**
|
|
10391
10549
|
* Generated bundle index. Do not edit.
|
|
10392
10550
|
*/
|
|
10393
10551
|
|
|
10394
|
-
export { CRAFT_EXCEPTION_SYMBOL, EXTERNALLY_PROVIDED, EmptyContext, GlobalPersisterHandlerService, STORE_CONFIG_TOKEN, SourceBrand, SourceBranded, VALIDATOR_OUTPUT_SYMBOL, addMany, addOne, afterRecomputation, asyncProcess, cAsyncValidate, cAsyncValidator, cEmail, cMax, cMaxLength, cMin, cMinLength, cPattern, cRequired, cValidate, cValidator, capitalize, computedIds, computedSource, computedTotal, contract, craft, craftAsyncProcesses, craftComputedStates, craftException, craftFactoryEntries, craftInject, craftInputs, craftMutations, craftQuery, craftQueryParam, craftQueryParams, craftSetAllQueriesParamsStandalone, craftSources, craftState, createMethodHandlers, fromEventToSource$, injectService, insertForm, insertFormAttributes, insertFormSubmit, insertLocalStoragePersister, insertNoopTypingAnchor, insertPaginationPlaceholderData, insertReactOnMutation, insertSelect, insertSelectFormTree, isCraftException, isSource, linkedSource, localStoragePersister, map, mapOne, mutation, on$, partialContext, query, queryParam, reactiveWritableSignal, removeAll, removeMany, removeOne, resourceById, serializeQueryParams, serializedQueryParamsObjectToString, setAll, setMany, setOne, signalSource, source$, sourceFromEvent, stackedSource, state, toInject, toSource, toggleMany, toggleOne, updateMany, updateOne, upsertMany, upsertOne, validatedFormValueSymbol };
|
|
10552
|
+
export { CRAFT_EXCEPTION_SYMBOL, EXTERNALLY_PROVIDED, EmptyContext, GlobalPersisterHandlerService, STORE_CONFIG_TOKEN, SourceBrand, SourceBranded, VALIDATOR_OUTPUT_SYMBOL, addMany, addOne, afterRecomputation, asyncProcess, cAsyncValidate, cAsyncValidator, cEmail, cMax, cMaxLength, cMin, cMinLength, cPattern, cRequired, cValidate, cValidator, capitalize, computedIds, computedSource, computedTotal, contract, craft, craftAsyncProcesses, craftComputedStates, craftException, craftFactoryEntries, craftInject, craftInputs, craftMutations, craftQuery, craftQueryParam, craftQueryParams, craftSetAllQueriesParamsStandalone, craftSources, craftState, createMethodHandlers, fromEventToSource$, injectService, insertEntities, insertForm, insertFormAttributes, insertFormSubmit, insertLocalStoragePersister, insertNoopTypingAnchor, insertPaginationPlaceholderData, insertReactOnMutation, insertSelect, insertSelectFormTree, isCraftException, isSource, linkedSource, localStoragePersister, map, mapOne, mutation, on$, partialContext, query, queryParam, reactiveWritableSignal, removeAll, removeMany, removeOne, resourceById, serializeQueryParams, serializedQueryParamsObjectToString, setAll, setMany, setOne, signalSource, source$, sourceFromEvent, stackedSource, state, toInject, toSource, toggleMany, toggleOne, updateMany, updateOne, upsertMany, upsertOne, validatedFormValueSymbol };
|
|
10395
10553
|
//# sourceMappingURL=craft-ng-core.mjs.map
|