@e22m4u/js-repository 0.5.1 → 0.5.3

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.
@@ -403,6 +403,8 @@ function modelNameToModelKey(modelName) {
403
403
  "The model name should be a non-empty String without spaces, but %v was given.",
404
404
  modelName
405
405
  );
406
+ if (modelName.toLowerCase() !== "model")
407
+ modelName = modelName.replace(/[-_]?Model$/, "").replace(/[-_](MODEL|model)$/, "");
406
408
  return modelName.toLowerCase().replace(/[-_]/g, "");
407
409
  }
408
410
  var init_model_name_to_model_key = __esm({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-repository",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "Реализация репозитория для работы с базами данных в Node.js",
5
5
  "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
6
6
  "license": "MIT",
@@ -1,11 +1,16 @@
1
1
  /**
2
2
  * Data type.
3
3
  */
4
- export declare enum DataType {
5
- ANY = 'any',
6
- STRING = 'string',
7
- NUMBER = 'number',
8
- BOOLEAN = 'boolean',
9
- ARRAY = 'array',
10
- OBJECT = 'object',
11
- }
4
+ export declare const DataType: {
5
+ ANY: 'any';
6
+ STRING: 'string';
7
+ NUMBER: 'number';
8
+ BOOLEAN: 'boolean';
9
+ ARRAY: 'array';
10
+ OBJECT: 'object';
11
+ };
12
+
13
+ /**
14
+ * Type of DataType.
15
+ */
16
+ export type DataType = (typeof DataType)[keyof typeof DataType];
@@ -7,8 +7,8 @@ import {PropertyTransformOptions} from './property-transformer/index.js';
7
7
  * Full property definition.
8
8
  */
9
9
  export declare type FullPropertyDefinition = {
10
- type: `${DataType}`;
11
- itemType?: `${DataType}`;
10
+ type: DataType;
11
+ itemType?: DataType;
12
12
  itemModel?: string;
13
13
  model?: string;
14
14
  primaryKey?: boolean;
@@ -18,7 +18,7 @@ export declare type FullPropertyDefinition = {
18
18
  default?: unknown;
19
19
  validate?: PropertyValidateOptions;
20
20
  transform?: PropertyTransformOptions;
21
- unique?: boolean | `${PropertyUniqueness}`;
21
+ unique?: boolean | PropertyUniqueness;
22
22
  };
23
23
 
24
24
  /**
@@ -1,8 +1,14 @@
1
1
  /**
2
2
  * Property uniqueness.
3
3
  */
4
- export declare enum PropertyUniqueness {
5
- STRICT = 'strict',
6
- SPARSE = 'sparse',
7
- NON_UNIQUE = 'nonUnique',
8
- }
4
+ export declare const PropertyUniqueness: {
5
+ STRICT: 'strict';
6
+ SPARSE: 'sparse';
7
+ NON_UNIQUE: 'nonUnique';
8
+ };
9
+
10
+ /**
11
+ * Type of PropertyUniqueness.
12
+ */
13
+ export type PropertyUniqueness =
14
+ (typeof PropertyUniqueness)[keyof typeof PropertyUniqueness];
@@ -50,7 +50,7 @@ export declare type RelationDefinition =
50
50
  * ```
51
51
  */
52
52
  export declare type BelongsToDefinition = {
53
- type: `${RelationType.BELONGS_TO}`;
53
+ type: typeof RelationType.BELONGS_TO;
54
54
  polymorphic?: false;
55
55
  model: string;
56
56
  foreignKey?: string;
@@ -78,7 +78,7 @@ export declare type BelongsToDefinition = {
78
78
  * ```
79
79
  */
80
80
  export declare type PolyBelongsToDefinition = {
81
- type: `${RelationType.BELONGS_TO}`;
81
+ type: typeof RelationType.BELONGS_TO;
82
82
  polymorphic: true;
83
83
  foreignKey?: string;
84
84
  discriminator?: string;
@@ -97,7 +97,7 @@ export declare type PolyBelongsToDefinition = {
97
97
  * ```
98
98
  */
99
99
  export declare type HasOneDefinition = {
100
- type: `${RelationType.HAS_ONE}`;
100
+ type: typeof RelationType.HAS_ONE;
101
101
  model: string;
102
102
  foreignKey: string;
103
103
  polymorphic?: false;
@@ -117,7 +117,7 @@ export declare type HasOneDefinition = {
117
117
  * ```
118
118
  */
119
119
  export declare type PolyHasOneDefinitionWithTargetRelationName = {
120
- type: `${RelationType.HAS_ONE}`;
120
+ type: typeof RelationType.HAS_ONE;
121
121
  model: string;
122
122
  polymorphic: string;
123
123
  foreignKey?: undefined;
@@ -139,7 +139,7 @@ export declare type PolyHasOneDefinitionWithTargetRelationName = {
139
139
  * ```
140
140
  */
141
141
  export declare type PolyHasOneDefinitionWithTargetKeys = {
142
- type: `${RelationType.HAS_ONE}`;
142
+ type: typeof RelationType.HAS_ONE;
143
143
  model: string;
144
144
  polymorphic: true;
145
145
  foreignKey: string;
@@ -159,7 +159,7 @@ export declare type PolyHasOneDefinitionWithTargetKeys = {
159
159
  * ```
160
160
  */
161
161
  export declare type HasManyDefinition = {
162
- type: `${RelationType.HAS_MANY}`;
162
+ type: typeof RelationType.HAS_MANY;
163
163
  model: string;
164
164
  foreignKey: string;
165
165
  polymorphic?: false;
@@ -179,7 +179,7 @@ export declare type HasManyDefinition = {
179
179
  * ```
180
180
  */
181
181
  export declare type PolyHasManyDefinitionWithTargetRelationName = {
182
- type: `${RelationType.HAS_MANY}`;
182
+ type: typeof RelationType.HAS_MANY;
183
183
  model: string;
184
184
  polymorphic: string;
185
185
  foreignKey?: undefined;
@@ -201,7 +201,7 @@ export declare type PolyHasManyDefinitionWithTargetRelationName = {
201
201
  * ```
202
202
  */
203
203
  export declare type PolyHasManyDefinitionWithTargetKeys = {
204
- type: `${RelationType.HAS_MANY}`;
204
+ type: typeof RelationType.HAS_MANY;
205
205
  model: string;
206
206
  polymorphic: true;
207
207
  foreignKey: string;
@@ -229,7 +229,7 @@ export declare type PolyHasManyDefinitionWithTargetKeys = {
229
229
  * ```
230
230
  */
231
231
  export declare type ReferencesManyDefinition = {
232
- type: `${RelationType.REFERENCES_MANY}`;
232
+ type: typeof RelationType.REFERENCES_MANY;
233
233
  model: string;
234
234
  foreignKey?: string;
235
235
  discriminator?: undefined;
@@ -1,9 +1,14 @@
1
1
  /**
2
2
  * Relation type.
3
3
  */
4
- export declare enum RelationType {
5
- BELONGS_TO = 'belongsTo',
6
- HAS_ONE = 'hasOne',
7
- HAS_MANY = 'hasMany',
8
- REFERENCES_MANY = 'referencesMany',
9
- }
4
+ export declare const RelationType: {
5
+ BELONGS_TO: 'belongsTo';
6
+ HAS_ONE: 'hasOne';
7
+ HAS_MANY: 'hasMany';
8
+ REFERENCES_MANY: 'referencesMany';
9
+ };
10
+
11
+ /**
12
+ * Type of RelationType.
13
+ */
14
+ export type RelationType = (typeof RelationType)[keyof typeof RelationType];
@@ -13,5 +13,9 @@ export function modelNameToModelKey(modelName) {
13
13
  'without spaces, but %v was given.',
14
14
  modelName,
15
15
  );
16
+ if (modelName.toLowerCase() !== 'model')
17
+ modelName = modelName
18
+ .replace(/[-_]?Model$/, '')
19
+ .replace(/[-_](MODEL|model)$/, '');
16
20
  return modelName.toLowerCase().replace(/[-_]/g, '');
17
21
  }
@@ -12,6 +12,8 @@ describe('modelNameToModelKey', function () {
12
12
  'UserProfileDetails',
13
13
  'user-profile-details',
14
14
  'user_profile_details',
15
+ 'User-Profile-Details',
16
+ 'User_Profile_Details',
15
17
  'USER-PROFILE-DETAILS',
16
18
  'USER_PROFILE_DETAILS',
17
19
  'USERPROFILEDETAILS',
@@ -34,6 +36,29 @@ describe('modelNameToModelKey', function () {
34
36
  expect(modelNameToModelKey(modelName)).to.be.eq(expected);
35
37
  });
36
38
 
39
+ it('should remove the "model" word from a model name', function () {
40
+ const modelNames = [
41
+ 'userProfileDetailsModel',
42
+ 'UserProfileDetailsModel',
43
+ 'user-profile-details-model',
44
+ 'user_profile_details_model',
45
+ 'User-Profile-Details-Model',
46
+ 'User_Profile_Details_Model',
47
+ 'USER-PROFILE-DETAILS-MODEL',
48
+ 'USER_PROFILE_DETAILS_MODEL',
49
+ ];
50
+ modelNames.forEach(v =>
51
+ expect(modelNameToModelKey(v)).to.be.eq('userprofiledetails'),
52
+ );
53
+ });
54
+
55
+ it('should not remove the "model" suffix as a part of last word in a model name', function () {
56
+ const exceptions = ['SUPERMODEL', 'supermodel'];
57
+ exceptions.forEach(v =>
58
+ expect(modelNameToModelKey(v)).to.be.eq('supermodel'),
59
+ );
60
+ });
61
+
37
62
  it('should throw an error for an empty string', function () {
38
63
  const throwable = () => modelNameToModelKey('');
39
64
  expect(throwable).to.throw(