@chevre/domain 23.0.0-alpha.11 → 23.0.0-alpha.13

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,49 @@
1
+ // tslint:disable:no-console
2
+ import * as mongoose from 'mongoose';
3
+
4
+ import { chevre } from '../../../../lib/index';
5
+
6
+ // const PROJECT_ID = process.env.PROJECT_ID;
7
+ mongoose.Model.on('index', (...args) => {
8
+ console.error('******** index event emitted. ********\n', args);
9
+ });
10
+
11
+ async function main() {
12
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
13
+
14
+ const offerCatalogRepo = await chevre.repository.OfferCatalog.createInstance(mongoose.connection);
15
+
16
+ try {
17
+ await offerCatalogRepo.updateManyOfferCatalogsByIds({
18
+ id: {
19
+ $in: ['xxx']
20
+ },
21
+ itemOffered: {
22
+ typeOf: {
23
+ $eq: chevre.factory.product.ProductType.EventService
24
+ }
25
+ },
26
+ $push: {
27
+ itemListElement: {
28
+ $each: [{ id: 'xxx', typeOf: chevre.factory.offerType.Offer }],
29
+ $slice: 300
30
+ }
31
+ },
32
+ $pull: {
33
+ itemListElement: {
34
+ $elemMatch: {
35
+ id: {
36
+ $in: ['xxx']
37
+ }
38
+ }
39
+ }
40
+ }
41
+ });
42
+ } catch (error) {
43
+ throw await chevre.errorHandler.handleMongoError(error);
44
+ }
45
+ }
46
+
47
+ main()
48
+ .then(console.log)
49
+ .catch(console.error);
@@ -10,16 +10,20 @@ async function main() {
10
10
 
11
11
  const productHasOfferCatalogRepo = await chevre.repository.ProductHasOfferCatalog.createInstance(mongoose.connection);
12
12
 
13
- const offerCatalogs = (await productHasOfferCatalogRepo.findOfferCatalogs(
14
- {
15
- limit: 10,
16
- page: 1,
17
- project: { id: { $eq: project.id } },
18
- product: { id: { $in: ['656038908b1cd5ce629f5992', '60c1c0031fb182000bed5eff'] } }
19
- }
20
- ));
21
- // tslint:disable-next-line:no-null-keyword
22
- console.dir(offerCatalogs, { depth: null });
13
+ try {
14
+ const offerCatalogs = (await productHasOfferCatalogRepo.findOfferCatalogs(
15
+ {
16
+ limit: 10,
17
+ page: 1,
18
+ project: { id: { $eq: project.id } },
19
+ product: { id: { $in: ['656038908b1cd5ce629f5992', '60c1c0031fb182000bed5eff'] } }
20
+ }
21
+ ));
22
+ // tslint:disable-next-line:no-null-keyword
23
+ console.dir(offerCatalogs, { depth: null });
24
+ } catch (error) {
25
+ throw await chevre.errorHandler.handleMongoError(error);
26
+ }
23
27
  }
24
28
 
25
29
  main()
@@ -1,7 +1,11 @@
1
+ /**
2
+ * https://www.mongodb.com/ja-jp/docs/manual/reference/error-codes/
3
+ */
1
4
  export declare enum MongoErrorCode {
2
- DuplicateKey = 11000,
5
+ ConflictUpdateOperator = 40,
3
6
  MaxTimeMSExpired = 50,
4
- ExceededTimeLimit = 262
7
+ ExceededTimeLimit = 262,
8
+ DuplicateKey = 11000
5
9
  }
6
10
  export declare function isMongoError(error: unknown): Promise<boolean>;
7
11
  export declare function handleMongoError(error: unknown): Promise<unknown>;
@@ -24,11 +24,15 @@ exports.handleAWSError = handleAWSError;
24
24
  const http_status_1 = require("http-status");
25
25
  const factory_1 = require("./factory");
26
26
  let mongo;
