@chevre/domain 23.2.0-alpha.38 → 23.2.0-alpha.40

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,7 +1,7 @@
1
1
  // tslint:disable:no-console
2
2
  import * as mongoose from 'mongoose';
3
3
 
4
- import { chevre } from '../../../lib/index';
4
+ import { chevre } from '../../../../lib/index';
5
5
 
6
6
  const PROJECT_ID = String(process.env.PROJECT_ID);
7
7
 
@@ -35,8 +35,8 @@ async function main() {
35
35
  },
36
36
  $unset: {}
37
37
  }
38
- ]
39
- // { replace: true }
38
+ ],
39
+ { update: true }
40
40
  );
41
41
  console.log('result:', result);
42
42
  }
@@ -1,7 +1,7 @@
1
1
  // tslint:disable:no-console
2
2
  import * as mongoose from 'mongoose';
3
3
 
4
- import { chevre } from '../../../lib/index';
4
+ import { chevre } from '../../../../lib/index';
5
5
 
6
6
  const PROJECT_ID = String(process.env.PROJECT_ID);
7
7
 
@@ -35,8 +35,9 @@ async function main() {
35
35
  },
36
36
  $unset: {}
37
37
  }
38
- ]
39
- // { replace: true }
38
+ ],
39
+ { update: true }
40
+ // { update: false }
40
41
  );
41
42
  console.log('result:', result);
42
43
  }
