@barcidev/ngx-autogen 0.1.2 → 0.1.4

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.
@@ -0,0 +1,200 @@
1
+ import { computed, inject } from '@angular/core';
2
+ import { patchState, signalStore, type, withComputed, withHooks, withMethods, withState } from '@ngrx/signals';
3
+ import {
4
+ addEntity,
5
+ entityConfig,
6
+ removeEntity,
7
+ setAllEntities,
8
+ updateEntity,
9
+ withEntities
10
+ } from '@ngrx/signals/entities';
11
+ import { rxMethod } from '@ngrx/signals/rxjs-interop';
12
+ import { catchError, of, pipe, switchMap, tap } from 'rxjs';
13
+
14
+ import { RequestConfig } from 'src/app/shared/state/entity.model';
15
+ import { withPagination } from 'src/app/shared/state/with-entity-pagination';
16
+ import { withEntityStatus } from 'src/app/shared/state/with-entity-status';
17
+ iimport {
18
+ Add<%= classify(name) %>,
19
+ <%= classify(name) %>Dto,
20
+ Update<%= classify(name) %>
21
+ } from '<%= grouped ? "./models/<%= dasherize(name) %>.model" : "./<%= dasherize(name) %>.model"';
22
+ import { <%= classify(name) %>Service } from '<%= grouped ? "./services/./<%= dasherize(name) %>.service" : "./<%= dasherize(name) %>.service"';
23
+
24
+ const config = entityConfig({
25
+ entity: type<<%= classify(name) %>Dto>(),
26
+ selectId: (entity) => entity.<%= pk %>,
27
+ });
28
+
29
+ export const <%= classify(name) %>Store = signalStore(
30
+ withEntities(config),
31
+ withEntityStatus(),
32
+ withPagination(),
33
+ withComputed(({ entities, entityMap, status: { idSelected } }) => ({
34
+ <%= camelize(pluralize(name)) %>: computed(() => entities()),
35
+ <%= camelize(name) %>Seleccionado: computed(() => {
36
+ const <%= pk %> = idSelected();
37
+ return <%= pk %> ? entityMap()[<%= pk %>] : null;
38
+ })
39
+ })),
40
+ withMethods((store, <%= camelize(name) %>Service = inject(<%= classify(name) %>Service)) => ({
41
+ add<%= classify(name) %>: rxMethod<RequestConfig<Add<%= classify(name) %>, <%= classify(name) %>Dto>>(
42
+ pipe(
43
+ tap(() => {
44
+ patchState(store, (state) => ({ status: { ...state.status, addLoading: true } }));
45
+ }),
46
+ switchMap(({ onError, onSuccess, payload: { request } }) => {
47
+ return <%= camelize(name) %>Service.add<%= classify(name) %>$(request).pipe(
48
+ tap((<%= pk %>) => {
49
+ const new<%= classify(name) %>: <%= classify(name) %>Dto = { ...request, <%= pk %>};
50
+ patchState(store, addEntity(new<%= classify(name) %>, config), (state) => ({
51
+ ...state,
52
+ status: { ...state.status, addError: null, addLoading: false }
53
+ }));
54
+ if (onSuccess) {
55
+ onSuccess(newHorario);
56
+ }
57
+ }),
58
+ catchError(() => {
59
+ const error = new Error('');
60
+ patchState(store, (state) => ({
61
+ status: { ...state.status, addError: error, addLoading: false }
62
+ }));
63
+ if (onError) {
64
+ onError();
65
+ }
66
+ return EMPTY;
67
+ })
68
+ );
69
+ })
70
+ )
71
+ ),
72
+ load<%= classify(pluralize(name)) %>: rxMethod<void>(
73
+ pipe(
74
+ tap(() => {
75
+ patchState(store, (state) => ({ status: { ...state.status, loading: true } }));
76
+ }),
77
+ switchMap(() => {
78
+ return <%= camelize(name) %>Service.get<%= classify(pluralize(name)) %>$().pipe(
79
+ tap((response) => {
80
+ patchState(store, setAllEntities(response, config), (state) => ({
81
+ status: { ...state.status, error: null, loaded: true, loading: false }
82
+ }));
83
+ }),
84
+ catchError(() => {
85
+ patchState(store, (state) => ({
86
+ status: { ...state.status, error: new Error('Error al cargar horario-accesos'), loading: false }
87
+ }));
88
+ return EMPTY;
89
+ })
90
+ );
91
+ })
92
+ )
93
+ ),
94
+ remove<%= classify(name) %>: rxMethod<RequestConfig<number, boolean>>(
95
+ pipe(
96
+ tap(({ payload }) => {
97
+ patchState(store, (state) => ({
98
+ status: {
99
+ ...state.status,
100
+ _removeLoading: true,
101
+ idsRemoving: [...(state.status.idsRemoving || []), payload]
102
+ }
103
+ }));
104
+ }),
105
+ switchMap(({ onError, onSuccess, payload }) => {
106
+ return <%= camelize(name) %>Service.remove<%= classify(name) %>$(payload).pipe(
107
+ tap((response) => {
108
+ if (response) {
109
+ const idsRemoving = store.status.idsRemoving() || [];
110
+ patchState(store, removeEntity(payload), (state) => ({
111
+ status: {
112
+ ...state.status,
113
+ _removeLoading: false,
114
+ error: null,
115
+ idsRemoving: idsRemoving.filter((idRemoving) => idRemoving !== payload)
116
+ }
117
+ }));
118
+ if (onSuccess) {
119
+ onSuccess(response);
120
+ }
121
+ } else {
122
+ throw new Error();
123
+ }
124
+ }),
125
+ catchError(() => {
126
+ const idsRemoving = store.status.idsRemoving() || [];
127
+ patchState(store, (state) => ({
128
+ status: {
129
+ ...state.status,
130
+ _removeLoading: false,
131
+ error: new Error(),
132
+ idsRemoving: idsRemoving.filter((idRemoving) => idRemoving !== payload)
133
+ }
134
+ }));
135
+ if (onError) {
136
+ onError();
137
+ }
138
+ return EMPTY;
139
+ })
140
+ );
141
+ })
142
+ )
143
+ ),
144
+ update<%= classify(name) %>: rxMethod<RequestConfig<Update<%= classify(name) %>, boolean>>(
145
+ pipe(
146
+ tap(({ payload }) => {
147
+ patchState(store, (state) => ({
148
+ status: {
149
+ ...state.status,
150
+ _updateLoading: true,
151
+ idsUpdating: [...(state.status.idsUpdating || []), payload.cod]
152
+ }
153
+ }));
154
+ }),
155
+ switchMap(({ onError, onSuccess, payload }) => {
156
+ return <%= camelize(name) %>Service.update<%= classify(name) %>$(entity).pipe(
157
+ tap((response) => {
158
+ if (response) {
159
+ const idsUpdating = store.status.idsUpdating() || [];
160
+ patchState(store, updateEntity({ changes: payload, id: payload.cod }, config), (state) => ({
161
+ status: {
162
+ ...state.status,
163
+ _updateLoading: false,
164
+ error: null,
165
+ idsUpdating: idsUpdating.filter((idUpdating) => idUpdating !== payload.cod)
166
+ }
167
+ }));
168
+ if (onSuccess) {
169
+ onSuccess(response);
170
+ }
171
+ } else {
172
+ throw new Error('');
173
+ }
174
+ }),
175
+ catchError(() => {
176
+ const idsUpdating = store.status.idsUpdating() || [];
177
+ patchState(store, (state) => ({
178
+ status: {
179
+ ...state.status,
180
+ _updateLoading: false,
181
+ error: new Error('Error al actualizar horario-acceso'),
182
+ idsUpdating: idsUpdating.filter((idUpdating) => idUpdating !== payload.cod)
183
+ }
184
+ }));
185
+ if (onError) {
186
+ onError();
187
+ }
188
+ return EMPTY;
189
+ })
190
+ );
191
+ })
192
+ )
193
+ ),
194
+ })),
195
+ withHooks({
196
+ onInit: (store) => {
197
+ store.load<%= classify(pluralize(name)) %>();
198
+ },
199
+ })
200
+ );
@@ -68,16 +68,24 @@ function signalStore(options) {
68
68
  if (globalConfig && globalConfig.pk && !options.pk) {
69
69
  options.pk = globalConfig.pk;
70
70
  }
71
+ if (!options.path) {
72
+ options.path = process.cwd();
73
+ }
74
+ // Si la carpeta final no se llama 'state', agregarla
75
+ let movePath = (0, core_1.normalize)(options.path);
76
+ const pathParts = movePath.split(/[\\/]/).filter(Boolean);
77
+ if (pathParts[pathParts.length - 1] !== "state") {
78
+ movePath = (0, core_1.join)(movePath, "state");
79
+ }
71
80
  treeRef = tree;
72
- const movePath = (0, core_1.normalize)(options.path);
73
81
  const indexPath = (0, core_1.join)(movePath, "index.ts");
74
82
  const nameDash = core_1.strings.dasherize(options.name);
75
83
  const entityName = core_1.strings.classify(options.name);
76
84
  const entityHeader = `/* ${entityName.toUpperCase()} */`;
77
85
  const exportBlock = [
78
86
  entityHeader,
79
- `export * from './${nameDash}/${nameDash}.model';`,
80
- `export * from './${nameDash}/${nameDash}.service';`,
87
+ `export * from './${nameDash}${options.grouped ? "/models" : ""}/${nameDash}.model';`,
88
+ `export * from './${nameDash}${options.grouped ? "/services" : ""}/${nameDash}.service';`,
81
89
  `export * from './${nameDash}/${nameDash}.store';`,
82
90
  "",
83
91
  ].join("\n");
@@ -103,7 +111,35 @@ function signalStore(options) {
103
111
  else {
104
112
  tree.create(indexPath, content);
105
113
  }
106
- const templateStoreSource = (0, schematics_1.apply)((0, schematics_1.url)("./files/store"), [
114
+ // Generar archivos en las carpetas store, services y models
115
+ const namePath = (0, core_1.join)(movePath, core_1.strings.dasherize(options.name));
116
+ const rules = [];
117
+ // store
118
+ rules.push((0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)("./files/state/store"), [
119
+ (0, schematics_1.applyTemplates)(Object.assign(Object.assign(Object.assign({}, core_1.strings), options), { pluralize: (word) => {
120
+ switch (options.lang) {
121
+ case "es":
122
+ return pluralizeEs(word);
123
+ default:
124
+ return pluralizeEn(word);
125
+ }
126
+ }, pk: options.pk || "id" })),
127
+ (0, schematics_1.move)("store"),
128
+ ])));
129
+ // services
130
+ rules.push((0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)("./files/state/services"), [
131
+ (0, schematics_1.applyTemplates)(Object.assign(Object.assign(Object.assign({}, core_1.strings), options), { pluralize: (word) => {
132
+ switch (options.lang) {
133
+ case "es":
134
+ return pluralizeEs(word);
135
+ default:
136
+ return pluralizeEn(word);
137
+ }
138
+ }, pk: options.pk || "id" })),
139
+ (0, schematics_1.move)((0, core_1.join)(namePath, options.grouped ? "services" : "")),
140
+ ])));
141
+ // models
142
+ rules.push((0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)("./files/state/models"), [
107
143
  (0, schematics_1.applyTemplates)(Object.assign(Object.assign(Object.assign({}, core_1.strings), options), { pluralize: (word) => {
108
144
  switch (options.lang) {
109
145
  case "es":
@@ -112,10 +148,11 @@ function signalStore(options) {
112
148
  return pluralizeEn(word);
113
149
  }
114
150
  }, pk: options.pk || "id" })),
115
- (0, schematics_1.move)((0, core_1.join)(movePath, core_1.strings.dasherize(options.name))),
116
- ]);
117
- const commonEntityRule = mergeFilesSmart("./files/entity", (0, core_1.join)(movePath, "common/entity"), options);
118
- return (0, schematics_1.chain)([(0, schematics_1.mergeWith)(templateStoreSource), commonEntityRule]);
151
+ (0, schematics_1.move)((0, core_1.join)(namePath, options.grouped ? "models" : "")),
152
+ ])));
153
+ // common entity
154
+ rules.push(mergeFilesSmart("./files/entity", "src/app/shared/state", options));
155
+ return (0, schematics_1.chain)(rules);
119
156
  });