27
+ /**
28
+ * https://www.mongodb.com/ja-jp/docs/manual/reference/error-codes/
29
+ */
27
30
  var MongoErrorCode;
28
31
  (function (MongoErrorCode) {
29
- MongoErrorCode[MongoErrorCode["DuplicateKey"] = 11000] = "DuplicateKey";
32
+ MongoErrorCode[MongoErrorCode["ConflictUpdateOperator"] = 40] = "ConflictUpdateOperator";
30
33
  MongoErrorCode[MongoErrorCode["MaxTimeMSExpired"] = 50] = "MaxTimeMSExpired";
31
34
  MongoErrorCode[MongoErrorCode["ExceededTimeLimit"] = 262] = "ExceededTimeLimit";
35
+ MongoErrorCode[MongoErrorCode["DuplicateKey"] = 11000] = "DuplicateKey";
32
36
  })(MongoErrorCode || (exports.MongoErrorCode = MongoErrorCode = {}));
33
37
  function isMongoError(error) {
34
38
  return __awaiter(this, void 0, void 0, function* () {
@@ -46,9 +50,11 @@ function handleMongoError(error) {
46
50
  let handledError = error;
47
51
  if (handledError instanceof mongo.MongoError || handledError instanceof mongo.MongoServerError) {
48
52
  switch (handledError.code) {
53
+ case MongoErrorCode.ConflictUpdateOperator:
54
+ handledError = new factory_1.errors.Argument('', `Some updating fields conflict. code:${handledError.code} message:${handledError.message}`);
55
+ break;
49
56
  case MongoErrorCode.DuplicateKey:
50
57
  handledError = new factory_1.errors.AlreadyInUse('', [], `Some fields already in use. code:${handledError.code} message:${handledError.message}`);
51
- // no op
52
58
  break;
53
59
  case MongoErrorCode.MaxTimeMSExpired:
54
60
  case MongoErrorCode.ExceededTimeLimit:
@@ -1,5 +1,5 @@
1
1
  import type { BulkWriteResult } from 'mongodb';
2
- import type { Connection, Document, FilterQuery } from 'mongoose';
2
+ import { Connection, Document, FilterQuery } from 'mongoose';
3
3
  import * as factory from '../factory';
4
4
  import * as EventFactory from '../factory/event';
5
5
  export interface IAttributes4patchUpdate {
@@ -63,6 +63,10 @@ export type ICreatingEvent4ttts = Pick<factory.event.screeningEvent.IAttributes,
63
63
  export declare class EventRepo {
64
64
  private readonly eventModel;
65
65
  constructor(connection: Connection);
66
+ /**
67
+ * イベントIDを生成する
68
+ */
69
+ static CREATE_ID(): string;
66
70
  static CREATE_MONGO_CONDITIONS(conditions: ISearchConditions): FilterQuery<factory.event.screeningEvent.IEvent>[];
67
71
  /**
68
72
  * 複数イベントを作成する
@@ -21,11 +21,14 @@ var __rest = (this && this.__rest) || function (s, e) {
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.EventRepo = void 0;
24
+ const mongoose_1 = require("mongoose");
25
+ const uniqid = require("uniqid");
26
+ const errorHandler_1 = require("../errorHandler");
24
27
  const factory = require("../factory");
25
28
  const EventFactory = require("../factory/event");
26
- const event_1 = require("./mongoose/schemas/event");
27
- const errorHandler_1 = require("../errorHandler");
28
29
  const settings_1 = require("../settings");
30
+ const event_1 = require("./mongoose/schemas/event");
31
+ const USE_OBJECT_ID_AS_EVENT_ID = process.env.USE_OBJECT_ID_AS_EVENT_ID === '1';
29
32
  const AVAILABLE_PUBLIC_PROJECT_FIELDS = [
30
33
  'additionalProperty', 'aggregateReservation', 'attendeeCount', 'checkInCount', 'coaInfo',
31
34
  // 'description',
@@ -41,6 +44,13 @@ class EventRepo {
41
44
  constructor(connection) {
42
45
  this.eventModel = connection.model(event_1.modelName, (0, event_1.createSchema)());
43
46
  }
47
+ /**
48
+ * イベントIDを生成する
49
+ */
50
+ static CREATE_ID() {
51
+ // implement using ObjectId(2025-10-17~)
52
+ return (USE_OBJECT_ID_AS_EVENT_ID) ? new mongoose_1.Types.ObjectId().toHexString() : uniqid();
53
+ }
44
54
  // tslint:disable-next-line:cyclomatic-complexity max-func-body-length
45
55
  static CREATE_MONGO_CONDITIONS(conditions) {
46
56
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4;
@@ -260,9 +270,8 @@ class EventRepo {
260
270
  */
261
271
  createManyEvents(params) {
262
272
  return __awaiter(this, void 0, void 0, function* () {
263
- const uniqid = yield Promise.resolve().then(() => require('uniqid'));
264
273
  const insertingDocs = params.attributes.map((p) => {
265
- return Object.assign({ _id: uniqid() }, p);
274
+ return Object.assign({ _id: EventRepo.CREATE_ID() }, p);
266
275
  });
267
276
  try {
268
277
  yield this.eventModel.insertMany(insertingDocs, { rawResult: true } // rawResult(2024-08-02~)
@@ -292,7 +301,6 @@ class EventRepo {
292
301
  upsertManyByAdditionalProperty(params, options) {
293
302
  return __awaiter(this, void 0, void 0, function* () {
294
303
  const { update } = options;
295
- const uniqid = yield Promise.resolve().then(() => require('uniqid'));
296
304
  const bulkWriteOps = [];
297
305
  const additionalProperties = [];
298
306
  const { events, additionalPropertyFilter, eventSeries, screeningRoom } = params;
@@ -347,7 +355,7 @@ class EventRepo {
347
355
  maximumAttendeeCapacity, remainingAttendeeCapacity, checkInCount, attendeeCount, aggregateReservation, // ←適用しない
348
356
  eventStatus, superEvent } = creatingEventParams, // <-上書き可能な属性を限定的に
349
357
  setOnInsertFields = __rest(creatingEventParams, ["coaInfo", "maximumAttendeeCapacity", "remainingAttendeeCapacity", "checkInCount", "attendeeCount", "aggregateReservation", "eventStatus", "superEvent"]);
350
- const setOnInsert = Object.assign(Object.assign({}, setOnInsertFields), { _id: uniqid() });
358
+ const setOnInsert = Object.assign(Object.assign({}, setOnInsertFields), { _id: EventRepo.CREATE_ID() });
351
359
  bulkWriteOps.push({
352
360
  updateOne: {
353
361
  filter,
@@ -389,7 +397,6 @@ class EventRepo {
389
397
  upsertManyScreeningEventByIdentifier(params, options) {
390
398
  return __awaiter(this, void 0, void 0, function* () {
391
399
  const { update } = options;
392
- const uniqid = yield Promise.resolve().then(() => require('uniqid'));
393
400
  const bulkWriteOps = [];
394
401
  const queryFilters = [];
395
402
  if (Array.isArray(params)) {
@@ -423,7 +430,7 @@ class EventRepo {
423
430
  }
424
431
  else {
425
432
  const { id, coaInfo, description, maximumAttendeeCapacity, remainingAttendeeCapacity, checkInCount, attendeeCount, aggregateReservation } = $set, setOnInsertFields = __rest($set, ["id", "coaInfo", "description", "maximumAttendeeCapacity", "remainingAttendeeCapacity", "checkInCount", "attendeeCount", "aggregateReservation"]);
426
- const setOnInsert = Object.assign(Object.assign({}, setOnInsertFields), { identifier, _id: uniqid() });
433
+ const setOnInsert = Object.assign(Object.assign({}, setOnInsertFields), { identifier, _id: EventRepo.CREATE_ID() });
427
434
  const updateOne = {
428
435
  filter,
429
436
  update: {
@@ -485,8 +492,7 @@ class EventRepo {
485
492
  }
486
493
  try {
487
494
  if (params.id === undefined) {
488
- const uniqid = yield Promise.resolve().then(() => require('uniqid'));
489
- const id = uniqid();
495
+ const id = EventRepo.CREATE_ID();
490
496
  doc = yield this.eventModel.create(Object.assign(Object.assign({}, params.attributes), { _id: id }));
491
497
  savedEventId = id;
492
498
  }
@@ -566,8 +572,7 @@ class EventRepo {
566
572
  if (typeof identifier !== 'string' || identifier.length === 0) {
567
573
  throw new factory.errors.ArgumentNull('identifier');
568
574
  }
569
- const uniqid = yield Promise.resolve().then(() => require('uniqid'));
570
- const id = uniqid();
575
+ const id = EventRepo.CREATE_ID();
571
576
  const doc = yield this.eventModel.findOneAndUpdate(
572
577
  // 全イベントにidentifierを保証したので、additionalPropertyによるフィルターからidentifierによるフィルターへ変更(2025-09-05~)
573
578
  // {
package/package.json CHANGED
@@ -115,5 +115,5 @@
115
115
  "postversion": "git push origin --tags",
116
116
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
117
117
  },
118
- "version": "23.0.0-alpha.11"
118
+ "version": "23.0.0-alpha.13"
119
119
  }
@@ -1,59 +0,0 @@
1
- // tslint:disable:no-console
2
- import * as mongoose from 'mongoose';
3
-
4
- import { chevre } from '../../../lib/index';
5
-
6
- // const PROJECT_ID = process.env.PROJECT_ID;
7
- mongoose.Model.on('index', (...args) => {
8
- console.error('******** index event emitted. ********\n', args);
9
- });
10
-
11
- async function main() {
12
- await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
13
-
14
- const eventRepo = await chevre.repository.Event.createInstance(mongoose.connection);
15
- const offerCatalogRepo = await chevre.repository.OfferCatalog.createInstance(mongoose.connection);
16
- const offerCatalogItemRepo = await chevre.repository.OfferCatalogItem.createInstance(mongoose.connection);
17
- const productRepo = await chevre.repository.Product.createInstance(mongoose.connection);
18
-
19
- const result = await (await chevre.service.offer.createService()).event.searchOfferCatalogItems({
20
- event: {
21
- id: 'bm0f0cadm'
22
- },
23
- limit: 10,
24
- page: 1,
25
- options: { includedInDataCatalog: { id: 'blpj322ni' } }
26
- })({
27
- event: eventRepo,
28
- offerCatalog: offerCatalogRepo,
29
- offerCatalogItem: offerCatalogItemRepo,
30
- product: productRepo
31
- });
32
- console.log(result);
33
- console.log(result.length);
34
-
35
- // console.log('searching...');
36
- // const catalogs = await catalogItemRepo.search(
37
- // {
38
- // project: { id: { $eq: PROJECT_ID } },
39
- // sort: { identifier: chevre.factory.sortType.Descending },
40
- // limit: 2,
41
- // page: 1,
42
- // itemListElement: { typeOf: { $eq: 'Offer' } }
43
- // }
44
- // );
45
- // console.log(catalogs[0]?.id, typeof catalogs[0]?.id);
46
- // console.log(catalogs.length);
47
-
48
- // const numCatalogs = await catalogItemRepo.count(
49
- // {
50
- // project: { id: { $eq: PROJECT_ID } },
51
- // itemListElement: { typeOf: { $eq: 'Offer' } }
52
- // }
53
- // );
54
- // console.log('numCatalogs:', numCatalogs);
55
- }
56
-
57
- main()
58
- .then(console.log)
59
- .catch(console.error);