@deepnote/database-integrations 1.0.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.
package/README.md ADDED
@@ -0,0 +1,221 @@
1
+ # @deepnote/database-integrations
2
+
3
+ The database-integrations package defines the Deepnote database integrations.
4
+
5
+ ## Overview
6
+
7
+ This package provides TypeScript types and utilities for working with Deepnote SQL block integrations and other database integrations, including:
8
+
9
+ - **Integration types**
10
+ - **Authentication method types**
11
+ - **Integration metadata schemas**
12
+
13
+ ## Supported Integrations
14
+
15
+ - DataFrame SQL (`pandas-dataframe`)
16
+ - Amazon Athena (`athena`)
17
+ - Amazon Redshift (`redshift`)
18
+ - ClickHouse (`clickhouse`)
19
+ - Databricks (`databricks`)
20
+ - Dremio (`dremio`)
21
+ - Google AlloyDB (`alloydb`)
22
+ - Google BigQuery (`big-query`)
23
+ - Google Spanner (`spanner`)
24
+ - MariaDB (`mariadb`)
25
+ - Materialize (`materialize`)
26
+ - Microsoft SQL Server (Azure SQL) (`sql-server`)
27
+ - MindsDB (`mindsdb`)
28
+ - MongoDB (`mongodb`)
29
+ - MySQL (`mysql`)
30
+ - PostgreSQL (`pgsql`)
31
+ - Snowflake (`snowflake`)
32
+ - Trino (`trino`)
33
+
34
+ ## API reference
35
+
36
+ ### Integration types
37
+
38
+ - types
39
+ - `type DatabaseIntegrationType` – All supported database integration types
40
+ - `type DatabaseIntegrationTypeWithSslSupport` – Database integration types that support SSL (subset of `DatabaseIntegrationType`)
41
+ - `type SqlIntegrationType` – SQL integration types (subset of `DatabaseIntegrationType`), excludes `mongodb`
42
+ - runtime values
43
+ - `databaseIntegrationTypes` – Array of all supported database integration types (`DatabaseIntegrationType[]`)
44
+ - `databaseIntegrationTypesWithSslSupport` – Array of all database integration types that support SSL (`DatabaseIntegrationTypeWithSslSupport[]`)
45
+ - `sqlIntegrationTypes` – Array of all SQL integration types (`SqlIntegrationType[]`)
46
+
47
+ ### Integration metadata
48
+
49
+ - zod schemas
50
+ - `databaseMetadataSchemasByType` – Zod schemas for validating integration metadata by integration type
51
+ ```ts
52
+ databaseMetadataSchemasByType['big-query'].safeParse({ ... })
53
+ ```
54
+ - types
55
+ - `type DatabaseIntegrationMetadataByType` – Integration metadata types by integration type
56
+ ```ts
57
+ const metadata: DatabaseIntegrationMetadataByType['big-query'] = { ... }
58
+ ```
59
+
60
+ ### Environment variables and configuration
61
+
62
+ - utilities
63
+ - `getIntegrationListEnv(integrations: Array<DatabaseIntegrationConfig>, params: { projectRootDirectory: string, snowflakePartnerIdentifier?: string })` – Creates a list of environment variables to be set when executing a notebook that uses the integrations, the variables are in a format consumed by the Deepnote Toolkit
64
+ - arguments:
65
+ - `integrations` – list of active integrations
66
+ - `params.projectRootDirectory` – project root directory, used to construct paths to CA certificates
67
+ - `params.snowflakePartnerIdentifier` (optional) – Snowflake partner identifier, used to construct Snowflake connection URLs; it is used for Snowflake's internal telemetry/analytics, it does not affect the functionality of the integration
68
+ - returns:
69
+ - `envVars: Array<EnvVar>` – List of environment variables to be set when executing a notebook that uses the integrations
70
+ - The variable names produced have the following format:
71
+ - `*INTEGRATION_NAME*_*METADATA_KEY*` - for each metadata field of the integration
72
+ - `SQL_*INTEGRATION_ID*` - SQL Alchemy config for each SQL integration
73
+ - `errors: Array<Error>` – List of errors that occurred when generating the environment variables. These are not thrown to allow partial functionality, even when some integrations are misconfigured.
74
+ - `getSqlAlchemyInput(sqlIntegration: SqlIntegrationConfig, params: { projectRootDirectory: string, snowflakePartnerIdentifier?: string })` – Creates a SQL Alchemy config for a SQL integration, used to connect to the database from SQL blocks. This is used internally within `getIntegrationListEnv()`.
75
+ - arguments:
76
+ - `sqlIntegration` – SQL integration configuration
77
+ - `params.projectRootDirectory` – project root directory, used to construct paths to CA certificates
78
+ - `params.snowflakePartnerIdentifier` (optional) – see above
79
+ - returns:
80
+ - `SqlAlchemyInput | null` – SQL Alchemy config for the integration, `null` when the integration has federated authentication
81
+ - throws:
82
+ - `BigQueryServiceAccountParseError` – when parsing BigQuery service account fails
83
+ - `SpannerServiceAccountParseError` – when parsing Spanner service account fails
84
+ - types
85
+ - `type DatabaseIntegrationConfig` – Configuration for an integration when generating environment variables
86
+ - `type` – integration type
87
+ - `id` – integration ID (usually UUID)
88
+ - `name` – integration name
89
+ - `metadata` – integration metadata
90
+ - `federated_auth_method` (optional) – federated authentication method, used only when the integration has federated authentication enabled. The auth method configured via the metadata is also checked, these should match.
91
+ - `type SqlIntegrationConfig` – Configuration for a SQL integration when generating environment variables (subset of `DatabaseIntegrationConfig`, excluding non-SQL integrations)
92
+ - `type SqlAlchemyInput` - SQL Alchemy config for a SQL integration
93
+ - errors
94
+ - `BigQueryServiceAccountParseError` – When parsing BigQuery service account fails
95
+ - `cause: Error` – The original error
96
+ - `SpannerServiceAccountParseError` – When parsing Spanner service account fails
97
+ - `cause: Error` – The original error
98
+
99
+ ### Auth methods
100
+
101
+ These values can be used to configure the integrations that have multiple methods for authentication.
102
+
103
+ - types
104
+ - `type DatabaseAuthMethod` – Authentication method for a database integration
105
+ - `type AwsAuthMethod` – Authentication method for an AWS integration
106
+ - `type BigQueryAuthMethod` – Authentication method for a BigQuery integration
107
+ - `type SnowflakeAuthMethod` – Authentication method for a Snowflake integration
108
+ - `type FederatedAuthMethod` – Authentication method for a federated authentication integration
109
+ - runtime values
110
+ - `AwsAuthMethods` – All supported AWS authentication methods
111
+ - `BigQueryAuthMethods` – All supported BigQuery authentication methods
112
+ - `DatabaseAuthMethods` – All supported database authentication methods
113
+ - `SnowflakeAuthMethods` – All supported Snowflake authentication methods
114
+ - `federatedAuthMethods` – All supported federated authentication methods
115
+
116
+ ## Usage in TypeScript
117
+
118
+ ### Example: Show list of available integrations
119
+
120
+ ```ts
121
+ import { databaseIntegrationTypes } from "@deepnote/database-integrations";
122
+
123
+ console.log(databaseIntegrationTypes); // ['athena', 'redshift', …]
124
+ ```
125
+
126
+ ### Example: Validate integration metadata
127
+
128
+ ```ts
129
+ import { databaseMetadataSchemasByType } from "@deepnote/database-integrations";
130
+
131
+ const validationResult = databaseMetadataSchemasByType["big-query"].safeParse({
132
+ authMethod: BigQueryAuthMethods.ServiceAccount,
133
+ service_account: "my-account",
134
+ });
135
+ if (validationResult.error) {
136
+ console.error(validationResult.error);
137
+ }
138
+ ```
139
+
140
+ ### Example: Getting environment variables for configured integrations
141
+
142
+ ```ts
143
+ import { getIntegrationListEnv } from "@deepnote/database-integrations";
144
+
145
+ const integrations = [
146
+ {
147
+ type: "pgsql",
148
+ id: "my-postgres",
149
+ name: "My PostgreSQL Integration",
150
+ metadata: {
151
+ host: "my-host",
152
+ user: "my-user",
153
+ password: "my-password",
154
+ database: "my-database",
155
+ },
156
+ },
157
+ ];
158
+
159
+ const { envVars } = getIntegrationListEnv(integrations, {
160
+ projectRootDirectory: "/path/to/project",
161
+ });
162
+
163
+ // envVars = [
164
+ // { name: "INTEGRATION_MY_POSTGRES_HOST", value: "my-host" },
165
+ // { name: "INTEGRATION_MY_POSTGRES_USER", value: "my-user" },
166
+ // { name: "INTEGRATION_MY_POSTGRES_PASSWORD", value: "my-password" },
167
+ // { name: "INTEGRATION_MY_POSTGRES_DATABASE", value: "my-database" },
168
+ // { name: "SQL_MY_POSTGRES", value: '{"url": "postgresql://my-user:my-password@my-host/my-database", "params": {}, "param_style": "pyformat"}' },
169
+ // ]
170
+ ```
171
+
172
+ ### Example: Mapped types
173
+
174
+ ```ts
175
+ import {
176
+ type DatabaseIntegrationMetadataByType,
177
+ type DatabaseIntegrationType,
178
+ } from "@deepnote/database-integrations";
179
+
180
+ const myIntegrationDocsLinks: Record<
181
+ DatabaseIntegrationType,
182
+ { docsLink: string }
183
+ > = {
184
+ "big-query": { docsLink: "https://example.com/my-docs/bq" },
185
+ // …
186
+ };
187
+ ```
188
+
189
+ ## Usage in notebooks
190
+
191
+ SQL block execution (set up in `@deepnote/blocks`) picks the environment variables and passes them to the Deepnote Toolkit (`_dntk.execute_sql()`)
192
+
193
+ ### MongoDB usage
194
+
195
+ MongoDB is not a SQL integration, it is not used via SQL blocks but rather via code blocks and `pymongo`.
196
+
197
+ ```python
198
+ import os
199
+ from pymongo import MongoClient
200
+
201
+ # Connect to MongoDB using the connection string from environment variables
202
+ client = MongoClient(os.environ["INTEGRATION_NAME_CONNECTION_STRING"])
203
+ db = client[os.environ["INTEGRATION_NAME_DATABASE"]]
204
+ # Get the users collection
205
+ users_collection = db["users"]
206
+ # Find all documents in the users collection
207
+ users = list(users_collection.find())
208
+ ```
209
+
210
+ Note: The `INTEGRATION_NAME_` prefix of the env variables is constructed using the following logic.
211
+
212
+ > Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '\_' (underscore) from the characters defined in the Portable Character Set and do not begin with a digit.
213
+
214
+ ```python
215
+ import re
216
+
217
+ def convert_to_environment_variable_name(integration_name: str) -> str:
218
+ without_first_digit = f"_{integration_name}" if re.match(r'^\d', integration_name) else integration_name
219
+ upper_cased = without_first_digit.upper()
220
+ return re.sub(r'[^\w]', '_', upper_cased)
221
+ ```