120
157
  }
121
158
  //# sourceMappingURL=index.js.map
@@ -1,6 +1,7 @@
1
- export interface StoreSchemaOptions {
2
- name: string;
3
- path: string;
4
- pk?: string;
5
- lang: "en" | "es";
6
- }
1
+ export interface StoreSchemaOptions {
2
+ name: string;
3
+ path?: string;
4
+ grouped: boolean;
5
+ pk?: string;
6
+ lang: "en" | "es";
7
+ }
@@ -1,30 +1,34 @@
1
- {
2
- "$schema": "http://json-schema.org/schema",
3
- "$id": "SignalStoreSchematic",
4
- "title": "Signal Store Schema",
5
- "type": "object",
6
- "properties": {
7
- "name": {
8
- "type": "string",
9
- "description": "Name of the entity.",
10
- "x-prompt": "What's the name of the entity?",
11
- "priority": 1
12
- },
13
- "path": {
14
- "type": "string",
15
- "description": "Destination path.",
16
- "default": "src/app/core"
17
- },
18
- "lang": {
19
- "type": "string",
20
- "description": "Language for pluralization ('en' for English, 'es' for Spanish).",
21
- "enum": ["en", "es"],
22
- "default": "en"
23
- },
24
- "pk": {
25
- "type": "string",
26
- "description": "Name of the primary key."
27
- }
28
- },
29
- "required": ["name"]
1
+ {
2
+ "$schema": "http://json-schema.org/schema",
3
+ "$id": "SignalStoreSchematic",
4
+ "title": "Signal Store Schema",
5
+ "type": "object",
6
+ "properties": {
7
+ "name": {
8
+ "type": "string",
9
+ "description": "Name of the entity.",
10
+ "x-prompt": "What's the name of the entity?",
11
+ "priority": 1
12
+ },
13
+ "path": {
14
+ "type": "string",
15
+ "description": "Destination path."
16
+ },
17
+ "grouped": {
18
+ "type": "boolean",
19
+ "description": "Whether the files should be grouped in subfolders.",
20
+ "default": false
21
+ },
22
+ "lang": {
23
+ "type": "string",
24
+ "description": "Language for pluralization ('en' for English, 'es' for Spanish).",
25
+ "enum": ["en", "es"],
26
+ "default": "en"
27
+ },
28
+ "pk": {
29
+ "type": "string",
30
+ "description": "Name of the primary key."
31
+ }
32
+ },
33
+ "required": ["name"]
30
34
  }
@@ -1,212 +0,0 @@
1
- import { computed, inject } from '@angular/core';
2
- import { patchState, signalStore, type, withComputed, withHooks, withMethods, withState } from '@ngrx/signals';
3
- import {
4
- addEntity,
5
- entityConfig,
6
- removeEntity,
7
- setAllEntities,
8
- updateEntity,
9
- withEntities
10
- } from '@ngrx/signals/entities';
11
- import { rxMethod } from '@ngrx/signals/rxjs-interop';
12
- import { catchError, of, pipe, switchMap, tap } from 'rxjs';
13
-
14
- import { EntityStatus } from '../common/entity/entity.model';
15
- import {
16
- <%= classify(name) %>Dto,
17
- Add<%= classify(name) %>,
18
- Update<%= classify(name) %>
19
- } from './<%= dasherize(name) %>.model';
20
- import { <%= classify(name) %>Service } from './<%= dasherize(name) %>.service';
21
-
22
- const initialStatus: EntityStatus = {
23
- error: null,
24
- loaded: false,
25
- loading: false,
26
- };
27
-
28
- const config = entityConfig({
29
- entity: type<<%= classify(name) %>Dto>(),
30
- selectId: (entity) => entity.<%= pk %>,
31
- });
32
-
33
- export const <%= classify(name) %>Store = signalStore(
34
- withEntities(config),
35
- withState({
36
- _status: initialStatus,
37
- }),
38
- withComputed(({ entityMap, _status }) => ({
39
- <%= camelize(pluralize(name)) %>: computed(() => Object.values(entityMap())),
40
- <%= camelize(name) %>Seleccionado: computed(() => {
41
- const <%= pk %> = _status().idSelected;
42
- return <%= pk %> ? entityMap()[<%= pk %>] : null;
43
- }),
44
- error: computed(() => _status().error),
45
- loaded: computed(() => _status().loaded),
46
- loading: computed(() => _status().loading),
47
- loadingRemove: computed(
48
- () => (<%= pk %>?: number) =>
49
- (<%= pk %> ? _status().idsRemoving?.includes(<%= pk %>) : _status().removeLoading) || false
50
- ),
51
- loadingUpdate: computed(
52
- () => (<%= pk %>?: number) =>
53
- (<%= pk %> ? _status().idsUpdating?.includes(<%= pk %>) : _status().updateLoading) || false
54
- ),
55
- })),
56
- withMethods((store, <%= camelize(name) %>Service = inject(<%= classify(name) %>Service)) => ({
57
- add<%= classify(name) %>: rxMethod<Add<%= classify(name) %>>(
58
- pipe(
59
- tap(() => {
60
- patchState(store, { _status: { ...store._status(), addLoading: true } });
61
- }),
62
- switchMap((entity) => {
63
- return <%= camelize(name) %>Service.add<%= classify(name) %>$(entity).pipe(
64
- tap((<%= pk %>) => {
65
- patchState(store, addEntity({ ...entity, <%= pk %> }, config), {
66
- _status: {
67
- ...store._status(),
68
- addLoading: false,
69
- error: null,
70
- },
71
- });
72
- }),
73
- catchError(() => {
74
- patchState(store, {
75
- _status: {
76
- ...store._status(),
77
- addLoading: false,
78
- error: new Error('Error al agregar <%= name %>'),
79
- },
80
- });
81
- return of(entity);
82
- })
83
- );
84
- })
85
- )
86
- ),
87
- load<%= classify(pluralize(name)) %>: rxMethod<void>(
88
- pipe(
89
- tap(() => {
90
- patchState(store, { _status: { ...store._status(), loading: true } });
91
- }),
92
- switchMap(() => {
93
- return <%= camelize(name) %>Service.get<%= classify(pluralize(name)) %>$().pipe(
94
- tap((response) => {
95
- patchState(store, setAllEntities(response, config), {
96
- _status: {
97
- ...store._status(),
98
- error: null,
99
- loaded: true,
100
- loading: false,
101
- },
102
- });
103
- }),
104
- catchError(() => {
105
- patchState(store, {
106
- _status: {
107
- ...store._status(),
108
- error: new Error('Error al cargar <%= pluralize(name) %>'),
109
- loading: false,
110
- },
111
- });
112
- return of([]);
113
- })
114
- );
115
- })
116
- )
117
- ),
118
- remove<%= classify(name) %>: rxMethod<number>(
119
- pipe(
120
- tap((<%= pk %>) => {
121
- patchState(store, {
122
- _status: {
123
- ...store._status(),
124
- removeLoading: true,
125
- idsRemoving: [...(store._status().idsRemoving || []), <%= pk %>],
126
- },
127
- });
128
- }),
129
- switchMap((<%= pk %>) => {
130
- return <%= camelize(name) %>Service.remove<%= classify(name) %>$(<%= pk %>).pipe(
131
- tap((success) => {
132
- if (success) {
133
- const idsRemoving = store._status().idsRemoving || [];
134
- patchState(store, removeEntity(<%= pk %>), {
135
- _status: {
136
- ...store._status(),
137
- removeLoading: false,
138
- error: null,
139
- idsRemoving: idsRemoving.filter((idRemoving) => idRemoving !== <%= pk %>),
140
- },
141
- });
142
- } else {
143
- throw new Error('Error al eliminar <%= name %>');
144
- }
145
- }),
146
- catchError(() => {
147
- const idsRemoving = store._status().idsRemoving || [];
148
- patchState(store, {
149
- _status: {
150
- ...store._status(),
151
- removeLoading: false,
152
- error: new Error('Error al eliminar <%= name %>'),
153
- idsRemoving: idsRemoving.filter((idRemoving) => idRemoving !== <%= pk %>),
154
- },
155
- });
156
- return of(false);
157
- })
158
- );
159
- })
160
- )
161
- ),
162
- update<%= classify(name) %>: rxMethod<Update<%= classify(name) %>>(
163
- pipe(
164
- tap((entity) => {
165
- patchState(store, {
166
- _status: {
167
- ...store._status(),
168
- idsUpdating: [...(store._status().idsUpdating || []), entity.<%= pk %>],
169
- updateLoading: true,
170
- },
171
- });
172
- }),
173
- switchMap((entity) => {
174
- return <%= camelize(name) %>Service.update<%= classify(name) %>$(entity).pipe(
175
- tap((success) => {
176
- if (success) {
177
- const idsUpdating = store._status().idsUpdating || [];
178
- patchState(store, updateEntity({ changes: entity, id: entity.<%= pk %> }, config), {
179
- _status: {
180
- ...store._status(),
181
- error: null,
182
- idsUpdating: idsUpdating.filter((idUpdating) => idUpdating !== entity.<%= pk %>),
183
- updateLoading: false,
184
- },
185
- });
186
- } else {
187
- throw new Error('Error al actualizar <%= name %>');
188
- }
189
- }),
190
- catchError(() => {
191
- const idsUpdating = store._status().idsUpdating || [];
192
- patchState(store, {
193
- _status: {
194
- ...store._status(),
195
- error: new Error('Error al actualizar <%= name %>'),
196
- idsUpdating: idsUpdating.filter((idUpdating) => idUpdating !== entity.<%= pk %>),
197
- updateLoading: false,
198
- },
199
- });
200
- return of(false);
201
- })
202
- );
203
- })
204
- )
205
- ),
206
- })),
207
- withHooks({
208
- onInit: (store) => {
209
- store.load<%= classify(pluralize(name)) %>();
210
- },
211
- })
212
- );