@chevre/domain 21.15.0-alpha.4 → 21.15.0-alpha.6

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,32 @@
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
+ async function main() {
9
+ await mongoose.connect(<string>process.env.MONGOLAB_URI);
10
+
11
+ const categoryCodeRepo = await chevre.repository.CategoryCode.createInstance(mongoose.connection);
12
+
13
+ const categoryCodes = await categoryCodeRepo.searchByAggregate(
14
+ {
15
+ limit: 100,
16
+ page: 1,
17
+ sort: { codeValue: chevre.factory.sortType.Ascending }
18
+ // id: { $eq: 'xxx' }
19
+ // project: { id: { $eq: project.id } }
20
+ // paymentAccepted: { paymentMethodType: { $eq: 'Cash' } },
21
+ // hasMerchantReturnPolicy: { applicablePaymentMethod: {} }
22
+ },
23
+ [],
24
+ ['project', 'additionalProperty', 'typeOf']
25
+ );
26
+ console.log('categoryCodes found', categoryCodes);
27
+ console.log(categoryCodes.length, 'categoryCodes found');
28
+ }
29
+
30
+ main()
31
+ .then()
32
+ .catch(console.error);
@@ -10,11 +10,28 @@ async function main() {
10
10
 
11
11
  const sellerRepo = await chevre.repository.Seller.createInstance(mongoose.connection);
12
12
 
13
- const sellers = await sellerRepo.searchByAggregate(
13
+ const sellersByAggregate = await sellerRepo.searchByAggregate(
14
14
  {
15
15
  limit: 5,
16
16
  page: 1,
17
- sort: { branchCode: chevre.factory.sortType.Descending }
17
+ sort: { branchCode: chevre.factory.sortType.Descending },
18
+ id: { $eq: 'xxx' }
19
+ // project: { id: { $eq: project.id } }
20
+ // paymentAccepted: { paymentMethodType: { $eq: 'Cash' } },
21
+ // hasMerchantReturnPolicy: { applicablePaymentMethod: {} }
22
+ },
23
+ [],
24
+ ['hasMerchantReturnPolicy', 'additionalProperty', 'project', 'name', 'typeOf', 'url', 'telephone']
25
+ );
26
+ console.log('sellers found', sellersByAggregate, sellersByAggregate[0]?.hasMerchantReturnPolicy);
27
+ console.log(sellersByAggregate.length, 'sellers found');
28
+
29
+ const sellers = await sellerRepo.search(
30
+ {
31
+ limit: 5,
32
+ page: 1,
33
+ sort: { branchCode: chevre.factory.sortType.Descending },
34
+ id: { $eq: 'xxx' }
18
35
  // project: { id: { $eq: project.id } }
19
36
  // paymentAccepted: { paymentMethodType: { $eq: 'Cash' } },
20
37
  // hasMerchantReturnPolicy: { applicablePaymentMethod: {} }
@@ -22,7 +22,7 @@
22
22
  /// <reference types="mongoose/types/validation" />
23
23
  /// <reference types="mongoose/types/virtuals" />
24
24
  /// <reference types="mongoose/types/inferschematype" />
25
- import type { Connection } from 'mongoose';
25
+ import { AnyExpression, Connection, FilterQuery } from 'mongoose';
26
26
  import * as factory from '../factory';
27
27
  /**
28
28
  * 区分リポジトリ
@@ -30,8 +30,15 @@ import * as factory from '../factory';
30
30
  export declare class MongoRepository {
31
31
  private readonly categoryCodeModel;
32
32
  constructor(connection: Connection);
33
- static CREATE_MONGO_CONDITIONS(params: factory.categoryCode.ISearchConditions): any[];
33
+ static CREATE_MONGO_CONDITIONS(params: factory.categoryCode.ISearchConditions): FilterQuery<factory.categoryCode.ICategoryCode>[];
34
+ static CREATE_AGGREGATE_PROJECTION(inclusion: string[], exclusion: (keyof factory.categoryCode.ICategoryCode)[]): {
35
+ [field: string]: AnyExpression;
36
+ };
34
37
  count(params: factory.categoryCode.ISearchConditions): Promise<number>;
38
+ /**
39
+ * 集計検索(publicな属性検索が目的)
40
+ */
41
+ searchByAggregate(conditions: factory.categoryCode.ISearchConditions, inclusion: (keyof factory.categoryCode.ICategoryCode)[], exclusion: (keyof factory.categoryCode.ICategoryCode)[]): Promise<factory.categoryCode.ICategoryCode[]>;
35
42
  /**
36
43
  * 検索
37
44
  */
@@ -61,5 +68,5 @@ export declare class MongoRepository {
61
68
  id: string;
62
69
  };
63
70
  }): Promise<void>;
64
- getCursor(conditions: any, projection: any): import("mongoose").Cursor<any, import("mongoose").QueryOptions<any>>;
71
+ getCursor(conditions: FilterQuery<factory.categoryCode.ICategoryCode>, projection: any): import("mongoose").Cursor<any, import("mongoose").QueryOptions<any>>;
65
72
  }
@@ -21,6 +21,7 @@ var __rest = (this && this.__rest) || function (s, e) {
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.MongoRepository = void 0;
24
+ const mongoose_1 = require("mongoose");
24
25
  const categoryCode_1 = require("./mongoose/schemas/categoryCode");
25
26
  const factory = require("../factory");
26
27
  const settings_1 = require("../settings");
@@ -34,7 +35,6 @@ class MongoRepository {
34
35
  // tslint:disable-next-line:cyclomatic-complexity max-func-body-length
35
36
  static CREATE_MONGO_CONDITIONS(params) {
36
37
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
37
- // MongoDB検索条件
38
38
  const andConditions = [];
39
39
  // tslint:disable-next-line:no-single-line-block-comment
40
40
  /* istanbul ignore else */
@@ -51,11 +51,13 @@ class MongoRepository {
51
51
  }
52
52
  const idEq = (_a = params.id) === null || _a === void 0 ? void 0 : _a.$eq;
53
53
  if (typeof idEq === 'string') {
54
- andConditions.push({ _id: { $eq: idEq } });
54
+ // andConditions.push({ _id: { $eq: idEq } });
55
+ andConditions.push({ _id: { $eq: new mongoose_1.Types.ObjectId(idEq) } });
55
56
  }
56
57
  const idIn = (_b = params.id) === null || _b === void 0 ? void 0 : _b.$in;
57
58
  if (Array.isArray(idIn)) {
58
- andConditions.push({ _id: { $in: idIn } });
59
+ // andConditions.push({ _id: { $in: idIn } });
60
+ andConditions.push({ _id: { $in: idIn.map((id) => new mongoose_1.Types.ObjectId(id)) } });
59
61
  }
60
62
  // tslint:disable-next-line:no-single-line-block-comment
61
63
  /* istanbul ignore else */
@@ -149,6 +151,33 @@ class MongoRepository {
149
151
  }
150
152
  return andConditions;
151
153
  }
154
+ static CREATE_AGGREGATE_PROJECTION(inclusion, exclusion) {
155
+ const projectStage = {
156
+ _id: 0,
157
+ // id: { $toString: '$_id' },
158
+ project: '$project',
159
+ typeOf: '$typeOf',
160
+ additionalProperty: '$additionalProperty',
161
+ color: '$color',
162
+ image: '$image',
163
+ codeValue: '$codeValue',
164
+ // inCodeSet: '$inCodeSet',
165
+ name: '$name',
166
+ paymentMethod: '$paymentMethod'
167
+ };
168
+ if (inclusion.length > 0) {
169
+ // no op
170
+ }
171
+ else if (exclusion.length > 0) {
172
+ exclusion.forEach((field) => {
173
+ if (typeof projectStage[field] === 'string' || typeof projectStage[field] === 'object') {
174
+ // tslint:disable-next-line:no-dynamic-delete
175
+ delete projectStage[field];
176
+ }
177
+ });
178
+ }
179
+ return projectStage;
180
+ }
152
181
  count(params) {
153
182
  return __awaiter(this, void 0, void 0, function* () {
154
183
  const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
@@ -157,6 +186,33 @@ class MongoRepository {
157
186
  .exec();
158
187
  });
159
188
  }
189
+ /**
190
+ * 集計検索(publicな属性検索が目的)
191
+ */
192
+ searchByAggregate(conditions, inclusion, exclusion) {
193
+ var _a;
194
+ return __awaiter(this, void 0, void 0, function* () {
195
+ const matchStages = MongoRepository.CREATE_MONGO_CONDITIONS(conditions)
196
+ .map((c) => ({ $match: c }));
197
+ const projectStage = MongoRepository.CREATE_AGGREGATE_PROJECTION(inclusion, exclusion);
198
+ const sortByCodeValue = (_a = conditions.sort) === null || _a === void 0 ? void 0 : _a.codeValue;
199
+ const aggregate = this.categoryCodeModel.aggregate([
200
+ ...matchStages,
201
+ ...(typeof sortByCodeValue === 'number')
202
+ ? [{ $sort: { codeValue: sortByCodeValue } }]
203
+ : [],
204
+ { $project: projectStage }
205
+ ]);
206
+ // tslint:disable-next-line:no-single-line-block-comment
207
+ /* istanbul ignore else */
208
+ if (typeof conditions.limit === 'number' && conditions.limit > 0) {
209
+ const page = (typeof conditions.page === 'number' && conditions.page > 0) ? conditions.page : 1;
210
+ aggregate.limit(conditions.limit * page)
211
+ .skip(conditions.limit * (page - 1));
212
+ }
213
+ return aggregate.exec();
214
+ });
215
+ }
160
216
  /**
161
217
  * 検索
162
218
  */
@@ -42,12 +42,14 @@ class MongoRepository {
42
42
  }
43
43
  const idEq = (_c = params.id) === null || _c === void 0 ? void 0 : _c.$eq;
44
44
  if (typeof idEq === 'string') {
45
- andConditions.push({ _id: { $eq: idEq } });
45
+ // andConditions.push({ _id: { $eq: idEq } });
46
+ andConditions.push({ _id: { $eq: new mongoose_1.Types.ObjectId(idEq) } });
46
47
  }
47
48
  // メンバー条件追加(2023-07-24~)
48
49
  const memberMemberOfIdIn = (_f = (_e = (_d = params.member) === null || _d === void 0 ? void 0 : _d.memberOf) === null || _e === void 0 ? void 0 : _e.id) === null || _f === void 0 ? void 0 : _f.$in;
49
50
  if (Array.isArray(memberMemberOfIdIn)) {
50
- andConditions.push({ _id: { $in: memberMemberOfIdIn } });
51
+ // andConditions.push({ _id: { $in: memberMemberOfIdIn } });
52
+ andConditions.push({ _id: { $in: memberMemberOfIdIn.map((memberMemberOfId) => new mongoose_1.Types.ObjectId(memberMemberOfId)) } });
51
53
  }
52
54
  const nameRegex = params.name;
53
55
  if (typeof nameRegex === 'string' && nameRegex.length > 0) {
package/package.json CHANGED
@@ -117,5 +117,5 @@
117
117
  "postversion": "git push origin --tags",
118
118
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
119
119
  },
120
- "version": "21.15.0-alpha.4"
120
+ "version": "21.15.0-alpha.6"
121
121
  }