@reldens/storage 0.81.0 → 0.82.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.
@@ -21,7 +21,7 @@ class PrismaSchemaGenerator
21
21
  this.maxWaitTime = sc.get(props, 'maxWaitTime', 30000);
22
22
  this.prismaSchemaPath = sc.get(props, 'prismaSchemaPath', FileHandler.joinPaths(process.cwd(), 'prisma'));
23
23
  this.schemaFilePath = FileHandler.joinPaths(this.prismaSchemaPath, 'schema.prisma');
24
- this.clientOutputPath = sc.get(props, 'clientOutputPath', '');
24
+ this.clientOutputPath = sc.get(props, 'clientOutputPath', './client');
25
25
  this.generateBinaryTargets = sc.get(props, 'generateBinaryTargets', ['native', 'debian-openssl-1.1.x']);
26
26
  this.dbParams = sc.get(props, 'dbParams', process.env.RELDENS_DB_PARAMS || '');
27
27
  }
@@ -30,11 +30,12 @@ class PrismaSchemaGenerator
30
30
  {
31
31
  FileHandler.createFolder(this.prismaSchemaPath);
32
32
  this.generateSchemaFile();
33
+ this.setDatabaseEnvironmentVariables();
33
34
  Logger.info('Running prisma introspect "npx prisma db pull"...');
34
35
  try {
35
- execSync('npx prisma db pull', { stdio: 'inherit' });
36
+ execSync('npx prisma db pull', {stdio: 'inherit'});
36
37
  } catch(error) {
37
- Logger.critical('Failed to pull database schema: ' + error.message);
38
+ Logger.critical('Failed to pull database schema: '+error.message);
38
39
  return false;
39
40
  }
40
41
  let generateCommand = 'npx prisma generate';
@@ -43,33 +44,42 @@ class PrismaSchemaGenerator
43
44
  }
44
45
  Logger.info('Running prisma generate "'+generateCommand+'"...');
45
46
  try {
46
- execSync(generateCommand, { stdio: 'inherit' });
47
+ execSync(generateCommand, {stdio: 'inherit'});
47
48
  } catch(error) {
48
49
  let errorString = error.toString();
49
50
  if(errorString.includes('EPERM') && errorString.includes('query_engine-windows.dll.node')){
50
51
  Logger.warning('Windows permission error detected with query engine file.');
51
52
  Logger.warning('This is a known Prisma issue on Windows.');
52
- Logger.warning('Please manually run: ' + generateCommand);
53
+ Logger.warning('Please manually run: '+generateCommand);
53
54
  Logger.warning('You may need to close any processes using the Prisma client first.');
54
55
  return false;
55
56
  }
56
- Logger.critical('Generate command failed: ' + error.message);
57
+ Logger.critical('Generate command failed: '+error.message);
57
58
  return false;
58
59
  }
59
60
  await this.waitForSchemaGeneration();
60
61
  return true;
61
62
  }
62
63
 
64
+ setDatabaseEnvironmentVariables()
65
+ {
66
+ let datasourceProvider = this.getDatasourceProvider();
67
+ process.env.RELDENS_DB_URL = this.buildConnectionString(datasourceProvider);
68
+ if(this.dataProxy){
69
+ process.env.RELDENS_DB_DIRECT_URL = this.buildDirectConnectionString(datasourceProvider);
70
+ }
71
+ }
72
+
63
73
  async waitForSchemaGeneration()
