@reldens/storage 0.50.0 → 0.51.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.
@@ -50,7 +50,7 @@ class PrismaDataServer extends BaseDataServer
50
50
  this.entities = {};
51
51
  for(let i of Object.keys(this.rawEntities)){
52
52
  let rawEntity = this.rawEntities[i];
53
- let tableName = rawEntity.tableName.toString();
53
+ let tableName = rawEntity.tableName;
54
54
  let prismaModel = this.prisma[tableName];
55
55
  if(!prismaModel){
56
56
  Logger.critical('Invalid raw entity "'+i+'". No matching Prisma model found for "'+tableName+'".');
@@ -18,14 +18,17 @@ class PrismaSchemaGenerator
18
18
  this.debug = sc.get(props, 'debug', false);
19
19
  this.dataProxy = sc.get(props, 'dataProxy', false);
20
20
  this.checkInterval = sc.get(props, 'checkInterval', 1000);
21
+ this.maxWaitTime = sc.get(props, 'maxWaitTime', 30000);
21
22
  this.prismaSchemaPath = sc.get(props, 'prismaSchemaPath', FileHandler.joinPaths(process.cwd(), 'prisma'));
22
23
  this.schemaFilePath = FileHandler.joinPaths(this.prismaSchemaPath, 'schema.prisma');
24
+ this.clientOutputPath = sc.get(props, 'clientOutputPath', './prisma/client');
25
+ this.generateBinaryTargets = sc.get(props, 'generateBinaryTargets', ['native']);
23
26
  }
24
27
 
25
28
  async generate()
26
29
  {
27
30
  try {
28
- FileHandler.createFolder(this.prismaSchemaPath, { recursive: true });
31
+ FileHandler.createFolder(this.prismaSchemaPath);
29
32
  this.generateSchemaFile();
30
33
  Logger.info('Running prisma introspect "npx prisma db pull"...');
31
34
  execSync('npx prisma db pull', { stdio: 'inherit' });
@@ -45,13 +48,20 @@ class PrismaSchemaGenerator
45
48
 
46
49
  async waitForSchemaGeneration()
47
50
  {
48
- let generatedClientPath = FileHandler.joinPaths(this.prismaSchemaPath, '..', 'node_modules', '.prisma', 'client');
51
+ let generatedClientPath = FileHandler.joinPaths(this.prismaSchemaPath, this.clientOutputPath);
49
52
  let clientIndexPath = FileHandler.joinPaths(generatedClientPath, 'index.js');
53
+ let startTime = Date.now();
50
54
  return new Promise((resolve) => {
51
55
  let interval = setInterval(() => {
52
56
  if(FileHandler.exists(clientIndexPath)){
53
57
  clearInterval(interval);
54
58
  resolve();
59
+ return;
60
+ }
61
+ if(Date.now() - startTime > this.maxWaitTime){
62
+ clearInterval(interval);
63
+ Logger.warning('Schema generation wait timeout reached.');
64
+ resolve();
55
65
  }
56
66
  }, this.checkInterval);
57
67
  });
@@ -59,30 +69,80 @@ class PrismaSchemaGenerator
59
69
 
60
70
  generateSchemaFile()
61
71
  {
62
- let datasourceProvider = 'mysql';
72
+ let datasourceProvider = this.getDatasourceProvider();
73
+ let schemaContent = this.buildSchemaContent(
74
+ datasourceProvider,
75
+ this.buildConnectionString(datasourceProvider),
76
+ this.buildDirectConnectionString(datasourceProvider)
77
+ );
78
+ FileHandler.writeFile(this.schemaFilePath, schemaContent);
79
+ Logger.info('Generated Prisma schema file at: '+this.schemaFilePath);
80
+ }
81
+
82
+ getDatasourceProvider()
83
+ {
63
84
  if('postgresql' === this.client || 'postgres' === this.client){
64
- datasourceProvider = 'postgresql';
85
+ return 'postgresql';
65
86
  }
66
87
  if('mongodb' === this.client){
67
- datasourceProvider = 'mongodb';
88
+ return 'mongodb';
68
89
  }
69
- let connectionString = datasourceProvider + '://'
70
- + this.config.user + ':' + this.config.password
71
- + '@' + this.config.host + ':' + this.config.port + '/'
72
- + this.config.database;
73
- let schemaContent = `
74
- generator client {
75
- provider = "prisma-client-js"
76
- output = "../node_modules/.prisma/client"
77
- }
90
+ return 'mysql';
91
+ }
78
92
 
79
- datasource db {
80
- provider = "${datasourceProvider}"
81
- url = "${connectionString}"
82
- }
83
- `;
84
- FileHandler.writeFile(this.schemaFilePath, schemaContent);
85
- Logger.info('Generated Prisma schema file at: '+this.schemaFilePath);
93
+ buildConnectionString(datasourceProvider)
94
+ {
95
+ if(this.dataProxy){
96
+ datasourceProvider = 'prisma';
97
+ }
98
+ return datasourceProvider + this.buildConnectionDataString();
99
+ }
100
+
101
+ buildDirectConnectionString(datasourceProvider)
102
+ {
103
+ if(!this.dataProxy){
104
+ return '';
105
+ }
106
+ return datasourceProvider + this.buildConnectionDataString();
107
+ }
108
+
109
+ buildConnectionDataString()
110
+ {
111
+ return '://' + this.config.user + ':' + this.config.password
112
+ + '@' + this.config.host+':' + this.config.port
113
+ + '/' + this.config.database;
114
+ }
115
+
116
+ buildSchemaContent(datasourceProvider, connectionString, directConnectionString)
117
+ {
118
+ return this.buildGeneratorBlock() + '\n\n'
119
+ + this.buildDatasourceBlock(datasourceProvider, connectionString, directConnectionString) + '\n';
120
+ }
121
+
122
+ buildGeneratorBlock()
123
+ {
124
+ let generatorContent = 'generator client {\n';
125
+ generatorContent += ' provider = "prisma-client-js"\n';
126
+ generatorContent += ' output = "' + this.clientOutputPath + '"\n';
127
+ if(0 < this.generateBinaryTargets.length){
128
+ generatorContent += ' binaryTargets = [' +
129
+ this.generateBinaryTargets.map(target => '"' + target + '"').join(', ')
130
+ + ']\n';
131
+ }
132
+ generatorContent += '}';
133
+ return generatorContent;
134
+ }
135
+
136
+ buildDatasourceBlock(datasourceProvider, connectionString, directConnectionString)
137
+ {
138
+ let datasourceContent = 'datasource db {\n';
139
+ datasourceContent += ' provider = "' + datasourceProvider + '"\n';
140
+ datasourceContent += ' url = "' + connectionString + '"\n';
141
+ if('' !== directConnectionString){
142
+ datasourceContent += ' directUrl = "' + directConnectionString + '"\n';
143
+ }
144
+ datasourceContent += '}';
145
+ return datasourceContent;
86
146
  }
87
147
 
88
148
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/storage",
3
3
  "scope": "@reldens",
4
- "version": "0.50.0",
4
+ "version": "0.51.0",
5
5
  "description": "Reldens - Storage",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",