@reldens/storage 0.34.0 → 0.36.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.
|
@@ -12,7 +12,7 @@ const { Logger } = require('@reldens/utils');
|
|
|
12
12
|
let args = process.argv.slice(2);
|
|
13
13
|
|
|
14
14
|
let connectionData = {
|
|
15
|
-
client: '
|
|
15
|
+
client: 'mysql',
|
|
16
16
|
config: {
|
|
17
17
|
user: '',
|
|
18
18
|
password: '',
|
|
@@ -70,10 +70,7 @@ if(!connectionData.config.user || !connectionData.config.database){
|
|
|
70
70
|
process.exit();
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
let generator = new PrismaSchemaGenerator({
|
|
74
|
-
...connectionData,
|
|
75
|
-
prismaSchemaPath: projectPath+'/prisma'
|
|
76
|
-
});
|
|
73
|
+
let generator = new PrismaSchemaGenerator({...connectionData, prismaSchemaPath: projectPath+'/prisma'});
|
|
77
74
|
|
|
78
75
|
generator.generate().then((success) => {
|
|
79
76
|
if(!success){
|
|
@@ -77,17 +77,103 @@ class PrismaDataServer extends BaseDataServer
|
|
|
77
77
|
async rawQuery(content)
|
|
78
78
|
{
|
|
79
79
|
try {
|
|
80
|
-
let
|
|
81
|
-
|
|
80
|
+
let statements = this.splitSqlStatements(content);
|
|
81
|
+
let results = [];
|
|
82
|
+
for(let statement of statements){
|
|
83
|
+
let cleanStatement = statement.trim();
|
|
84
|
+
if('' === cleanStatement){
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
let queryResult = await this.prisma.$executeRawUnsafe(cleanStatement);
|
|
89
|
+
results.push(queryResult);
|
|
90
|
+
} catch(stmtError) {
|
|
91
|
+
Logger.error('Statement "'+cleanStatement+'" execution failed: '+stmtError.message);
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if(0 === results.length){
|
|
82
96
|
return false;
|
|
83
97
|
}
|
|
84
|
-
return
|
|
98
|
+
return 1 === results.length ? results[0] : results;
|
|
85
99
|
} catch(error) {
|
|
86
100
|
Logger.error('Raw query failed: '+error.message);
|
|
87
101
|
return false;
|
|
88
102
|
}
|
|
89
103
|
}
|
|
90
104
|
|
|
105
|
+
splitSqlStatements(sqlContent)
|
|
106
|
+
{
|
|
107
|
+
let statements = [];
|
|
108
|
+
let currentStatement = '';
|
|
109
|
+
let inQuote = false;
|
|
110
|
+
let quoteChar = '';
|
|
111
|
+
let inComment = false;
|
|
112
|
+
let commentType = '';
|
|
113
|
+
for(let i = 0; i < sqlContent.length; i++){
|
|
114
|
+
let char = sqlContent.charAt(i);
|
|
115
|
+
let nextChar = i < sqlContent.length - 1 ? sqlContent.charAt(i + 1) : '';
|
|
116
|
+
if(inComment){
|
|
117
|
+
let shouldEndComment = false;
|
|
118
|
+
if('*' === commentType && '*' === char && '/' === nextChar){
|
|
119
|
+
shouldEndComment = true;
|
|
120
|
+
i++;
|
|
121
|
+
}
|
|
122
|
+
if('-' === commentType && '\n' === char){
|
|
123
|
+
shouldEndComment = true;
|
|
124
|
+
}
|
|
125
|
+
if(shouldEndComment){
|
|
126
|
+
inComment = false;
|
|
127
|
+
}
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
let shouldStartComment = this.shouldStartComment(char, nextChar, inQuote);
|
|
131
|
+
if(shouldStartComment){
|
|
132
|
+
inComment = true;
|
|
133
|
+
commentType = '/' === char ? '*' : '-';
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
let isQuoteChar = ('"' === char || '\'' === char || '`' === char);
|
|
137
|
+
let isEscaped = (i > 0 && '\\' === sqlContent.charAt(i-1));
|
|
138
|
+
if(isQuoteChar && !isEscaped){
|
|
139
|
+
if(!inQuote){
|
|
140
|
+
inQuote = true;
|
|
141
|
+
quoteChar = char;
|
|
142
|
+
currentStatement += char;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
let shouldEndQuote = quoteChar === char;
|
|
146
|
+
if(shouldEndQuote){
|
|
147
|
+
inQuote = false;
|
|
148
|
+
}
|
|
149
|
+
currentStatement += char;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
let shouldEndStatement = (';' === char && !inQuote);
|
|
153
|
+
if(shouldEndStatement){
|
|
154
|
+
statements.push(currentStatement);
|
|
155
|
+
currentStatement = '';
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
currentStatement += char;
|
|
159
|
+
}
|
|
160
|
+
let hasRemainingStatement = '' !== currentStatement.trim();
|
|
161
|
+
if(hasRemainingStatement){
|
|
162
|
+
statements.push(currentStatement);
|
|
163
|
+
}
|
|
164
|
+
return statements;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
shouldStartComment(char, nextChar, inQuote)
|
|
168
|
+
{
|
|
169
|
+
if(inQuote){
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
let isBlockComment = ('/' === char && '*' === nextChar);
|
|
173
|
+
let isLineComment = ('-' === char && '-' === nextChar);
|
|
174
|
+
return isBlockComment || isLineComment;
|
|
175
|
+
}
|
|
176
|
+
|
|
91
177
|
async fetchEntitiesFromDatabase()
|
|
92
178
|
{
|
|
93
179
|
if(!this.initialized){
|
|
@@ -16,10 +16,8 @@ class PrismaSchemaGenerator
|
|
|
16
16
|
this.config = sc.get(props, 'config', {});
|
|
17
17
|
this.client = sc.get(props, 'client', 'mysql');
|
|
18
18
|
this.debug = sc.get(props, 'debug', false);
|
|
19
|
-
this.prismaSchemaPath = sc.get(props, 'prismaSchemaPath',
|
|
20
|
-
|
|
21
|
-
this.schemaFilePath = FileHandler.joinPaths(this.prismaSchemaPath,
|
|
22
|
-
'schema.prisma');
|
|
19
|
+
this.prismaSchemaPath = sc.get(props, 'prismaSchemaPath', FileHandler.joinPaths(process.cwd(), 'prisma'));
|
|
20
|
+
this.schemaFilePath = FileHandler.joinPaths(this.prismaSchemaPath, 'schema.prisma');
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
async generate()
|