@squadbase/vite-server 0.0.1-build-4 → 0.0.1-build-6
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.
- package/dist/cli/index.js +31 -46
- package/dist/index.js +38 -50
- package/dist/main.js +38 -50
- package/dist/types/data-source.d.ts +4 -6
- package/dist/vite-plugin.js +29 -44
- package/package.json +4 -2
package/dist/cli/index.js
CHANGED
|
@@ -102,29 +102,29 @@ function createPostgreSQLClient(connectionString) {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
// src/connector-client/env.ts
|
|
105
|
-
function resolveEnvVar(entry, key,
|
|
105
|
+
function resolveEnvVar(entry, key, connectionId) {
|
|
106
106
|
const envVarName = entry.envVars[key];
|
|
107
107
|
if (!envVarName) {
|
|
108
|
-
throw new Error(`
|
|
108
|
+
throw new Error(`Connection "${connectionId}" is missing envVars mapping for key "${key}"`);
|
|
109
109
|
}
|
|
110
110
|
const value = process.env[envVarName];
|
|
111
111
|
if (!value) {
|
|
112
|
-
throw new Error(`Environment variable "${envVarName}" (for "${
|
|
112
|
+
throw new Error(`Environment variable "${envVarName}" (for connection "${connectionId}", key "${key}") is not set`);
|
|
113
113
|
}
|
|
114
114
|
return value;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
// src/connector-client/bigquery.ts
|
|
118
|
-
function createBigQueryClient(entry,
|
|
119
|
-
const projectId = resolveEnvVar(entry, "project-id",
|
|
120
|
-
const serviceAccountJsonBase64 = resolveEnvVar(entry, "service-account-json-base64",
|
|
118
|
+
function createBigQueryClient(entry, connectionId) {
|
|
119
|
+
const projectId = resolveEnvVar(entry, "project-id", connectionId);
|
|
120
|
+
const serviceAccountJsonBase64 = resolveEnvVar(entry, "service-account-key-json-base64", connectionId);
|
|
121
121
|
const serviceAccountJson = Buffer.from(serviceAccountJsonBase64, "base64").toString("utf-8");
|
|
122
122
|
let gcpCredentials;
|
|
123
123
|
try {
|
|
124
124
|
gcpCredentials = JSON.parse(serviceAccountJson);
|
|
125
125
|
} catch {
|
|
126
126
|
throw new Error(
|
|
127
|
-
`BigQuery service account JSON (decoded from base64) is not valid JSON for
|
|
127
|
+
`BigQuery service account JSON (decoded from base64) is not valid JSON for connection "${connectionId}"`
|
|
128
128
|
);
|
|
129
129
|
}
|
|
130
130
|
return {
|
|
@@ -139,12 +139,12 @@ function createBigQueryClient(entry, slug) {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
// src/connector-client/snowflake.ts
|
|
142
|
-
function createSnowflakeClient(entry,
|
|
143
|
-
const accountIdentifier = resolveEnvVar(entry, "account",
|
|
144
|
-
const user = resolveEnvVar(entry, "user",
|
|
145
|
-
const role = resolveEnvVar(entry, "role",
|
|
146
|
-
const warehouse = resolveEnvVar(entry, "warehouse",
|
|
147
|
-
const privateKeyBase64 = resolveEnvVar(entry, "private-key-base64",
|
|
142
|
+
function createSnowflakeClient(entry, connectionId) {
|
|
143
|
+
const accountIdentifier = resolveEnvVar(entry, "account", connectionId);
|
|
144
|
+
const user = resolveEnvVar(entry, "user", connectionId);
|
|
145
|
+
const role = resolveEnvVar(entry, "role", connectionId);
|
|
146
|
+
const warehouse = resolveEnvVar(entry, "warehouse", connectionId);
|
|
147
|
+
const privateKeyBase64 = resolveEnvVar(entry, "private-key-base64", connectionId);
|
|
148
148
|
const privateKey = Buffer.from(privateKeyBase64, "base64").toString("utf-8");
|
|
149
149
|
return {
|
|
150
150
|
async query(sql) {
|
|
@@ -199,53 +199,38 @@ function createConnectorRegistry() {
|
|
|
199
199
|
}
|
|
200
200
|
return connectionsCache;
|
|
201
201
|
}
|
|
202
|
-
async function getClient2(
|
|
203
|
-
if (!connectorSlug) {
|
|
204
|
-
const cacheKey = "__squadbase-db__";
|
|
205
|
-
const cached2 = clientCache.get(cacheKey);
|
|
206
|
-
if (cached2) return cached2;
|
|
207
|
-
const url = process.env.SQUADBASE_POSTGRESQL_URL;
|
|
208
|
-
if (!url) throw new Error("SQUADBASE_POSTGRESQL_URL environment variable is not set");
|
|
209
|
-
const client = createPostgreSQLClient(url);
|
|
210
|
-
clientCache.set(cacheKey, client);
|
|
211
|
-
return client;
|
|
212
|
-
}
|
|
213
|
-
const cached = clientCache.get(connectorSlug);
|
|
214
|
-
if (cached) return cached;
|
|
202
|
+
async function getClient2(connectionId) {
|
|
215
203
|
const connections = loadConnections();
|
|
216
|
-
const entry = connections[
|
|
204
|
+
const entry = connections[connectionId];
|
|
217
205
|
if (!entry) {
|
|
218
|
-
throw new Error(`
|
|
219
|
-
}
|
|
220
|
-
const resolvedType = connectorType ?? entry.connectorType;
|
|
221
|
-
if (!resolvedType) {
|
|
222
|
-
throw new Error(
|
|
223
|
-
`connector type could not be determined for slug '${connectorSlug}'. Specify connectorType in the data-source JSON or in .squadbase/connections.json.`
|
|
224
|
-
);
|
|
206
|
+
throw new Error(`connection '${connectionId}' not found in .squadbase/connections.json`);
|
|
225
207
|
}
|
|
226
|
-
|
|
227
|
-
|
|
208
|
+
const connectorSlug = entry.connector.slug;
|
|
209
|
+
const cached = clientCache.get(connectionId);
|
|
210
|
+
if (cached) return { client: cached, connectorSlug };
|
|
211
|
+
if (connectorSlug === "snowflake") {
|
|
212
|
+
return { client: createSnowflakeClient(entry, connectionId), connectorSlug };
|
|
228
213
|
}
|
|
229
|
-
if (
|
|
230
|
-
return createBigQueryClient(entry, connectorSlug
|
|
214
|
+
if (connectorSlug === "bigquery") {
|
|
215
|
+
return { client: createBigQueryClient(entry, connectionId), connectorSlug };
|
|
231
216
|
}
|
|
232
|
-
if (
|
|
217
|
+
if (connectorSlug === "postgresql" || connectorSlug === "squadbase-db") {
|
|
233
218
|
const urlEnvName = entry.envVars["connection-url"];
|
|
234
219
|
if (!urlEnvName) {
|
|
235
|
-
throw new Error(`'connection-url' is not defined in envVars for
|
|
220
|
+
throw new Error(`'connection-url' is not defined in envVars for connection '${connectionId}'`);
|
|
236
221
|
}
|
|
237
222
|
const connectionUrl = process.env[urlEnvName];
|
|
238
223
|
if (!connectionUrl) {
|
|
239
224
|
throw new Error(
|
|
240
|
-
`environment variable '${urlEnvName}' (mapped from
|
|
225
|
+
`environment variable '${urlEnvName}' (mapped from connection '${connectionId}') is not set`
|
|
241
226
|
);
|
|
242
227
|
}
|
|
243
228
|
const client = createPostgreSQLClient(connectionUrl);
|
|
244
|
-
clientCache.set(
|
|
245
|
-
return client;
|
|
229
|
+
clientCache.set(connectionId, client);
|
|
230
|
+
return { client, connectorSlug };
|
|
246
231
|
}
|
|
247
232
|
throw new Error(
|
|
248
|
-
`connector type '${
|
|
233
|
+
`connector type '${connectorSlug}' is not supported. Supported: "snowflake", "bigquery", "postgresql", "squadbase-db"`
|
|
249
234
|
);
|
|
250
235
|
}
|
|
251
236
|
function reloadEnvFile2(envPath) {
|
|
@@ -351,8 +336,8 @@ function createStubContext(params) {
|
|
|
351
336
|
async function runSqlDataSource(slug, def, params, limit) {
|
|
352
337
|
const start = Date.now();
|
|
353
338
|
try {
|
|
354
|
-
const client = await getClient(def.
|
|
355
|
-
const isExternal =
|
|
339
|
+
const { client, connectorSlug } = await getClient(def.connectionId);
|
|
340
|
+
const isExternal = connectorSlug === "snowflake" || connectorSlug === "bigquery";
|
|
356
341
|
let queryText;
|
|
357
342
|
let queryValues;
|
|
358
343
|
if (isExternal) {
|
package/dist/index.js
CHANGED
|
@@ -26,29 +26,29 @@ function createPostgreSQLClient(connectionString) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// src/connector-client/env.ts
|
|
29
|
-
function resolveEnvVar(entry, key,
|
|
29
|
+
function resolveEnvVar(entry, key, connectionId) {
|
|
30
30
|
const envVarName = entry.envVars[key];
|
|
31
31
|
if (!envVarName) {
|
|
32
|
-
throw new Error(`
|
|
32
|
+
throw new Error(`Connection "${connectionId}" is missing envVars mapping for key "${key}"`);
|
|
33
33
|
}
|
|
34
34
|
const value = process.env[envVarName];
|
|
35
35
|
if (!value) {
|
|
36
|
-
throw new Error(`Environment variable "${envVarName}" (for "${
|
|
36
|
+
throw new Error(`Environment variable "${envVarName}" (for connection "${connectionId}", key "${key}") is not set`);
|
|
37
37
|
}
|
|
38
38
|
return value;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
// src/connector-client/bigquery.ts
|
|
42
|
-
function createBigQueryClient(entry,
|
|
43
|
-
const projectId = resolveEnvVar(entry, "project-id",
|
|
44
|
-
const serviceAccountJsonBase64 = resolveEnvVar(entry, "service-account-json-base64",
|
|
42
|
+
function createBigQueryClient(entry, connectionId) {
|
|
43
|
+
const projectId = resolveEnvVar(entry, "project-id", connectionId);
|
|
44
|
+
const serviceAccountJsonBase64 = resolveEnvVar(entry, "service-account-key-json-base64", connectionId);
|
|
45
45
|
const serviceAccountJson = Buffer.from(serviceAccountJsonBase64, "base64").toString("utf-8");
|
|
46
46
|
let gcpCredentials;
|
|
47
47
|
try {
|
|
48
48
|
gcpCredentials = JSON.parse(serviceAccountJson);
|
|
49
49
|
} catch {
|
|
50
50
|
throw new Error(
|
|
51
|
-
`BigQuery service account JSON (decoded from base64) is not valid JSON for
|
|
51
|
+
`BigQuery service account JSON (decoded from base64) is not valid JSON for connection "${connectionId}"`
|
|
52
52
|
);
|
|
53
53
|
}
|
|
54
54
|
return {
|
|
@@ -63,12 +63,12 @@ function createBigQueryClient(entry, slug) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// src/connector-client/snowflake.ts
|
|
66
|
-
function createSnowflakeClient(entry,
|
|
67
|
-
const accountIdentifier = resolveEnvVar(entry, "account",
|
|
68
|
-
const user = resolveEnvVar(entry, "user",
|
|
69
|
-
const role = resolveEnvVar(entry, "role",
|
|
70
|
-
const warehouse = resolveEnvVar(entry, "warehouse",
|
|
71
|
-
const privateKeyBase64 = resolveEnvVar(entry, "private-key-base64",
|
|
66
|
+
function createSnowflakeClient(entry, connectionId) {
|
|
67
|
+
const accountIdentifier = resolveEnvVar(entry, "account", connectionId);
|
|
68
|
+
const user = resolveEnvVar(entry, "user", connectionId);
|
|
69
|
+
const role = resolveEnvVar(entry, "role", connectionId);
|
|
70
|
+
const warehouse = resolveEnvVar(entry, "warehouse", connectionId);
|
|
71
|
+
const privateKeyBase64 = resolveEnvVar(entry, "private-key-base64", connectionId);
|
|
72
72
|
const privateKey = Buffer.from(privateKeyBase64, "base64").toString("utf-8");
|
|
73
73
|
return {
|
|
74
74
|
async query(sql) {
|
|
@@ -123,53 +123,38 @@ function createConnectorRegistry() {
|
|
|
123
123
|
}
|
|
124
124
|
return connectionsCache;
|
|
125
125
|
}
|
|
126
|
-
async function getClient2(
|
|
127
|
-
if (!connectorSlug) {
|
|
128
|
-
const cacheKey = "__squadbase-db__";
|
|
129
|
-
const cached2 = clientCache.get(cacheKey);
|
|
130
|
-
if (cached2) return cached2;
|
|
131
|
-
const url = process.env.SQUADBASE_POSTGRESQL_URL;
|
|
132
|
-
if (!url) throw new Error("SQUADBASE_POSTGRESQL_URL environment variable is not set");
|
|
133
|
-
const client = createPostgreSQLClient(url);
|
|
134
|
-
clientCache.set(cacheKey, client);
|
|
135
|
-
return client;
|
|
136
|
-
}
|
|
137
|
-
const cached = clientCache.get(connectorSlug);
|
|
138
|
-
if (cached) return cached;
|
|
126
|
+
async function getClient2(connectionId) {
|
|
139
127
|
const connections = loadConnections();
|
|
140
|
-
const entry = connections[
|
|
128
|
+
const entry = connections[connectionId];
|
|
141
129
|
if (!entry) {
|
|
142
|
-
throw new Error(`
|
|
143
|
-
}
|
|
144
|
-
const resolvedType = connectorType ?? entry.connectorType;
|
|
145
|
-
if (!resolvedType) {
|
|
146
|
-
throw new Error(
|
|
147
|
-
`connector type could not be determined for slug '${connectorSlug}'. Specify connectorType in the data-source JSON or in .squadbase/connections.json.`
|
|
148
|
-
);
|
|
130
|
+
throw new Error(`connection '${connectionId}' not found in .squadbase/connections.json`);
|
|
149
131
|
}
|
|
150
|
-
|
|
151
|
-
|
|
132
|
+
const connectorSlug = entry.connector.slug;
|
|
133
|
+
const cached = clientCache.get(connectionId);
|
|
134
|
+
if (cached) return { client: cached, connectorSlug };
|
|
135
|
+
if (connectorSlug === "snowflake") {
|
|
136
|
+
return { client: createSnowflakeClient(entry, connectionId), connectorSlug };
|
|
152
137
|
}
|
|
153
|
-
if (
|
|
154
|
-
return createBigQueryClient(entry, connectorSlug
|
|
138
|
+
if (connectorSlug === "bigquery") {
|
|
139
|
+
return { client: createBigQueryClient(entry, connectionId), connectorSlug };
|
|
155
140
|
}
|
|
156
|
-
if (
|
|
141
|
+
if (connectorSlug === "postgresql" || connectorSlug === "squadbase-db") {
|
|
157
142
|
const urlEnvName = entry.envVars["connection-url"];
|
|
158
143
|
if (!urlEnvName) {
|
|
159
|
-
throw new Error(`'connection-url' is not defined in envVars for
|
|
144
|
+
throw new Error(`'connection-url' is not defined in envVars for connection '${connectionId}'`);
|
|
160
145
|
}
|
|
161
146
|
const connectionUrl = process.env[urlEnvName];
|
|
162
147
|
if (!connectionUrl) {
|
|
163
148
|
throw new Error(
|
|
164
|
-
`environment variable '${urlEnvName}' (mapped from
|
|
149
|
+
`environment variable '${urlEnvName}' (mapped from connection '${connectionId}') is not set`
|
|
165
150
|
);
|
|
166
151
|
}
|
|
167
152
|
const client = createPostgreSQLClient(connectionUrl);
|
|
168
|
-
clientCache.set(
|
|
169
|
-
return client;
|
|
153
|
+
clientCache.set(connectionId, client);
|
|
154
|
+
return { client, connectorSlug };
|
|
170
155
|
}
|
|
171
156
|
throw new Error(
|
|
172
|
-
`connector type '${
|
|
157
|
+
`connector type '${connectorSlug}' is not supported. Supported: "snowflake", "bigquery", "postgresql", "squadbase-db"`
|
|
173
158
|
);
|
|
174
159
|
}
|
|
175
160
|
function reloadEnvFile2(envPath) {
|
|
@@ -277,6 +262,10 @@ async function initialize() {
|
|
|
277
262
|
console.warn(`[registry] Skipping ${file}: missing description`);
|
|
278
263
|
return;
|
|
279
264
|
}
|
|
265
|
+
if (!def.connectionId) {
|
|
266
|
+
console.warn(`[registry] Skipping ${file}: missing connectionId`);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
280
269
|
if (def.type === "typescript") {
|
|
281
270
|
if (!def.handlerPath) {
|
|
282
271
|
console.warn(`[registry] Skipping ${file}: missing handlerPath`);
|
|
@@ -287,6 +276,7 @@ async function initialize() {
|
|
|
287
276
|
description: def.description,
|
|
288
277
|
parameters: def.parameters ?? [],
|
|
289
278
|
response: def.response,
|
|
279
|
+
connectionId: def.connectionId,
|
|
290
280
|
cacheConfig: def.cache,
|
|
291
281
|
handler: async () => {
|
|
292
282
|
throw new Error("TypeScript handler must be called via _tsHandlerPath");
|
|
@@ -306,13 +296,12 @@ async function initialize() {
|
|
|
306
296
|
description: sqlDef.description,
|
|
307
297
|
parameters: sqlDef.parameters ?? [],
|
|
308
298
|
response: sqlDef.response,
|
|
309
|
-
|
|
299
|
+
connectionId: sqlDef.connectionId,
|
|
310
300
|
cacheConfig: sqlDef.cache,
|
|
311
301
|
_query: sqlDef.query,
|
|
312
|
-
_connectorType: sqlDef.connectorType,
|
|
313
302
|
handler: async (runtimeParams) => {
|
|
314
|
-
const client = await getClient(sqlDef.
|
|
315
|
-
const isExternalConnector =
|
|
303
|
+
const { client, connectorSlug } = await getClient(sqlDef.connectionId);
|
|
304
|
+
const isExternalConnector = connectorSlug === "snowflake" || connectorSlug === "bigquery";
|
|
316
305
|
let queryText;
|
|
317
306
|
let queryValues;
|
|
318
307
|
if (isExternalConnector) {
|
|
@@ -391,8 +380,7 @@ function buildMeta(slug, def) {
|
|
|
391
380
|
parameters: def.parameters,
|
|
392
381
|
response: def.response,
|
|
393
382
|
query: def._query,
|
|
394
|
-
|
|
395
|
-
connectorSlug: def.connectorSlug,
|
|
383
|
+
connectionId: def.connectionId,
|
|
396
384
|
handlerPath: def._tsHandlerPath ? path2.relative(currentDirPath, def._tsHandlerPath) : void 0,
|
|
397
385
|
cache: def.cacheConfig
|
|
398
386
|
};
|
package/dist/main.js
CHANGED
|
@@ -26,29 +26,29 @@ function createPostgreSQLClient(connectionString) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// src/connector-client/env.ts
|
|
29
|
-
function resolveEnvVar(entry, key,
|
|
29
|
+
function resolveEnvVar(entry, key, connectionId) {
|
|
30
30
|
const envVarName = entry.envVars[key];
|
|
31
31
|
if (!envVarName) {
|
|
32
|
-
throw new Error(`
|
|
32
|
+
throw new Error(`Connection "${connectionId}" is missing envVars mapping for key "${key}"`);
|
|
33
33
|
}
|
|
34
34
|
const value = process.env[envVarName];
|
|
35
35
|
if (!value) {
|
|
36
|
-
throw new Error(`Environment variable "${envVarName}" (for "${
|
|
36
|
+
throw new Error(`Environment variable "${envVarName}" (for connection "${connectionId}", key "${key}") is not set`);
|
|
37
37
|
}
|
|
38
38
|
return value;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
// src/connector-client/bigquery.ts
|
|
42
|
-
function createBigQueryClient(entry,
|
|
43
|
-
const projectId = resolveEnvVar(entry, "project-id",
|
|
44
|
-
const serviceAccountJsonBase64 = resolveEnvVar(entry, "service-account-json-base64",
|
|
42
|
+
function createBigQueryClient(entry, connectionId) {
|
|
43
|
+
const projectId = resolveEnvVar(entry, "project-id", connectionId);
|
|
44
|
+
const serviceAccountJsonBase64 = resolveEnvVar(entry, "service-account-key-json-base64", connectionId);
|
|
45
45
|
const serviceAccountJson = Buffer.from(serviceAccountJsonBase64, "base64").toString("utf-8");
|
|
46
46
|
let gcpCredentials;
|
|
47
47
|
try {
|
|
48
48
|
gcpCredentials = JSON.parse(serviceAccountJson);
|
|
49
49
|
} catch {
|
|
50
50
|
throw new Error(
|
|
51
|
-
`BigQuery service account JSON (decoded from base64) is not valid JSON for
|
|
51
|
+
`BigQuery service account JSON (decoded from base64) is not valid JSON for connection "${connectionId}"`
|
|
52
52
|
);
|
|
53
53
|
}
|
|
54
54
|
return {
|
|
@@ -63,12 +63,12 @@ function createBigQueryClient(entry, slug) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// src/connector-client/snowflake.ts
|
|
66
|
-
function createSnowflakeClient(entry,
|
|
67
|
-
const accountIdentifier = resolveEnvVar(entry, "account",
|
|
68
|
-
const user = resolveEnvVar(entry, "user",
|
|
69
|
-
const role = resolveEnvVar(entry, "role",
|
|
70
|
-
const warehouse = resolveEnvVar(entry, "warehouse",
|
|
71
|
-
const privateKeyBase64 = resolveEnvVar(entry, "private-key-base64",
|
|
66
|
+
function createSnowflakeClient(entry, connectionId) {
|
|
67
|
+
const accountIdentifier = resolveEnvVar(entry, "account", connectionId);
|
|
68
|
+
const user = resolveEnvVar(entry, "user", connectionId);
|
|
69
|
+
const role = resolveEnvVar(entry, "role", connectionId);
|
|
70
|
+
const warehouse = resolveEnvVar(entry, "warehouse", connectionId);
|
|
71
|
+
const privateKeyBase64 = resolveEnvVar(entry, "private-key-base64", connectionId);
|
|
72
72
|
const privateKey = Buffer.from(privateKeyBase64, "base64").toString("utf-8");
|
|
73
73
|
return {
|
|
74
74
|
async query(sql) {
|
|
@@ -123,53 +123,38 @@ function createConnectorRegistry() {
|
|
|
123
123
|
}
|
|
124
124
|
return connectionsCache;
|
|
125
125
|
}
|
|
126
|
-
async function getClient2(
|
|
127
|
-
if (!connectorSlug) {
|
|
128
|
-
const cacheKey = "__squadbase-db__";
|
|
129
|
-
const cached2 = clientCache.get(cacheKey);
|
|
130
|
-
if (cached2) return cached2;
|
|
131
|
-
const url = process.env.SQUADBASE_POSTGRESQL_URL;
|
|
132
|
-
if (!url) throw new Error("SQUADBASE_POSTGRESQL_URL environment variable is not set");
|
|
133
|
-
const client = createPostgreSQLClient(url);
|
|
134
|
-
clientCache.set(cacheKey, client);
|
|
135
|
-
return client;
|
|
136
|
-
}
|
|
137
|
-
const cached = clientCache.get(connectorSlug);
|
|
138
|
-
if (cached) return cached;
|
|
126
|
+
async function getClient2(connectionId) {
|
|
139
127
|
const connections = loadConnections();
|
|
140
|
-
const entry = connections[
|
|
128
|
+
const entry = connections[connectionId];
|
|
141
129
|
if (!entry) {
|
|
142
|
-
throw new Error(`
|
|
143
|
-
}
|
|
144
|
-
const resolvedType = connectorType ?? entry.connectorType;
|
|
145
|
-
if (!resolvedType) {
|
|
146
|
-
throw new Error(
|
|
147
|
-
`connector type could not be determined for slug '${connectorSlug}'. Specify connectorType in the data-source JSON or in .squadbase/connections.json.`
|
|
148
|
-
);
|
|
130
|
+
throw new Error(`connection '${connectionId}' not found in .squadbase/connections.json`);
|
|
149
131
|
}
|
|
150
|
-
|
|
151
|
-
|
|
132
|
+
const connectorSlug = entry.connector.slug;
|
|
133
|
+
const cached = clientCache.get(connectionId);
|
|
134
|
+
if (cached) return { client: cached, connectorSlug };
|
|
135
|
+
if (connectorSlug === "snowflake") {
|
|
136
|
+
return { client: createSnowflakeClient(entry, connectionId), connectorSlug };
|
|
152
137
|
}
|
|
153
|
-
if (
|
|
154
|
-
return createBigQueryClient(entry, connectorSlug
|
|
138
|
+
if (connectorSlug === "bigquery") {
|
|
139
|
+
return { client: createBigQueryClient(entry, connectionId), connectorSlug };
|
|
155
140
|
}
|
|
156
|
-
if (
|
|
141
|
+
if (connectorSlug === "postgresql" || connectorSlug === "squadbase-db") {
|
|
157
142
|
const urlEnvName = entry.envVars["connection-url"];
|
|
158
143
|
if (!urlEnvName) {
|
|
159
|
-
throw new Error(`'connection-url' is not defined in envVars for
|
|
144
|
+
throw new Error(`'connection-url' is not defined in envVars for connection '${connectionId}'`);
|
|
160
145
|
}
|
|
161
146
|
const connectionUrl = process.env[urlEnvName];
|
|
162
147
|
if (!connectionUrl) {
|
|
163
148
|
throw new Error(
|
|
164
|
-
`environment variable '${urlEnvName}' (mapped from
|
|
149
|
+
`environment variable '${urlEnvName}' (mapped from connection '${connectionId}') is not set`
|
|
165
150
|
);
|
|
166
151
|
}
|
|
167
152
|
const client = createPostgreSQLClient(connectionUrl);
|
|
168
|
-
clientCache.set(
|
|
169
|
-
return client;
|
|
153
|
+
clientCache.set(connectionId, client);
|
|
154
|
+
return { client, connectorSlug };
|
|
170
155
|
}
|
|
171
156
|
throw new Error(
|
|
172
|
-
`connector type '${
|
|
157
|
+
`connector type '${connectorSlug}' is not supported. Supported: "snowflake", "bigquery", "postgresql", "squadbase-db"`
|
|
173
158
|
);
|
|
174
159
|
}
|
|
175
160
|
function reloadEnvFile2(envPath) {
|
|
@@ -277,6 +262,10 @@ async function initialize() {
|
|
|
277
262
|
console.warn(`[registry] Skipping ${file}: missing description`);
|
|
278
263
|
return;
|
|
279
264
|
}
|
|
265
|
+
if (!def.connectionId) {
|
|
266
|
+
console.warn(`[registry] Skipping ${file}: missing connectionId`);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
280
269
|
if (def.type === "typescript") {
|
|
281
270
|
if (!def.handlerPath) {
|
|
282
271
|
console.warn(`[registry] Skipping ${file}: missing handlerPath`);
|
|
@@ -287,6 +276,7 @@ async function initialize() {
|
|
|
287
276
|
description: def.description,
|
|
288
277
|
parameters: def.parameters ?? [],
|
|
289
278
|
response: def.response,
|
|
279
|
+
connectionId: def.connectionId,
|
|
290
280
|
cacheConfig: def.cache,
|
|
291
281
|
handler: async () => {
|
|
292
282
|
throw new Error("TypeScript handler must be called via _tsHandlerPath");
|
|
@@ -306,13 +296,12 @@ async function initialize() {
|
|
|
306
296
|
description: sqlDef.description,
|
|
307
297
|
parameters: sqlDef.parameters ?? [],
|
|
308
298
|
response: sqlDef.response,
|
|
309
|
-
|
|
299
|
+
connectionId: sqlDef.connectionId,
|
|
310
300
|
cacheConfig: sqlDef.cache,
|
|
311
301
|
_query: sqlDef.query,
|
|
312
|
-
_connectorType: sqlDef.connectorType,
|
|
313
302
|
handler: async (runtimeParams) => {
|
|
314
|
-
const client = await getClient(sqlDef.
|
|
315
|
-
const isExternalConnector =
|
|
303
|
+
const { client, connectorSlug } = await getClient(sqlDef.connectionId);
|
|
304
|
+
const isExternalConnector = connectorSlug === "snowflake" || connectorSlug === "bigquery";
|
|
316
305
|
let queryText;
|
|
317
306
|
let queryValues;
|
|
318
307
|
if (isExternalConnector) {
|
|
@@ -391,8 +380,7 @@ function buildMeta(slug, def) {
|
|
|
391
380
|
parameters: def.parameters,
|
|
392
381
|
response: def.response,
|
|
393
382
|
query: def._query,
|
|
394
|
-
|
|
395
|
-
connectorSlug: def.connectorSlug,
|
|
383
|
+
connectionId: def.connectionId,
|
|
396
384
|
handlerPath: def._tsHandlerPath ? path2.relative(currentDirPath, def._tsHandlerPath) : void 0,
|
|
397
385
|
cache: def.cacheConfig
|
|
398
386
|
};
|
|
@@ -47,13 +47,12 @@ interface DataSourceDefinition {
|
|
|
47
47
|
description: string;
|
|
48
48
|
parameters: ParameterMeta[];
|
|
49
49
|
response?: DataSourceResponse;
|
|
50
|
-
|
|
50
|
+
connectionId: string;
|
|
51
51
|
cacheConfig?: DataSourceCacheConfig;
|
|
52
52
|
handler: (params: Record<string, unknown>) => Promise<unknown> | unknown;
|
|
53
53
|
_isTypescript?: boolean;
|
|
54
54
|
_tsHandlerPath?: string;
|
|
55
55
|
_query?: string;
|
|
56
|
-
_connectorType?: string;
|
|
57
56
|
}
|
|
58
57
|
interface DataSourceMeta {
|
|
59
58
|
slug: string;
|
|
@@ -62,8 +61,7 @@ interface DataSourceMeta {
|
|
|
62
61
|
parameters: ParameterMeta[];
|
|
63
62
|
response?: DataSourceResponse;
|
|
64
63
|
query?: string;
|
|
65
|
-
|
|
66
|
-
connectorSlug?: string;
|
|
64
|
+
connectionId: string;
|
|
67
65
|
handlerPath?: string;
|
|
68
66
|
cache?: DataSourceCacheConfig;
|
|
69
67
|
}
|
|
@@ -73,14 +71,14 @@ interface JsonDataSourceDefinition {
|
|
|
73
71
|
parameters?: ParameterMeta[];
|
|
74
72
|
response?: DataSourceResponse;
|
|
75
73
|
query: string;
|
|
76
|
-
|
|
77
|
-
connectorSlug?: string;
|
|
74
|
+
connectionId: string;
|
|
78
75
|
cache?: DataSourceCacheConfig;
|
|
79
76
|
}
|
|
80
77
|
interface JsonTypeScriptDataSourceDefinition {
|
|
81
78
|
description: string;
|
|
82
79
|
type: "typescript";
|
|
83
80
|
handlerPath: string;
|
|
81
|
+
connectionId: string;
|
|
84
82
|
parameters?: ParameterMeta[];
|
|
85
83
|
response?: DataSourceResponse;
|
|
86
84
|
cache?: DataSourceCacheConfig;
|
package/dist/vite-plugin.js
CHANGED
|
@@ -26,29 +26,29 @@ function createPostgreSQLClient(connectionString) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// src/connector-client/env.ts
|
|
29
|
-
function resolveEnvVar(entry, key,
|
|
29
|
+
function resolveEnvVar(entry, key, connectionId) {
|
|
30
30
|
const envVarName = entry.envVars[key];
|
|
31
31
|
if (!envVarName) {
|
|
32
|
-
throw new Error(`
|
|
32
|
+
throw new Error(`Connection "${connectionId}" is missing envVars mapping for key "${key}"`);
|
|
33
33
|
}
|
|
34
34
|
const value = process.env[envVarName];
|
|
35
35
|
if (!value) {
|
|
36
|
-
throw new Error(`Environment variable "${envVarName}" (for "${
|
|
36
|
+
throw new Error(`Environment variable "${envVarName}" (for connection "${connectionId}", key "${key}") is not set`);
|
|
37
37
|
}
|
|
38
38
|
return value;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
// src/connector-client/bigquery.ts
|
|
42
|
-
function createBigQueryClient(entry,
|
|
43
|
-
const projectId = resolveEnvVar(entry, "project-id",
|
|
44
|
-
const serviceAccountJsonBase64 = resolveEnvVar(entry, "service-account-json-base64",
|
|
42
|
+
function createBigQueryClient(entry, connectionId) {
|
|
43
|
+
const projectId = resolveEnvVar(entry, "project-id", connectionId);
|
|
44
|
+
const serviceAccountJsonBase64 = resolveEnvVar(entry, "service-account-key-json-base64", connectionId);
|
|
45
45
|
const serviceAccountJson = Buffer.from(serviceAccountJsonBase64, "base64").toString("utf-8");
|
|
46
46
|
let gcpCredentials;
|
|
47
47
|
try {
|
|
48
48
|
gcpCredentials = JSON.parse(serviceAccountJson);
|
|
49
49
|
} catch {
|
|
50
50
|
throw new Error(
|
|
51
|
-
`BigQuery service account JSON (decoded from base64) is not valid JSON for
|
|
51
|
+
`BigQuery service account JSON (decoded from base64) is not valid JSON for connection "${connectionId}"`
|
|
52
52
|
);
|
|
53
53
|
}
|
|
54
54
|
return {
|
|
@@ -63,12 +63,12 @@ function createBigQueryClient(entry, slug) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// src/connector-client/snowflake.ts
|
|
66
|
-
function createSnowflakeClient(entry,
|
|
67
|
-
const accountIdentifier = resolveEnvVar(entry, "account",
|
|
68
|
-
const user = resolveEnvVar(entry, "user",
|
|
69
|
-
const role = resolveEnvVar(entry, "role",
|
|
70
|
-
const warehouse = resolveEnvVar(entry, "warehouse",
|
|
71
|
-
const privateKeyBase64 = resolveEnvVar(entry, "private-key-base64",
|
|
66
|
+
function createSnowflakeClient(entry, connectionId) {
|
|
67
|
+
const accountIdentifier = resolveEnvVar(entry, "account", connectionId);
|
|
68
|
+
const user = resolveEnvVar(entry, "user", connectionId);
|
|
69
|
+
const role = resolveEnvVar(entry, "role", connectionId);
|
|
70
|
+
const warehouse = resolveEnvVar(entry, "warehouse", connectionId);
|
|
71
|
+
const privateKeyBase64 = resolveEnvVar(entry, "private-key-base64", connectionId);
|
|
72
72
|
const privateKey = Buffer.from(privateKeyBase64, "base64").toString("utf-8");
|
|
73
73
|
return {
|
|
74
74
|
async query(sql) {
|
|
@@ -123,53 +123,38 @@ function createConnectorRegistry() {
|
|
|
123
123
|
}
|
|
124
124
|
return connectionsCache;
|
|
125
125
|
}
|
|
126
|
-
async function getClient2(
|
|
127
|
-
if (!connectorSlug) {
|
|
128
|
-
const cacheKey = "__squadbase-db__";
|
|
129
|
-
const cached2 = clientCache.get(cacheKey);
|
|
130
|
-
if (cached2) return cached2;
|
|
131
|
-
const url = process.env.SQUADBASE_POSTGRESQL_URL;
|
|
132
|
-
if (!url) throw new Error("SQUADBASE_POSTGRESQL_URL environment variable is not set");
|
|
133
|
-
const client = createPostgreSQLClient(url);
|
|
134
|
-
clientCache.set(cacheKey, client);
|
|
135
|
-
return client;
|
|
136
|
-
}
|
|
137
|
-
const cached = clientCache.get(connectorSlug);
|
|
138
|
-
if (cached) return cached;
|
|
126
|
+
async function getClient2(connectionId) {
|
|
139
127
|
const connections = loadConnections();
|
|
140
|
-
const entry = connections[
|
|
128
|
+
const entry = connections[connectionId];
|
|
141
129
|
if (!entry) {
|
|
142
|
-
throw new Error(`
|
|
143
|
-
}
|
|
144
|
-
const resolvedType = connectorType ?? entry.connectorType;
|
|
145
|
-
if (!resolvedType) {
|
|
146
|
-
throw new Error(
|
|
147
|
-
`connector type could not be determined for slug '${connectorSlug}'. Specify connectorType in the data-source JSON or in .squadbase/connections.json.`
|
|
148
|
-
);
|
|
130
|
+
throw new Error(`connection '${connectionId}' not found in .squadbase/connections.json`);
|
|
149
131
|
}
|
|
150
|
-
|
|
151
|
-
|
|
132
|
+
const connectorSlug = entry.connector.slug;
|
|
133
|
+
const cached = clientCache.get(connectionId);
|
|
134
|
+
if (cached) return { client: cached, connectorSlug };
|
|
135
|
+
if (connectorSlug === "snowflake") {
|
|
136
|
+
return { client: createSnowflakeClient(entry, connectionId), connectorSlug };
|
|
152
137
|
}
|
|
153
|
-
if (
|
|
154
|
-
return createBigQueryClient(entry, connectorSlug
|
|
138
|
+
if (connectorSlug === "bigquery") {
|
|
139
|
+
return { client: createBigQueryClient(entry, connectionId), connectorSlug };
|
|
155
140
|
}
|
|
156
|
-
if (
|
|
141
|
+
if (connectorSlug === "postgresql" || connectorSlug === "squadbase-db") {
|
|
157
142
|
const urlEnvName = entry.envVars["connection-url"];
|
|
158
143
|
if (!urlEnvName) {
|
|
159
|
-
throw new Error(`'connection-url' is not defined in envVars for
|
|
144
|
+
throw new Error(`'connection-url' is not defined in envVars for connection '${connectionId}'`);
|
|
160
145
|
}
|
|
161
146
|
const connectionUrl = process.env[urlEnvName];
|
|
162
147
|
if (!connectionUrl) {
|
|
163
148
|
throw new Error(
|
|
164
|
-
`environment variable '${urlEnvName}' (mapped from
|
|
149
|
+
`environment variable '${urlEnvName}' (mapped from connection '${connectionId}') is not set`
|
|
165
150
|
);
|
|
166
151
|
}
|
|
167
152
|
const client = createPostgreSQLClient(connectionUrl);
|
|
168
|
-
clientCache.set(
|
|
169
|
-
return client;
|
|
153
|
+
clientCache.set(connectionId, client);
|
|
154
|
+
return { client, connectorSlug };
|
|
170
155
|
}
|
|
171
156
|
throw new Error(
|
|
172
|
-
`connector type '${
|
|
157
|
+
`connector type '${connectorSlug}' is not supported. Supported: "snowflake", "bigquery", "postgresql", "squadbase-db"`
|
|
173
158
|
);
|
|
174
159
|
}
|
|
175
160
|
function reloadEnvFile2(envPath) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@squadbase/vite-server",
|
|
3
|
-
"version": "0.0.1-build-
|
|
3
|
+
"version": "0.0.1-build-6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"types": "./dist/vite-plugin.d.ts"
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
|
-
"files": [
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
24
26
|
"publishConfig": {
|
|
25
27
|
"access": "public"
|
|
26
28
|
},
|