Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .changeset/pink-waves-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@data-client/endpoint': patch
'@data-client/graphql': patch
'@data-client/rest': patch
---

Add Collection.remove

```ts
ctrl.set(MyResource.getList.schema.remove, { id });
```

```ts
const removeItem = MyResource.delete.extend({
schema: MyResource.getList.schema.remove
})
```
14 changes: 14 additions & 0 deletions docs/rest/api/Collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,20 @@ A creation schema that places at the _end_ of this collection

A creation schema that places at the _start_ of this collection

### remove

Remove item[s] from a collection by value.

```ts
ctrl.set(MyResource.getList.schema.remove, { id });
```

```ts
const removeItem = MyResource.delete.extend({
schema: MyResource.getList.schema.remove
})
```

### assign

A creation schema that [assigns](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
Expand Down
5 changes: 5 additions & 0 deletions packages/endpoint/src/schemaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ export interface CollectionInterface<
*/
unshift: CollectionArrayAdder<S>;

/** Schema to remove (by value) from a Collection
* @see https://dataclient.io/rest/api/Collection#remove
*/
remove: CollectionArrayAdder<S>;

/** Schema to merge with a Values Collection
* @see https://dataclient.io/rest/api/Collection#assign
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/endpoint/src/schemas/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const unshiftMerge = (existing: any, incoming: any) => {
const valuesMerge = (existing: any, incoming: any) => {
return { ...existing, ...incoming };
};
const removeMerge = (existing: Array<any>, incoming: any) => {
return existing.filter((item: any) => !incoming.includes(item));
};

const createArray = (value: any) => [...value];
const createValue = (value: any) => ({ ...value });

Expand Down Expand Up @@ -49,6 +53,9 @@ export default class CollectionSchema<
declare assign: S extends ValuesType<any> ? CollectionSchema<S, Args, Parent>
: undefined;

declare remove: S extends ArrayType<any> ? CollectionSchema<S, Args, Parent>
: undefined;

addWith<P extends any[] = Args>(
merge: (existing: any, incoming: any) => any,
createCollectionFilter?: (
Expand Down Expand Up @@ -118,6 +125,7 @@ export default class CollectionSchema<
this.createIfValid = createArray;
this.push = CreateAdder(this, pushMerge);
this.unshift = CreateAdder(this, unshiftMerge);
this.remove = CreateAdder(this, removeMerge);
} else if (schema instanceof Values) {
this.createIfValid = createValue;
this.assign = CreateAdder(this, valuesMerge);
Expand Down
Loading