@tech4good/angular-schematics 0.0.0 → 13.0.1

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.
@@ -1,525 +1,525 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.entity = void 0;
4
- const camelcase_formatter_1 = require("../shared/camelcase-formatter");
5
- const statements_adder_1 = require("../shared/statements-adder");
6
- const fs = require('fs');
7
- // You don't have to export the function as default. You can also have more than one rule factory
8
- // per file.
9
- function entity(_options) {
10
- return (tree, _context) => {
11
- const { name, description } = _options;
12
- const camelName = camelcase_formatter_1.CamelCaseFormatter(name, false);
13
- const lowerCamelName = camelcase_formatter_1.CamelCaseFormatter(name, true);
14
- const ActionsFile = `import { Action } from '@ngrx/store';
15
- import { LoadAction } from '../app.actions';
16
- import { ${camelName} } from './${name}.model';
17
-
18
- export enum ${camelName}ActionTypes {
19
- LOAD = '[${camelName}] load ${lowerCamelName}',
20
- LOAD_SUCCESS = '[${camelName}] load ${lowerCamelName} success',
21
- LOAD_FAIL = '[${camelName}] load ${lowerCamelName} fail',
22
- ADD = '[${camelName}] add ${lowerCamelName}',
23
- ADD_SUCCESS = '[${camelName}] add ${lowerCamelName} success',
24
- ADD_FAIL = '[${camelName}] add ${lowerCamelName} fail',
25
- UPDATE = '[${camelName}] update ${lowerCamelName}',
26
- UPDATE_SUCCESS = '[${camelName}] update ${lowerCamelName} success',
27
- UPDATE_FAIL = '[${camelName}] update ${lowerCamelName} fail',
28
- UPSERT = '[${camelName}] upsert ${lowerCamelName}',
29
- UPSERT_SUCCESS = '[${camelName}] upsert ${lowerCamelName} success',
30
- UPSERT_FAIL = '[${camelName}] upsert ${lowerCamelName} fail',
31
- REMOVE = '[${camelName}] remove ${lowerCamelName}',
32
- REMOVE_SUCCESS = '[${camelName}] remove ${lowerCamelName} success',
33
- REMOVE_FAIL = '[${camelName}] remove ${lowerCamelName} fail',
34
- LOADED = '[${camelName}] loaded',
35
- ADDED = '[${camelName}] added',
36
- MODIFIED = '[${camelName}] modified',
37
- REMOVED = '[${camelName}] removed',
38
- }
39
-
40
- // See Firebase Docs for current constraints on valid queries
41
- export class Load${camelName} implements Action {
42
- readonly type = ${camelName}ActionTypes.LOAD;
43
- constructor(
44
- // property, comparator, value
45
- public queryParams: [string, string, any][],
46
- public queryOptions: {
47
- orderBy?: string | [string, string],
48
- limit?: number,
49
- startAt?: string,
50
- startAfter?: string,
51
- endAt?: string,
52
- endBefore?: string,
53
- },
54
- public correlationId: string,
55
- public followupActions?: (${lowerCamelName}: ${camelName}) => LoadAction[],
56
- ) {}
57
- }
58
-
59
- export class Load${camelName}Success implements Action {
60
- readonly type = ${camelName}ActionTypes.LOAD_SUCCESS;
61
- constructor(
62
- public queryParams: [string, string, any][],
63
- public queryOptions: {
64
- orderBy?: string | [string, string],
65
- limit?: number,
66
- startAt?: string,
67
- startAfter?: string,
68
- endAt?: string,
69
- endBefore?: string,
70
- },
71
- public correlationId: string,
72
- public followupActions?: (${lowerCamelName}: ${camelName}) => LoadAction[],
73
- ) {}
74
- }
75
-
76
- export class Load${camelName}Fail implements Action {
77
- readonly type = ${camelName}ActionTypes.LOAD_FAIL;
78
- constructor(
79
- public error: any,
80
- public correlationId?: string,
81
- ) {}
82
- }
83
-
84
- export class Add${camelName} implements Action {
85
- readonly type = ${camelName}ActionTypes.ADD;
86
- constructor(
87
- public ${lowerCamelName}: ${camelName},
88
- public correlationId?: string,
89
- ) {}
90
- }
91
-
92
- export class Add${camelName}Success implements Action {
93
- readonly type = ${camelName}ActionTypes.ADD_SUCCESS;
94
- constructor(
95
- public ${lowerCamelName}: ${camelName},
96
- public correlationId?: string,
97
- ) {}
98
- }
99
-
100
- export class Add${camelName}Fail implements Action {
101
- readonly type = ${camelName}ActionTypes.ADD_FAIL;
102
- constructor(
103
- public error: any,
104
- public correlationId?: string,
105
- ) {}
106
- }
107
-
108
- export class Update${camelName} implements Action {
109
- readonly type = ${camelName}ActionTypes.UPDATE;
110
- constructor(
111
- public __id: string,
112
- public changes: Partial<${camelName}>,
113
- public correlationId?: string,
114
- ) { }
115
- }
116
-
117
- export class Update${camelName}Success implements Action {
118
- readonly type = ${camelName}ActionTypes.UPDATE_SUCCESS;
119
- constructor(
120
- public __id: string,
121
- public changes: Partial<${camelName}>,
122
- public correlationId?: string,
123
- ) {}
124
- }
125
-
126
- export class Update${camelName}Fail implements Action {
127
- readonly type = ${camelName}ActionTypes.UPDATE_FAIL;
128
- constructor(
129
- public error: any,
130
- public correlationId?: string,
131
- ) {}
132
- }
133
-
134
- export class Upsert${camelName} implements Action {
135
- readonly type = ${camelName}ActionTypes.UPSERT;
136
- constructor(
137
- public ${lowerCamelName}: ${camelName},
138
- public correlationId?: string,
139
- ) {}
140
- }
141
-
142
- export class Upsert${camelName}Success implements Action {
143
- readonly type = ${camelName}ActionTypes.UPSERT_SUCCESS;
144
- constructor(
145
- public ${lowerCamelName}: ${camelName},
146
- public correlationId?: string,
147
- ) {}
148
- }
149
-
150
- export class Upsert${camelName}Fail implements Action {
151
- readonly type = ${camelName}ActionTypes.UPSERT_FAIL;
152
- constructor(
153
- public error: any,
154
- public correlationId?: string,
155
- ) {}
156
- }
157
-
158
- export class Remove${camelName} implements Action {
159
- readonly type = ${camelName}ActionTypes.REMOVE;
160
- constructor(
161
- public __id: string,
162
- public correlationId?: string,
163
- ) { }
164
- }
165
-
166
- export class Remove${camelName}Success implements Action {
167
- readonly type = ${camelName}ActionTypes.REMOVE_SUCCESS;
168
- constructor(
169
- public __id: string,
170
- public correlationId?: string,
171
- ) {}
172
- }
173
-
174
- export class Remove${camelName}Fail implements Action {
175
- readonly type = ${camelName}ActionTypes.REMOVE_FAIL;
176
- constructor(
177
- public error: any,
178
- public correlationId?: string,
179
- ) {}
180
- }
181
-
182
- export class Loaded${camelName} implements Action {
183
- readonly type = ${camelName}ActionTypes.LOADED;
184
- constructor(
185
- public payload: ${camelName}[],
186
- public correlationId?: string,
187
- ) {}
188
- }
189
-
190
- export class Added${camelName} implements Action {
191
- readonly type = ${camelName}ActionTypes.ADDED;
192
- constructor(
193
- public payload: ${camelName},
194
- public correlationId?: string,
195
- ) {}
196
- }
197
-
198
- export class Modified${camelName} implements Action {
199
- readonly type = ${camelName}ActionTypes.MODIFIED;
200
- constructor(
201
- public payload: ${camelName},
202
- public correlationId?: string,
203
- ) {}
204
- }
205
-
206
- export class Removed${camelName} implements Action {
207
- readonly type = ${camelName}ActionTypes.REMOVED;
208
- constructor(
209
- public payload: ${camelName},
210
- public correlationId?: string,
211
- ) {}
212
- }
213
-
214
- export type ${camelName}Actions =
215
- Load${camelName} |
216
- Load${camelName}Success |
217
- Load${camelName}Fail |
218
- Add${camelName} |
219
- Add${camelName}Success |
220
- Add${camelName}Fail |
221
- Update${camelName} |
222
- Update${camelName}Success |
223
- Update${camelName}Fail |
224
- Upsert${camelName} |
225
- Upsert${camelName}Success |
226
- Upsert${camelName}Fail |
227
- Remove${camelName} |
228
- Remove${camelName}Success |
229
- Remove${camelName}Fail |
230
- Loaded${camelName} |
231
- Added${camelName} |
232
- Modified${camelName} |
233
- Removed${camelName};`;
234
- const EffectsFile = `import { Injectable } from '@angular/core';
235
- import { Action } from '@ngrx/store';
236
- import { Actions, createEffect, Effect, ofType } from '@ngrx/effects';
237
- import { EMPTY, of, Observable, throwError, forkJoin, from, pipe, merge } from 'rxjs';
238
- import { switchMap, flatMap, mergeMap, map, catchError, tap, take } from 'rxjs/operators';
239
- import { FirebaseService } from '../../firebase/firebase.service';
240
- import { CachedLoadConnectionsService } from '../cached-load-connections.service';
241
-
242
- import { ${camelName} } from './${name}.model';
243
- import { ${camelName}ActionTypes, ${camelName}Actions,
244
- Load${camelName}, Load${camelName}Success, Load${camelName}Fail,
245
- Add${camelName}, Add${camelName}Success, Add${camelName}Fail,
246
- Update${camelName}, Update${camelName}Success, Update${camelName}Fail,
247
- Upsert${camelName}, Upsert${camelName}Success, Upsert${camelName}Fail,
248
- Remove${camelName}, Remove${camelName}Success, Remove${camelName}Fail } from './${name}.actions';
249
-
250
- @Injectable()
251
- export class ${camelName}Effects {
252
-
253
- /** Process the load action to create firebase connections. */
254
- load$: Observable<Action> = createEffect(
255
- () => this.actions$.pipe(
256
- ofType<Load${camelName}>(${camelName}ActionTypes.LOAD),
257
- mergeMap((action: Load${camelName}) => {
258
- const connection = this.clc.processLoadAction(action);
259
- return connection.loadStream.pipe(
260
- take(1),
261
- map(() => new Load${camelName}Success(action.queryParams, action.queryOptions, action.correlationId, action.followupActions)),
262
- catchError((error) => of(new Load${camelName}Fail(error, action.correlationId))),
263
- );
264
- }),
265
- ),
266
- );
267
-
268
- /** Process the add action to update database and initiate callbacks. */
269
- add$: Observable<Action> = createEffect(
270
- () => this.actions$.pipe(
271
- ofType<Add${camelName}>(${camelName}ActionTypes.ADD),
272
- mergeMap((action: Add${camelName}) => {
273
- return this.db.addEntity('${lowerCamelName}s', action.${lowerCamelName}).pipe(
274
- mergeMap(() => merge(
275
- of(new Add${camelName}Success(action.${lowerCamelName}, action.correlationId)),
276
- this.actionsOnAdd(action),
277
- )),
278
- catchError((error) => of(new Add${camelName}Fail(error, action.correlationId))),
279
- );
280
- }),
281
- ),
282
- );
283
-
284
- /** Process the update action to update database and initiate callbacks. */
285
- update$: Observable<Action> = createEffect(
286
- () => this.actions$.pipe(
287
- ofType<Update${camelName}>(${camelName}ActionTypes.UPDATE),
288
- mergeMap((action: Update${camelName}) => {
289
- return this.db.updateEntity('${lowerCamelName}s', action.__id, action.changes).pipe(
290
- mergeMap(() => merge(
291
- of(new Update${camelName}Success(action.__id, action.changes, action.correlationId)),
292
- this.actionsOnUpdate(action),
293
- )),
294
- catchError((error) => of(new Update${camelName}Fail(error, action.correlationId))),
295
- );
296
- }),
297
- ),
298
- );
299
-
300
- /** Process the upsert action to update database and initiate callbacks. */
301
- upsert$: Observable<Action> = createEffect(
302
- () => this.actions$.pipe(
303
- ofType<Upsert${camelName}>(${camelName}ActionTypes.UPSERT),
304
- mergeMap((action: Upsert${camelName}) => {
305
- return this.db.upsertEntity('${lowerCamelName}s', action.${lowerCamelName}).pipe(
306
- mergeMap((results) => merge(
307
- of(new Upsert${camelName}Success(action.${lowerCamelName}, action.correlationId)),
308
- results.type === 'add' ? this.actionsOnAdd(new Add${camelName}(action.${lowerCamelName}, action.correlationId)) : this.actionsOnUpdate(new Update${camelName}(action.${lowerCamelName}.__id, action.${lowerCamelName}, action.correlationId)),
309
- )),
310
- catchError((error) => of(new Upsert${camelName}Fail(error, action.correlationId))),
311
- );
312
- }),
313
- ),
314
- );
315
-
316
- /** Process the remove action to update database and initiate callbacks. */
317
- remove$: Observable<Action> = createEffect(
318
- () => this.actions$.pipe(
319
- ofType<Remove${camelName}>(${camelName}ActionTypes.REMOVE),
320
- mergeMap((action: Remove${camelName}) => {
321
- return this.db.removeEntity('${lowerCamelName}s', action.__id).pipe(
322
- mergeMap(() => merge(
323
- of(new Remove${camelName}Success(action.__id, action.correlationId)),
324
- this.actionsOnRemove(action),
325
- )),
326
- catchError((error) => of(new Remove${camelName}Fail(error, action.correlationId))),
327
- );
328
- }),
329
- ),
330
- );
331
-
332
- actionsOnAdd(action: Add${camelName}): Observable<Action> {
333
- return EMPTY;
334
- }
335
-
336
- actionsOnUpdate(action: Update${camelName}): Observable<Action> {
337
- return EMPTY;
338
- }
339
-
340
- actionsOnRemove(action: Remove${camelName}): Observable<Action> {
341
- return EMPTY;
342
- }
343
-
344
- constructor(
345
- private actions$: Actions,
346
- private db: FirebaseService,
347
- private clc: CachedLoadConnectionsService,
348
- ) {}
349
- }`;
350
- const ModelFile = `import { firestore } from 'firebase/app';
351
-
352
- /** ${description} */
353
- export interface ${camelName} {
354
- __id: string;
355
- _createdAt?: firestore.Timestamp;
356
- _updatedAt?: firestore.Timestamp;
357
- }`;
358
- const SpecFile = `import { reducer, initialState } from './${name}.reducer';
359
-
360
- describe('${camelName} Reducer', () => {
361
- describe('unknown action', () => {
362
- it('should return the initial state', () => {
363
- const action = {} as any;
364
-
365
- const result = reducer(initialState, action);
366
-
367
- expect(result).toBe(initialState);
368
- });
369
- });
370
- });`;
371
- const ReducerFile = `import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
372
- import { ${camelName} } from './${name}.model';
373
- import { ${camelName}Actions, ${camelName}ActionTypes } from './${name}.actions';
374
- import { createFeatureSelector } from '@ngrx/store';
375
-
376
- export interface State extends EntityState<${camelName}> {
377
- // additional entities state properties
378
- }
379
-
380
- export const adapter: EntityAdapter<${camelName}> = createEntityAdapter<${camelName}>({
381
- selectId: (${lowerCamelName}: ${camelName}) => ${lowerCamelName}.__id,
382
- sortComparer: false,
383
- });
384
-
385
- export const initialState: State = adapter.getInitialState({
386
- // additional entity state properties
387
- });
388
-
389
- export function reducer(
390
- state: State = initialState,
391
- action: ${camelName}Actions) {
392
-
393
- switch (action.type) {
394
-
395
- case ${camelName}ActionTypes.ADDED:
396
- return adapter.upsertOne(action.payload, state);
397
-
398
- case ${camelName}ActionTypes.LOADED:
399
- return adapter.upsertMany(action.payload, state);
400
-
401
- case ${camelName}ActionTypes.MODIFIED:
402
- return adapter.updateOne({
403
- id: action.payload.__id,
404
- changes: action.payload,
405
- }, state);
406
-
407
- case ${camelName}ActionTypes.REMOVED:
408
- return adapter.removeOne(action.payload.__id, state);
409
-
410
- default:
411
- return state;
412
- }
413
- }
414
-
415
- export const get${camelName}State = createFeatureSelector<State>('${lowerCamelName}');
416
-
417
- export const {
418
- selectIds,
419
- selectEntities,
420
- selectAll,
421
- selectTotal,
422
- } = adapter.getSelectors(get${camelName}State);`;
423
- const ServiceFile = `import { Injectable } from '@angular/core';
424
- import { Observable } from 'rxjs';
425
- import { CachedSelectorsService } from '../cached-selectors.service';
426
- import { FirebaseService } from '../../firebase/firebase.service';
427
- import { ${camelName} } from './${name}.model';
428
-
429
- @Injectable({
430
- providedIn: 'root',
431
- })
432
- export class ${camelName}Service {
433
-
434
- constructor(
435
- private cs: CachedSelectorsService,
436
- private db: FirebaseService,
437
- ) { }
438
-
439
- /** Select a ${lowerCamelName} from the store. */
440
- public select${camelName} = <T extends ${camelName}>(
441
- id: string,
442
- correlationId: string,
443
- childrenFn?: (e: ${camelName}) => { [index: string]: Observable<any> },
444
- ): Observable<T> => {
445
-
446
- return this.cs.selectEntityObj<${camelName}, T>('${lowerCamelName}', id, correlationId, childrenFn);
447
- }
448
-
449
- /** Select ${lowerCamelName}s from the store. */
450
- public select${camelName}s = <T extends ${camelName}>(
451
- queryParams: [string, string, any][],
452
- correlationId: string,
453
- childrenFn?: (e: ${camelName}) => { [index: string]: Observable<any> },
454
- ): Observable<Array<T>> => {
455
-
456
- return this.cs.selectEntityList<${camelName}, T>('${lowerCamelName}', queryParams, correlationId, childrenFn);
457
- }
458
-
459
- /** Get a ${lowerCamelName} directly from the database. */
460
- public get${camelName} = <T extends ${camelName}>(
461
- id: string,
462
- childrenFn?: (e: ${camelName}) => { [index: string]: Observable<any> },
463
- ): Observable<T> => {
464
-
465
- return this.db.queryObjOnce<${camelName}, T>('${lowerCamelName}s', id, childrenFn);
466
- }
467
-
468
- /** Get ${lowerCamelName}s directly from the database. */
469
- public get${camelName}s = <T extends ${camelName}>(
470
- queryParams: [string, string, any][],
471
- queryOptions?: { [index: string]: any },
472
- childrenFn?: (e: ${camelName}) => { [index: string]: Observable<any> },
473
- ): Observable<Array<T>> => {
474
-
475
- return this.db.queryListOnce<${camelName}, T>('${lowerCamelName}s', queryParams, queryOptions, childrenFn);
476
- }
477
- }`;
478
- statements_adder_1.AddStatements('src/app/core/core.module.ts', [
479
- '// Entity Effects',
480
- '// Entity Effects'
481
- ], [
482
- `import { ${camelName}Effects } from './store/${name}/${name}.effects';`,
483
- ` ${camelName}Effects,`
484
- ], tree);
485
- statements_adder_1.AddStatements('src/app/core/firebase/firebase.mock.service.ts', [
486
- '// Entity Models',
487
- 'this.mockDBChanges = {'
488
- ], [
489
- `import { ${camelName} } from '../store/${name}/${name}.model';`,
490
- ` ${lowerCamelName}s: new BehaviorSubject<Array<{ type: string, result: ${camelName} }>>\n(this.mockDBService.getInitialDBStateChanges('${lowerCamelName}s')),`
491
- ], tree);
492
- statements_adder_1.AddStatements('src/app/core/store/app.reducer.ts', [
493
- '// Entity Reducers',
494
- '// Entity State',
495
- '// Entity Reducers'
496
- ], [
497
- `import * as from${camelName} from './${name}/${name}.reducer';`,
498
- ` ${lowerCamelName}: from${camelName}.State;`,
499
- ` ${lowerCamelName}: from${camelName}.reducer,`
500
- ], tree);
501
- statements_adder_1.AddStatements('src/app/core/store/app.selectors.ts', [
502
- '// Entity Selectors',
503
- '// Entity Selectors',
504
- '// Entity Selectors'
505
- ], [
506
- `import { ${camelName}Service } from './${name}/${name}.service';`,
507
- ` private ${lowerCamelName}: ${camelName}Service,`,
508
- ` public get${camelName} = this.${lowerCamelName}.get${camelName};\n public get${camelName}s = this.${lowerCamelName}.get${camelName}s;\n public select${camelName} = this.${lowerCamelName}.select${camelName};\n public select${camelName}s = this.${lowerCamelName}.select${camelName}s;`
509
- ], tree);
510
- // create directories before adding files
511
- const dir = `./src/app/core/store/${name}`;
512
- if (!fs.existsSync(dir)) {
513
- fs.mkdirSync(dir);
514
- }
515
- tree.create(`${dir}/${name}.actions.ts`, ActionsFile);
516
- tree.create(`${dir}/${name}.effects.ts`, EffectsFile);
517
- tree.create(`${dir}/${name}.model.ts`, ModelFile);
518
- tree.create(`${dir}/${name}.reducer.spec.ts`, SpecFile);
519
- tree.create(`${dir}/${name}.reducer.ts`, ReducerFile);
520
- tree.create(`${dir}/${name}.service.ts`, ServiceFile);
521
- return tree;
522
- };
523
- }
524
- exports.entity = entity;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.entity = void 0;
4
+ const camelcase_formatter_1 = require("../shared/camelcase-formatter");
5
+ const statements_adder_1 = require("../shared/statements-adder");
6
+ const fs = require('fs');
7
+ // You don't have to export the function as default. You can also have more than one rule factory
8
+ // per file.
9
+ function entity(_options) {
10
+ return (tree, _context) => {
11
+ const { name, description } = _options;
12
+ const camelName = (0, camelcase_formatter_1.CamelCaseFormatter)(name, false);
13
+ const lowerCamelName = (0, camelcase_formatter_1.CamelCaseFormatter)(name, true);
14
+ const ActionsFile = `import { Action } from '@ngrx/store';
15
+ import { LoadAction } from '../app.actions';
16
+ import { ${camelName} } from './${name}.model';
17
+
18
+ export enum ${camelName}ActionTypes {
19
+ LOAD = '[${camelName}] load ${lowerCamelName}',
20
+ LOAD_SUCCESS = '[${camelName}] load ${lowerCamelName} success',
21
+ LOAD_FAIL = '[${camelName}] load ${lowerCamelName} fail',
22
+ ADD = '[${camelName}] add ${lowerCamelName}',
23
+ ADD_SUCCESS = '[${camelName}] add ${lowerCamelName} success',
24
+ ADD_FAIL = '[${camelName}] add ${lowerCamelName} fail',
25
+ UPDATE = '[${camelName}] update ${lowerCamelName}',
26
+ UPDATE_SUCCESS = '[${camelName}] update ${lowerCamelName} success',
27
+ UPDATE_FAIL = '[${camelName}] update ${lowerCamelName} fail',
28
+ UPSERT = '[${camelName}] upsert ${lowerCamelName}',
29
+ UPSERT_SUCCESS = '[${camelName}] upsert ${lowerCamelName} success',
30
+ UPSERT_FAIL = '[${camelName}] upsert ${lowerCamelName} fail',
31
+ REMOVE = '[${camelName}] remove ${lowerCamelName}',
32
+ REMOVE_SUCCESS = '[${camelName}] remove ${lowerCamelName} success',
33
+ REMOVE_FAIL = '[${camelName}] remove ${lowerCamelName} fail',
34
+ LOADED = '[${camelName}] loaded',
35
+ ADDED = '[${camelName}] added',
36
+ MODIFIED = '[${camelName}] modified',
37
+ REMOVED = '[${camelName}] removed',
38
+ }
39
+
40
+ // See Firebase Docs for current constraints on valid queries
41
+ export class Load${camelName} implements Action {
42
+ readonly type = ${camelName}ActionTypes.LOAD;
43
+ constructor(
44
+ // property, comparator, value
45
+ public queryParams: [string, string, any][],
46
+ public queryOptions: {
47
+ orderBy?: string | [string, string],
48
+ limit?: number,
49
+ startAt?: string,
50
+ startAfter?: string,
51
+ endAt?: string,
52
+ endBefore?: string,
53
+ },
54
+ public correlationId: string,
55
+ public followupActions?: (${lowerCamelName}: ${camelName}) => LoadAction[],
56
+ ) {}
57
+ }
58
+
59
+ export class Load${camelName}Success implements Action {
60
+ readonly type = ${camelName}ActionTypes.LOAD_SUCCESS;
61
+ constructor(
62
+ public queryParams: [string, string, any][],
63
+ public queryOptions: {
64
+ orderBy?: string | [string, string],
65
+ limit?: number,
66
+ startAt?: string,
67
+ startAfter?: string,
68
+ endAt?: string,
69
+ endBefore?: string,
70
+ },
71
+ public correlationId: string,
72
+ public followupActions?: (${lowerCamelName}: ${camelName}) => LoadAction[],
73
+ ) {}
74
+ }
75
+
76
+ export class Load${camelName}Fail implements Action {
77
+ readonly type = ${camelName}ActionTypes.LOAD_FAIL;
78
+ constructor(
79
+ public error: any,
80
+ public correlationId?: string,
81
+ ) {}
82
+ }
83
+
84
+ export class Add${camelName} implements Action {
85
+ readonly type = ${camelName}ActionTypes.ADD;
86
+ constructor(
87
+ public ${lowerCamelName}: ${camelName},
88
+ public correlationId?: string,
89
+ ) {}
90
+ }
91
+
92
+ export class Add${camelName}Success implements Action {
93
+ readonly type = ${camelName}ActionTypes.ADD_SUCCESS;
94
+ constructor(
95
+ public ${lowerCamelName}: ${camelName},
96
+ public correlationId?: string,
97
+ ) {}
98
+ }
99
+
100
+ export class Add${camelName}Fail implements Action {
101
+ readonly type = ${camelName}ActionTypes.ADD_FAIL;
102
+ constructor(
103
+ public error: any,
104
+ public correlationId?: string,
105
+ ) {}
106
+ }
107
+
108
+ export class Update${camelName} implements Action {
109
+ readonly type = ${camelName}ActionTypes.UPDATE;
110
+ constructor(
111
+ public __id: string,
112
+ public changes: Partial<${camelName}>,
113
+ public correlationId?: string,
114
+ ) { }
115
+ }
116
+
117
+ export class Update${camelName}Success implements Action {
118
+ readonly type = ${camelName}ActionTypes.UPDATE_SUCCESS;
119
+ constructor(
120
+ public __id: string,
121
+ public changes: Partial<${camelName}>,
122
+ public correlationId?: string,
123
+ ) {}
124
+ }
125
+
126
+ export class Update${camelName}Fail implements Action {
127
+ readonly type = ${camelName}ActionTypes.UPDATE_FAIL;
128
+ constructor(
129
+ public error: any,
130
+ public correlationId?: string,
131
+ ) {}
132
+ }
133
+
134
+ export class Upsert${camelName} implements Action {
135
+ readonly type = ${camelName}ActionTypes.UPSERT;
136
+ constructor(
137
+ public ${lowerCamelName}: ${camelName},
138
+ public correlationId?: string,
139
+ ) {}
140
+ }
141
+
142
+ export class Upsert${camelName}Success implements Action {
143
+ readonly type = ${camelName}ActionTypes.UPSERT_SUCCESS;
144
+ constructor(
145
+ public ${lowerCamelName}: ${camelName},
146
+ public correlationId?: string,
147
+ ) {}
148
+ }
149
+
150
+ export class Upsert${camelName}Fail implements Action {
151
+ readonly type = ${camelName}ActionTypes.UPSERT_FAIL;
152
+ constructor(
153
+ public error: any,
154
+ public correlationId?: string,
155
+ ) {}
156
+ }
157
+
158
+ export class Remove${camelName} implements Action {
159
+ readonly type = ${camelName}ActionTypes.REMOVE;
160
+ constructor(
161
+ public __id: string,
162
+ public correlationId?: string,
163
+ ) { }
164
+ }
165
+
166
+ export class Remove${camelName}Success implements Action {
167
+ readonly type = ${camelName}ActionTypes.REMOVE_SUCCESS;
168
+ constructor(
169
+ public __id: string,
170
+ public correlationId?: string,
171
+ ) {}
172
+ }
173
+
174
+ export class Remove${camelName}Fail implements Action {
175
+ readonly type = ${camelName}ActionTypes.REMOVE_FAIL;
176
+ constructor(
177
+ public error: any,
178
+ public correlationId?: string,
179
+ ) {}
180
+ }
181
+
182
+ export class Loaded${camelName} implements Action {
183
+ readonly type = ${camelName}ActionTypes.LOADED;
184
+ constructor(
185
+ public payload: ${camelName}[],
186
+ public correlationId?: string,
187
+ ) {}
188
+ }
189
+
190
+ export class Added${camelName} implements Action {
191
+ readonly type = ${camelName}ActionTypes.ADDED;
192
+ constructor(
193
+ public payload: ${camelName},
194
+ public correlationId?: string,
195
+ ) {}
196
+ }
197
+
198
+ export class Modified${camelName} implements Action {
199
+ readonly type = ${camelName}ActionTypes.MODIFIED;
200
+ constructor(
201
+ public payload: ${camelName},
202
+ public correlationId?: string,
203
+ ) {}
204
+ }
205
+
206
+ export class Removed${camelName} implements Action {
207
+ readonly type = ${camelName}ActionTypes.REMOVED;
208
+ constructor(
209
+ public payload: ${camelName},
210
+ public correlationId?: string,
211
+ ) {}
212
+ }
213
+
214
+ export type ${camelName}Actions =
215
+ Load${camelName} |
216
+ Load${camelName}Success |
217
+ Load${camelName}Fail |
218
+ Add${camelName} |
219
+ Add${camelName}Success |
220
+ Add${camelName}Fail |
221
+ Update${camelName} |
222
+ Update${camelName}Success |
223
+ Update${camelName}Fail |
224
+ Upsert${camelName} |
225
+ Upsert${camelName}Success |
226
+ Upsert${camelName}Fail |
227
+ Remove${camelName} |
228
+ Remove${camelName}Success |
229
+ Remove${camelName}Fail |
230
+ Loaded${camelName} |
231
+ Added${camelName} |
232
+ Modified${camelName} |
233
+ Removed${camelName};`;
234
+ const EffectsFile = `import { Injectable } from '@angular/core';
235
+ import { Action } from '@ngrx/store';
236
+ import { Actions, createEffect, Effect, ofType } from '@ngrx/effects';
237
+ import { EMPTY, of, Observable, throwError, forkJoin, from, pipe, merge } from 'rxjs';
238
+ import { switchMap, flatMap, mergeMap, map, catchError, tap, take } from 'rxjs/operators';
239
+ import { FirebaseService } from '../../firebase/firebase.service';
240
+ import { CachedLoadConnectionsService } from '../cached-load-connections.service';
241
+
242
+ import { ${camelName} } from './${name}.model';
243
+ import { ${camelName}ActionTypes, ${camelName}Actions,
244
+ Load${camelName}, Load${camelName}Success, Load${camelName}Fail,
245
+ Add${camelName}, Add${camelName}Success, Add${camelName}Fail,
246
+ Update${camelName}, Update${camelName}Success, Update${camelName}Fail,
247
+ Upsert${camelName}, Upsert${camelName}Success, Upsert${camelName}Fail,
248
+ Remove${camelName}, Remove${camelName}Success, Remove${camelName}Fail } from './${name}.actions';
249
+
250
+ @Injectable()
251
+ export class ${camelName}Effects {
252
+
253
+ /** Process the load action to create firebase connections. */
254
+ load$: Observable<Action> = createEffect(
255
+ () => this.actions$.pipe(
256
+ ofType<Load${camelName}>(${camelName}ActionTypes.LOAD),
257
+ mergeMap((action: Load${camelName}) => {
258
+ const connection = this.clc.processLoadAction(action);
259
+ return connection.loadStream.pipe(
260
+ take(1),
261
+ map(() => new Load${camelName}Success(action.queryParams, action.queryOptions, action.correlationId, action.followupActions)),
262
+ catchError((error) => of(new Load${camelName}Fail(error, action.correlationId))),
263
+ );
264
+ }),
265
+ ),
266
+ );
267
+
268
+ /** Process the add action to update database and initiate callbacks. */
269
+ add$: Observable<Action> = createEffect(
270
+ () => this.actions$.pipe(
271
+ ofType<Add${camelName}>(${camelName}ActionTypes.ADD),
272
+ mergeMap((action: Add${camelName}) => {
273
+ return this.db.addEntity('${lowerCamelName}s', action.${lowerCamelName}).pipe(
274
+ mergeMap(() => merge(
275
+ of(new Add${camelName}Success(action.${lowerCamelName}, action.correlationId)),
276
+ this.actionsOnAdd(action),
277
+ )),
278
+ catchError((error) => of(new Add${camelName}Fail(error, action.correlationId))),
279
+ );
280
+ }),
281
+ ),
282
+ );
283
+
284
+ /** Process the update action to update database and initiate callbacks. */
285
+ update$: Observable<Action> = createEffect(
286
+ () => this.actions$.pipe(
287
+ ofType<Update${camelName}>(${camelName}ActionTypes.UPDATE),
288
+ mergeMap((action: Update${camelName}) => {
289
+ return this.db.updateEntity('${lowerCamelName}s', action.__id, action.changes).pipe(
290
+ mergeMap(() => merge(
291
+ of(new Update${camelName}Success(action.__id, action.changes, action.correlationId)),
292
+ this.actionsOnUpdate(action),
293
+ )),
294
+ catchError((error) => of(new Update${camelName}Fail(error, action.correlationId))),
295
+ );
296
+ }),
297
+ ),
298
+ );
299
+
300
+ /** Process the upsert action to update database and initiate callbacks. */
301
+ upsert$: Observable<Action> = createEffect(
302
+ () => this.actions$.pipe(
303
+ ofType<Upsert${camelName}>(${camelName}ActionTypes.UPSERT),
304
+ mergeMap((action: Upsert${camelName}) => {
305
+ return this.db.upsertEntity('${lowerCamelName}s', action.${lowerCamelName}).pipe(
306
+ mergeMap((results) => merge(
307
+ of(new Upsert${camelName}Success(action.${lowerCamelName}, action.correlationId)),
308
+ results.type === 'add' ? this.actionsOnAdd(new Add${camelName}(action.${lowerCamelName}, action.correlationId)) : this.actionsOnUpdate(new Update${camelName}(action.${lowerCamelName}.__id, action.${lowerCamelName}, action.correlationId)),
309
+ )),
310
+ catchError((error) => of(new Upsert${camelName}Fail(error, action.correlationId))),
311
+ );
312
+ }),
313
+ ),
314
+ );
315
+
316
+ /** Process the remove action to update database and initiate callbacks. */
317
+ remove$: Observable<Action> = createEffect(
318
+ () => this.actions$.pipe(
319
+ ofType<Remove${camelName}>(${camelName}ActionTypes.REMOVE),
320
+ mergeMap((action: Remove${camelName}) => {
321
+ return this.db.removeEntity('${lowerCamelName}s', action.__id).pipe(
322
+ mergeMap(() => merge(
323
+ of(new Remove${camelName}Success(action.__id, action.correlationId)),
324
+ this.actionsOnRemove(action),
325
+ )),
326
+ catchError((error) => of(new Remove${camelName}Fail(error, action.correlationId))),
327
+ );
328
+ }),
329
+ ),
330
+ );
331
+
332
+ actionsOnAdd(action: Add${camelName}): Observable<Action> {
333
+ return EMPTY;
334
+ }
335
+
336
+ actionsOnUpdate(action: Update${camelName}): Observable<Action> {
337
+ return EMPTY;
338
+ }
339
+
340
+ actionsOnRemove(action: Remove${camelName}): Observable<Action> {
341
+ return EMPTY;
342
+ }
343
+
344
+ constructor(
345
+ private actions$: Actions,
346
+ private db: FirebaseService,
347
+ private clc: CachedLoadConnectionsService,
348
+ ) {}
349
+ }`;
350
+ const ModelFile = `import { Timestamp } from '@angular/fire/firestore';
351
+
352
+ /** ${description} */
353
+ export interface ${camelName} {
354
+ __id: string;
355
+ _createdAt?: Timestamp;
356
+ _updatedAt?: Timestamp;
357
+ }`;
358
+ const SpecFile = `import { reducer, initialState } from './${name}.reducer';
359
+
360
+ describe('${camelName} Reducer', () => {
361
+ describe('unknown action', () => {
362
+ it('should return the initial state', () => {
363
+ const action = {} as any;
364
+
365
+ const result = reducer(initialState, action);
366
+
367
+ expect(result).toBe(initialState);
368
+ });
369
+ });
370
+ });`;
371
+ const ReducerFile = `import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
372
+ import { ${camelName} } from './${name}.model';
373
+ import { ${camelName}Actions, ${camelName}ActionTypes } from './${name}.actions';
374
+ import { createFeatureSelector } from '@ngrx/store';
375
+
376
+ export interface State extends EntityState<${camelName}> {
377
+ // additional entities state properties
378
+ }
379
+
380
+ export const adapter: EntityAdapter<${camelName}> = createEntityAdapter<${camelName}>({
381
+ selectId: (${lowerCamelName}: ${camelName}) => ${lowerCamelName}.__id,
382
+ sortComparer: false,
383
+ });
384
+
385
+ export const initialState: State = adapter.getInitialState({
386
+ // additional entity state properties
387
+ });
388
+
389
+ export function reducer(
390
+ state: State = initialState,
391
+ action: ${camelName}Actions) {
392
+
393
+ switch (action.type) {
394
+
395
+ case ${camelName}ActionTypes.ADDED:
396
+ return adapter.upsertOne(action.payload, state);
397
+
398
+ case ${camelName}ActionTypes.LOADED:
399
+ return adapter.upsertMany(action.payload, state);
400
+
401
+ case ${camelName}ActionTypes.MODIFIED:
402
+ return adapter.updateOne({
403
+ id: action.payload.__id,
404
+ changes: action.payload,
405
+ }, state);
406
+
407
+ case ${camelName}ActionTypes.REMOVED:
408
+ return adapter.removeOne(action.payload.__id, state);
409
+
410
+ default:
411
+ return state;
412
+ }
413
+ }
414
+
415
+ export const get${camelName}State = createFeatureSelector<State>('${lowerCamelName}');
416
+
417
+ export const {
418
+ selectIds,
419
+ selectEntities,
420
+ selectAll,
421
+ selectTotal,
422
+ } = adapter.getSelectors(get${camelName}State);`;
423
+ const ServiceFile = `import { Injectable } from '@angular/core';
424
+ import { Observable } from 'rxjs';
425
+ import { CachedSelectorsService } from '../cached-selectors.service';
426
+ import { FirebaseService } from '../../firebase/firebase.service';
427
+ import { ${camelName} } from './${name}.model';
428
+
429
+ @Injectable({
430
+ providedIn: 'root',
431
+ })
432
+ export class ${camelName}Service {
433
+
434
+ constructor(
435
+ private cs: CachedSelectorsService,
436
+ private db: FirebaseService,
437
+ ) { }
438
+
439
+ /** Select a ${lowerCamelName} from the store. */
440
+ public select${camelName} = <T extends ${camelName}>(
441
+ id: string,
442
+ correlationId: string,
443
+ childrenFn?: (e: ${camelName}) => { [index: string]: Observable<any> },
444
+ ): Observable<T> => {
445
+
446
+ return this.cs.selectEntityObj<${camelName}, T>('${lowerCamelName}', id, correlationId, childrenFn);
447
+ }
448
+
449
+ /** Select ${lowerCamelName}s from the store. */
450
+ public select${camelName}s = <T extends ${camelName}>(
451
+ queryParams: [string, string, any][],
452
+ correlationId: string,
453
+ childrenFn?: (e: ${camelName}) => { [index: string]: Observable<any> },
454
+ ): Observable<Array<T>> => {
455
+
456
+ return this.cs.selectEntityList<${camelName}, T>('${lowerCamelName}', queryParams, correlationId, childrenFn);
457
+ }
458
+
459
+ /** Get a ${lowerCamelName} directly from the database. */
460
+ public get${camelName} = <T extends ${camelName}>(
461
+ id: string,
462
+ childrenFn?: (e: ${camelName}) => { [index: string]: Observable<any> },
463
+ ): Observable<T> => {
464
+
465
+ return this.db.queryObjOnce<${camelName}, T>('${lowerCamelName}s', id, childrenFn);
466
+ }
467
+
468
+ /** Get ${lowerCamelName}s directly from the database. */
469
+ public get${camelName}s = <T extends ${camelName}>(
470
+ queryParams: [string, string, any][],
471
+ queryOptions?: { [index: string]: any },
472
+ childrenFn?: (e: ${camelName}) => { [index: string]: Observable<any> },
473
+ ): Observable<Array<T>> => {
474
+
475
+ return this.db.queryListOnce<${camelName}, T>('${lowerCamelName}s', queryParams, queryOptions, childrenFn);
476
+ }
477
+ }`;
478
+ (0, statements_adder_1.AddStatements)('src/app/core/core.module.ts', [
479
+ '// Entity Effects',
480
+ '// Entity Effects'
481
+ ], [
482
+ `import { ${camelName}Effects } from './store/${name}/${name}.effects';`,
483
+ ` ${camelName}Effects,`
484
+ ], tree);
485
+ (0, statements_adder_1.AddStatements)('src/app/core/firebase/firebase.mock.service.ts', [
486
+ '// Entity Models',
487
+ 'this.mockDBChanges = {'
488
+ ], [
489
+ `import { ${camelName} } from '../store/${name}/${name}.model';`,
490
+ ` ${lowerCamelName}s: new BehaviorSubject<Array<{ type: string, result: ${camelName} }>>\n(this.mockDBService.getInitialDBStateChanges('${lowerCamelName}s')),`
491
+ ], tree);
492
+ (0, statements_adder_1.AddStatements)('src/app/core/store/app.reducer.ts', [
493
+ '// Entity Reducers',
494
+ '// Entity State',
495
+ '// Entity Reducers'
496
+ ], [
497
+ `import * as from${camelName} from './${name}/${name}.reducer';`,
498
+ ` ${lowerCamelName}: from${camelName}.State;`,
499
+ ` ${lowerCamelName}: from${camelName}.reducer,`
500
+ ], tree);
501
+ (0, statements_adder_1.AddStatements)('src/app/core/store/app.selectors.ts', [
502
+ '// Entity Selectors',
503
+ '// Entity Selectors',
504
+ '// Entity Selectors'
505
+ ], [
506
+ `import { ${camelName}Service } from './${name}/${name}.service';`,
507
+ ` private ${lowerCamelName}: ${camelName}Service,`,
508
+ ` public get${camelName} = this.${lowerCamelName}.get${camelName};\n public get${camelName}s = this.${lowerCamelName}.get${camelName}s;\n public select${camelName} = this.${lowerCamelName}.select${camelName};\n public select${camelName}s = this.${lowerCamelName}.select${camelName}s;`
509
+ ], tree);
510
+ // create directories before adding files
511
+ const dir = `./src/app/core/store/${name}`;
512
+ if (!fs.existsSync(dir)) {
513
+ fs.mkdirSync(dir);
514
+ }
515
+ tree.create(`${dir}/${name}.actions.ts`, ActionsFile);
516
+ tree.create(`${dir}/${name}.effects.ts`, EffectsFile);
517
+ tree.create(`${dir}/${name}.model.ts`, ModelFile);
518
+ tree.create(`${dir}/${name}.reducer.spec.ts`, SpecFile);
519
+ tree.create(`${dir}/${name}.reducer.ts`, ReducerFile);
520
+ tree.create(`${dir}/${name}.service.ts`, ServiceFile);
521
+ return tree;
522
+ };
523
+ }
524
+ exports.entity = entity;
525
525
  //# sourceMappingURL=index.js.map