feathers-utils 1.10.0-0 → 1.10.0

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/README.md CHANGED
@@ -19,9 +19,11 @@ npm i feathers-utils
19
19
 
20
20
  ### Hooks
21
21
 
22
- - `checkMulti()`: throws if the request is **multi** data, but the service has `allowsMulti(method)` returns `false`
23
- - `runPerItem`
24
- - `setData({ allowUndefined: Boolean })`
22
+ - `checkMulti`: throws if the request is **multi** data, but the service has `allowsMulti(method)` returns `false`
23
+ - `createRelated`: simply create related items from a hook.
24
+ - `removeRelated`: simple remove related items from a hook. Basically `cascade` at feathers level.
25
+ - `runPerItem`: run a function for every item. Meant for `multi:true`.
26
+ - `setData`: map properties from `context` to `data`. Something like `userId: context.params.user.id`
25
27
 
26
28
  ### Mixins
27
29
 
@@ -1,3 +1,3 @@
1
1
  import type { HookContext } from "@feathersjs/feathers";
2
2
  import type { CreateRelatedOptions } from "../types";
3
- export declare function createRelated<S = Record<string, any>>({ service, multi, data }: CreateRelatedOptions<S>): (context: HookContext) => Promise<HookContext>;
3
+ export declare function createRelated<S = Record<string, any>>({ service, multi, data, createItemsInDataArraySeparately }: CreateRelatedOptions<S>): (context: HookContext) => Promise<HookContext>;
@@ -1,13 +1,16 @@
1
1
  import { checkContext } from "feathers-hooks-common";
2
2
  import { getItemsIsArray } from "../utils/getItemsIsArray";
3
- export function createRelated({ service, multi = true, data }) {
3
+ export function createRelated({ service, multi = true, data, createItemsInDataArraySeparately = true }) {
4
4
  if (!service || !data) {
5
5
  throw "initialize hook 'createRelated' completely!";
6
6
  }
7
7
  return async (context) => {
8
8
  checkContext(context, "after", undefined, "createRelated");
9
9
  const { items } = getItemsIsArray(context);
10
- const dataToCreate = await Promise.all(items.map(async (item) => data(item, context)));
10
+ let dataToCreate = (await Promise.all(items.map(async (item) => data(item, context)))).filter(x => !!x);
11
+ if (createItemsInDataArraySeparately) {
12
+ dataToCreate = dataToCreate.flat();
13
+ }
11
14
  if (!dataToCreate || dataToCreate.length <= 0) {
12
15
  return context;
13
16
  }
@@ -34,6 +34,7 @@ export interface CreateRelatedOptions<S = Record<string, any>> {
34
34
  service: keyof S;
35
35
  multi?: boolean;
36
36
  data: (item: any, context: HookContext) => Promisable<Record<string, any>>;
37
+ createItemsInDataArraySeparately?: boolean;
37
38
  }
38
39
  export interface InitDebounceMixinOptions {
39
40
  default: Partial<DebouncedStoreOptions>;
@@ -1,3 +1,3 @@
1
1
  import type { HookContext } from "@feathersjs/feathers";
2
2
  import type { CreateRelatedOptions } from "../types";
3
- export declare function createRelated<S = Record<string, any>>({ service, multi, data }: CreateRelatedOptions<S>): (context: HookContext) => Promise<HookContext>;
3
+ export declare function createRelated<S = Record<string, any>>({ service, multi, data, createItemsInDataArraySeparately }: CreateRelatedOptions<S>): (context: HookContext) => Promise<HookContext>;
@@ -12,14 +12,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.createRelated = void 0;
13
13
  const feathers_hooks_common_1 = require("feathers-hooks-common");
14
14
  const getItemsIsArray_1 = require("../utils/getItemsIsArray");
15
- function createRelated({ service, multi = true, data }) {
15
+ function createRelated({ service, multi = true, data, createItemsInDataArraySeparately = true }) {
16
16
  if (!service || !data) {
17
17
  throw "initialize hook 'createRelated' completely!";
18
18
  }
19
19
  return (context) => __awaiter(this, void 0, void 0, function* () {
20
20
  (0, feathers_hooks_common_1.checkContext)(context, "after", undefined, "createRelated");
21
21
  const { items } = (0, getItemsIsArray_1.getItemsIsArray)(context);
22
- const dataToCreate = yield Promise.all(items.map((item) => __awaiter(this, void 0, void 0, function* () { return data(item, context); })));
22
+ let dataToCreate = (yield Promise.all(items.map((item) => __awaiter(this, void 0, void 0, function* () { return data(item, context); })))).filter(x => !!x);
23
+ if (createItemsInDataArraySeparately) {
24
+ dataToCreate = dataToCreate.flat();
25
+ }
23
26
  if (!dataToCreate || dataToCreate.length <= 0) {
24
27
  return context;
25
28
  }
package/dist/types.d.ts CHANGED
@@ -34,6 +34,7 @@ export interface CreateRelatedOptions<S = Record<string, any>> {
34
34
  service: keyof S;
35
35
  multi?: boolean;
36
36
  data: (item: any, context: HookContext) => Promisable<Record<string, any>>;
37
+ createItemsInDataArraySeparately?: boolean;
37
38
  }
38
39
  export interface InitDebounceMixinOptions {
39
40
  default: Partial<DebouncedStoreOptions>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "feathers-utils",
3
- "version": "1.10.0-0",
3
+ "version": "1.10.0",
4
4
  "description": "Some utils for projects using '@feathersjs/feathers'",
5
5
  "author": "fratzinger",
6
6
  "repository": {
@@ -6,7 +6,8 @@ import { getItemsIsArray } from "../utils/getItemsIsArray";
6
6
  export function createRelated<S = Record<string, any>>({
7
7
  service,
8
8
  multi = true,
9
- data
9
+ data,
10
+ createItemsInDataArraySeparately = true
10
11
  }: CreateRelatedOptions<S>) {
11
12
  if (!service || !data) {
12
13
  throw "initialize hook 'createRelated' completely!";
@@ -16,9 +17,13 @@ export function createRelated<S = Record<string, any>>({
16
17
 
17
18
  const { items } = getItemsIsArray(context);
18
19
 
19
- const dataToCreate = await Promise.all(
20
+ let dataToCreate = (await Promise.all(
20
21
  items.map(async item => data(item, context))
21
- );
22
+ )).filter(x => !!x);
23
+
24
+ if (createItemsInDataArraySeparately) {
25
+ dataToCreate = dataToCreate.flat();
26
+ }
22
27
 
23
28
  if (!dataToCreate || dataToCreate.length <= 0) {
24
29
  return context;
package/src/types.ts CHANGED
@@ -47,6 +47,7 @@ export interface CreateRelatedOptions<S = Record<string, any>> {
47
47
  service: keyof S
48
48
  multi?: boolean
49
49
  data: (item: any, context: HookContext) => Promisable<Record<string, any>>
50
+ createItemsInDataArraySeparately?: boolean
50
51
  }
51
52
 
52
53
  //#endregion