@chevre/domain 22.3.0-alpha.4 → 22.3.0-alpha.5

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.
@@ -12,29 +12,19 @@ mongoose.Model.on('index', (...args) => {
12
12
  async function main() {
13
13
  await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
14
14
 
15
- const actionRepo = await chevre.repository.Action.createInstance(mongoose.connection);
15
+ const customerRepo = await chevre.repository.Customer.createInstance(mongoose.connection);
16
16
 
17
- const docs = await actionRepo.search(
17
+ const docs = await customerRepo.projectFields(
18
18
  {
19
19
  limit: 1,
20
- page: 1,
21
- typeOf: { $eq: chevre.factory.actionType.AuthorizeAction }
20
+ page: 1
22
21
  },
23
- ['id', 'typeOf', 'actionStatus'],
24
- ['result', 'cancelAction', 'instrument', 'object']
22
+ ['contactPoint', 'additionalProperty'],
23
+ ['contactPoint', 'additionalProperty']
25
24
  );
26
25
  // tslint:disable-next-line:no-null-keyword
27
26
  console.dir(docs, { depth: null });
28
27
  console.log(docs.length, 'docs found');
29
-
30
- const actions = await actionRepo.searchByOrderNumber(
31
- {
32
- orderNumber: 'CIN9-4893721-9248784'
33
- }
34
- );
35
- // tslint:disable-next-line:no-null-keyword
36
- console.dir(actions, { depth: null });
37
- console.log(actions.length, 'actions found');
38
28
  }
39
29
 
40
30
  main()
@@ -24,7 +24,10 @@
24
24
  /// <reference types="mongoose/types/inferschematype" />
25
25
  import type { Connection, FilterQuery } from 'mongoose';
26
26
  import * as factory from '../factory';
27
- type IKeyOfProjection = keyof factory.customer.ICustomer | '_id';
27
+ type IUnset = {
28
+ [key in keyof Pick<factory.customer.ICustomer, 'telephone' | 'url'>]?: 1;
29
+ };
30
+ type IKeyOfProjection = keyof factory.customer.ICustomer;
28
31
  /**
29
32
  * 顧客リポジトリ
30
33
  */
@@ -34,14 +37,17 @@ export declare class CustomerRepo {
34
37
  static CREATE_MONGO_CONDITIONS(params: factory.customer.ISearchConditions): FilterQuery<factory.customer.ICustomer>[];
35
38
  save(params: {
36
39
  id?: string;
37
- attributes: factory.customer.ICustomer;
38
- }): Promise<factory.customer.ICustomer>;
40
+ attributes: Omit<factory.customer.ICustomer, 'id'> & {
41
+ id?: never;
42
+ $unset?: IUnset;
43
+ };
44
+ }): Promise<{
45
+ id: string;
46
+ }>;
39
47
  /**
40
48
  * 顧客検索
41
49
  */
42
- search(conditions: factory.customer.ISearchConditions, projection?: {
43
- [key in IKeyOfProjection]?: 0;
44
- }): Promise<factory.customer.ICustomer[]>;
50
+ projectFields(conditions: factory.customer.ISearchConditions, inclusion: IKeyOfProjection[], exclusion: IKeyOfProjection[]): Promise<factory.customer.ICustomer[]>;
45
51
  deleteById(params: {
46
52
  id: string;
47
53
  }): Promise<void>;
@@ -24,6 +24,17 @@ exports.CustomerRepo = void 0;
24
24
  const customer_1 = require("./mongoose/schemas/customer");
25
25
  const factory = require("../factory");
26
26
  const settings_1 = require("../settings");
27
+ const AVAILABLE_PROJECT_FIELDS = [
28
+ 'additionalProperty',
29
+ 'contactPoint',
30
+ // 'email',
31
+ 'branchCode',
32
+ 'name',
33
+ 'project',
34
+ 'typeOf',
35
+ 'url'
36
+ // 'telephone'f
37
+ ];
27
38
  /**
28
39
  * 顧客リポジトリ
29
40
  */
@@ -67,33 +78,49 @@ class CustomerRepo {
67
78
  return andConditions;
68
79
  }
69
80
  save(params) {
81
+ var _a, _b;
70
82
  return __awaiter(this, void 0, void 0, function* () {
71
- let customer;
83
+ let savedId;
72
84
  if (params.id === undefined) {
73
- const doc = yield this.customerModel.create(params.attributes);
74
- customer = doc.toObject();
85
+ const _c = params.attributes, { id, $unset } = _c, creatingDoc = __rest(_c, ["id", "$unset"]);
86
+ const result = yield this.customerModel.insertMany(creatingDoc, { rawResult: true });
87
+ const insertedId = (_b = (_a = result.insertedIds) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.toHexString();
88
+ if (typeof insertedId !== 'string') {
89
+ throw new factory.errors.Internal(`customer not saved unexpectedly. result:${JSON.stringify(result)}`);
90
+ }
91
+ savedId = insertedId;
75
92
  }
76
93
  else {
77
- // 上書き禁止属性を除外(2022-08-24~)
78
- const _a = params.attributes, { id, branchCode, project, typeOf } = _a, updateFields = __rest(_a, ["id", "branchCode", "project", "typeOf"]);
79
- const doc = yield this.customerModel.findOneAndUpdate({ _id: params.id }, updateFields, { upsert: false, new: true })
94
+ const _d = params.attributes, { id, branchCode, project, typeOf, $unset } = _d, updateFields = __rest(_d, ["id", "branchCode", "project", "typeOf", "$unset"]);
95
+ const doc = yield this.customerModel.findOneAndUpdate({ _id: { $eq: params.id } }, Object.assign({ $set: updateFields }, ($unset !== undefined) ? { $unset } : undefined), { upsert: false, new: true, projection: { _id: 1, id: { $toString: '$_id' } } })
96
+ .lean()
80
97
  .exec();
81
98
  if (doc === null) {
82
99
  throw new factory.errors.NotFound(this.customerModel.modelName);
83
100
  }
84
- customer = doc.toObject();
101
+ savedId = params.id;
85
102
  }
86
- return customer;
103
+ return { id: savedId };
87
104
  });
88
105
  }
89
106
  /**
90
107
  * 顧客検索
91
108
  */
92
- search(conditions, projection) {
109
+ projectFields(conditions, inclusion, exclusion) {
93
110
  var _a;
94
111
  return __awaiter(this, void 0, void 0, function* () {
95
112
  const andConditions = CustomerRepo.CREATE_MONGO_CONDITIONS(conditions);
96
- const query = this.customerModel.find((andConditions.length > 0) ? { $and: andConditions } : {}, Object.assign({ __v: 0, createdAt: 0, updatedAt: 0 }, projection));
113
+ let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS;
114
+ if (Array.isArray(inclusion) && inclusion.length > 0) {
115
+ positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key));
116
+ }
117
+ else {
118
+ if (Array.isArray(exclusion) && exclusion.length > 0) {
119
+ positiveProjectionFields = positiveProjectionFields.filter((key) => !exclusion.includes(key));
120
+ }
121
+ }
122
+ const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1]))));
123
+ const query = this.customerModel.find((andConditions.length > 0) ? { $and: andConditions } : {}, projection);
97
124
  if (typeof conditions.limit === 'number' && conditions.limit > 0) {
98
125
  const page = (typeof conditions.page === 'number' && conditions.page > 0) ? conditions.page : 1;
99
126
  query.limit(conditions.limit)
@@ -105,13 +132,13 @@ class CustomerRepo {
105
132
  query.sort({ branchCode: conditions.sort.branchCode });
106
133
  }
107
134
  return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
108
- .exec()
109
- .then((docs) => docs.map((doc) => doc.toObject()));
135
+ .lean() // 2024-09-03~
136
+ .exec();
110
137
  });