64
74
  {
65
- let clientPath = this.clientOutputPath || 'node_modules/.prisma/client';
75
+ let clientPath = this.clientOutputPath || './client';
66
76
  let generatedClientPath = this.resolveClientPath(clientPath);
67
77
  let clientIndexPath = FileHandler.joinPaths(generatedClientPath, 'index.js');
68
78
  let startTime = Date.now();
69
79
  return new Promise((resolve) => {
70
80
  let interval = setInterval(() => {
71
81
  let awaitTime = Date.now() - startTime;
72
- Logger.info('Awaiting on schema generation: ' + clientIndexPath, (awaitTime/1000).toFixed(0));
82
+ Logger.info('Awaiting on schema generation: '+clientIndexPath, (awaitTime/1000).toFixed(0));
73
83
  if(FileHandler.exists(clientIndexPath)){
74
84
  clearInterval(interval);
75
85
  resolve();
@@ -98,11 +108,7 @@ class PrismaSchemaGenerator
98
108
  generateSchemaFile()
99
109
  {
100
110
  let datasourceProvider = this.getDatasourceProvider();
101
- let schemaContent = this.buildSchemaContent(
102
- datasourceProvider,
103
- this.buildConnectionString(datasourceProvider),
104
- this.buildDirectConnectionString(datasourceProvider)
105
- );
111
+ let schemaContent = this.buildSchemaContent(datasourceProvider);
106
112
  FileHandler.writeFile(this.schemaFilePath, schemaContent);
107
113
  Logger.info('Generated Prisma schema file at: '+this.schemaFilePath);
108
114
  }
@@ -123,7 +129,7 @@ class PrismaSchemaGenerator
123
129
  if(this.dataProxy){
124
130
  datasourceProvider = 'prisma';
125
131
  }
126
- return datasourceProvider + this.buildConnectionDataString();
132
+ return datasourceProvider+this.buildConnectionDataString();
127
133
  }
128
134
 
129
135
  buildDirectConnectionString(datasourceProvider)
@@ -131,47 +137,48 @@ class PrismaSchemaGenerator
131
137
  if(!this.dataProxy){
132
138
  return '';
133
139
  }
134
- return datasourceProvider + this.buildConnectionDataString();
140
+ return datasourceProvider+this.buildConnectionDataString();
135
141
  }
136
142
 
137
143
  buildConnectionDataString()
138
144
  {
139
- return '://' + this.config.user + ':' + this.config.password
140
- + '@' + this.config.host+':' + this.config.port
141
- + '/' + this.config.database
142
- + (this.dbParams ? '?' + this.dbParams : '');
145
+ return '://'+this.config.user+':'+this.config.password
146
+ +'@'+this.config.host+':'+this.config.port
147
+ +'/'+this.config.database
148
+ +(this.dbParams ? '?'+this.dbParams : '');
143
149
  }
144
150
 
145
- buildSchemaContent(datasourceProvider, connectionString, directConnectionString)
151
+ buildSchemaContent(datasourceProvider)
146
152
  {
147
- return this.buildGeneratorBlock() + '\n\n'
148
- + this.buildDatasourceBlock(datasourceProvider, connectionString, directConnectionString) + '\n';
153
+ return this.buildGeneratorBlock()+'\n\n'
154
+ +this.buildDatasourceBlock(datasourceProvider)+'\n';
149
155
  }
150
156
 
151
157
  buildGeneratorBlock()
152
158
  {
153
159
  let generatorContent = 'generator client {\n';
154
160
  generatorContent += ' provider = "prisma-client-js"\n';
155
- if(this.clientOutputPath){
156
- let normalizedPath = this.clientOutputPath.replace(/\\/g, '/');
157
- generatorContent += ' output = "' + normalizedPath + '"\n';
161
+ let outputPath = this.clientOutputPath || './client';
162
+ if(!outputPath.startsWith('./')){
163
+ outputPath = './client';
158
164
  }
165
+ generatorContent += ' output = "'+outputPath+'"\n';
159
166
  if(0 < this.generateBinaryTargets.length){
160
- generatorContent += ' binaryTargets = [' +
161
- this.generateBinaryTargets.map(target => '"' + target + '"').join(', ')
162
- + ']\n';
167
+ generatorContent += ' binaryTargets = ['
168
+ +this.generateBinaryTargets.map(target => '"'+target+'"').join(', ')
169
+ +']\n';
163
170
  }
164
171
  generatorContent += '}';
165
172
  return generatorContent;
166
173
  }
167
174
 
168
- buildDatasourceBlock(datasourceProvider, connectionString, directConnectionString)
175
+ buildDatasourceBlock(datasourceProvider)
169
176
  {
170
177
  let datasourceContent = 'datasource db {\n';
171
- datasourceContent += ' provider = "' + datasourceProvider + '"\n';
172
- datasourceContent += ' url = "' + connectionString + '"\n';
173
- if('' !== directConnectionString){
174
- datasourceContent += ' directUrl = "' + directConnectionString + '"\n';
178
+ datasourceContent += ' provider = "'+datasourceProvider+'"\n';
179
+ datasourceContent += ' url = env("RELDENS_DB_URL")\n';
180
+ if(this.dataProxy){
181
+ datasourceContent += ' directUrl = env("RELDENS_DB_DIRECT_URL")\n';
175
182
  }
176
183
  datasourceContent += '}';
177
184
  return datasourceContent;
@@ -72,6 +72,11 @@ class PrismaTypeCaster
72
72
  return null === value || undefined === value || '' === value;
73
73
  }
74
74
 
75
+ isValidCastedValue(value)
76
+ {
77
+ return undefined !== value;
78
+ }
79
+
75
80
  castValue(value, fieldType, fieldName = null)
76
81
  {
77
82
  if('id' === fieldName){
@@ -226,6 +231,30 @@ class PrismaTypeCaster
226
231
  return String(value);
227
232
  }
228
233
 
234
+ normalizeReturnData(data)
235
+ {
236
+ if(null === data || undefined === data){
237
+ return data;
238
+ }
239
+ if('bigint' === typeof data){
240
+ return Number(data);
241
+ }
242
+ if(data instanceof Date){
243
+ return data;
244
+ }
245
+ if(sc.isArray(data)){
246
+ return data.map(item => this.normalizeReturnData(item));
247
+ }
248
+ if(sc.isObject(data)){
249
+ let result = {};
250
+ for(let key of Object.keys(data)){
251
+ result[key] = this.normalizeReturnData(data[key]);
252
+ }
253
+ return result;
254
+ }
255
+ return data;
256
+ }
257
+
229
258
  }
230
259
 
231
260
  module.exports.PrismaTypeCaster = PrismaTypeCaster;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/storage",
3
3
  "scope": "@reldens",
4
- "version": "0.81.0",
4
+ "version": "0.82.0",
5
5
  "description": "Reldens - Storage",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",