@meridianjs/framework-utils 0.1.0 → 0.1.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.
package/dist/index.js CHANGED
@@ -190,48 +190,70 @@ function MeridianService(models) {
190
190
  #container;
191
191
  constructor(container) {
192
192
  this.#container = container;
193
+ const hasCustomMethod = (name) => typeof this[name] === "function";
193
194
  for (const [modelName, _modelDef] of Object.entries(models)) {
194
195
  const pluralName = `${modelName}s`;
195
196
  const repoToken = `${modelName.charAt(0).toLowerCase()}${modelName.slice(1)}Repository`;
196
197
  const capitalized = `${modelName.charAt(0).toUpperCase()}${modelName.slice(1)}`;
197
198
  const capitalizedPlural = `${pluralName.charAt(0).toUpperCase()}${pluralName.slice(1)}`;
198
- this[`list${capitalizedPlural}`] = async (filters = {}, options = {}) => {
199
- const repo = this.#container.resolve(repoToken);
200
- return repo.find(filters, options);
201
- };
202
- this[`listAndCount${capitalizedPlural}`] = async (filters = {}, options = {}) => {
203
- const repo = this.#container.resolve(repoToken);
204
- return repo.findAndCount(filters, options);
205
- };
206
- this[`retrieve${capitalized}`] = async (id) => {
207
- const repo = this.#container.resolve(repoToken);
208
- return repo.findOneOrFail({ id });
209
- };
210
- this[`create${capitalized}`] = async (data) => {
211
- const repo = this.#container.resolve(repoToken);
212
- const entity = repo.create(data);
213
- await repo.persistAndFlush(entity);
214
- return entity;
215
- };
216
- this[`update${capitalized}`] = async (id, data) => {
217
- const repo = this.#container.resolve(repoToken);
218
- const entity = await repo.findOneOrFail({ id });
219
- Object.assign(entity, data);
220
- await repo.flush();
221
- return entity;
222
- };
223
- this[`delete${capitalized}`] = async (id) => {
224
- const repo = this.#container.resolve(repoToken);
225
- const entity = await repo.findOneOrFail({ id });
226
- await repo.removeAndFlush(entity);
227
- };
228
- this[`softDelete${capitalized}`] = async (id) => {
229
- const repo = this.#container.resolve(repoToken);
230
- const entity = await repo.findOneOrFail({ id });
231
- entity.deleted_at = /* @__PURE__ */ new Date();
232
- await repo.flush();
233
- return entity;
234
- };
199
+ const listMethod = `list${capitalizedPlural}`;
200
+ if (!hasCustomMethod(listMethod)) {
201
+ this[listMethod] = async (filters = {}, options = {}) => {
202
+ const repo = this.#container.resolve(repoToken);
203
+ return repo.find(filters, options);
204
+ };
205
+ }
206
+ const listAndCountMethod = `listAndCount${capitalizedPlural}`;
207
+ if (!hasCustomMethod(listAndCountMethod)) {
208
+ this[listAndCountMethod] = async (filters = {}, options = {}) => {
209
+ const repo = this.#container.resolve(repoToken);
210
+ return repo.findAndCount(filters, options);
211
+ };
212
+ }
213
+ const retrieveMethod = `retrieve${capitalized}`;
214
+ if (!hasCustomMethod(retrieveMethod)) {
215
+ this[retrieveMethod] = async (id) => {
216
+ const repo = this.#container.resolve(repoToken);
217
+ return repo.findOneOrFail({ id });
218
+ };
219
+ }
220
+ const createMethod = `create${capitalized}`;
221
+ if (!hasCustomMethod(createMethod)) {
222
+ this[createMethod] = async (data) => {
223
+ const repo = this.#container.resolve(repoToken);
224
+ const entity = repo.create(data);
225
+ await repo.persistAndFlush(entity);
226
+ return entity;
227
+ };
228
+ }
229
+ const updateMethod = `update${capitalized}`;
230
+ if (!hasCustomMethod(updateMethod)) {
231
+ this[updateMethod] = async (id, data) => {
232
+ const repo = this.#container.resolve(repoToken);
233
+ const entity = await repo.findOneOrFail({ id });
234
+ Object.assign(entity, data);
235
+ await repo.flush();
236
+ return entity;
237
+ };
238
+ }
239
+ const deleteMethod = `delete${capitalized}`;
240
+ if (!hasCustomMethod(deleteMethod)) {
241
+ this[deleteMethod] = async (id) => {
242
+ const repo = this.#container.resolve(repoToken);
243
+ const entity = await repo.findOneOrFail({ id });
244
+ await repo.removeAndFlush(entity);
245
+ };
246
+ }
247
+ const softDeleteMethod = `softDelete${capitalized}`;
248
+ if (!hasCustomMethod(softDeleteMethod)) {
249
+ this[softDeleteMethod] = async (id) => {
250
+ const repo = this.#container.resolve(repoToken);
251
+ const entity = await repo.findOneOrFail({ id });
252
+ entity.deleted_at = /* @__PURE__ */ new Date();
253
+ await repo.flush();
254
+ return entity;
255
+ };
256
+ }
235
257
  }
236
258
  }
237
259
  }