@@ -35,7 +35,12 @@ export declare class OfferCatalogRepo {
35
35
  $unset?: {
36
36
  [key in keyof factory.offerCatalog.IOfferCatalog]?: 1;
37
37
  };
38
- }[]): Promise<{
38
+ }[], options: {
39
+ /**
40
+ * support only update(2026-01-30~)
41
+ */
42
+ update: boolean;
43
+ }): Promise<{
39
44
  bulkWriteResult: BulkWriteResult;
40
45
  modifiedCatalogs: {
41
46
  id: string;
@@ -195,11 +195,9 @@ class OfferCatalogRepo {
195
195
  /**
196
196
  * コードをキーにして冪等置換(2023-12-14~)
197
197
  */
198
- upsertManyByIdentifier(params
199
- // options?: {
200
- // }
201
- ) {
198
+ upsertManyByIdentifier(params, options) {
202
199
  return __awaiter(this, void 0, void 0, function* () {
200
+ const { update } = options;
203
201
  const uniqid = yield Promise.resolve().then(() => require('uniqid'));
204
202
  const bulkWriteOps = [];
205
203
  const queryFilters = [];
@@ -218,19 +216,30 @@ class OfferCatalogRepo {
218
216
  'project.id': { $eq: project.id },
219
217
  identifier: { $eq: identifier }
220
218
  });
221
- const newId = uniqid();
222
- const setOnInsert = {
223
- identifier,
224
- project,
225
- typeOf,
226
- _id: newId
227
- };
228
- const updateOne = {
229
- filter,
230
- update: Object.assign({ $setOnInsert: setOnInsert, $set: setFields }, ($unset !== undefined) ? { $unset } : undefined),
231
- upsert: true
232
- };
233
- bulkWriteOps.push({ updateOne });
219
+ if (update === true) {
220
+ const updateOne = {
221
+ filter,
222
+ update: Object.assign({ $set: setFields }, ($unset !== undefined) ? { $unset } : undefined),
223
+ upsert: false
224
+ };
225
+ bulkWriteOps.push({ updateOne });
226
+ }
227
+ else {
228
+ // デフォルトはupsert操作
229
+ const newId = uniqid();
230
+ const setOnInsert = {
231
+ identifier,
232
+ project,
233
+ typeOf,
234
+ _id: newId
235
+ };
236
+ const updateOne = {
237
+ filter,
238
+ update: Object.assign({ $setOnInsert: setOnInsert, $set: setFields }, ($unset !== undefined) ? { $unset } : undefined),
239
+ upsert: true
240
+ };
241
+ bulkWriteOps.push({ updateOne });
242
+ }
234
243
  });
235
244
  }
236
245
  if (bulkWriteOps.length > 0) {
@@ -32,7 +32,12 @@ export declare class OfferCatalogItemRepo {
32
32
  $unset?: {
33
33
  [key in keyof factory.offerCatalog.IOfferCatalog]?: 1;
34
34
  };
35
- }[]): Promise<{
35
+ }[], options: {
36
+ /**
37
+ * support only update(2026-01-30~)
38
+ */
39
+ update: boolean;
40
+ }): Promise<{
36
41
  bulkWriteResult: BulkWriteResult;
37
42
  modifiedCatalogs: {
38
43
  id: string;
@@ -159,11 +159,9 @@ class OfferCatalogItemRepo {
159
159
  /**
160
160
  * コードをキーにして冪等置換(2023-12-14~)
161
161
  */
162
- upsertManyByIdentifier(params
163
- // options?: {
164
- // }
165
- ) {
162
+ upsertManyByIdentifier(params, options) {
166
163
  return __awaiter(this, void 0, void 0, function* () {
164
+ const { update } = options;
167
165
  const bulkWriteOps = [];
168
166
  const queryFilters = [];
169
167
  if (Array.isArray(params)) {
@@ -181,17 +179,28 @@ class OfferCatalogItemRepo {
181
179
  'project.id': { $eq: project.id },
182
180
  identifier: { $eq: identifier }
183
181
  });
184
- const setOnInsert = {
185
- identifier,
186
- project,
187
- typeOf
188
- };
189
- const updateOne = {
190
- filter,
191
- update: Object.assign({ $setOnInsert: setOnInsert, $set: setFields }, ($unset !== undefined) ? { $unset } : undefined),
192
- upsert: true
193
- };
194
- bulkWriteOps.push({ updateOne });
182
+ if (update === true) {
183
+ const updateOne = {
184
+ filter,
185
+ update: Object.assign({ $set: setFields }, ($unset !== undefined) ? { $unset } : undefined),
186
+ upsert: false
187
+ };
188
+ bulkWriteOps.push({ updateOne });
189
+ }
190
+ else {
191
+ // デフォルトはupsert操作
192
+ const setOnInsert = {
193
+ identifier,
194
+ project,
195
+ typeOf
196
+ };
197
+ const updateOne = {
198
+ filter,
199
+ update: Object.assign({ $setOnInsert: setOnInsert, $set: setFields }, ($unset !== undefined) ? { $unset } : undefined),
200
+ upsert: true
201
+ };
202
+ bulkWriteOps.push({ updateOne });
203
+ }
195
204
  });
196
205
  }
197
206
  if (bulkWriteOps.length > 0) {
package/package.json CHANGED
@@ -116,5 +116,5 @@
116
116
  "postversion": "git push origin --tags",
117
117
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
118
118
  },
119
- "version": "23.2.0-alpha.38"
119
+ "version": "23.2.0-alpha.40"
120
120
  }
@@ -1,154 +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: String(process.env.PROJECT_ID) };
7
- const NUM_CREATE_OFFERS = 300;
8
- const NUM_CREATE_SUB_CATALOGS = 10;
9
-
10
- // tslint:disable-next-line:max-func-body-length
11
- async function main() {
12
- await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
13
-
14
- const aggregateOfferRepo = await chevre.repository.AggregateOffer.createInstance(mongoose.connection);
15
- const offerCatalogRepo = await chevre.repository.OfferCatalog.createInstance(mongoose.connection);
16
- const productRepo = await chevre.repository.Product.createInstance(mongoose.connection);
17
- const offerCatalogItemRepo = await chevre.repository.OfferCatalogItem.createInstance(mongoose.connection);
18
-
19
- const savingOffers: Omit<chevre.factory.unitPriceOffer.IUnitPriceOffer, 'id'>[] =
20
- // tslint:disable-next-line:prefer-array-literal
21
- [...Array(NUM_CREATE_OFFERS)].map<Omit<chevre.factory.unitPriceOffer.IUnitPriceOffer, 'id'>>((__, i) => {
22
- const identifier = `sampleFreeOffer${i}`;
23
- const priceSpecification: chevre.factory.unitPriceOffer.IUnitPriceOfferPriceSpecification = {
24
- typeOf: chevre.factory.priceSpecificationType.UnitPriceSpecification,
25
- priceCurrency: chevre.factory.priceCurrency.JPY,
26
- valueAddedTaxIncluded: true,
27
- price: 0,
28
- referenceQuantity: {
29
- value: 1,
30
- typeOf: 'QuantitativeValue',
31
- unitCode: chevre.factory.unitCode.C62
32
- },
33
- accounting: {
34
- typeOf: 'Accounting',
35
- accountsReceivable: 0
36
- }
37
- };
38
-
39
- return {
40
- project: { id: project.id, typeOf: chevre.factory.organizationType.Project },
41
- typeOf: chevre.factory.offerType.Offer,
42
- identifier,
43
- description: { en: identifier, ja: identifier },
44
- alternateName: { ja: identifier },
45
- name: { en: identifier, ja: identifier },
46
- availability: chevre.factory.itemAvailability.InStock,
47
- availableAtOrFrom: [
48
- { id: '51qbjcfr72h62m06vtv5kkhgje' }
49
- ],
50
- itemOffered: { typeOf: chevre.factory.product.ProductType.EventService },
51
- priceCurrency: chevre.factory.priceCurrency.JPY,
52
- priceSpecification
53
- };
54
- });
55
- const saveOfferResult = await aggregateOfferRepo.upsertByIdentifier(savingOffers);
56
- console.log('saveOfferResult:', saveOfferResult);
57
-
58
- const offerIds: string[] = (Array.isArray(saveOfferResult?.modifiedOffers)) ? saveOfferResult.modifiedOffers.map(({ id }) => id) : [];
59
- const catalogIdentifier = 'sampleFreeOffersCatalog';
60
- const savingOfferCatalog: chevre.factory.offerCatalog.IOfferCatalog = {
61
- project: { id: project.id, typeOf: chevre.factory.organizationType.Project },
62
- typeOf: 'OfferCatalog',
63
- identifier: catalogIdentifier,
64
- description: { en: catalogIdentifier, ja: catalogIdentifier },
65
- name: { en: catalogIdentifier, ja: catalogIdentifier },
66
- itemListElement: offerIds.map((id) => ({ id, typeOf: chevre.factory.offerType.Offer })),
67
- itemOffered: { typeOf: chevre.factory.product.ProductType.EventService }
68
- };
69
- const saveCatalogResult = await offerCatalogRepo.saveByIdentifier(savingOfferCatalog);
70
- console.log('saveCatalogResult:', saveCatalogResult);
71
-
72
- const productId = 'sampleFreeEventService';
73
- const savingProduct: chevre.factory.product.IProduct & {
74
- offers?: never;
75
- } = {
76
- project: { id: project.id, typeOf: chevre.factory.organizationType.Project },
77
- typeOf: chevre.factory.product.ProductType.EventService,
78
- name: { en: productId, ja: productId },
79
- productID: productId,
80
- hasOfferCatalog: {
81
- typeOf: 'OfferCatalog',
82
- itemListElement: [{ id: saveCatalogResult.id }]
83
- }
84
- };
85
- const saveProductResult = await productRepo.upsertManyByProductId([{
86
- $set: savingProduct
87
- }]);
88
- console.log('saveProductResult:', saveProductResult);
89
-
90
- // 以下サブカタログ
91
- const offerIds4subCatalog: string[] = (Array.isArray(saveOfferResult?.modifiedOffers))
92
- // tslint:disable-next-line:no-magic-numbers
93
- ? saveOfferResult.modifiedOffers.slice(0, 100)
94
- .map(({ id }) => id) :
95
- [];
96
- const savingSubOfferCatalogs: chevre.factory.offerCatalog.IOfferCatalog[] =
97
- // tslint:disable-next-line:prefer-array-literal
98
- [...Array(NUM_CREATE_SUB_CATALOGS)].map<chevre.factory.offerCatalog.IOfferCatalog>((__, i) => {
99
- const subCatalogIdentifier = `sampleFreeOffersSubCatalog${i}`;
100
-
101
- return {
102
- project: { id: project.id, typeOf: chevre.factory.organizationType.Project },
103
- typeOf: 'OfferCatalog',
104
- identifier: subCatalogIdentifier,
105
- description: { en: subCatalogIdentifier, ja: subCatalogIdentifier },
106
- name: { en: subCatalogIdentifier, ja: subCatalogIdentifier },
107
- itemListElement: offerIds4subCatalog.map((id) => ({ id, typeOf: chevre.factory.offerType.Offer })),
108
- itemOffered: { typeOf: chevre.factory.product.ProductType.EventService }
109
- };
110
- });
111
- const saveSubCatalogResult = await offerCatalogItemRepo.upsertManyByIdentifier(savingSubOfferCatalogs.map((savingSubOfferCatalog) => {
112
- return {
113
- $set: savingSubOfferCatalog
114
- };
115
- }));
116
- console.log('saveSubCatalogResult:', saveSubCatalogResult);
117
-
118
- const subCatalogIds: string[] =
119
- (Array.isArray(saveSubCatalogResult?.modifiedCatalogs)) ? saveSubCatalogResult.modifiedCatalogs.map(({ id }) => id) : [];
120
- const catalogBySubCatalogIdentifier = 'sampleFreeOffersCatalogBySub';
121
- const savingOfferCatalogBySubCatalog: chevre.factory.offerCatalog.IOfferCatalog = {
122
- project: { id: project.id, typeOf: chevre.factory.organizationType.Project },
123
- typeOf: 'OfferCatalog',
124
- identifier: catalogBySubCatalogIdentifier,
125
- description: { en: catalogBySubCatalogIdentifier, ja: catalogBySubCatalogIdentifier },
126
- name: { en: catalogBySubCatalogIdentifier, ja: catalogBySubCatalogIdentifier },
127
- itemListElement: subCatalogIds.map((id) => ({ id, typeOf: 'OfferCatalog' })),
128
- itemOffered: { typeOf: chevre.factory.product.ProductType.EventService }
129
- };
130
- const saveCatalogBySubResult = await offerCatalogRepo.saveByIdentifier(savingOfferCatalogBySubCatalog);
131
- console.log('saveCatalogBySubResult:', saveCatalogBySubResult);
132
-
133
- const productIdBySubCatalog = 'sampleFreeEventServiceBySub';
134
- const savingProductBySubCatalog: chevre.factory.product.IProduct & {
135
- offers?: never;
136
- } = {
137
- project: { id: project.id, typeOf: chevre.factory.organizationType.Project },
138
- typeOf: chevre.factory.product.ProductType.EventService,
139
- name: { en: productIdBySubCatalog, ja: productIdBySubCatalog },
140
- productID: productIdBySubCatalog,
141
- hasOfferCatalog: {
142
- typeOf: 'OfferCatalog',
143
- itemListElement: [{ id: saveCatalogBySubResult.id }]
144
- }
145
- };
146
- const saveProductBySubCatalogResult = await productRepo.upsertManyByProductId([{
147
- $set: savingProductBySubCatalog
148
- }]);
149
- console.log('saveProductBySubCatalogResult:', saveProductBySubCatalogResult);
150
- }
151
-
152
- main()
153
- .then()
154
- .catch(console.error);