@chevre/domain 21.18.0-alpha.2 → 21.18.0-alpha.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.
- package/example/src/chevre/upsertMoviesByIdentifier.ts +57 -0
- package/example/src/duration.ts +22 -0
- package/lib/chevre/repo/creativeWork.d.ts +5 -1
- package/lib/chevre/repo/creativeWork.js +43 -22
- package/lib/chevre/repo/offerCatalog.d.ts +8 -2
- package/lib/chevre/repo/offerCatalog.js +8 -8
- package/package.json +1 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// tslint:disable:no-console
|
|
2
|
+
import * as moment from 'moment';
|
|
3
|
+
import * as mongoose from 'mongoose';
|
|
4
|
+
|
|
5
|
+
import { chevre } from '../../../lib/index';
|
|
6
|
+
|
|
7
|
+
const PROJECT_ID = String(process.env.PROJECT_ID);
|
|
8
|
+
|
|
9
|
+
async function main() {
|
|
10
|
+
await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
|
|
11
|
+
|
|
12
|
+
const creativeWorkRepo = await chevre.repository.CreativeWork.createInstance(mongoose.connection);
|
|
13
|
+
|
|
14
|
+
const result = await creativeWorkRepo.upsertMoviesByIdentifier(
|
|
15
|
+
[
|
|
16
|
+
{
|
|
17
|
+
attributes: {
|
|
18
|
+
id: '',
|
|
19
|
+
identifier: '00001',
|
|
20
|
+
typeOf: chevre.factory.creativeWorkType.Movie,
|
|
21
|
+
duration: 'PT2H',
|
|
22
|
+
name: { ja: '名探偵コナン ゼロの執行人', en: 'Detective Conan Zero Enforcer' },
|
|
23
|
+
offers: {
|
|
24
|
+
typeOf: chevre.factory.offerType.Offer,
|
|
25
|
+
availabilityEnds: moment('2118-11-04T15:00:00.000Z')
|
|
26
|
+
.toDate(),
|
|
27
|
+
availabilityStarts: moment('2018-11-25T15:00:00.000Z')
|
|
28
|
+
.toDate()
|
|
29
|
+
},
|
|
30
|
+
project: { typeOf: chevre.factory.organizationType.Project, id: PROJECT_ID },
|
|
31
|
+
additionalProperty: [
|
|
32
|
+
{
|
|
33
|
+
name: 'posterImage',
|
|
34
|
+
value: 'https://iwiz-movies.c.yimg.jp/c/movies/pict/p/p/90/cb/174755_02.jpg'
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
headline: 'サブタイトルサブタイトルサブタイトル',
|
|
38
|
+
datePublished: moment('2018-11-04T15:00:00.000Z')
|
|
39
|
+
.toDate(),
|
|
40
|
+
distributor: {
|
|
41
|
+
id: '5e35398a8067a30012dd6717',
|
|
42
|
+
codeValue: '001',
|
|
43
|
+
...{
|
|
44
|
+
distributorType: '001'
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
{ replace: true }
|
|
51
|
+
);
|
|
52
|
+
console.log('result:', result);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
main()
|
|
56
|
+
.then(console.log)
|
|
57
|
+
.catch(console.error);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// tslint:disable:no-console
|
|
2
|
+
import * as moment from 'moment';
|
|
3
|
+
|
|
4
|
+
const MINUTES = 10;
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const duration = moment.duration(MINUTES, 'minutes');
|
|
8
|
+
const newDuration = moment.duration(duration.toISOString());
|
|
9
|
+
console.log(duration);
|
|
10
|
+
console.log(newDuration);
|
|
11
|
+
console.log(duration.toISOString());
|
|
12
|
+
console.log(newDuration.toISOString());
|
|
13
|
+
console.log(newDuration.isValid());
|
|
14
|
+
console.log(moment.duration('invalid')
|
|
15
|
+
.isValid());
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
main()
|
|
19
|
+
.then(() => {
|
|
20
|
+
console.log('success!');
|
|
21
|
+
})
|
|
22
|
+
.catch(console.error);
|
|
@@ -42,7 +42,11 @@ export declare class MongoRepository {
|
|
|
42
42
|
*/
|
|
43
43
|
upsertMoviesByIdentifier(params: {
|
|
44
44
|
attributes: factory.creativeWork.movie.ICreativeWork;
|
|
45
|
-
}[]
|
|
45
|
+
}[], options?: {
|
|
46
|
+
replace?: boolean;
|
|
47
|
+
}): Promise<{
|
|
48
|
+
bulkWriteResult: BulkWriteResult;
|
|
49
|
+
} | void>;
|
|
46
50
|
/**
|
|
47
51
|
* コンテンツを検索する
|
|
48
52
|
*/
|
|
@@ -138,36 +138,57 @@ class MongoRepository {
|
|
|
138
138
|
/**
|
|
139
139
|
* コードをキーにして冪等作成
|
|
140
140
|
*/
|
|
141
|
-
|
|
141
|
+
// tslint:disable-next-line:max-func-body-length
|
|
142
|
+
upsertMoviesByIdentifier(params, options) {
|
|
142
143
|
return __awaiter(this, void 0, void 0, function* () {
|
|
143
144
|
const bulkWriteOps = [];
|
|
144
145
|
if (Array.isArray(params)) {
|
|
145
146
|
params.forEach((creatingMovieParams) => {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
147
|
+
if ((options === null || options === void 0 ? void 0 : options.replace) === true) {
|
|
148
|
+
const _a = creatingMovieParams.attributes, { id } = _a, replaceFields = __rest(_a, ["id"]);
|
|
149
|
+
if (typeof replaceFields.identifier !== 'string' || replaceFields.identifier.length === 0) {
|
|
150
|
+
throw new factory.errors.ArgumentNull('identifier');
|
|
151
|
+
}
|
|
152
|
+
const replacement = replaceFields;
|
|
153
|
+
const replaceOne = {
|
|
154
|
+
filter: {
|
|
155
|
+
typeOf: replaceFields.typeOf,
|
|
156
|
+
'project.id': { $eq: replaceFields.project.id },
|
|
157
|
+
identifier: { $eq: replaceFields.identifier }
|
|
158
|
+
},
|
|
159
|
+
replacement,
|
|
160
|
+
upsert: true
|
|
161
|
+
};
|
|
162
|
+
bulkWriteOps.push({ replaceOne });
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
const _b = creatingMovieParams.attributes, { typeOf, project, identifier, duration, name, additionalProperty, contentRating, headline, distributor, thumbnailUrl, datePublished } = _b, setOnInsertFields = __rest(_b, ["typeOf", "project", "identifier", "duration", "name", "additionalProperty", "contentRating", "headline", "distributor", "thumbnailUrl", "datePublished"]);
|
|
166
|
+
if (typeof identifier !== 'string' || identifier.length === 0) {
|
|
167
|
+
throw new factory.errors.ArgumentNull('identifier');
|
|
168
|
+
}
|
|
169
|
+
const updateFilter = {
|
|
170
|
+
$setOnInsert: Object.assign(Object.assign({}, setOnInsertFields), { typeOf,
|
|
171
|
+
project,
|
|
172
|
+
identifier }),
|
|
173
|
+
// 変更可能な属性のみ上書き
|
|
174
|
+
$set: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (name !== undefined) ? { name } : undefined), (contentRating !== undefined) ? { contentRating } : undefined), (duration !== undefined) ? { duration } : undefined), (headline !== undefined) ? { headline } : undefined), (datePublished !== undefined) ? { datePublished } : undefined), (distributor !== undefined) ? { distributor } : undefined), (typeof thumbnailUrl === 'string') ? { thumbnailUrl } : undefined), (Array.isArray(additionalProperty)) ? { additionalProperty } : [])
|
|
175
|
+
};
|
|
176
|
+
const updateOne = {
|
|
177
|
+
filter: {
|
|
178
|
+
typeOf: typeOf,
|
|
179
|
+
'project.id': { $eq: project.id },
|
|
180
|
+
identifier: { $eq: identifier }
|
|
181
|
+
},
|
|
182
|
+
update: updateFilter,
|
|
183
|
+
upsert: true
|
|
184
|
+
};
|
|
185
|
+
bulkWriteOps.push({ updateOne });
|
|
149
186
|
}
|
|
150
|
-
const updateFilter = {
|
|
151
|
-
$setOnInsert: Object.assign(Object.assign({}, setOnInsertFields), { typeOf,
|
|
152
|
-
project,
|
|
153
|
-
identifier }),
|
|
154
|
-
// 変更可能な属性のみ上書き
|
|
155
|
-
$set: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (name !== undefined) ? { name } : undefined), (contentRating !== undefined) ? { contentRating } : undefined), (duration !== undefined) ? { duration } : undefined), (headline !== undefined) ? { headline } : undefined), (datePublished !== undefined) ? { datePublished } : undefined), (distributor !== undefined) ? { distributor } : undefined), (typeof thumbnailUrl === 'string') ? { thumbnailUrl } : undefined), (Array.isArray(additionalProperty)) ? { additionalProperty } : [])
|
|
156
|
-
};
|
|
157
|
-
const updateOne = {
|
|
158
|
-
filter: {
|
|
159
|
-
typeOf: typeOf,
|
|
160
|
-
'project.id': { $eq: project.id },
|
|
161
|
-
identifier: { $eq: identifier }
|
|
162
|
-
},
|
|
163
|
-
update: updateFilter,
|
|
164
|
-
upsert: true
|
|
165
|
-
};
|
|
166
|
-
bulkWriteOps.push({ updateOne });
|
|
167
187
|
});
|
|
168
188
|
}
|
|
169
189
|
if (bulkWriteOps.length > 0) {
|
|
170
|
-
|
|
190
|
+
const bulkWriteResult = yield this.creativeWorkModel.bulkWrite(bulkWriteOps, { ordered: false });
|
|
191
|
+
return { bulkWriteResult };
|
|
171
192
|
}
|
|
172
193
|
});
|
|
173
194
|
}
|
|
@@ -35,11 +35,17 @@ export declare class MongoRepository {
|
|
|
35
35
|
private readonly offerCatalogModel;
|
|
36
36
|
constructor(connection: Connection);
|
|
37
37
|
static CREATE_MONGO_CONDITIONS(params: factory.offerCatalog.ISearchConditions): any[];
|
|
38
|
-
save(params: factory.offerCatalog.IOfferCatalog
|
|
38
|
+
save(params: factory.offerCatalog.IOfferCatalog & {
|
|
39
|
+
$unset?: any;
|
|
40
|
+
}): Promise<{
|
|
41
|
+
id: string;
|
|
42
|
+
}>;
|
|
39
43
|
/**
|
|
40
44
|
* プロジェクトとコードから冪等保管
|
|
41
45
|
*/
|
|
42
|
-
saveByIdentifier(params: factory.offerCatalog.IOfferCatalog): Promise<
|
|
46
|
+
saveByIdentifier(params: factory.offerCatalog.IOfferCatalog): Promise<{
|
|
47
|
+
id: string;
|
|
48
|
+
}>;
|
|
43
49
|
/**
|
|
44
50
|
* 同期日時を更新する
|
|
45
51
|
*/
|
|
@@ -133,13 +133,13 @@ class MongoRepository {
|
|
|
133
133
|
let doc;
|
|
134
134
|
if (params.id === '') {
|
|
135
135
|
const uniqid = yield Promise.resolve().then(() => require('uniqid'));
|
|
136
|
-
const
|
|
137
|
-
|
|
136
|
+
const newId = uniqid();
|
|
137
|
+
const { id, $unset } = params, creatingDoc = __rest(params, ["id", "$unset"]);
|
|
138
|
+
doc = yield this.offerCatalogModel.create(Object.assign(Object.assign({}, creatingDoc), { _id: newId }));
|
|
138
139
|
}
|
|
139
140
|
else {
|
|
140
|
-
// 上書き禁止属性を除外
|
|
141
|
-
|
|
142
|
-
doc = yield this.offerCatalogModel.findOneAndUpdate({ _id: params.id }, updateFields, { upsert: false, new: true })
|
|
141
|
+
const { id, identifier, itemOffered, project, typeOf, $unset } = params, updateFields = __rest(params, ["id", "identifier", "itemOffered", "project", "typeOf", "$unset"]); // 上書き禁止属性を除外
|
|
142
|
+
doc = yield this.offerCatalogModel.findOneAndUpdate({ _id: { $eq: params.id } }, Object.assign({ $set: updateFields }, (params.$unset !== undefined) ? { $unset: params.$unset } : undefined), { upsert: false, new: true, projection: { _id: 1 } })
|
|
143
143
|
.exec();
|
|
144
144
|
}
|
|
145
145
|
if (doc === null) {
|
|
@@ -155,14 +155,14 @@ class MongoRepository {
|
|
|
155
155
|
return __awaiter(this, void 0, void 0, function* () {
|
|
156
156
|
const uniqid = yield Promise.resolve().then(() => require('uniqid'));
|
|
157
157
|
const newId = uniqid();
|
|
158
|
-
const { id, identifier, project, typeOf } = params, updateFields = __rest(params, ["id", "identifier", "project", "typeOf"]);
|
|
158
|
+
const { id, identifier, itemOffered, project, typeOf } = params, updateFields = __rest(params, ["id", "identifier", "itemOffered", "project", "typeOf"]);
|
|
159
159
|
const doc = yield this.offerCatalogModel.findOneAndUpdate({
|
|
160
160
|
identifier: { $eq: params.identifier },
|
|
161
161
|
'project.id': { $eq: params.project.id }
|
|
162
162
|
}, {
|
|
163
163
|
$set: updateFields,
|
|
164
|
-
$setOnInsert: { _id: newId, identifier, project, typeOf }
|
|
165
|
-
}, { upsert: true, new: true })
|
|
164
|
+
$setOnInsert: { _id: newId, identifier, itemOffered, project, typeOf }
|
|
165
|
+
}, { upsert: true, new: true, projection: { _id: 1 } })
|
|
166
166
|
.exec();
|
|
167
167
|
return doc.toObject();
|
|
168
168
|
});
|
package/package.json
CHANGED