-
-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathproxy.ts
More file actions
249 lines (222 loc) · 8.62 KB
/
proxy.ts
File metadata and controls
249 lines (222 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import {
ConfigExpr,
InvocationExpr,
isDataSource,
isInvocationExpr,
isLiteralExpr,
LiteralExpr,
} from '@zenstackhq/language/ast';
import { getStringLiteral } from '@zenstackhq/language/utils';
import { ZenStackClient, type ClientContract } from '@zenstackhq/orm';
import { MysqlDialect } from '@zenstackhq/orm/dialects/mysql';
import { PostgresDialect } from '@zenstackhq/orm/dialects/postgres';
import { SqliteDialect } from '@zenstackhq/orm/dialects/sqlite';
import { RPCApiHandler } from '@zenstackhq/server/api';
import { ZenStackMiddleware } from '@zenstackhq/server/express';
import type BetterSqlite3 from 'better-sqlite3';
import colors from 'colors';
import cors from 'cors';
import express from 'express';
import { createJiti } from 'jiti';
import type { createPool as MysqlCreatePool } from 'mysql2';
import path from 'node:path';
import type { Pool as PgPoolType } from 'pg';
import { CliError } from '../cli-error';
import { getVersion } from '../utils/version-utils';
import { getOutputPath, getSchemaFile, loadSchemaDocument } from './action-utils';
import type { SchemaDef } from '@zenstackhq/orm/schema';
type Options = {
output?: string;
schema?: string;
port?: number;
logLevel?: string[];
databaseUrl?: string;
};
export async function run(options: Options) {
const allowedLogLevels = ['error', 'query'] as const;
const log = options.logLevel?.filter((level): level is (typeof allowedLogLevels)[number] =>
allowedLogLevels.includes(level as any),
);
const schemaFile = getSchemaFile(options.schema);
console.log(colors.gray(`Loading ZModel schema from: ${schemaFile}`));
let outputPath = getOutputPath(options, schemaFile);
// Ensure outputPath is absolute
if (!path.isAbsolute(outputPath)) {
outputPath = path.resolve(process.cwd(), outputPath);
}
const model = await loadSchemaDocument(schemaFile);
const dataSource = model.declarations.find(isDataSource);
let databaseUrl = options.databaseUrl;
if (!databaseUrl) {
const schemaUrl = dataSource?.fields.find((f) => f.name === 'url')?.value;
if (!schemaUrl) {
throw new CliError(
`The schema's "datasource" does not have a "url" field, please provide it with -d option.`,
);
}
databaseUrl = evaluateUrl(schemaUrl);
}
const provider = getStringLiteral(dataSource?.fields.find((f) => f.name === 'provider')?.value)!;
const dialect = await createDialect(provider, databaseUrl!, outputPath);
const jiti = createJiti(import.meta.url);
const schemaModule = (await jiti.import(path.join(outputPath, 'schema'))) as any;
// Build omit configuration for computed fields
const schema = schemaModule.schema as SchemaDef;
const omit: Record<string, Record<string, boolean>> = {};
for (const [modelName, modelDef] of Object.entries(schema.models)) {
const computedFields: Record<string, boolean> = {};
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
if (fieldDef.computed === true) {
computedFields[fieldName] = true;
}
}
if (Object.keys(computedFields).length > 0) {
omit[modelName] = computedFields;
}
}
const db = new ZenStackClient(schema, {
dialect: dialect,
log: log && log.length > 0 ? log : undefined,
omit: Object.keys(omit).length > 0 ? omit : undefined,
});
// check whether the database is reachable
try {
await db.$connect();
} catch (err) {
throw new CliError(`Failed to connect to the database: ${err instanceof Error ? err.message : String(err)}`);
}
startServer(db, schemaModule.schema, options);
}
function evaluateUrl(schemaUrl: ConfigExpr) {
if (isLiteralExpr(schemaUrl)) {
// Handle string literal
return getStringLiteral(schemaUrl);
} else if (isInvocationExpr(schemaUrl)) {
const envFunction = schemaUrl as InvocationExpr;
const envName = getStringLiteral(envFunction.args[0]?.value as LiteralExpr)!;
const envValue = process.env[envName];
if (!envValue) {
throw new CliError(`Environment variable ${envName} is not set`);
}
return envValue;
} else {
throw new CliError(`Unable to resolve the "url" field value.`);
}
}
function redactDatabaseUrl(url: string): string {
try {
const parsedUrl = new URL(url);
if (parsedUrl.password) {
parsedUrl.password = '***';
}
if (parsedUrl.username) {
parsedUrl.username = '***';
}
return parsedUrl.toString();
} catch {
// If URL parsing fails, return the original
return url;
}
}
async function createDialect(provider: string, databaseUrl: string, outputPath: string) {
switch (provider) {
case 'sqlite': {
let SQLite: typeof BetterSqlite3;
try {
SQLite = (await import('better-sqlite3')).default;
} catch {
throw new CliError(
`Package "better-sqlite3" is required for SQLite support. Please install it with: npm install better-sqlite3`,
);
}
let resolvedUrl = databaseUrl.trim();
if (resolvedUrl.startsWith('file:')) {
const filePath = resolvedUrl.substring('file:'.length);
if (!path.isAbsolute(filePath)) {
resolvedUrl = path.join(outputPath, filePath);
}
}
console.log(colors.gray(`Connecting to SQLite database at: ${resolvedUrl}`));
return new SqliteDialect({
database: new SQLite(resolvedUrl),
});
}
case 'postgresql': {
let PgPool: typeof PgPoolType;
try {
PgPool = (await import('pg')).Pool;
} catch {
throw new CliError(
`Package "pg" is required for PostgreSQL support. Please install it with: npm install pg`,
);
}
console.log(colors.gray(`Connecting to PostgreSQL database at: ${redactDatabaseUrl(databaseUrl)}`));
return new PostgresDialect({
pool: new PgPool({
connectionString: databaseUrl,
}),
});
}
case 'mysql': {
let createMysqlPool: typeof MysqlCreatePool;
try {
createMysqlPool = (await import('mysql2')).createPool;
} catch {
throw new CliError(
`Package "mysql2" is required for MySQL support. Please install it with: npm install mysql2`,
);
}
console.log(colors.gray(`Connecting to MySQL database at: ${redactDatabaseUrl(databaseUrl)}`));
return new MysqlDialect({
pool: createMysqlPool(databaseUrl),
});
}
default:
throw new CliError(`Unsupported database provider: ${provider}`);
}
}
function startServer(client: ClientContract<any, any>, schema: any, options: Options) {
const app = express();
app.use(cors());
app.use(express.json({ limit: '5mb' }));
app.use(express.urlencoded({ extended: true, limit: '5mb' }));
app.use(
'/api/model',
ZenStackMiddleware({
apiHandler: new RPCApiHandler({ schema }),
getClient: () => client,
}),
);
app.get('/api/schema', (_req, res: express.Response) => {
res.json({ ...schema, zenstackVersion: getVersion() });
});
const server = app.listen(options.port, () => {
console.log(`ZenStack proxy server is running on port: ${options.port}`);
console.log(`You can visit ZenStack Studio at: ${colors.blue('https://studio.zenstack.dev')}`);
});
server.on('error', (err: NodeJS.ErrnoException) => {
if (err.code === 'EADDRINUSE') {
console.error(
colors.red(`Port ${options.port} is already in use. Please choose a different port using -p option.`),
);
} else {
throw new CliError(`Failed to start the server: ${err.message}`);
}
process.exit(1);
});
// Graceful shutdown
process.on('SIGTERM', async () => {
server.close(() => {
console.log('\nZenStack proxy server closed');
});
await client.$disconnect();
process.exit(0);
});
process.on('SIGINT', async () => {
server.close(() => {
console.log('\nZenStack proxy server closed');
});
await client.$disconnect();
process.exit(0);
});
}