111
138
  }
112
139
  deleteById(params) {
113
140
  return __awaiter(this, void 0, void 0, function* () {
114
- yield this.customerModel.findOneAndRemove({ _id: params.id })
141
+ yield this.customerModel.findOneAndRemove({ _id: { $eq: params.id } }, { projection: { _id: 1 } })
115
142
  .exec();
116
143
  });
117
144
  }
@@ -5,6 +5,6 @@ type IModel = Model<IDocType>;
5
5
  type ISchemaDefinition = SchemaDefinition<IDocType>;
6
6
  type ISchema = Schema<IDocType, IModel, {}, {}, {}, {}, ISchemaDefinition, IDocType>;
7
7
  declare const modelName = "MerchantReturnPolicy";
8
- declare function createSchema(): ISchema;
9
8
  declare const indexes: [d: IndexDefinition, o: IndexOptions][];
9
+ declare function createSchema(): ISchema;
10
10
  export { createSchema, IModel, indexes, modelName };
@@ -27,10 +27,8 @@ const schemaOptions = {
27
27
  writeConcern: writeConcern_1.writeConcern,
28
28
  strict: true,
29
29
  strictQuery: false,
30
- timestamps: {
31
- createdAt: 'createdAt',
32
- updatedAt: 'updatedAt'
33
- },
30
+ timestamps: false,
31
+ versionKey: false,
34
32
  toJSON: {
35
33
  getters: false,
36
34
  virtuals: false,
@@ -44,6 +42,8 @@ const schemaOptions = {
44
42
  versionKey: false
45
43
  }
46
44
  };
45
+ const indexes = [];
46
+ exports.indexes = indexes;
47
47
  /**
48
48
  * 返品ポリシースキーマ
49
49
  */
@@ -51,18 +51,12 @@ let schema;
51
51
  function createSchema() {
52
52
  if (schema === undefined) {
53
53
  schema = new mongoose_1.Schema(schemaDefinition, schemaOptions);
54
+ if (settings_1.MONGO_AUTO_INDEX) {
55
+ indexes.forEach((indexParams) => {
56
+ schema === null || schema === void 0 ? void 0 : schema.index(...indexParams);
57
+ });
58
+ }
54
59
  }
55
60
  return schema;
56
61
  }
57
62
  exports.createSchema = createSchema;
58
- const indexes = [
59
- [
60
- { createdAt: 1 },
61
- { name: 'searchByCreatedAt' }
62
- ],
63
- [
64
- { updatedAt: 1 },
65
- { name: 'searchByUpdatedAt' }
66
- ]
67
- ];
68
- exports.indexes = indexes;
package/package.json CHANGED
@@ -110,5 +110,5 @@
110
110
  "postversion": "git push origin --tags",
111
111
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
112
112
  },
113
- "version": "22.3.0-alpha.4"
113
+ "version": "22.3.0-alpha.5"
114
114
  }