@reldens/storage 0.105.0 → 0.106.0

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.
@@ -215,6 +215,7 @@ class EntitiesGeneration extends BaseGenerator
215
215
  let type = column.type.toLowerCase();
216
216
  this.addTypeAttribute(props, column, type, columnName, referenceCounts);
217
217
  this.addRequiredAttribute(props, column);
218
+ this.addUniqueAttribute(props, column);
218
219
  props.push('dbType: \''+type+'\'');
219
220
  return props;
220
221
  }
@@ -295,6 +296,13 @@ class EntitiesGeneration extends BaseGenerator
295
296
  }
296
297
  }
297
298
 
299
+ addUniqueAttribute(props, column)
300
+ {
301
+ if('UNI' === column.key){
302
+ props.push('isUnique: true');
303
+ }
304
+ }
305
+
298
306
  getReferenceTable(column)
299
307
  {
300
308
  if(column.referencedTable){
@@ -39,6 +39,7 @@ class PrismaMetadataLoader
39
39
  let metadata = {
40
40
  requiredFields: [],
41
41
  optionalFields: [],
42
+ uniqueFields: [],
42
43
  fieldTypes: {},
43
44
  fieldDefaults: {},
44
45
  idFieldType: 'String',
@@ -81,6 +82,9 @@ class PrismaMetadataLoader
81
82
  if(field.isOptional || field.hasDefaultValue){
82
83
  metadata.optionalFields.push(field.name);
83
84
  }
85
+ if(field.isUnique){
86
+ metadata.uniqueFields.push(field.name);
87
+ }
84
88
  if(field.kind === 'scalar' && field.isList === false){
85
89
  let isNumericType = this.isNumericFieldType(field.type);
86
90
  let isReferenceField = field.relationFromFields && 0 < field.relationFromFields.length;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/storage",
3
3
  "scope": "@reldens",
4
- "version": "0.105.0",
4
+ "version": "0.106.0",
5
5
  "description": "Reldens - Storage",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",
@@ -51,16 +51,16 @@
51
51
  "test:driver": "node tests/run-tests.js --driver="
52
52
  },
53
53
  "dependencies": {
54
- "@mikro-orm/core": "7.1.5",
55
- "@mikro-orm/mongodb": "7.1.5",
56
- "@mikro-orm/mysql": "7.1.5",
54
+ "@mikro-orm/core": "7.1.6",
55
+ "@mikro-orm/mongodb": "7.1.6",
56
+ "@mikro-orm/mysql": "7.1.6",
57
57
  "@prisma/adapter-mariadb": "7.8.0",
58
58
  "@prisma/client": "7.8.0",
59
59
  "@reldens/server-utils": "^0.53.0",
60
60
  "@reldens/utils": "^0.57.0",
61
61
  "knex": "3.3.0",
62
62
  "mysql": "2.18.1",
63
- "mysql2": "3.22.6",
63
+ "mysql2": "3.23.0",
64
64
  "objection": "3.1.5",
65
65
  "prisma": "7.8.0"
66
66
  }
@@ -26,6 +26,7 @@ class TestCategoriesEntity extends EntityProperties
26
26
  },
27
27
  slug: {
28
28
  isRequired: true,
29
+ isUnique: true,
29
30
  dbType: 'varchar'
30
31
  },
31
32
  description: {
@@ -33,6 +33,7 @@ class TestProductsEntity extends EntityProperties
33
33
  },
34
34
  sku: {
35
35
  isRequired: true,
36
+ isUnique: true,
36
37
  dbType: 'varchar'
37
38
  },
38
39
  description: {
@@ -15,6 +15,7 @@ const RawQueriesTest = require('./integration/test-raw-queries');
15
15
  const EntityManagerTest = require('./unit/test-entity-manager');
16
16
  const TypeMapperTest = require('./unit/test-type-mapper');
17
17
  const DriversUnitTest = require('./unit/test-drivers');
18
+ const EntitiesGenerationTest = require('./unit/test-entities-generation');
18
19
 
19
20
  if(!process.env.RELDENS_TEST_DB_HOST){
20
21
  let envPath = FileHandler.joinPaths(__dirname, '.env.test');
@@ -187,6 +188,11 @@ class RunTests
187
188
  this.allCounts.total += driversUnitResult.total;
188
189
  this.allCounts.passed += driversUnitResult.passed;
189
190
  this.allCounts.failed += driversUnitResult.failed;
191
+ let entitiesGenerationTest = new EntitiesGenerationTest();
192
+ let entitiesGenerationResult = await entitiesGenerationTest.run();
193
+ this.allCounts.total += entitiesGenerationResult.total;
194
+ this.allCounts.passed += entitiesGenerationResult.passed;
195
+ this.allCounts.failed += entitiesGenerationResult.failed;
190
196
  }
191
197
 
192
198
  async runPreFlightChecks(config)
@@ -0,0 +1,152 @@
1
+ /**
2
+ *
3
+ * Reldens - EntitiesGeneration Test
4
+ *
5
+ */
6
+
7
+ const { TestRunner, assert } = require('../utils/test-runner');
8
+ const { EntitiesGeneration } = require('../../lib/generators/entities-generation');
9
+
10
+ class EntitiesGenerationTest
11
+ {
12
+
13
+ constructor()
14
+ {
15
+ this.runner = new TestRunner();
16
+ }
17
+
18
+ createColumn(props)
19
+ {
20
+ return Object.assign(
21
+ {
22
+ name: 'test_column',
23
+ type: 'varchar',
24
+ columnType: 'varchar(100)',
25
+ length: '100',
26
+ nullable: true,
27
+ key: '',
28
+ extra: '',
29
+ default: null
30
+ },
31
+ props
32
+ );
33
+ }
34
+
35
+ async run()
36
+ {
37
+ this.runner.suite('EntitiesGeneration');
38
+ await this.testAddUniqueAttribute();
39
+ await this.testGetPropertyAttributesUnique();
40
+ await this.testGeneratePropertiesConfigUnique();
41
+ return this.runner.getResults();
42
+ }
43
+
44
+ async testAddUniqueAttribute()
45
+ {
46
+ this.runner.group('addUniqueAttribute');
47
+ let generation = new EntitiesGeneration({});
48
+ await this.runner.test('should push isUnique for a UNI column', async () => {
49
+ let props = [];
50
+ generation.addUniqueAttribute(props, this.createColumn({key: 'UNI'}));
51
+ assert.deepStrictEqual(props, ['isUnique: true']);
52
+ });
53
+ await this.runner.test('should not push isUnique for a non indexed column', async () => {
54
+ let props = [];
55
+ generation.addUniqueAttribute(props, this.createColumn({key: ''}));
56
+ assert.deepStrictEqual(props, []);
57
+ });
58
+ await this.runner.test('should not push isUnique for a MUL column', async () => {
59
+ let props = [];
60
+ generation.addUniqueAttribute(props, this.createColumn({key: 'MUL'}));
61
+ assert.deepStrictEqual(props, []);
62
+ });
63
+ await this.runner.test('should not push isUnique for a PRI column', async () => {
64
+ let props = [];
65
+ generation.addUniqueAttribute(props, this.createColumn({key: 'PRI'}));
66
+ assert.deepStrictEqual(props, []);
67
+ });
68
+ }
69
+
70
+ async testGetPropertyAttributesUnique()
71
+ {
72
+ this.runner.group('getPropertyAttributes - isUnique');
73
+ let generation = new EntitiesGeneration({});
74
+ await this.runner.test('should include isUnique for a UNI column', async () => {
75
+ let column = this.createColumn({name: 'slug', key: 'UNI'});
76
+ let props = generation.getPropertyAttributes(column, 'slug', {});
77
+ assert.ok(props.includes('isUnique: true'));
78
+ });
79
+ await this.runner.test('should not include isUnique for a non unique column', async () => {
80
+ let column = this.createColumn({name: 'tags', key: ''});
81
+ let props = generation.getPropertyAttributes(column, 'tags', {});
82
+ assert.deepStrictEqual(props, ['dbType: \'varchar\'']);
83
+ });
84
+ await this.runner.test('should include both isRequired and isUnique for a required unique column', async () => {
85
+ let column = this.createColumn({name: 'sku', key: 'UNI', nullable: false});
86
+ let props = generation.getPropertyAttributes(column, 'sku', {});
87
+ assert.deepStrictEqual(props, ['isRequired: true', 'isUnique: true', 'dbType: \'varchar\'']);
88
+ });
89
+ await this.runner.test('should include isUnique only for a nullable unique column', async () => {
90
+ let column = this.createColumn({name: 'sku', key: 'UNI', nullable: true});
91
+ let props = generation.getPropertyAttributes(column, 'sku', {});
92
+ assert.deepStrictEqual(props, ['isUnique: true', 'dbType: \'varchar\'']);
93
+ });
94
+ await this.runner.test('should not include isUnique for a composite unique member column', async () => {
95
+ let column = this.createColumn({name: 'owner_id', type: 'int', key: 'MUL', nullable: false});
96
+ let props = generation.getPropertyAttributes(column, 'owner_id', {});
97
+ assert.deepStrictEqual(props, ['type: \'number\'', 'isRequired: true', 'dbType: \'int\'']);
98
+ });
99
+ await this.runner.test('should not include isUnique for the primary key column', async () => {
100
+ let column = this.createColumn({name: 'id', type: 'int', key: 'PRI', nullable: false, extra: 'auto_increment'});
101
+ let props = generation.getPropertyAttributes(column, 'id', {});
102
+ assert.deepStrictEqual(props, ['type: \'number\'', 'isRequired: true', 'dbType: \'int\'']);
103
+ });
104
+ await this.runner.test('should include isUnique for a unique reference column', async () => {
105
+ let column = this.createColumn({
106
+ name: 'profile_id',
107
+ type: 'int',
108
+ key: 'UNI',
109
+ nullable: false,
110
+ referencedTable: 'profiles',
111
+ referencedColumn: 'id'
112
+ });
113
+ let props = generation.getPropertyAttributes(column, 'profile_id', {profiles: 1});
114
+ assert.deepStrictEqual(props, [
115
+ 'type: \'reference\'',
116
+ 'reference: \'profiles\'',
117
+ 'alias: \'related_profiles\'',
118
+ 'isRequired: true',
119
+ 'isUnique: true',
120
+ 'dbType: \'int\''
121
+ ]);
122
+ });
123
+ }
124
+
125
+ async testGeneratePropertiesConfigUnique()
126
+ {
127
+ this.runner.group('generatePropertiesConfig - isUnique');
128
+ let generation = new EntitiesGeneration({});
129
+ await this.runner.test('should render isUnique in the generated properties config', async () => {
130
+ let columns = {
131
+ id: this.createColumn({name: 'id', type: 'int', key: 'PRI', nullable: false}),
132
+ slug: this.createColumn({name: 'slug', key: 'UNI', nullable: false}),
133
+ tags: this.createColumn({name: 'tags', key: ''})
134
+ };
135
+ let propertiesConfig = generation.generatePropertiesConfig(columns, false, 'id');
136
+ assert.ok(propertiesConfig.includes('slug: {'));
137
+ assert.ok(propertiesConfig.includes('isUnique: true'));
138
+ assert.strictEqual(propertiesConfig.split('isUnique: true').length - 1, 1);
139
+ });
140
+ await this.runner.test('should not render isUnique when no unique columns exist', async () => {
141
+ let columns = {
142
+ id: this.createColumn({name: 'id', type: 'int', key: 'PRI', nullable: false}),
143
+ tags: this.createColumn({name: 'tags', key: ''})
144
+ };
145
+ let propertiesConfig = generation.generatePropertiesConfig(columns, false, 'id');
146
+ assert.ok(!propertiesConfig.includes('isUnique'));
147
+ });
148
+ }
149
+
150
+ }
151
+
152
+ module.exports = EntitiesGenerationTest;