package/dist/index.mjs CHANGED
@@ -140,48 +140,70 @@ function MeridianService(models) {
140
140
  #container;
141
141
  constructor(container) {
142
142
  this.#container = container;
143
+ const hasCustomMethod = (name) => typeof this[name] === "function";
143
144
  for (const [modelName, _modelDef] of Object.entries(models)) {
144
145
  const pluralName = `${modelName}s`;
145
146
  const repoToken = `${modelName.charAt(0).toLowerCase()}${modelName.slice(1)}Repository`;
146
147
  const capitalized = `${modelName.charAt(0).toUpperCase()}${modelName.slice(1)}`;
147
148
  const capitalizedPlural = `${pluralName.charAt(0).toUpperCase()}${pluralName.slice(1)}`;
148
- this[`list${capitalizedPlural}`] = async (filters = {}, options = {}) => {
149
- const repo = this.#container.resolve(repoToken);
150
- return repo.find(filters, options);
151
- };
152
- this[`listAndCount${capitalizedPlural}`] = async (filters = {}, options = {}) => {
153
- const repo = this.#container.resolve(repoToken);
154
- return repo.findAndCount(filters, options);
155
- };
156
- this[`retrieve${capitalized}`] = async (id) => {
157
- const repo = this.#container.resolve(repoToken);
158
- return repo.findOneOrFail({ id });
159
- };
160
- this[`create${capitalized}`] = async (data) => {
161
- const repo = this.#container.resolve(repoToken);
162
- const entity = repo.create(data);
163
- await repo.persistAndFlush(entity);
164
- return entity;
165
- };
166
- this[`update${capitalized}`] = async (id, data) => {
167
- const repo = this.#container.resolve(repoToken);
168
- const entity = await repo.findOneOrFail({ id });
169
- Object.assign(entity, data);
170
- await repo.flush();
171
- return entity;
172
- };
173
- this[`delete${capitalized}`] = async (id) => {
174
- const repo = this.#container.resolve(repoToken);
175
- const entity = await repo.findOneOrFail({ id });
176
- await repo.removeAndFlush(entity);
177
- };
178
- this[`softDelete${capitalized}`] = async (id) => {
179
- const repo = this.#container.resolve(repoToken);
180
- const entity = await repo.findOneOrFail({ id });
181
- entity.deleted_at = /* @__PURE__ */ new Date();
182
- await repo.flush();
183
- return entity;
184
- };
149
+ const listMethod = `list${capitalizedPlural}`;
150
+ if (!hasCustomMethod(listMethod)) {
151
+ this[listMethod] = async (filters = {}, options = {}) => {
152
+ const repo = this.#container.resolve(repoToken);
153
+ return repo.find(filters, options);
154
+ };
155
+ }
156
+ const listAndCountMethod = `listAndCount${capitalizedPlural}`;
157
+ if (!hasCustomMethod(listAndCountMethod)) {
158
+ this[listAndCountMethod] = async (filters = {}, options = {}) => {
159
+ const repo = this.#container.resolve(repoToken);
160
+ return repo.findAndCount(filters, options);
161
+ };
162
+ }
163
+ const retrieveMethod = `retrieve${capitalized}`;
164
+ if (!hasCustomMethod(retrieveMethod)) {
165
+ this[retrieveMethod] = async (id) => {
166
+ const repo = this.#container.resolve(repoToken);
167
+ return repo.findOneOrFail({ id });
168
+ };
169
+ }
170
+ const createMethod = `create${capitalized}`;
171
+ if (!hasCustomMethod(createMethod)) {
172
+ this[createMethod] = async (data) => {
173
+ const repo = this.#container.resolve(repoToken);
174
+ const entity = repo.create(data);
175
+ await repo.persistAndFlush(entity);
176
+ return entity;
177
+ };
178
+ }
179
+ const updateMethod = `update${capitalized}`;
180
+ if (!hasCustomMethod(updateMethod)) {
181
+ this[updateMethod] = async (id, data) => {
182
+ const repo = this.#container.resolve(repoToken);
183
+ const entity = await repo.findOneOrFail({ id });
184
+ Object.assign(entity, data);
185
+ await repo.flush();
186
+ return entity;
187
+ };
188
+ }
189
+ const deleteMethod = `delete${capitalized}`;
190
+ if (!hasCustomMethod(deleteMethod)) {
191
+ this[deleteMethod] = async (id) => {
192
+ const repo = this.#container.resolve(repoToken);
193
+ const entity = await repo.findOneOrFail({ id });
194
+ await repo.removeAndFlush(entity);
195
+ };
196
+ }
197
+ const softDeleteMethod = `softDelete${capitalized}`;
198
+ if (!hasCustomMethod(softDeleteMethod)) {
199
+ this[softDeleteMethod] = async (id) => {
200
+ const repo = this.#container.resolve(repoToken);
201
+ const entity = await repo.findOneOrFail({ id });
202
+ entity.deleted_at = /* @__PURE__ */ new Date();
203
+ await repo.flush();
204
+ return entity;
205
+ };
206
+ }
185
207
  }
186
208
  }
187
209
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meridianjs/framework-utils",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Utilities for building Meridian modules: DML, service factory, defineModule, defineLink",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",