@malloydata/db-publisher 0.0.270-dev250429163414
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/client/api.d.ts +1537 -0
- package/dist/client/api.js +1572 -0
- package/dist/client/api.js.map +1 -0
- package/dist/client/base.d.ts +66 -0
- package/dist/client/base.js +69 -0
- package/dist/client/base.js.map +1 -0
- package/dist/client/common.d.ts +65 -0
- package/dist/client/common.js +147 -0
- package/dist/client/common.js.map +1 -0
- package/dist/client/configuration.d.ts +91 -0
- package/dist/client/configuration.js +50 -0
- package/dist/client/configuration.js.map +1 -0
- package/dist/client/index.d.ts +15 -0
- package/dist/client/index.js +32 -0
- package/dist/client/index.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/publisher_connection.d.ts +29 -0
- package/dist/publisher_connection.js +119 -0
- package/dist/publisher_connection.js.map +1 -0
- package/dist/publisher_connection.spec.d.ts +1 -0
- package/dist/publisher_connection.spec.js +806 -0
- package/dist/publisher_connection.spec.js.map +1 -0
- package/openapitools.json +7 -0
- package/package.json +32 -0
- package/publisher-api-doc.yaml +1026 -0
- package/src/client/.openapi-generator/FILES +9 -0
- package/src/client/.openapi-generator/VERSION +1 -0
- package/src/client/.openapi-generator-ignore +23 -0
- package/src/client/api.ts +2342 -0
- package/src/client/base.ts +86 -0
- package/src/client/common.ts +150 -0
- package/src/client/configuration.ts +115 -0
- package/src/client/git_push.sh +57 -0
- package/src/client/index.ts +19 -0
- package/src/index.ts +8 -0
- package/src/publisher_connection.spec.ts +1118 -0
- package/src/publisher_connection.ts +223 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
Connection,
|
|
10
|
+
MalloyQueryData,
|
|
11
|
+
PersistSQLResults,
|
|
12
|
+
PooledConnection,
|
|
13
|
+
QueryDataRow,
|
|
14
|
+
QueryRunStats,
|
|
15
|
+
StreamingConnection,
|
|
16
|
+
SQLSourceDef,
|
|
17
|
+
SQLSourceRequest,
|
|
18
|
+
TableSourceDef,
|
|
19
|
+
RunSQLOptions,
|
|
20
|
+
TestableConnection,
|
|
21
|
+
} from '@malloydata/malloy';
|
|
22
|
+
import {BaseConnection} from '@malloydata/malloy/connection';
|
|
23
|
+
import type {ConnectionAttributes, RawAxiosRequestConfig} from './client';
|
|
24
|
+
import {Configuration, ConnectionsApi} from './client';
|
|
25
|
+
|
|
26
|
+
interface PublisherConnectionOptions {
|
|
27
|
+
connectionUri: string;
|
|
28
|
+
accessToken?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class PublisherConnection
|
|
32
|
+
extends BaseConnection
|
|
33
|
+
implements
|
|
34
|
+
Connection,
|
|
35
|
+
StreamingConnection,
|
|
36
|
+
TestableConnection,
|
|
37
|
+
PersistSQLResults
|
|
38
|
+
{
|
|
39
|
+
public readonly name: string;
|
|
40
|
+
public readonly projectName: string;
|
|
41
|
+
private connectionsApi: ConnectionsApi;
|
|
42
|
+
private connectionAttributes: ConnectionAttributes;
|
|
43
|
+
private accessToken: string | undefined;
|
|
44
|
+
|
|
45
|
+
static async create(name: string, options: PublisherConnectionOptions) {
|
|
46
|
+
const url = new URL(options.connectionUri);
|
|
47
|
+
const urlParts = url.pathname.split('/');
|
|
48
|
+
if (urlParts.length !== 7) {
|
|
49
|
+
const fmt = '/api/v0/projects/{projectName}/connections/{connectionName}';
|
|
50
|
+
throw new Error(
|
|
51
|
+
`Invalid connection URI: ${options.connectionUri}. Expected format: ${fmt}`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
const apiTag = urlParts[1];
|
|
55
|
+
const versionTag = urlParts[2];
|
|
56
|
+
const projectName = urlParts[4];
|
|
57
|
+
const connectionName = urlParts[6];
|
|
58
|
+
|
|
59
|
+
if (name !== connectionName) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`Connection name mismatch: ${name} !== ${connectionName}. Connection name must match the URI path.`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const apiUrl = `${url.origin}/${apiTag}/${versionTag}`;
|
|
65
|
+
const configuration = new Configuration({
|
|
66
|
+
basePath: apiUrl,
|
|
67
|
+
});
|
|
68
|
+
const connectionsApi = new ConnectionsApi(configuration);
|
|
69
|
+
const response = await connectionsApi.getConnection(projectName, name, {
|
|
70
|
+
headers: PublisherConnection.getAuthHeaders(options.accessToken),
|
|
71
|
+
});
|
|
72
|
+
const connectionAttributes = response.data
|
|
73
|
+
.attributes as ConnectionAttributes;
|
|
74
|
+
const connection = new PublisherConnection(
|
|
75
|
+
name,
|
|
76
|
+
projectName,
|
|
77
|
+
connectionsApi,
|
|
78
|
+
connectionAttributes,
|
|
79
|
+
options.accessToken
|
|
80
|
+
);
|
|
81
|
+
await connection.test();
|
|
82
|
+
return connection;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private static getAuthHeaders(
|
|
86
|
+
accessToken: string | undefined
|
|
87
|
+
): RawAxiosRequestConfig['headers'] {
|
|
88
|
+
return {
|
|
89
|
+
...(accessToken && {Authorization: `Bearer ${accessToken}`}),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private constructor(
|
|
94
|
+
name: string,
|
|
95
|
+
projectName: string,
|
|
96
|
+
connectionsApi: ConnectionsApi,
|
|
97
|
+
connectionAttributes: ConnectionAttributes,
|
|
98
|
+
accessToken: string | undefined
|
|
99
|
+
) {
|
|
100
|
+
super();
|
|
101
|
+
this.name = name;
|
|
102
|
+
this.projectName = projectName;
|
|
103
|
+
this.connectionsApi = connectionsApi;
|
|
104
|
+
this.connectionAttributes = connectionAttributes;
|
|
105
|
+
this.accessToken = accessToken;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public get dialectName(): string {
|
|
109
|
+
return this.connectionAttributes.dialectName as string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public isPool(): this is PooledConnection {
|
|
113
|
+
return this.connectionAttributes.isPool as boolean;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public canPersist(): this is PersistSQLResults {
|
|
117
|
+
return this.connectionAttributes.canPersist as boolean;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public canStream(): this is StreamingConnection {
|
|
121
|
+
return this.connectionAttributes.canStream as boolean;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public async fetchTableSchema(
|
|
125
|
+
tableKey: string,
|
|
126
|
+
tablePath: string
|
|
127
|
+
): Promise<TableSourceDef> {
|
|
128
|
+
const response = await this.connectionsApi.getTablesource(
|
|
129
|
+
this.projectName,
|
|
130
|
+
this.name,
|
|
131
|
+
tableKey,
|
|
132
|
+
tablePath,
|
|
133
|
+
{
|
|
134
|
+
headers: PublisherConnection.getAuthHeaders(this.accessToken),
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
return JSON.parse(response.data.source as string) as TableSourceDef;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public async fetchSelectSchema(
|
|
141
|
+
sqlRef: SQLSourceRequest
|
|
142
|
+
): Promise<SQLSourceDef> {
|
|
143
|
+
const response = await this.connectionsApi.getSqlsource(
|
|
144
|
+
this.projectName,
|
|
145
|
+
this.name,
|
|
146
|
+
sqlRef.selectStr,
|
|
147
|
+
{
|
|
148
|
+
headers: PublisherConnection.getAuthHeaders(this.accessToken),
|
|
149
|
+
}
|
|
150
|
+
);
|
|
151
|
+
return JSON.parse(response.data.source as string) as SQLSourceDef;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public async estimateQueryCost(_sqlCommand: string): Promise<QueryRunStats> {
|
|
155
|
+
// Most connection types don't support cost estimation.
|
|
156
|
+
return {};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public async runSQL(
|
|
160
|
+
sql: string,
|
|
161
|
+
options: RunSQLOptions = {}
|
|
162
|
+
): Promise<MalloyQueryData> {
|
|
163
|
+
// TODO: Add support for abortSignal.
|
|
164
|
+
options.abortSignal = undefined;
|
|
165
|
+
const response = await this.connectionsApi.getQuerydata(
|
|
166
|
+
this.projectName,
|
|
167
|
+
this.name,
|
|
168
|
+
sql,
|
|
169
|
+
JSON.stringify(options),
|
|
170
|
+
{
|
|
171
|
+
headers: PublisherConnection.getAuthHeaders(this.accessToken),
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
return JSON.parse(response.data.data as string) as MalloyQueryData;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public async *runSQLStream(
|
|
178
|
+
sqlCommand: string,
|
|
179
|
+
options: RunSQLOptions = {}
|
|
180
|
+
): AsyncIterableIterator<QueryDataRow> {
|
|
181
|
+
// TODO: Add support for abortSignal.
|
|
182
|
+
options.abortSignal = undefined;
|
|
183
|
+
// TODO: Add real streaming support to publisher API.
|
|
184
|
+
const response = await this.connectionsApi.getQuerydata(
|
|
185
|
+
this.projectName,
|
|
186
|
+
this.name,
|
|
187
|
+
sqlCommand,
|
|
188
|
+
JSON.stringify(options),
|
|
189
|
+
{
|
|
190
|
+
headers: PublisherConnection.getAuthHeaders(this.accessToken),
|
|
191
|
+
}
|
|
192
|
+
);
|
|
193
|
+
const queryData = JSON.parse(
|
|
194
|
+
response.data.data as string
|
|
195
|
+
) as MalloyQueryData;
|
|
196
|
+
for (const row of queryData.rows) {
|
|
197
|
+
yield row;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public async test(): Promise<void> {
|
|
202
|
+
await this.connectionsApi.getTest(this.projectName, this.name, {
|
|
203
|
+
headers: PublisherConnection.getAuthHeaders(this.accessToken),
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
public async manifestTemporaryTable(sqlCommand: string): Promise<string> {
|
|
208
|
+
const response = await this.connectionsApi.getTemporarytable(
|
|
209
|
+
this.projectName,
|
|
210
|
+
this.name,
|
|
211
|
+
sqlCommand,
|
|
212
|
+
{
|
|
213
|
+
headers: PublisherConnection.getAuthHeaders(this.accessToken),
|
|
214
|
+
}
|
|
215
|
+
);
|
|
216
|
+
return response.data.table as string;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
public async close(): Promise<void> {
|
|
220
|
+
// Can't close the remote connection.
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
}
|