@e22m4u/js-repository-mongodb-adapter 0.6.0 → 0.6.2

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/README.md CHANGED
@@ -26,10 +26,10 @@ npm install @e22m4u/js-repository-mongodb-adapter
26
26
  ```js
27
27
  import {DatabaseSchema} from '@e22m4u/js-repository';
28
28
 
29
- const schema = new DatabaseSchema();
29
+ const dbs = new DatabaseSchema();
30
30
 
31
31
  // объявление источника
32
- schema.defineDatasource({
32
+ dbs.defineDatasource({
33
33
  name: 'myMongo', // название источника
34
34
  adapter: 'mongodb', // имя адаптера
35
35
  // параметры
@@ -39,7 +39,7 @@ schema.defineDatasource({
39
39
  });
40
40
 
41
41
  // объявление модели
42
- schema.defineModel({
42
+ dbs.defineModel({
43
43
  name: 'user', // название модели
44
44
  datasource: 'myMongo', // используемый источник (см. выше)
45
45
  properties: { // поля модели
@@ -49,7 +49,7 @@ schema.defineModel({
49
49
  });
50
50
 
51
51
  // получаем репозиторий по названию модели и создаем запись
52
- const userRep = schema.getRepository('user');
52
+ const userRep = dbs.getRepository('user');
53
53
  const user = await userRep.create({name: 'John', surname: 'Doe'});
54
54
 
55
55
  console.log(user);
@@ -1215,7 +1215,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
1215
1215
  _getCollectionNameByModelName(modelName) {
1216
1216
  const modelDef = this.getService(import_js_repository8.DefinitionRegistry).getModel(modelName);
1217
1217
  if (modelDef.tableName != null) return modelDef.tableName;
1218
- return modelNameToCollectionName(modelName);
1218
+ return modelNameToCollectionName(modelDef.name);
1219
1219
  }
1220
1220
  /**
1221
1221
  * Get collection.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-repository-mongodb-adapter",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "MongoDB адаптер для @e22m4u/js-repository",
5
5
  "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
6
6
  "license": "MIT",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@e22m4u/js-format": "~0.1.0",
45
- "@e22m4u/js-repository": "~0.3.0"
45
+ "@e22m4u/js-repository": "~0.4.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@commitlint/cli": "~19.8.1",
@@ -307,7 +307,7 @@ export class MongodbAdapter extends Adapter {
307
307
  _getCollectionNameByModelName(modelName) {
308
308
  const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
309
309
  if (modelDef.tableName != null) return modelDef.tableName;
310
- return modelNameToCollectionName(modelName);
310
+ return modelNameToCollectionName(modelDef.name);
311
311
  }
312
312
 
313
313
  /**
@@ -43,61 +43,85 @@ describe('MongodbAdapter', function () {
43
43
  });
44
44
 
45
45
  describe('_getCollectionNameByModelName', function () {
46
- it('converts model name to camel case an pluralize it', async function () {
46
+ it('converts model name to camel case and pluralizes it', async function () {
47
47
  const schema = createSchema();
48
- schema.defineModel({name: 'fooBar', datasource: 'mongodb'});
49
- schema.defineModel({name: 'FooBar', datasource: 'mongodb'});
50
- schema.defineModel({name: 'foo_bar', datasource: 'mongodb'});
51
- schema.defineModel({name: 'FOO_BAR', datasource: 'mongodb'});
48
+ const modelNamesToCollectionNames = [
49
+ ['camelCaseEntity', 'camelCaseEntities'],
50
+ ['PascalCaseEntity', 'pascalCaseEntities'],
51
+ ['snake_case_entity', 'snakeCaseEntities'],
52
+ ['kebab-case-entity', 'kebabCaseEntities'],
53
+ ['UPPER_SNAKE_CASE_ENTITY', 'upperSnakeCaseEntities'],
54
+ ['UPPER-KEBAB-CASE-ENTITY', 'upperKebabCaseEntities'],
55
+ ];
56
+ modelNamesToCollectionNames.forEach(tuple =>
57
+ schema.defineModel({name: tuple[0], datasource: 'mongodb'}),
58
+ );
52
59
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
53
- expect(A._getCollectionNameByModelName('fooBar')).to.be.eq('fooBars');
54
- expect(A._getCollectionNameByModelName('FooBar')).to.be.eq('fooBars');
55
- expect(A._getCollectionNameByModelName('foo_bar')).to.be.eq('fooBars');
56
- expect(A._getCollectionNameByModelName('FOO_BAR')).to.be.eq('fooBars');
60
+ modelNamesToCollectionNames.forEach(tuple =>
61
+ expect(A._getCollectionNameByModelName(tuple[0])).to.be.eq(tuple[1]),
62
+ );
57
63
  });
58
64
 
59
65
  // prettier-ignore
60
- it('should cut off the "Model" suffix from the model name', async function () {
61
- const schema = createSchema();
62
- schema.defineModel({name: 'fooBarModel', datasource: 'mongodb'});
63
- schema.defineModel({name: 'FooBarModel', datasource: 'mongodb'});
64
- schema.defineModel({name: 'foo_bar_model', datasource: 'mongodb'});
65
- schema.defineModel({name: 'FOO_BAR_MODEL', datasource: 'mongodb'});
66
+ it('cuts off the "Model" suffix from the model name', async function () {
67
+ const schema = createSchema();
68
+ const modelNamesToCollectionNames = [
69
+ ['camelCaseEntityModel', 'camelCaseEntities'],
70
+ ['PascalCaseEntityModel', 'pascalCaseEntities'],
71
+ ['snake_case_entity_model', 'snakeCaseEntities'],
72
+ ['kebab-case-entity-model', 'kebabCaseEntities'],
73
+ ['UPPER_SNAKE_CASE_ENTITY_MODEL', 'upperSnakeCaseEntities'],
74
+ ['UPPER-KEBAB-CASE-ENTITY-MODEL', 'upperKebabCaseEntities'],
75
+ ];
76
+ modelNamesToCollectionNames.forEach(tuple =>
77
+ schema.defineModel({name: tuple[0], datasource: 'mongodb'}),
78
+ );
66
79
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
67
- expect(A._getCollectionNameByModelName('fooBarModel')).to.be.eq('fooBars');
68
- expect(A._getCollectionNameByModelName('FooBarModel')).to.be.eq('fooBars');
69
- expect(A._getCollectionNameByModelName('foo_bar_model')).to.be.eq('fooBars');
70
- expect(A._getCollectionNameByModelName('FOO_BAR_MODEL')).to.be.eq('fooBars');
80
+ modelNamesToCollectionNames.forEach(tuple =>
81
+ expect(A._getCollectionNameByModelName(tuple[0])).to.be.eq(tuple[1]),
82
+ );
71
83
  });
72
84
 
73
85
  it('converts already pluralized model name to camel case', async function () {
74
86
  const schema = createSchema();
75
- schema.defineModel({name: 'fooBars', datasource: 'mongodb'});
76
- schema.defineModel({name: 'FooBars', datasource: 'mongodb'});
77
- schema.defineModel({name: 'foo_bars', datasource: 'mongodb'});
78
- schema.defineModel({name: 'FOO_BARS', datasource: 'mongodb'});
87
+ const modelNamesToCollectionNames = [
88
+ ['camelCaseEntities', 'camelCaseEntities'],
89
+ ['PascalCaseEntities', 'pascalCaseEntities'],
90
+ ['snake_case_entities', 'snakeCaseEntities'],
91
+ ['kebab-case-entities', 'kebabCaseEntities'],
92
+ ['UPPER_SNAKE_CASE_ENTITIES', 'upperSnakeCaseEntities'],
93
+ ['UPPER-KEBAB-CASE-ENTITIES', 'upperKebabCaseEntities'],
94
+ ];
95
+ modelNamesToCollectionNames.forEach(tuple =>
96
+ schema.defineModel({name: tuple[0], datasource: 'mongodb'}),
97
+ );
79
98
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
80
- expect(A._getCollectionNameByModelName('fooBars')).to.be.eq('fooBars');
81
- expect(A._getCollectionNameByModelName('FooBars')).to.be.eq('fooBars');
82
- expect(A._getCollectionNameByModelName('foo_bars')).to.be.eq('fooBars');
83
- expect(A._getCollectionNameByModelName('FOO_BARS')).to.be.eq('fooBars');
99
+ modelNamesToCollectionNames.forEach(tuple =>
100
+ expect(A._getCollectionNameByModelName(tuple[0])).to.be.eq(tuple[1]),
101
+ );
84
102
  });
85
103
 
86
104
  // prettier-ignore
87
105
  it('converts already pluralized model name to camel case and cut off the "Model" suffix', async function () {
88
106
  const schema = createSchema();
89
- schema.defineModel({name: 'fooBarsModel', datasource: 'mongodb'});
90
- schema.defineModel({name: 'FooBarsModel', datasource: 'mongodb'});
91
- schema.defineModel({name: 'foo_bars_model', datasource: 'mongodb'});
92
- schema.defineModel({name: 'FOO_BARS_MODEL', datasource: 'mongodb'});
107
+ const modelNamesToCollectionNames = [
108
+ ['camelCaseEntitiesModel', 'camelCaseEntities'],
109
+ ['PascalCaseEntitiesModel', 'pascalCaseEntities'],
110
+ ['snake_case_entities_model', 'snakeCaseEntities'],
111
+ ['kebab-case-entities-model', 'kebabCaseEntities'],
112
+ ['UPPER_SNAKE_CASE_ENTITIES_MODEL', 'upperSnakeCaseEntities'],
113
+ ['UPPER-KEBAB-CASE-ENTITIES-MODEL', 'upperKebabCaseEntities'],
114
+ ];
115
+ modelNamesToCollectionNames.forEach(tuple =>
116
+ schema.defineModel({name: tuple[0], datasource: 'mongodb'}),
117
+ );
93
118
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
94
- expect(A._getCollectionNameByModelName('fooBarsModel')).to.be.eq('fooBars');
95
- expect(A._getCollectionNameByModelName('FooBarsModel')).to.be.eq('fooBars');
96
- expect(A._getCollectionNameByModelName('foo_bars_model')).to.be.eq('fooBars');
97
- expect(A._getCollectionNameByModelName('FOO_BARS_MODEL')).to.be.eq('fooBars');
119
+ modelNamesToCollectionNames.forEach(tuple =>
120
+ expect(A._getCollectionNameByModelName(tuple[0])).to.be.eq(tuple[1]),
121
+ );
98
122
  });
99
123
 
100
- it('returns a value from the "tableName" option if defined', async function () {
124
+ it('returns the value from the "tableName" option if defined', async function () {
101
125
  const schema = createSchema();
102
126
  schema.defineModel({
103
127
  name: 'fooBar',
@@ -142,55 +166,73 @@ describe('MongodbAdapter', function () {
142
166
 
143
167
  it('converts model name to camel case and pluralizes it', async function () {
144
168
  const schema = createSchema();
169
+ const modelNamesToCollectionNames = [
170
+ ['camelCaseEntity', 'camelCaseEntities'],
171
+ ['PascalCaseEntity', 'pascalCaseEntities'],
172
+ ['snake_case_entity', 'snakeCaseEntities'],
173
+ ['kebab-case-entity', 'kebabCaseEntities'],
174
+ ['UPPER_SNAKE_CASE_ENTITY', 'upperSnakeCaseEntities'],
175
+ ['UPPER-KEBAB-CASE-ENTITY', 'upperKebabCaseEntities'],
176
+ ];
145
177
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
146
- const modelNames = ['fooBar', 'FooBar', 'foo_bar', 'FOO_BAR'];
147
- modelNames.forEach(modelName => {
148
- schema.defineModel({name: modelName, datasource: 'mongodb'});
149
- const collection = A._getCollection(modelName);
150
- expect(collection.collectionName).to.equal('fooBars');
178
+ modelNamesToCollectionNames.forEach(tuple => {
179
+ schema.defineModel({name: tuple[0], datasource: 'mongodb'});
180
+ const collection = A._getCollection(tuple[0]);
181
+ expect(collection.collectionName).to.equal(tuple[1]);
151
182
  });
152
183
  });
153
184
 
154
- it('cuts off the "Model" suffix from the model name before naming', async function () {
185
+ it('cuts off the "Model" suffix from the model name', async function () {
155
186
  const schema = createSchema();
156
- const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
157
- const modelNames = [
158
- 'fooBarModel',
159
- 'FooBarModel',
160
- 'foo_bar_model',
161
- 'FOO_BAR_MODEL',
187
+ const modelNamesToCollectionNames = [
188
+ ['camelCaseEntityModel', 'camelCaseEntities'],
189
+ ['PascalCaseEntityModel', 'pascalCaseEntities'],
190
+ ['snake_case_entity_model', 'snakeCaseEntities'],
191
+ ['kebab-case-entity-model', 'kebabCaseEntities'],
192
+ ['UPPER_SNAKE_CASE_ENTITY_MODEL', 'upperSnakeCaseEntities'],
193
+ ['UPPER-KEBAB-CASE-ENTITY-MODEL', 'upperKebabCaseEntities'],
162
194
  ];
163
- modelNames.forEach(modelName => {
164
- schema.defineModel({name: modelName, datasource: 'mongodb'});
165
- const collection = A._getCollection(modelName);
166
- expect(collection.collectionName).to.equal('fooBars');
195
+ const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
196
+ modelNamesToCollectionNames.forEach(tuple => {
197
+ schema.defineModel({name: tuple[0], datasource: 'mongodb'});
198
+ const collection = A._getCollection(tuple[0]);
199
+ expect(collection.collectionName).to.equal(tuple[1]);
167
200
  });
168
201
  });
169
202
 
170
203
  it('converts already pluralized model name to camel case', async function () {
171
204
  const schema = createSchema();
205
+ const modelNamesToCollectionNames = [
206
+ ['camelCaseEntities', 'camelCaseEntities'],
207
+ ['PascalCaseEntities', 'pascalCaseEntities'],
208
+ ['snake_case_entities', 'snakeCaseEntities'],
209
+ ['kebab-case-entities', 'kebabCaseEntities'],
210
+ ['UPPER_SNAKE_CASE_ENTITIES', 'upperSnakeCaseEntities'],
211
+ ['UPPER-KEBAB-CASE-ENTITIES', 'upperKebabCaseEntities'],
212
+ ];
172
213
  const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
173
- const modelNames = ['fooBars', 'FooBars', 'foo_bars', 'FOO_BARS'];
174
- modelNames.forEach(modelName => {
175
- schema.defineModel({name: modelName, datasource: 'mongodb'});
176
- const collection = A._getCollection(modelName);
177
- expect(collection.collectionName).to.equal('fooBars');
214
+ modelNamesToCollectionNames.forEach(tuple => {
215
+ schema.defineModel({name: tuple[0], datasource: 'mongodb'});
216
+ const collection = A._getCollection(tuple[0]);
217
+ expect(collection.collectionName).to.equal(tuple[1]);
178
218
  });
179
219
  });
180
220
 
181
- it('handles already pluralized names with the "Model" suffix', async function () {
221
+ it('converts already pluralized model name to camel case and cut off the "Model" suffix', async function () {
182
222
  const schema = createSchema();
183
- const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
184
- const modelNames = [
185
- 'fooBarsModel',
186
- 'FooBarsModel',
187
- 'foo_bars_model',
188
- 'FOO_BARS_MODEL',
223
+ const modelNamesToCollectionNames = [
224
+ ['camelCaseEntitiesModel', 'camelCaseEntities'],
225
+ ['PascalCaseEntitiesModel', 'pascalCaseEntities'],
226
+ ['snake_case_entities_model', 'snakeCaseEntities'],
227
+ ['kebab-case-entities-model', 'kebabCaseEntities'],
228
+ ['UPPER_SNAKE_CASE_ENTITIES_MODEL', 'upperSnakeCaseEntities'],
229
+ ['UPPER-KEBAB-CASE-ENTITIES-MODEL', 'upperKebabCaseEntities'],
189
230
  ];
190
- modelNames.forEach(modelName => {
191
- schema.defineModel({name: modelName, datasource: 'mongodb'});
192
- const collection = A._getCollection(modelName);
193
- expect(collection.collectionName).to.equal('fooBars');
231
+ const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
232
+ modelNamesToCollectionNames.forEach(tuple => {
233
+ schema.defineModel({name: tuple[0], datasource: 'mongodb'});
234
+ const collection = A._getCollection(tuple[0]);
235
+ expect(collection.collectionName).to.equal(tuple[1]);
194
236
  });
195
237
  });
196
238