@tech4good/angular-schematics 13.2.0 → 13.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tech4good/angular-schematics",
3
- "version": "13.2.0",
3
+ "version": "13.3.0",
4
4
  "description": "A set of schematics for generating modules, containers, components, and entities",
5
5
  "scripts": {
6
6
  "build": "tsc -p tsconfig.json",
@@ -25,6 +25,12 @@
25
25
  "description": "Generate a new entity.",
26
26
  "factory": "./entity/index#entity",
27
27
  "schema": "./entity/schema.json"
28
+ },
29
+ "entity-in-example": {
30
+ "description": "Generate a new entity within the example repository (with a mock DB).",
31
+ "factory": "./entity-in-example/index#entity",
32
+ "schema": "./entity-in-example/schema.json"
28
33
  }
34
+
29
35
  }
30
36
  }
@@ -0,0 +1,270 @@
1
+ import { Action } from '@ngrx/store';
2
+ import { LoadAction } from '../app.actions';
3
+ import { <%= classify(name) %> } from './<%= dasherize(name) %>.model';
4
+
5
+ export enum <%= classify(name) %>ActionTypes {
6
+ STREAM = '[<%= classify(name) %>] stream <%= camelize(name) %>',
7
+ STREAM_SUCCESS = '[<%= classify(name) %>] stream <%= camelize(name) %> success',
8
+ STREAM_FAIL = '[<%= classify(name) %>] stream <%= camelize(name) %> fail',
9
+ LOAD = '[<%= classify(name) %>] load <%= camelize(name) %>',
10
+ LOAD_SUCCESS = '[<%= classify(name) %>] load <%= camelize(name) %> success',
11
+ LOAD_FAIL = '[<%= classify(name) %>] load <%= camelize(name) %> fail',
12
+ ADD = '[<%= classify(name) %>] add <%= camelize(name) %>',
13
+ ADD_SUCCESS = '[<%= classify(name) %>] add <%= camelize(name) %> success',
14
+ ADD_FAIL = '[<%= classify(name) %>] add <%= camelize(name) %> fail',
15
+ UPDATE = '[<%= classify(name) %>] update <%= camelize(name) %>',
16
+ UPDATE_SUCCESS = '[<%= classify(name) %>] update <%= camelize(name) %> success',
17
+ UPDATE_FAIL = '[<%= classify(name) %>] update <%= camelize(name) %> fail',
18
+ UPSERT = '[<%= classify(name) %>] upsert <%= camelize(name) %>',
19
+ UPSERT_SUCCESS = '[<%= classify(name) %>] upsert <%= camelize(name) %> success',
20
+ UPSERT_FAIL = '[<%= classify(name) %>] upsert <%= camelize(name) %> fail',
21
+ REMOVE = '[<%= classify(name) %>] remove <%= camelize(name) %>',
22
+ REMOVE_SUCCESS = '[<%= classify(name) %>] remove <%= camelize(name) %> success',
23
+ REMOVE_FAIL = '[<%= classify(name) %>] remove <%= camelize(name) %> fail',
24
+ LOADED = '[<%= classify(name) %>] loaded',
25
+ ADDED = '[<%= classify(name) %>] added',
26
+ MODIFIED = '[<%= classify(name) %>] modified',
27
+ REMOVED = '[<%= classify(name) %>] removed',
28
+ }
29
+
30
+ // See Firebase Docs for current constraints on valid queries
31
+ export class Stream<%= classify(name) %> implements Action {
32
+ readonly type = <%= classify(name) %>ActionTypes.STREAM;
33
+ constructor(
34
+ // property, comparator, value
35
+ public queryParams: [string, string, any][],
36
+ public queryOptions: {
37
+ orderBy?: string | [string, string],
38
+ limit?: number,
39
+ startAt?: string,
40
+ startAfter?: string,
41
+ endAt?: string,
42
+ endBefore?: string,
43
+ },
44
+ public correlationId: string,
45
+ public followupActions?: (<%= camelize(name) %>: <%= classify(name) %>) => LoadAction[],
46
+ ) {}
47
+ }
48
+
49
+ export class Stream<%= classify(name) %>Success implements Action {
50
+ readonly type = <%= classify(name) %>ActionTypes.STREAM_SUCCESS;
51
+ constructor(
52
+ public queryParams: [string, string, any][],
53
+ public queryOptions: {
54
+ orderBy?: string | [string, string],
55
+ limit?: number,
56
+ startAt?: string,
57
+ startAfter?: string,
58
+ endAt?: string,
59
+ endBefore?: string,
60
+ },
61
+ public correlationId: string,
62
+ public followupActions?: (<%= camelize(name) %>: <%= classify(name) %>) => LoadAction[],
63
+ ) {}
64
+ }
65
+
66
+ export class Stream<%= classify(name) %>Fail implements Action {
67
+ readonly type = <%= classify(name) %>ActionTypes.STREAM_FAIL;
68
+ constructor(
69
+ public error: any,
70
+ public correlationId?: string,
71
+ ) {}
72
+ }
73
+
74
+
75
+ export class Load<%= classify(name) %> implements Action {
76
+ readonly type = <%= classify(name) %>ActionTypes.LOAD;
77
+ constructor(
78
+ // property, comparator, value
79
+ public queryParams: [string, string, any][],
80
+ public queryOptions: {
81
+ orderBy?: string | [string, string],
82
+ limit?: number,
83
+ startAt?: string,
84
+ startAfter?: string,
85
+ endAt?: string,
86
+ endBefore?: string,
87
+ },
88
+ public correlationId: string,
89
+ public followupActions?: (<%= camelize(name) %>: <%= classify(name) %>) => LoadAction[],
90
+ ) {}
91
+ }
92
+
93
+ export class Load<%= classify(name) %>Success implements Action {
94
+ readonly type = <%= classify(name) %>ActionTypes.LOAD_SUCCESS;
95
+ constructor(
96
+ public queryParams: [string, string, any][],
97
+ public queryOptions: {
98
+ orderBy?: string | [string, string],
99
+ limit?: number,
100
+ startAt?: string,
101
+ startAfter?: string,
102
+ endAt?: string,
103
+ endBefore?: string,
104
+ },
105
+ public correlationId: string,
106
+ public followupActions?: (<%= camelize(name) %>: <%= classify(name) %>) => LoadAction[],
107
+ ) {}
108
+ }
109
+
110
+ export class Load<%= classify(name) %>Fail implements Action {
111
+ readonly type = <%= classify(name) %>ActionTypes.LOAD_FAIL;
112
+ constructor(
113
+ public error: any,
114
+ public correlationId?: string,
115
+ ) {}
116
+ }
117
+
118
+ export class Add<%= classify(name) %> implements Action {
119
+ readonly type = <%= classify(name) %>ActionTypes.ADD;
120
+ constructor(
121
+ public <%= camelize(name) %>: <%= classify(name) %>,
122
+ public correlationId?: string,
123
+ ) {}
124
+ }
125
+
126
+ export class Add<%= classify(name) %>Success implements Action {
127
+ readonly type = <%= classify(name) %>ActionTypes.ADD_SUCCESS;
128
+ constructor(
129
+ public <%= camelize(name) %>: <%= classify(name) %>,
130
+ public correlationId?: string,
131
+ ) {}
132
+ }
133
+
134
+ export class Add<%= classify(name) %>Fail implements Action {
135
+ readonly type = <%= classify(name) %>ActionTypes.ADD_FAIL;
136
+ constructor(
137
+ public error: any,
138
+ public correlationId?: string,
139
+ ) {}
140
+ }
141
+
142
+ export class Update<%= classify(name) %> implements Action {
143
+ readonly type = <%= classify(name) %>ActionTypes.UPDATE;
144
+ constructor(
145
+ public __id: string,
146
+ public changes: Partial<<%= classify(name) %>>,
147
+ public correlationId?: string,
148
+ ) { }
149
+ }
150
+
151
+ export class Update<%= classify(name) %>Success implements Action {
152
+ readonly type = <%= classify(name) %>ActionTypes.UPDATE_SUCCESS;
153
+ constructor(
154
+ public __id: string,
155
+ public changes: Partial<<%= classify(name) %>>,
156
+ public correlationId?: string,
157
+ ) {}
158
+ }
159
+
160
+ export class Update<%= classify(name) %>Fail implements Action {
161
+ readonly type = <%= classify(name) %>ActionTypes.UPDATE_FAIL;
162
+ constructor(
163
+ public error: any,
164
+ public correlationId?: string,
165
+ ) {}
166
+ }
167
+
168
+ export class Upsert<%= classify(name) %> implements Action {
169
+ readonly type = <%= classify(name) %>ActionTypes.UPSERT;
170
+ constructor(
171
+ public <%= camelize(name) %>: <%= classify(name) %>,
172
+ public correlationId?: string,
173
+ ) {}
174
+ }
175
+
176
+ export class Upsert<%= classify(name) %>Success implements Action {
177
+ readonly type = <%= classify(name) %>ActionTypes.UPSERT_SUCCESS;
178
+ constructor(
179
+ public <%= camelize(name) %>: <%= classify(name) %>,
180
+ public correlationId?: string,
181
+ ) {}
182
+ }
183
+
184
+ export class Upsert<%= classify(name) %>Fail implements Action {
185
+ readonly type = <%= classify(name) %>ActionTypes.UPSERT_FAIL;
186
+ constructor(
187
+ public error: any,
188
+ public correlationId?: string,
189
+ ) {}
190
+ }
191
+
192
+ export class Remove<%= classify(name) %> implements Action {
193
+ readonly type = <%= classify(name) %>ActionTypes.REMOVE;
194
+ constructor(
195
+ public __id: string,
196
+ public correlationId?: string,
197
+ ) { }
198
+ }
199
+
200
+ export class Remove<%= classify(name) %>Success implements Action {
201
+ readonly type = <%= classify(name) %>ActionTypes.REMOVE_SUCCESS;
202
+ constructor(
203
+ public __id: string,
204
+ public correlationId?: string,
205
+ ) {}
206
+ }
207
+
208
+ export class Remove<%= classify(name) %>Fail implements Action {
209
+ readonly type = <%= classify(name) %>ActionTypes.REMOVE_FAIL;
210
+ constructor(
211
+ public error: any,
212
+ public correlationId?: string,
213
+ ) {}
214
+ }
215
+
216
+ export class Loaded<%= classify(name) %> implements Action {
217
+ readonly type = <%= classify(name) %>ActionTypes.LOADED;
218
+ constructor(
219
+ public payload: <%= classify(name) %>[],
220
+ public correlationId?: string,
221
+ ) {}
222
+ }
223
+
224
+ export class Added<%= classify(name) %> implements Action {
225
+ readonly type = <%= classify(name) %>ActionTypes.ADDED;
226
+ constructor(
227
+ public payload: <%= classify(name) %>,
228
+ public correlationId?: string,
229
+ ) {}
230
+ }
231
+
232
+ export class Modified<%= classify(name) %> implements Action {
233
+ readonly type = <%= classify(name) %>ActionTypes.MODIFIED;
234
+ constructor(
235
+ public payload: <%= classify(name) %>,
236
+ public correlationId?: string,
237
+ ) {}
238
+ }
239
+
240
+ export class Removed<%= classify(name) %> implements Action {
241
+ readonly type = <%= classify(name) %>ActionTypes.REMOVED;
242
+ constructor(
243
+ public payload: <%= classify(name) %>,
244
+ public correlationId?: string,
245
+ ) {}
246
+ }
247
+
248
+ export type <%= classify(name) %>Actions =
249
+ Stream<%= classify(name) %> |
250
+ Stream<%= classify(name) %>Success |
251
+ Stream<%= classify(name) %>Fail |
252
+ Load<%= classify(name) %> |
253
+ Load<%= classify(name) %>Success |
254
+ Load<%= classify(name) %>Fail |
255
+ Add<%= classify(name) %> |
256
+ Add<%= classify(name) %>Success |
257
+ Add<%= classify(name) %>Fail |
258
+ Update<%= classify(name) %> |
259
+ Update<%= classify(name) %>Success |
260
+ Update<%= classify(name) %>Fail |
261
+ Upsert<%= classify(name) %> |
262
+ Upsert<%= classify(name) %>Success |
263
+ Upsert<%= classify(name) %>Fail |
264
+ Remove<%= classify(name) %> |
265
+ Remove<%= classify(name) %>Success |
266
+ Remove<%= classify(name) %>Fail |
267
+ Loaded<%= classify(name) %> |
268
+ Added<%= classify(name) %> |
269
+ Modified<%= classify(name) %> |
270
+ Removed<%= classify(name) %>;
@@ -0,0 +1,131 @@
1
+ import { Injectable } from '@angular/core';
2
+ import { Action } from '@ngrx/store';
3
+ import { Actions, createEffect, ofType } from '@ngrx/effects';
4
+ import { EMPTY, of, Observable, throwError, forkJoin, from, pipe, merge } from 'rxjs';
5
+ import { switchMap, flatMap, mergeMap, map, catchError, tap, take } from 'rxjs/operators';
6
+ import { FirebaseService } from '../../firebase/firebase.service';
7
+ import { CachedLoadConnectionsService } from '../cached-load-connections.service';
8
+
9
+ import { <%= classify(name) %> } from './<%= dasherize(name) %>.model';
10
+ import { <%= classify(name) %>ActionTypes, <%= classify(name) %>Actions,
11
+ Stream<%= classify(name) %>, Stream<%= classify(name) %>Success, Stream<%= classify(name) %>Fail,
12
+ Load<%= classify(name) %>, Load<%= classify(name) %>Success, Load<%= classify(name) %>Fail,
13
+ Add<%= classify(name) %>, Add<%= classify(name) %>Success, Add<%= classify(name) %>Fail,
14
+ Update<%= classify(name) %>, Update<%= classify(name) %>Success, Update<%= classify(name) %>Fail,
15
+ Upsert<%= classify(name) %>, Upsert<%= classify(name) %>Success, Upsert<%= classify(name) %>Fail,
16
+ Remove<%= classify(name) %>, Remove<%= classify(name) %>Success, Remove<%= classify(name) %>Fail } from './<%= dasherize(name) %>.actions';
17
+
18
+ @Injectable()
19
+ export class <%= classify(name) %>Effects {
20
+ /** Process the stream action to create firebase connections. */
21
+ stream$: Observable<Action> = createEffect(
22
+ () => this.actions$.pipe(
23
+ ofType<Stream<%= classify(name) %>>(<%= classify(name) %>ActionTypes.STREAM),
24
+ mergeMap((action: Stream<%= classify(name) %>) => {
25
+ const connection = this.clc.processLoadAction(action);
26
+ return connection.loadStream.pipe(
27
+ take(1),
28
+ map(() => new Stream<%= classify(name) %>Success(action.queryParams, action.queryOptions, action.correlationId, action.followupActions)),
29
+ catchError((error) => of(new Stream<%= classify(name) %>Fail(error, action.correlationId))),
30
+ );
31
+ }),
32
+ ),
33
+ );
34
+
35
+ /** Process the load action to create firebase connections. */
36
+ load$: Observable<Action> = createEffect(
37
+ () => this.actions$.pipe(
38
+ ofType<Load<%= classify(name) %>>(<%= classify(name) %>ActionTypes.LOAD),
39
+ mergeMap((action: Load<%= classify(name) %>) => {
40
+ const connection = this.clc.processLoadAction(action);
41
+ return connection.loadStream.pipe(
42
+ take(1),
43
+ map(() => new Load<%= classify(name) %>Success(action.queryParams, action.queryOptions, action.correlationId, action.followupActions)),
44
+ catchError((error) => of(new Load<%= classify(name) %>Fail(error, action.correlationId))),
45
+ );
46
+ }),
47
+ ),
48
+ );
49
+
50
+ /** Process the add action to update database and initiate callbacks. */
51
+ add$: Observable<Action> = createEffect(
52
+ () => this.actions$.pipe(
53
+ ofType<Add<%= classify(name) %>>(<%= classify(name) %>ActionTypes.ADD),
54
+ mergeMap((action: Add<%= classify(name) %>) => {
55
+ return this.db.addEntity('<%= camelize(name) %>s', action.<%= camelize(name) %>).pipe(
56
+ mergeMap(() => merge(
57
+ of(new Add<%= classify(name) %>Success(action.<%= camelize(name) %>, action.correlationId)),
58
+ this.actionsOnAdd(action),
59
+ )),
60
+ catchError((error) => of(new Add<%= classify(name) %>Fail(error, action.correlationId))),
61
+ );
62
+ }),
63
+ ),
64
+ );
65
+
66
+ /** Process the update action to update database and initiate callbacks. */
67
+ update$: Observable<Action> = createEffect(
68
+ () => this.actions$.pipe(
69
+ ofType<Update<%= classify(name) %>>(<%= classify(name) %>ActionTypes.UPDATE),
70
+ mergeMap((action: Update<%= classify(name) %>) => {
71
+ return this.db.updateEntity('<%= camelize(name) %>s', action.__id, action.changes).pipe(
72
+ mergeMap(() => merge(
73
+ of(new Update<%= classify(name) %>Success(action.__id, action.changes, action.correlationId)),
74
+ this.actionsOnUpdate(action),
75
+ )),
76
+ catchError((error) => of(new Update<%= classify(name) %>Fail(error, action.correlationId))),
77
+ );
78
+ }),
79
+ ),
80
+ );
81
+
82
+ /** Process the upsert action to update database and initiate callbacks. */
83
+ upsert$: Observable<Action> = createEffect(
84
+ () => this.actions$.pipe(
85
+ ofType<Upsert<%= classify(name) %>>(<%= classify(name) %>ActionTypes.UPSERT),
86
+ mergeMap((action: Upsert<%= classify(name) %>) => {
87
+ return this.db.upsertEntity('<%= camelize(name) %>s', action.<%= camelize(name) %>).pipe(
88
+ mergeMap((results) => merge(
89
+ of(new Upsert<%= classify(name) %>Success(action.<%= camelize(name) %>, action.correlationId)),
90
+ results.type === 'add' ? this.actionsOnAdd(new Add<%= classify(name) %>(action.<%= camelize(name) %>, action.correlationId)) : this.actionsOnUpdate(new Update<%= classify(name) %>(action.<%= camelize(name) %>.__id, action.<%= camelize(name) %>, action.correlationId)),
91
+ )),
92
+ catchError((error) => of(new Upsert<%= classify(name) %>Fail(error, action.correlationId))),
93
+ );
94
+ }),
95
+ ),
96
+ );
97
+
98
+ /** Process the remove action to update database and initiate callbacks. */
99
+ remove$: Observable<Action> = createEffect(
100
+ () => this.actions$.pipe(
101
+ ofType<Remove<%= classify(name) %>>(<%= classify(name) %>ActionTypes.REMOVE),
102
+ mergeMap((action: Remove<%= classify(name) %>) => {
103
+ return this.db.removeEntity('<%= camelize(name) %>s', action.__id).pipe(
104
+ mergeMap(() => merge(
105
+ of(new Remove<%= classify(name) %>Success(action.__id, action.correlationId)),
106
+ this.actionsOnRemove(action),
107
+ )),
108
+ catchError((error) => of(new Remove<%= classify(name) %>Fail(error, action.correlationId))),
109
+ );
110
+ }),
111
+ ),
112
+ );
113
+
114
+ actionsOnAdd(action: Add<%= classify(name) %>): Observable<Action> {
115
+ return EMPTY;
116
+ }
117
+
118
+ actionsOnUpdate(action: Update<%= classify(name) %>): Observable<Action> {
119
+ return EMPTY;
120
+ }
121
+
122
+ actionsOnRemove(action: Remove<%= classify(name) %>): Observable<Action> {
123
+ return EMPTY;
124
+ }
125
+
126
+ constructor(
127
+ private actions$: Actions,
128
+ private db: FirebaseService,
129
+ private clc: CachedLoadConnectionsService,
130
+ ) {}
131
+ }
@@ -0,0 +1,3 @@
1
+ export interface <%= classify(name) %> {
2
+ __id: string;
3
+ }
@@ -0,0 +1,13 @@
1
+ import { reducer, initialState } from './<%= dasherize(name) %>.reducer';
2
+
3
+ describe('<%= classify(name) %> Reducer', () => {
4
+ describe('unknown action', () => {
5
+ it('should return the initial state', () => {
6
+ const action = {} as any;
7
+
8
+ const result = reducer(initialState, action);
9
+
10
+ expect(result).toBe(initialState);
11
+ });
12
+ });
13
+ });
@@ -0,0 +1,51 @@
1
+ import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
2
+ import { <%= classify(name) %> } from './<%= dasherize(name) %>.model';
3
+ import { <%= classify(name) %>Actions, <%= classify(name) %>ActionTypes } from './<%= dasherize(name) %>.actions';
4
+ import { createFeatureSelector } from '@ngrx/store';
5
+
6
+ export interface State extends EntityState<<%= classify(name) %>> {
7
+ // additional entities state properties
8
+ }
9
+
10
+ export const adapter: EntityAdapter<<%= classify(name) %>> = createEntityAdapter<<%= classify(name) %>>({
11
+ selectId: (<%= camelize(name) %>: <%= classify(name) %>) => <%= camelize(name) %>.__id,
12
+ sortComparer: false,
13
+ });
14
+
15
+ export const initialState: State = adapter.getInitialState({
16
+ // additional entity state properties
17
+ });
18
+
19
+ export function reducer(
20
+ state: State = initialState,
21
+ action: <%= classify(name) %>Actions,
22
+ ) {
23
+ switch (action.type) {
24
+ case <%= classify(name) %>ActionTypes.ADDED:
25
+ return adapter.upsertOne(action.payload, state);
26
+
27
+ case <%= classify(name) %>ActionTypes.LOADED:
28
+ return adapter.upsertMany(action.payload, state);
29
+
30
+ case <%= classify(name) %>ActionTypes.MODIFIED:
31
+ return adapter.updateOne({
32
+ id: action.payload.__id,
33
+ changes: action.payload,
34
+ }, state);
35
+
36
+ case <%= classify(name) %>ActionTypes.REMOVED:
37
+ return adapter.removeOne(action.payload.__id, state);
38
+
39
+ default:
40
+ return state;
41
+ }
42
+ }
43
+
44
+ export const get<%= classify(name) %>State = createFeatureSelector<State>('<%= camelize(name) %>');
45
+
46
+ export const {
47
+ selectIds,
48
+ selectEntities,
49
+ selectAll,
50
+ selectTotal,
51
+ } = adapter.getSelectors(get<%= classify(name) %>State);
@@ -0,0 +1,50 @@
1
+ import { Injectable } from '@angular/core';
2
+ import { Observable } from 'rxjs';
3
+ import { CachedSelectorsService } from '../cached-selectors.service';
4
+ import { FirebaseService } from '../../firebase/firebase.service';
5
+ import { <%= classify(name) %> } from './<%= dasherize(name) %>.model';
6
+
7
+ @Injectable({
8
+ providedIn: 'root',
9
+ })
10
+ export class <%= classify(name) %>Service {
11
+ constructor(
12
+ private cs: CachedSelectorsService,
13
+ private db: FirebaseService,
14
+ ) { }
15
+
16
+ /** Select a <%= camelize(name) %> from the store. */
17
+ public select<%= classify(name) %> = <T extends <%= classify(name) %>>(
18
+ id: string,
19
+ correlationId: string,
20
+ childrenFn?: (e: <%= classify(name) %>) => { [index: string]: Observable<any> },
21
+ ): Observable<T> => {
22
+ return this.cs.selectEntityObj<<%= classify(name) %>, T>('<%= camelize(name) %>', id, correlationId, childrenFn);
23
+ };
24
+
25
+ /** Select <%= camelize(name) %>s from the store. */
26
+ public select<%= classify(name) %>s = <T extends <%= classify(name) %>>(
27
+ queryParams: [string, string, any][],
28
+ correlationId: string,
29
+ childrenFn?: (e: <%= classify(name) %>) => { [index: string]: Observable<any> },
30
+ ): Observable<Array<T>> => {
31
+ return this.cs.selectEntityList<<%= classify(name) %>, T>('<%= camelize(name) %>', queryParams, correlationId, childrenFn);
32
+ };
33
+
34
+ /** Get a <%= camelize(name) %> directly from the database. */
35
+ public get<%= classify(name) %> = <T extends <%= classify(name) %>>(
36
+ id: string,
37
+ childrenFn?: (e: <%= classify(name) %>) => { [index: string]: Observable<any> },
38
+ ): Observable<T> => {
39
+ return this.db.queryObjOnce<<%= classify(name) %>, T>('<%= camelize(name) %>s', id, childrenFn);
40
+ };
41
+
42
+ /** Get <%= camelize(name) %>s directly from the database. */
43
+ public get<%= classify(name) %>s = <T extends <%= classify(name) %>>(
44
+ queryParams: [string, string, any][],
45
+ queryOptions?: { [index: string]: any },
46
+ childrenFn?: (e: <%= classify(name) %>) => { [index: string]: Observable<any> },
47
+ ): Observable<Array<T>> => {
48
+ return this.db.queryListOnce<<%= classify(name) %>, T>('<%= camelize(name) %>s', queryParams, queryOptions, childrenFn);
49
+ };
50
+ }
@@ -0,0 +1,2 @@
1
+ import { Rule } from '@angular-devkit/schematics';
2
+ export declare function entity(options: any): Rule;
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.entity = void 0;
13
+ const schematics_1 = require("@angular-devkit/schematics");
14
+ const change_1 = require("@schematics/angular/utility/change");
15
+ const find_module_1 = require("@schematics/angular/utility/find-module");
16
+ const parse_name_1 = require("@schematics/angular/utility/parse-name");
17
+ const workspace_1 = require("@schematics/angular/utility/workspace");
18
+ const core_1 = require("@angular-devkit/core");
19
+ const statements_adder_1 = require("../shared/statements-adder");
20
+ function addInserts(options) {
21
+ return (host, context) => {
22
+ const classifiedName = core_1.strings.classify(options.name);
23
+ const camelizedName = core_1.strings.camelize(options.name);
24
+ context.logger.info(`names: ${classifiedName}, ${camelizedName}`);
25
+ // Add core module changes
26
+ const coreModulePath = options.module;
27
+ const coreModuleText = (0, statements_adder_1.getSourceText)(host, coreModulePath);
28
+ context.logger.info(`modulePath: ${coreModulePath}`);
29
+ const entityEffectsPath = `/${options.path}/` +
30
+ core_1.strings.dasherize(options.name) + '/' +
31
+ core_1.strings.dasherize(options.name) + '.effects';
32
+ context.logger.info(`entityEffectsPath: ${entityEffectsPath}`);
33
+ const coreToEffectsPath = (0, find_module_1.buildRelativePath)(coreModulePath, entityEffectsPath);
34
+ context.logger.info(`coreToEffectsPath: ${coreToEffectsPath}`);
35
+ let insertChangesInCore = (0, statements_adder_1.simpleInsert)(coreModuleText, coreModulePath, [
36
+ '// Entity Effects',
37
+ '// Entity Effects'
38
+ ], [
39
+ `import { ${classifiedName}Effects } from '${coreToEffectsPath}';`,
40
+ ` ${classifiedName}Effects,`
41
+ ]);
42
+ // Add app reducer changes
43
+ const appReducerPath = '//src/app/core/store/app.reducer.ts';
44
+ const appReducerText = (0, statements_adder_1.getSourceText)(host, appReducerPath);
45
+ context.logger.info(`appReducerPath: ${appReducerPath}`);
46
+ const entityReducerPath = `/${options.path}/` +
47
+ core_1.strings.dasherize(options.name) + '/' +
48
+ core_1.strings.dasherize(options.name) + '.reducer';
49
+ context.logger.info(`entityReducerPath: ${entityReducerPath}`);
50
+ const appToEntityReducerPath = (0, find_module_1.buildRelativePath)(appReducerPath, entityReducerPath);
51
+ let insertChangesInAppReducer = (0, statements_adder_1.simpleInsert)(appReducerText, appReducerPath, [
52
+ '// Entity Reducers',
53
+ '// Entity State',
54
+ '// Entity Reducers'
55
+ ], [
56
+ `import * as from${classifiedName} from '${appToEntityReducerPath}';`,
57
+ ` ${camelizedName}: from${classifiedName}.State;`,
58
+ ` ${camelizedName}: from${classifiedName}.reducer,`
59
+ ]);
60
+ // Add app selector changes
61
+ const appSelectorPath = '//src/app/core/store/app.selectors.ts';
62
+ const appSelectorText = (0, statements_adder_1.getSourceText)(host, appSelectorPath);
63
+ const entityServicePath = `/${options.path}/` +
64
+ core_1.strings.dasherize(options.name) + '/' +
65
+ core_1.strings.dasherize(options.name) + '.service';
66
+ const appToEntityServicePath = (0, find_module_1.buildRelativePath)(appSelectorPath, entityServicePath);
67
+ let insertChangesInAppSelector = (0, statements_adder_1.simpleInsert)(appSelectorText, appSelectorPath, [
68
+ '// Entity Selectors',
69
+ '// Entity Selectors',
70
+ '// Entity Selectors'
71
+ ], [
72
+ `import { ${classifiedName}Service } from '${appToEntityServicePath}';`,
73
+ ` private ${camelizedName}: ${classifiedName}Service,`,
74
+ ` public get${classifiedName} = this.${camelizedName}.get${classifiedName};\n public get${classifiedName}s = this.${camelizedName}.get${classifiedName}s;\n public select${classifiedName} = this.${camelizedName}.select${classifiedName};\n public select${classifiedName}s = this.${camelizedName}.select${classifiedName}s;`
75
+ ]);
76
+ // Add firebase service changes
77
+ const appFirebaseServicePath = '//src/app/core/firebase/firebase.service.ts';
78
+ const appFirebaseServiceText = (0, statements_adder_1.getSourceText)(host, appFirebaseServicePath);
79
+ const entityModelPath = `/${options.path}/` +
80
+ core_1.strings.dasherize(options.name) + '/' +
81
+ core_1.strings.dasherize(options.name) + '.model';
82
+ const appToEntityModelPath = (0, find_module_1.buildRelativePath)(appFirebaseServicePath, entityModelPath);
83
+ let insertChangesInFirebaseService = (0, statements_adder_1.simpleInsert)(appFirebaseServiceText, appFirebaseServicePath, [
84
+ '// Entity Models',
85
+ '// Entity Models'
86
+ ], [
87
+ `import { ${classifiedName} } from '${appToEntityModelPath}';`,
88
+ ` ${camelizedName}s: new BehaviorSubject<Array<{ type: string; result: ${classifiedName} }>>(
89
+ this.mockDBService.getInitialDBStateChanges('${camelizedName}s')
90
+ ),`
91
+ ]);
92
+ context.logger.info(`about to start committing`);
93
+ // For each of the files and corresponding set of changes, commit to the tree
94
+ [
95
+ [coreModulePath, insertChangesInCore],
96
+ [appReducerPath, insertChangesInAppReducer],
97
+ [appSelectorPath, insertChangesInAppSelector],
98
+ [appFirebaseServicePath, insertChangesInFirebaseService],
99
+ ].map(([filePath, insertChanges]) => {
100
+ const declarationRecorder = host.beginUpdate(filePath);
101
+ for (const change of insertChanges) {
102
+ if (change instanceof change_1.InsertChange) {
103
+ declarationRecorder.insertLeft(change.pos, change.toAdd);
104
+ }
105
+ }
106
+ host.commitUpdate(declarationRecorder);
107
+ });
108
+ return host;
109
+ };
110
+ }
111
+ function entity(options) {
112
+ return (tree, context) => __awaiter(this, void 0, void 0, function* () {
113
+ // Make sure the command is executed in the directory where the component should be generated
114
+ if (!options.inDirectory) {
115
+ context.logger.info("Please navigate to the directory where you want to generate the entity.");
116
+ return (0, schematics_1.noop)();
117
+ }
118
+ const workspace = yield (0, workspace_1.getWorkspace)(tree);
119
+ const project = workspace.projects.get(options.project);
120
+ if (!project) {
121
+ throw new schematics_1.SchematicsException(`Project "${options.project}" does not exist.`);
122
+ }
123
+ if (options.path === undefined) {
124
+ options.path = (0, workspace_1.buildDefaultPath)(project);
125
+ }
126
+ options.module = (0, find_module_1.findModuleFromOptions)(tree, options);
127
+ const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
128
+ options.name = parsedPath.name;
129
+ options.path = parsedPath.path;
130
+ const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
131
+ (0, schematics_1.applyTemplates)(Object.assign(Object.assign({}, core_1.strings), options)),
132
+ (0, schematics_1.move)(parsedPath.path),
133
+ ]);
134
+ return (0, schematics_1.chain)([
135
+ addInserts(options),
136
+ (0, schematics_1.mergeWith)(templateSource)
137
+ ]);
138
+ });
139
+ }
140
+ exports.entity = entity;
141
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2DAIoC;AAEpC,+DAA0E;AAC1E,yEAAmG;AACnG,uEAAmE;AACnE,qEAAuF;AACvF,+CAA+C;AAE/C,iEAAyE;AAEzE,SAAS,UAAU,CAAC,OAAY;IAC9B,OAAO,CAAC,IAAU,EAAE,OAAyB,EAAE,EAAE;QAE/C,MAAM,cAAc,GAAG,cAAO,CAAC,QAA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IAAA,gBAAG,EAAC,SAAS,CAAC,EAAE;YAC3C,IAAA,2BAAc,kCACT,cAAO,GACP,OAAO,EACV;YACF,IAAA,iBAAI,EAAC,UAAU,CAAC,IAAI,CAAC;SACtB,CAAC,CAAC;QAEH,OAAO,IAAA,kBAAK,EAAC;YACX,UAAU,CAAC,OAAO,CAAC;YACnB,IAAA,sBAAS,EAAC,cAAc,CAAC;SAC1B,CAAC,CAAC;IACL,CAAC,CAAA,CAAC;AACJ,CAAC;AAvCD,wBAuCC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const schematics_1 = require("@angular-devkit/schematics");
13
+ const testing_1 = require("@angular-devkit/schematics/testing");
14
+ const path = require("path");
15
+ const collectionPath = path.join(__dirname, '../collection.json');
16
+ describe('entity', () => {
17
+ it('works', () => __awaiter(void 0, void 0, void 0, function* () {
18
+ const runner = new testing_1.SchematicTestRunner('schematics', collectionPath);
19
+ const tree = yield runner.runSchematicAsync('entity', {}, schematics_1.Tree.empty()).toPromise();
20
+ expect(tree.files).toEqual([]);
21
+ }));
22
+ });
23
+ //# sourceMappingURL=index_spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index_spec.js","sourceRoot":"","sources":["index_spec.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,2DAAkD;AAClD,gEAAyE;AACzE,6BAA6B;AAG7B,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,oBAAoB,CAAC,CAAC;AAGlE,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,EAAE,CAAC,OAAO,EAAE,GAAS,EAAE;QACrB,MAAM,MAAM,GAAG,IAAI,6BAAmB,CAAC,YAAY,EAAE,cAAc,CAAC,CAAC;QACrE,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,iBAAiB,CAAC,QAAQ,EAAE,EAAE,EAAE,iBAAI,CAAC,KAAK,EAAE,CAAC,CAAC,SAAS,EAAE,CAAC;QAEpF,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IACjC,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,42 @@
1
+ {
2
+ "$schema": "http://json-schema.org/schema",
3
+ "$id": "EntitySchema",
4
+ "title": "Entity Schema",
5
+ "type": "object",
6
+ "description": "Creates a new entity in the default project.",
7
+ "properties": {
8
+ "index": {
9
+ "type": "number",
10
+ "default": 1
11
+ },
12
+ "path": {
13
+ "type": "string",
14
+ "format": "path",
15
+ "description": "The path at which to create the entity file, relative to the current workspace. Should typically be within /src/app/core/store.",
16
+ "visible": false
17
+ },
18
+ "project": {
19
+ "type": "string",
20
+ "description": "The name of the project.",
21
+ "$default": {
22
+ "$source": "projectName"
23
+ }
24
+ },
25
+ "name": {
26
+ "type": "string",
27
+ "description": "The name of the entity.",
28
+ "$default": {
29
+ "$source": "argv",
30
+ "index": 0
31
+ },
32
+ "x-prompt": "What name would you like to use for the entity?"
33
+ },
34
+ "inDirectory": {
35
+ "type": "boolean",
36
+ "x-prompt": "Are you currently in the directory where you want to generate the entity? (typically directly in src/app/core/store)"
37
+ }
38
+ },
39
+ "required": [
40
+ "name"
41
+ ]
42
+ }