@chevre/domain 21.19.0-alpha.7 → 21.19.0-alpha.8
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,46 @@
|
|
|
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
|
+
|
|
8
|
+
// tslint:disable-next-line:max-func-body-length
|
|
9
|
+
async function main() {
|
|
10
|
+
await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
|
|
11
|
+
|
|
12
|
+
const offerCatalogItemRepo = await chevre.repository.OfferCatalogItem.createInstance(mongoose.connection);
|
|
13
|
+
|
|
14
|
+
const result = await offerCatalogItemRepo.upsertManyByIdentifier(
|
|
15
|
+
[
|
|
16
|
+
{
|
|
17
|
+
$set: {
|
|
18
|
+
project: { typeOf: chevre.factory.organizationType.Project, id: PROJECT_ID },
|
|
19
|
+
typeOf: 'OfferCatalog',
|
|
20
|
+
id: '',
|
|
21
|
+
identifier: '2023122501CatalogItem',
|
|
22
|
+
itemListElement: [
|
|
23
|
+
{ id: '1001', typeOf: chevre.factory.offerType.Offer }
|
|
24
|
+
],
|
|
25
|
+
itemOffered: { typeOf: chevre.factory.product.ProductType.EventService },
|
|
26
|
+
name: {
|
|
27
|
+
en: 'xxx',
|
|
28
|
+
ja: 'xxx'
|
|
29
|
+
},
|
|
30
|
+
description: {
|
|
31
|
+
en: 'xxx',
|
|
32
|
+
ja: 'xxx'
|
|
33
|
+
},
|
|
34
|
+
additionalProperty: []
|
|
35
|
+
},
|
|
36
|
+
$unset: {}
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
// { replace: true }
|
|
40
|
+
);
|
|
41
|
+
console.log('result:', result);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
main()
|
|
45
|
+
.then(console.log)
|
|
46
|
+
.catch(console.error);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { BulkWriteResult } from 'mongodb';
|
|
1
2
|
import { Connection } from 'mongoose';
|
|
2
3
|
import * as factory from '../factory';
|
|
3
4
|
export type IAggregatedOfferCatalog = Pick<factory.offerCatalog.IOfferCatalog, 'id' | 'name' | 'description' | 'project' | 'typeOf' | 'identifier' | 'itemOffered' | 'additionalProperty' | 'relatedOffer'> & {
|
|
@@ -26,6 +27,20 @@ export declare class MongoRepository {
|
|
|
26
27
|
saveByIdentifier(params: factory.offerCatalog.IOfferCatalog): Promise<{
|
|
27
28
|
id: string;
|
|
28
29
|
}>;
|
|
30
|
+
/**
|
|
31
|
+
* コードをキーにして冪等置換(2023-12-14~)
|
|
32
|
+
*/
|
|
33
|
+
upsertManyByIdentifier(params: {
|
|
34
|
+
$set: factory.offerCatalog.IOfferCatalog;
|
|
35
|
+
$unset?: {
|
|
36
|
+
[key in keyof factory.offerCatalog.IOfferCatalog]?: 1;
|
|
37
|
+
};
|
|
38
|
+
}[]): Promise<{
|
|
39
|
+
bulkWriteResult: BulkWriteResult;
|
|
40
|
+
modifiedCatalogs: {
|
|
41
|
+
id: string;
|
|
42
|
+
}[];
|
|
43
|
+
} | void>;
|
|
29
44
|
/**
|
|
30
45
|
* 同期日時を更新する
|
|
31
46
|
*/
|
|
@@ -138,6 +138,53 @@ class MongoRepository {
|
|
|
138
138
|
return { id: doc.id };
|
|
139
139
|
});
|
|
140
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* コードをキーにして冪等置換(2023-12-14~)
|
|
143
|
+
*/
|
|
144
|
+
upsertManyByIdentifier(params
|
|
145
|
+
// options?: {
|
|
146
|
+
// }
|
|
147
|
+
) {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
const bulkWriteOps = [];
|
|
150
|
+
const queryFilters = [];
|
|
151
|
+
if (Array.isArray(params)) {
|
|
152
|
+
params.forEach(({ $set, $unset }) => {
|
|
153
|
+
const { id, identifier, project, typeOf } = $set, setFields = __rest($set, ["id", "identifier", "project", "typeOf"]);
|
|
154
|
+
if (typeof identifier !== 'string' || identifier.length === 0) {
|
|
155
|
+
throw new factory.errors.ArgumentNull('identifier');
|
|
156
|
+
}
|
|
157
|
+
// リソースのユニークネスを保証するfilter
|
|
158
|
+
const filter = {
|
|
159
|
+
'project.id': { $eq: project.id },
|
|
160
|
+
identifier: { $eq: identifier }
|
|
161
|
+
};
|
|
162
|
+
queryFilters.push({
|
|
163
|
+
'project.id': { $eq: project.id },
|
|
164
|
+
identifier: { $eq: identifier }
|
|
165
|
+
});
|
|
166
|
+
const setOnInsert = {
|
|
167
|
+
identifier,
|
|
168
|
+
project,
|
|
169
|
+
typeOf
|
|
170
|
+
};
|
|
171
|
+
const updateOne = {
|
|
172
|
+
filter,
|
|
173
|
+
update: Object.assign({ $setOnInsert: setOnInsert, $set: setFields }, ($unset !== undefined) ? { $unset } : undefined),
|
|
174
|
+
upsert: true
|
|
175
|
+
};
|
|
176
|
+
bulkWriteOps.push({ updateOne });
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
if (bulkWriteOps.length > 0) {
|
|
180
|
+
const bulkWriteResult = yield this.offerCatalogItemModel.bulkWrite(bulkWriteOps, { ordered: false });
|
|
181
|
+
// modifiedの場合upsertedIdsに含まれないので、idを検索する
|
|
182
|
+
const modifiedCatalogs = yield this.offerCatalogItemModel.find({ $or: queryFilters }, { _id: 1 })
|
|
183
|
+
.exec();
|
|
184
|
+
return { bulkWriteResult, modifiedCatalogs };
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
141
188
|
/**
|
|
142
189
|
* 同期日時を更新する
|
|
143
190
|
*/
|
package/package.json
CHANGED