Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
* New (additional) methods:
  * `createMany()`
  * `refreshMany()`
  * `updateMany()`
  * `deleteMany()`
* `request` parameter will now accept an array of requests (to `forkJoin()` them) in:
  * `createMany()`
  * `readMany()`
  * `refreshMany()`
  * `updateMany()`
  * `deleteMany()`
  Requests with suffix `*many` will run in parallel (using `forkJoin()`). If you need to run them differently, you can use regular methods with the [creation operator](https://rxjs.dev/guide/operators#creation-operators-1) of your choice.
* A function can be used as a value for the `comparator` field in configuration options or as an argument for `setComparator()` method;
* New method for override: `postInit()` - will be called in the next microtask after `constructor()`.
  You can override and use it to call the methods declared in the subclass and still don't bother about `constructor()` overriding;
* `getTrackByFieldFn()` helper will not use fields with empty values;
* Jest has been configured to run tests in this repo - pull requests are welcome!
  • Loading branch information
e-oz committed Nov 26, 2022
1 parent 1937d93 commit 8192803
Show file tree
Hide file tree
Showing 34 changed files with 26,527 additions and 10,870 deletions.
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
legacy-peer-deps = true
strict-peer-dependencies = false
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
### 1.2.0
* New (additional) methods:
* `createMany()`
* `refreshMany()`
* `updateMany()`
* `deleteMany()`
* `request` parameter will now accept an array of requests (to `forkJoin()` them) in:
* `createMany()`
* `readMany()`
* `refreshMany()`
* `updateMany()`
* `deleteMany()`
Requests with suffix `*many` will run in parallel (using `forkJoin()`). If you need to run them differently, you can use regular methods with the [creation operator](https://rxjs.dev/guide/operators#creation-operators-1) of your choice.
* A function can be used as a value for the `comparator` field in configuration options or as an argument for `setComparator()` method;
* New method for override: `postInit()` - will be called in the next microtask after `constructor()`.
You can override and use it to call the methods declared in the subclass and still don't bother about `constructor()` overriding;
* `getTrackByFieldFn()` helper will not use fields with empty values;
* Jest has been configured to run tests in this repo - pull requests are welcome!

## 1.1.1
* `readMany()` should update `totalCountFetched` if provided

Expand Down
75 changes: 66 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,16 +411,13 @@ interface DeleteParams<T, R = unknown> {

## Additional Methods

### refresh()
Item will be updated without adding it to `updating` or `mutating` lists, and __without__ toggling `isReading` state field of the collection.
Item will be added to the `refreshing` list, triggering modifications of related state fields.
Designed to be used for "reloading" the item data without triggering "disabled" statuses of controls.
### createMany()
Like `create()`, but will run multiple requests in parallel.
#### Parameters object
```ts
interface RefreshParams<T> {
request: Observable<T>;
item: T;
onSuccess?: (item: T) => void;
interface CreateManyParams<T> {
request: Observable<FetchedItems<T> | T[]> | Observable<T>[];
onSuccess?: (items: T[]) => void;
onError?: (error: unknown) => void;
}
```
Expand All @@ -430,7 +427,7 @@ interface RefreshParams<T> {
### readOne()
Read and update/add one item.
Will toggle `isReading` state field of the collection.
Designed to be used in components with no guarantee that the item is already fetched from the server (and, therefore, needs to fetch the item first).
Designed to be used in components with no guarantee that the item is already fetched from the server (and, therefore, needs to fetch the item first).
#### Parameters object
```ts
interface ReadOneParams<T> {
Expand Down Expand Up @@ -458,6 +455,66 @@ interface ReadManyParams<T> {

---

### refresh()
Item will be updated without adding it to `updating` or `mutating` lists, and __without__ toggling `isReading` state field of the collection.
Item will be added to the `refreshing` list, triggering modifications of related state fields.
Designed to be used for "reloading" the item data without triggering "disabled" statuses of controls.
#### Parameters object
```ts
interface RefreshParams<T> {
request: Observable<T>;
item: T;
onSuccess?: (item: T) => void;
onError?: (error: unknown) => void;
}
```

---

### refreshMany()
Like `refresh()`, but for multiple items. Requests will run in parallel.
#### Parameters object
```ts
interface RefreshManyParams<T> {
request: Observable<FetchedItems<T> | T[]> | Observable<T>[];
items: Partial<T>[];
onSuccess?: (item: T[]) => void;
onError?: (error: unknown) => void;
}
```

---

### updateMany()
Like `update()`, but for multiple items. Requests will run in parallel.
Consecutive `refreshRequest` (if set) will be executed using `refreshMany()`.
#### Parameters object
```ts
interface UpdateManyParams<T> {
request: Observable<T[]> | Observable<T>[];
refreshRequest?: Observable<FetchedItems<T> | T[]>;
items: Partial<T>[];
onSuccess?: (item: T[]) => void;
onError?: (error: unknown) => void;
}
```

---

### deleteMany()
Like `delete()`, but will run multiple queries in parallel.
#### Parameters object
```ts
interface DeleteManyParams<T, R = unknown> {
request: Observable<R> | Observable<R>[];
items: Partial<T>[];
onSuccess?: (response: R[]) => void;
onError?: (error: unknown) => void;
}
```

---

### setUniqueStatus()
Set the status for an item.
Only one item in the collection can have this status.
Expand Down
29 changes: 2 additions & 27 deletions angular.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"version": 2,
"projects": {
"ngx-collection": {
"projectType": "library",
"root": "projects/ngx-collection",
"sourceRoot": "projects/ngx-collection/src",
"prefix": "lib",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:ng-packagr",
"options": {
"project": "projects/ngx-collection/ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "projects/ngx-collection/tsconfig.lib.prod.json"
},
"development": {
"tsConfig": "projects/ngx-collection/tsconfig.lib.json"
}
},
"defaultConfiguration": "production"
}
}
}

"ngx-collection": "libs/ngx-collection"
}
}
69 changes: 69 additions & 0 deletions decorate-angular-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* This file decorates the Angular CLI with the Nx CLI to enable features such as computation caching
* and faster execution of tasks.
*
* It does this by:
*
* - Patching the Angular CLI to warn you in case you accidentally use the undecorated ng command.
* - Symlinking the ng to nx command, so all commands run through the Nx CLI
* - Updating the package.json postinstall script to give you control over this script
*
* The Nx CLI decorates the Angular CLI, so the Nx CLI is fully compatible with it.
* Every command you run should work the same when using the Nx CLI, except faster.
*
* Because of symlinking you can still type `ng build/test/lint` in the terminal. The ng command, in this case,
* will point to nx, which will perform optimizations before invoking ng. So the Angular CLI is always invoked.
* The Nx CLI simply does some optimizations before invoking the Angular CLI.
*
* To opt out of this patch:
* - Replace occurrences of nx with ng in your package.json
* - Remove the script from your postinstall script in your package.json
* - Delete and reinstall your node_modules
*/

const fs = require('fs');
const os = require('os');
const cp = require('child_process');
const isWindows = os.platform() === 'win32';
let output;
try {
output = require('@nrwl/workspace').output;
} catch (e) {
console.warn('Angular CLI could not be decorated to enable computation caching. Please ensure @nrwl/workspace is installed.');
process.exit(0);
}

/**
* Symlink of ng to nx, so you can keep using `ng build/test/lint` and still
* invoke the Nx CLI and get the benefits of computation caching.
*/
function symlinkNgCLItoNxCLI() {
try {
const ngPath = './node_modules/.bin/ng';
const nxPath = './node_modules/.bin/nx';
if (isWindows) {
/**
* This is the most reliable way to create symlink-like behavior on Windows.
* Such that it works in all shells and works with npx.
*/
['', '.cmd', '.ps1'].forEach(ext => {
if (fs.existsSync(nxPath + ext)) fs.writeFileSync(ngPath + ext, fs.readFileSync(nxPath + ext));
});
} else {
// If unix-based, symlink
cp.execSync(`ln -sf ./nx ${ngPath}`);
}
}
catch(e) {
output.error({ title: 'Unable to create a symlink from the Angular CLI to the Nx CLI:' + e.message });
throw e;
}
}

try {
symlinkNgCLItoNxCLI();
require('nx/src/adapter/decorate-cli').decorateCli();
output.log({ title: 'Angular CLI has been decorated to enable computation caching.' });
} catch(e) {
output.error({ title: 'Decoration of the Angular CLI did not complete successfully' });
}
1 change: 0 additions & 1 deletion index.ts

This file was deleted.

5 changes: 5 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getJestProjects } from '@nrwl/jest';

export default {
projects: getJestProjects()
};
3 changes: 3 additions & 0 deletions jest.preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const nxPreset = require('@nrwl/jest/preset').default;

module.exports = {...nxPreset}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -411,16 +411,13 @@ interface DeleteParams<T, R = unknown> {

## Additional Methods

### refresh()
Item will be updated without adding it to `updating` or `mutating` lists, and __without__ toggling `isReading` state field of the collection.
Item will be added to the `refreshing` list, triggering modifications of related state fields.
Designed to be used for "reloading" the item data without triggering "disabled" statuses of controls.
### createMany()
Like `create()`, but will run multiple requests in parallel.
#### Parameters object
```ts
interface RefreshParams<T> {
request: Observable<T>;
item: T;
onSuccess?: (item: T) => void;
interface CreateManyParams<T> {
request: Observable<FetchedItems<T> | T[]> | Observable<T>[];
onSuccess?: (items: T[]) => void;
onError?: (error: unknown) => void;
}
```
Expand All @@ -430,7 +427,7 @@ interface RefreshParams<T> {
### readOne()
Read and update/add one item.
Will toggle `isReading` state field of the collection.
Designed to be used in components with no guarantee that the item is already fetched from the server (and, therefore, needs to fetch the item first).
Designed to be used in components with no guarantee that the item is already fetched from the server (and, therefore, needs to fetch the item first).
#### Parameters object
```ts
interface ReadOneParams<T> {
Expand Down Expand Up @@ -458,6 +455,66 @@ interface ReadManyParams<T> {

---

### refresh()
Item will be updated without adding it to `updating` or `mutating` lists, and __without__ toggling `isReading` state field of the collection.
Item will be added to the `refreshing` list, triggering modifications of related state fields.
Designed to be used for "reloading" the item data without triggering "disabled" statuses of controls.
#### Parameters object
```ts
interface RefreshParams<T> {
request: Observable<T>;
item: T;
onSuccess?: (item: T) => void;
onError?: (error: unknown) => void;
}
```

---

### refreshMany()
Like `refresh()`, but for multiple items. Requests will run in parallel.
#### Parameters object
```ts
interface RefreshManyParams<T> {
request: Observable<FetchedItems<T> | T[]> | Observable<T>[];
items: Partial<T>[];
onSuccess?: (item: T[]) => void;
onError?: (error: unknown) => void;
}
```

---

### updateMany()
Like `update()`, but for multiple items. Requests will run in parallel.
Consecutive `refreshRequest` (if set) will be executed using `refreshMany()`.
#### Parameters object
```ts
interface UpdateManyParams<T> {
request: Observable<T[]> | Observable<T>[];
refreshRequest?: Observable<FetchedItems<T> | T[]>;
items: Partial<T>[];
onSuccess?: (item: T[]) => void;
onError?: (error: unknown) => void;
}
```

---

### deleteMany()
Like `delete()`, but will run multiple queries in parallel.
#### Parameters object
```ts
interface DeleteManyParams<T, R = unknown> {
request: Observable<R> | Observable<R>[];
items: Partial<T>[];
onSuccess?: (response: R[]) => void;
onError?: (error: unknown) => void;
}
```

---

### setUniqueStatus()
Set the status for an item.
Only one item in the collection can have this status.
Expand Down
21 changes: 21 additions & 0 deletions libs/ngx-collection/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable */
export default {
displayName: 'ngx-collection',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
},
transform: {
'^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
},
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
],
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-collection",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"author": {
"name": "Evgeniy OZ",
Expand All @@ -13,9 +13,10 @@
"keywords": ["angular", "collections"],
"private": false,
"peerDependencies": {
"@angular/core": "^15.0.0",
"@ngrx/component-store": "^15.0.0-beta.1",
"@ngrx/effects": "^15.0.0-beta.1",
"@angular/core": "^15.0.1",
"@ngrx/component-store": "^15.0.0-rc.0",
"@ngrx/effects": "^15.0.0-rc.0",
"@ngrx/store": "^15.0.0-rc.0",
"rxjs": "^7.5.7"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 8192803

Please sign in to comment.