@quillsql/node 0.6.8 → 0.6.10
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/{assets → cjs/assets}/pgtypes.js +1 -0
- package/dist/cjs/assets/pgtypes.js.map +1 -0
- package/dist/{clients → cjs/clients}/QuillServerClient.js +1 -0
- package/dist/cjs/clients/QuillServerClient.js.map +1 -0
- package/dist/{db → cjs/db}/BigQuery.js +1 -0
- package/dist/cjs/db/BigQuery.js.map +1 -0
- package/dist/{db → cjs/db}/CachedConnection.d.ts +1 -1
- package/dist/{db → cjs/db}/CachedConnection.js +2 -0
- package/dist/cjs/db/CachedConnection.js.map +1 -0
- package/dist/{db → cjs/db}/DatabaseHelper.js +1 -0
- package/dist/cjs/db/DatabaseHelper.js.map +1 -0
- package/dist/{db → cjs/db}/Mysql.js +1 -0
- package/dist/cjs/db/Mysql.js.map +1 -0
- package/dist/{db → cjs/db}/Postgres.js +5 -0
- package/dist/cjs/db/Postgres.js.map +1 -0
- package/dist/{db → cjs/db}/Snowflake.js +1 -0
- package/dist/cjs/db/Snowflake.js.map +1 -0
- package/dist/{index.ispec.js → cjs/index.ispec.js} +1 -0
- package/dist/cjs/index.ispec.js.map +1 -0
- package/dist/{index.js → cjs/index.js} +4 -1
- package/dist/cjs/index.js.map +1 -0
- package/dist/{index.uspec.js → cjs/index.uspec.js} +1 -0
- package/dist/cjs/index.uspec.js.map +1 -0
- package/dist/{models → cjs/models}/Cache.js +1 -0
- package/dist/cjs/models/Cache.js.map +1 -0
- package/dist/{models → cjs/models}/Client.js +1 -0
- package/dist/cjs/models/Client.js.map +1 -0
- package/dist/{models → cjs/models}/Database.js +1 -0
- package/dist/cjs/models/Database.js.map +1 -0
- package/dist/cjs/models/Filters.d.ts +83 -0
- package/dist/cjs/models/Filters.js +178 -0
- package/dist/cjs/models/Filters.js.map +1 -0
- package/dist/{models → cjs/models}/Formats.js +1 -0
- package/dist/cjs/models/Formats.js.map +1 -0
- package/dist/{models → cjs/models}/Quill.d.ts +12 -7
- package/dist/{models → cjs/models}/Quill.js +1 -0
- package/dist/cjs/models/Quill.js.map +1 -0
- package/dist/{utils → cjs/utils}/Error.js +1 -0
- package/dist/cjs/utils/Error.js.map +1 -0
- package/dist/{utils → cjs/utils}/RunQueryProcesses.js +1 -0
- package/dist/cjs/utils/RunQueryProcesses.js.map +1 -0
- package/dist/{utils → cjs/utils}/schemaConversion.js +1 -0
- package/dist/cjs/utils/schemaConversion.js.map +1 -0
- package/dist/{utils → cjs/utils}/textProcessing.js +1 -0
- package/dist/cjs/utils/textProcessing.js.map +1 -0
- package/dist/esm/assets/pgtypes.d.ts +4 -0
- package/dist/esm/assets/pgtypes.js +2783 -0
- package/dist/esm/assets/pgtypes.js.map +1 -0
- package/dist/esm/clients/QuillServerClient.d.ts +8 -0
- package/dist/esm/clients/QuillServerClient.js +24 -0
- package/dist/esm/clients/QuillServerClient.js.map +1 -0
- package/dist/esm/db/BigQuery.d.ts +27 -0
- package/dist/esm/db/BigQuery.js +201 -0
- package/dist/esm/db/BigQuery.js.map +1 -0
- package/dist/esm/db/CachedConnection.d.ts +23 -0
- package/dist/esm/db/CachedConnection.js +81 -0
- package/dist/esm/db/CachedConnection.js.map +1 -0
- package/dist/esm/db/DatabaseHelper.d.ts +50 -0
- package/dist/esm/db/DatabaseHelper.js +188 -0
- package/dist/esm/db/DatabaseHelper.js.map +1 -0
- package/dist/esm/db/Mysql.d.ts +30 -0
- package/dist/esm/db/Mysql.js +219 -0
- package/dist/esm/db/Mysql.js.map +1 -0
- package/dist/esm/db/Postgres.d.ts +32 -0
- package/dist/esm/db/Postgres.js +161 -0
- package/dist/esm/db/Postgres.js.map +1 -0
- package/dist/esm/db/Snowflake.d.ts +33 -0
- package/dist/esm/db/Snowflake.js +203 -0
- package/dist/esm/db/Snowflake.js.map +1 -0
- package/dist/esm/index.d.ts +41 -0
- package/dist/esm/index.ispec.d.ts +0 -0
- package/{src/index.ispec.ts → dist/esm/index.ispec.js} +2 -6
- package/dist/esm/index.ispec.js.map +1 -0
- package/dist/esm/index.js +267 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.uspec.d.ts +0 -0
- package/{src/index.uspec.ts → dist/esm/index.uspec.js} +2 -6
- package/dist/esm/index.uspec.js.map +1 -0
- package/dist/esm/models/Cache.d.ts +12 -0
- package/dist/esm/models/Cache.js +2 -0
- package/dist/esm/models/Cache.js.map +1 -0
- package/dist/esm/models/Client.d.ts +29 -0
- package/dist/esm/models/Client.js +2 -0
- package/dist/esm/models/Client.js.map +1 -0
- package/dist/esm/models/Database.d.ts +5 -0
- package/dist/esm/models/Database.js +2 -0
- package/dist/esm/models/Database.js.map +1 -0
- package/dist/esm/models/Filters.d.ts +83 -0
- package/dist/esm/models/Filters.js +174 -0
- package/dist/esm/models/Filters.js.map +1 -0
- package/dist/esm/models/Formats.d.ts +7 -0
- package/dist/esm/models/Formats.js +2 -0
- package/dist/esm/models/Formats.js.map +1 -0
- package/dist/esm/models/Quill.d.ts +82 -0
- package/dist/esm/models/Quill.js +2 -0
- package/dist/esm/models/Quill.js.map +1 -0
- package/dist/esm/utils/Error.d.ts +8 -0
- package/dist/esm/utils/Error.js +23 -0
- package/dist/esm/utils/Error.js.map +1 -0
- package/dist/esm/utils/RunQueryProcesses.d.ts +9 -0
- package/dist/esm/utils/RunQueryProcesses.js +30 -0
- package/dist/esm/utils/RunQueryProcesses.js.map +1 -0
- package/dist/esm/utils/schemaConversion.d.ts +1 -0
- package/dist/esm/utils/schemaConversion.js +12 -0
- package/dist/esm/utils/schemaConversion.js.map +1 -0
- package/dist/esm/utils/textProcessing.d.ts +2 -0
- package/dist/esm/utils/textProcessing.js +13 -0
- package/dist/esm/utils/textProcessing.js.map +1 -0
- package/package.json +11 -6
- package/eslint.config.mjs +0 -16
- package/examples/mysql-node/app.ts +0 -61
- package/examples/node-server/app.ts +0 -65
- package/jest.config.js +0 -19
- package/src/assets/pgtypes.ts +0 -2782
- package/src/clients/QuillServerClient.ts +0 -23
- package/src/db/BigQuery.ts +0 -220
- package/src/db/CachedConnection.ts +0 -109
- package/src/db/DatabaseHelper.ts +0 -373
- package/src/db/Mysql.ts +0 -262
- package/src/db/Postgres.ts +0 -185
- package/src/db/Snowflake.ts +0 -240
- package/src/index.ts +0 -392
- package/src/models/Cache.ts +0 -18
- package/src/models/Client.ts +0 -29
- package/src/models/Database.ts +0 -5
- package/src/models/Formats.ts +0 -19
- package/src/models/Quill.ts +0 -82
- package/src/utils/Error.ts +0 -35
- package/src/utils/RunQueryProcesses.ts +0 -33
- package/src/utils/schemaConversion.ts +0 -11
- package/src/utils/textProcessing.ts +0 -13
- package/tsconfig.json +0 -15
- /package/dist/{assets → cjs/assets}/pgtypes.d.ts +0 -0
- /package/dist/{clients → cjs/clients}/QuillServerClient.d.ts +0 -0
- /package/dist/{db → cjs/db}/BigQuery.d.ts +0 -0
- /package/dist/{db → cjs/db}/DatabaseHelper.d.ts +0 -0
- /package/dist/{db → cjs/db}/Mysql.d.ts +0 -0
- /package/dist/{db → cjs/db}/Postgres.d.ts +0 -0
- /package/dist/{db → cjs/db}/Snowflake.d.ts +0 -0
- /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/dist/{index.ispec.d.ts → cjs/index.ispec.d.ts} +0 -0
- /package/dist/{index.uspec.d.ts → cjs/index.uspec.d.ts} +0 -0
- /package/dist/{models → cjs/models}/Cache.d.ts +0 -0
- /package/dist/{models → cjs/models}/Client.d.ts +0 -0
- /package/dist/{models → cjs/models}/Database.d.ts +0 -0
- /package/dist/{models → cjs/models}/Formats.d.ts +0 -0
- /package/dist/{utils → cjs/utils}/Error.d.ts +0 -0
- /package/dist/{utils → cjs/utils}/RunQueryProcesses.d.ts +0 -0
- /package/dist/{utils → cjs/utils}/schemaConversion.d.ts +0 -0
- /package/dist/{utils → cjs/utils}/textProcessing.d.ts +0 -0
package/src/db/Postgres.ts
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { Pool } from "pg";
|
|
2
|
-
// import { Client } from "../models/Client";
|
|
3
|
-
import { QuillQueryResults } from "./DatabaseHelper";
|
|
4
|
-
import { capitalize, depluralize } from "../utils/textProcessing";
|
|
5
|
-
import { PG_TYPES } from "../assets/pgtypes";
|
|
6
|
-
|
|
7
|
-
export type PostgresConnectionConfig = {
|
|
8
|
-
connectionString: string;
|
|
9
|
-
ssl?: {
|
|
10
|
-
rejectUnauthorized: false;
|
|
11
|
-
ca?: string;
|
|
12
|
-
key?: string;
|
|
13
|
-
cert?: string;
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export function connectToPostgres(config: PostgresConnectionConfig): Pool {
|
|
18
|
-
return new Pool(config);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function disconnectFromPostgres(pool: Pool) {
|
|
22
|
-
pool.end();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export async function runQueryPostgres(
|
|
26
|
-
sql: string,
|
|
27
|
-
pool: Pool,
|
|
28
|
-
): Promise<QuillQueryResults> {
|
|
29
|
-
const results = await pool.query(sql);
|
|
30
|
-
return {
|
|
31
|
-
fields: results.fields.map((field: any) => ({
|
|
32
|
-
name: field.name,
|
|
33
|
-
dataTypeID: field.dataTypeID,
|
|
34
|
-
})),
|
|
35
|
-
rows: results.rows,
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export async function getSchemasPostgres(pool: Pool): Promise<string[]> {
|
|
40
|
-
const sql = `SELECT schema_name FROM information_schema.schemata
|
|
41
|
-
WHERE schema_name NOT LIKE 'pg_%' AND schema_name != 'information_schema';`;
|
|
42
|
-
const results = await runQueryPostgres(sql, pool);
|
|
43
|
-
return results.rows.map((row) => row.schema_name);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export async function getTablesBySchemaPostgres(
|
|
47
|
-
pool: Pool,
|
|
48
|
-
schemaNames: string[],
|
|
49
|
-
): Promise<{ tableName: string; schemaName: string }[]> {
|
|
50
|
-
const allColumns = await Promise.all(
|
|
51
|
-
schemaNames.map(async (schema) => {
|
|
52
|
-
const sql = `SELECT table_name, table_schema FROM information_schema.tables WHERE table_schema = '${schema}'`;
|
|
53
|
-
const results = await runQueryPostgres(sql, pool);
|
|
54
|
-
return results.rows.map((row) => {
|
|
55
|
-
return { tableName: row.table_name, schemaName: row.table_schema };
|
|
56
|
-
});
|
|
57
|
-
}),
|
|
58
|
-
);
|
|
59
|
-
return allColumns.flat();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export async function getColumnsByTablePostgres(
|
|
63
|
-
pool: Pool,
|
|
64
|
-
schemaName: string,
|
|
65
|
-
tableName: string,
|
|
66
|
-
): Promise<string[]> {
|
|
67
|
-
const sql = `SELECT column_name FROM information_schema.columns WHERE table_schema = '${schemaName}' and table_name = '${tableName}'`;
|
|
68
|
-
const results = await runQueryPostgres(sql, pool);
|
|
69
|
-
return results.rows.map((row) => row.column_name);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export async function getForeignKeysPostgres(
|
|
73
|
-
pool: Pool,
|
|
74
|
-
schemaName: string,
|
|
75
|
-
tableName: string,
|
|
76
|
-
primaryKey: string,
|
|
77
|
-
): Promise<string[]> {
|
|
78
|
-
const depluralizedTableName = depluralize(tableName);
|
|
79
|
-
let sql = `SELECT column_name FROM information_schema.columns
|
|
80
|
-
WHERE table_schema = '${schemaName}'
|
|
81
|
-
and table_name != '${tableName}'
|
|
82
|
-
and (column_name = '${primaryKey}'
|
|
83
|
-
or column_name = '${depluralizedTableName}_${primaryKey}'
|
|
84
|
-
or column_name = '${depluralizedTableName}${capitalize(primaryKey)}')`;
|
|
85
|
-
const results = await runQueryPostgres(sql, pool);
|
|
86
|
-
let foreignKeysString = results.rows.map((key) => {
|
|
87
|
-
return key.column_name;
|
|
88
|
-
});
|
|
89
|
-
foreignKeysString = foreignKeysString.filter(
|
|
90
|
-
(key) => key !== "id" && key !== "_id_",
|
|
91
|
-
);
|
|
92
|
-
foreignKeysString = [...new Set(foreignKeysString)];
|
|
93
|
-
if (foreignKeysString.length === 0) {
|
|
94
|
-
sql = `SELECT column_name FROM information_schema.columns
|
|
95
|
-
WHERE table_schema = '${schemaName}'
|
|
96
|
-
and table_name != '${tableName}'
|
|
97
|
-
and (column_name like '${tableName}%'
|
|
98
|
-
or column_name like '%\\_id'
|
|
99
|
-
or column_name like '%Id'
|
|
100
|
-
or column_name like '%\\_${primaryKey}'
|
|
101
|
-
or column_name like '%${capitalize(primaryKey)}')`;
|
|
102
|
-
const results = await runQueryPostgres(sql, pool);
|
|
103
|
-
foreignKeysString = results.rows.map((key) => {
|
|
104
|
-
return key.column_name;
|
|
105
|
-
});
|
|
106
|
-
foreignKeysString = [...new Set(foreignKeysString)];
|
|
107
|
-
}
|
|
108
|
-
return foreignKeysString;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export async function getSchemaColumnInfoPostgress(
|
|
112
|
-
pool: Pool,
|
|
113
|
-
schemaName: string,
|
|
114
|
-
tableNames: { tableName: string; schemaName: string }[],
|
|
115
|
-
): Promise<
|
|
116
|
-
{ tableName: string; columns: { columnName: string; dataTypeID: number }[] }[]
|
|
117
|
-
> {
|
|
118
|
-
const allColumns = await Promise.all(
|
|
119
|
-
tableNames.map(async (tableName) => {
|
|
120
|
-
const query = `
|
|
121
|
-
SELECT column_name as "columnName", udt_name as "fieldType"
|
|
122
|
-
FROM information_schema.columns
|
|
123
|
-
WHERE table_schema = '${tableName.schemaName}'
|
|
124
|
-
AND table_name = '${tableName.tableName}'
|
|
125
|
-
ORDER BY ordinal_position;
|
|
126
|
-
`;
|
|
127
|
-
const results = await runQueryPostgres(query, pool);
|
|
128
|
-
return {
|
|
129
|
-
tableName: `${tableName.schemaName}.${tableName.tableName}`,
|
|
130
|
-
displayName: `${tableName.schemaName}.${tableName.tableName}`,
|
|
131
|
-
columns: results.rows.map((row) => {
|
|
132
|
-
let pgType = PG_TYPES.find((pgType) => {
|
|
133
|
-
return pgType.typname === row.fieldType;
|
|
134
|
-
})?.oid;
|
|
135
|
-
if (!pgType) {
|
|
136
|
-
pgType = 1043;
|
|
137
|
-
}
|
|
138
|
-
return {
|
|
139
|
-
columnName: row.columnName,
|
|
140
|
-
displayName: row.columnName,
|
|
141
|
-
dataTypeID: pgType,
|
|
142
|
-
fieldType: row.fieldType,
|
|
143
|
-
};
|
|
144
|
-
}),
|
|
145
|
-
};
|
|
146
|
-
}),
|
|
147
|
-
);
|
|
148
|
-
return allColumns;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export function formatPostgresConfig(
|
|
152
|
-
connectionString: string,
|
|
153
|
-
): PostgresConnectionConfig {
|
|
154
|
-
return { connectionString, ssl: { rejectUnauthorized: false } };
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// CURRENTLY UNUSED BUT MAYBE USEFUL IN THE FUTURE
|
|
158
|
-
// function getSslConfig(client: Client):
|
|
159
|
-
// | {
|
|
160
|
-
// rejectUnauthorized: false;
|
|
161
|
-
// ca?: string;
|
|
162
|
-
// key?: string;
|
|
163
|
-
// cert?: string;
|
|
164
|
-
// }
|
|
165
|
-
// | undefined {
|
|
166
|
-
// if (!client.useSsl) {
|
|
167
|
-
// return undefined;
|
|
168
|
-
// }
|
|
169
|
-
// if (client.serverCa && client.clientKey && client.clientCert) {
|
|
170
|
-
// return {
|
|
171
|
-
// rejectUnauthorized: false,
|
|
172
|
-
// ca: client.serverCa,
|
|
173
|
-
// key: client.clientKey,
|
|
174
|
-
// cert: client.clientCert,
|
|
175
|
-
// };
|
|
176
|
-
// }
|
|
177
|
-
// if (client.serverCa) {
|
|
178
|
-
// return {
|
|
179
|
-
// rejectUnauthorized: false,
|
|
180
|
-
// ca: client.serverCa,
|
|
181
|
-
// };
|
|
182
|
-
// }
|
|
183
|
-
// // if using ssl with no certificates
|
|
184
|
-
// return { rejectUnauthorized: false };
|
|
185
|
-
// }
|
package/src/db/Snowflake.ts
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import snowflake from "snowflake-sdk";
|
|
2
|
-
import { QuillQueryResults } from "./DatabaseHelper";
|
|
3
|
-
import { capitalize, depluralize } from "../utils/textProcessing";
|
|
4
|
-
|
|
5
|
-
const POSTGRES_SNOWFLAKE_MAP: { [type: string]: number } = {
|
|
6
|
-
// Numeric Types
|
|
7
|
-
NUMBER: 1700, // NUMERIC
|
|
8
|
-
DECIMAL: 1700, // NUMERIC
|
|
9
|
-
NUMERIC: 1700, // NUMERIC
|
|
10
|
-
INT: 23, // INTEGER
|
|
11
|
-
INTEGER: 23, // INTEGER
|
|
12
|
-
BIGINT: 20, // BIGINT
|
|
13
|
-
SMALLINT: 21, // SMALLINT
|
|
14
|
-
TINYINT: 21, // SMALLINT (PostgreSQL doesn't have TINYINT)
|
|
15
|
-
BYTEINT: 21, // SMALLINT (PostgreSQL doesn't have BYTEINT)
|
|
16
|
-
FLOAT: 701, // DOUBLE PRECISION
|
|
17
|
-
FLOAT4: 700, // REAL
|
|
18
|
-
FLOAT8: 701, // DOUBLE PRECISION
|
|
19
|
-
DOUBLE: 701, // DOUBLE PRECISION
|
|
20
|
-
REAL: 700, // REAL
|
|
21
|
-
|
|
22
|
-
// Boolean Type
|
|
23
|
-
BOOLEAN: 16, // BOOLEAN
|
|
24
|
-
|
|
25
|
-
// String Types
|
|
26
|
-
VARCHAR: 1043, // VARCHAR
|
|
27
|
-
CHAR: 1042, // CHAR
|
|
28
|
-
CHARACTER: 1042, // CHAR
|
|
29
|
-
STRING: 25, // TEXT
|
|
30
|
-
TEXT: 25, // TEXT
|
|
31
|
-
|
|
32
|
-
// Binary Types
|
|
33
|
-
BINARY: 17, // BYTEA
|
|
34
|
-
VARBINARY: 17, // BYTEA
|
|
35
|
-
|
|
36
|
-
// Date and Time Types
|
|
37
|
-
DATE: 1082, // DATE
|
|
38
|
-
DATETIME: 1184, // TIMESTAMP
|
|
39
|
-
TIME: 1083, // TIME
|
|
40
|
-
TIMESTAMP: 1184, // TIMESTAMP
|
|
41
|
-
TIMESTAMP_LTZ: 1184, // TIMESTAMP WITH TIME ZONE
|
|
42
|
-
TIMESTAMP_NTZ: 1114, // TIMESTAMP WITHOUT TIME ZONE
|
|
43
|
-
TIMESTAMP_TZ: 1184, // TIMESTAMP WITH TIME ZONE
|
|
44
|
-
|
|
45
|
-
// Semi-structured Data Types
|
|
46
|
-
VARIANT: 3802, // JSONB
|
|
47
|
-
OBJECT: 3802, // JSONB
|
|
48
|
-
ARRAY: 2277, // JSON[] (closest equivalent)
|
|
49
|
-
|
|
50
|
-
// Geospatial Type
|
|
51
|
-
GEOGRAPHY: 25, // TEXT (PostgreSQL doesn't have a direct equivalent)
|
|
52
|
-
GEOMETRY: 25, // TEXT (PostgreSQL doesn't have a direct equivalent)
|
|
53
|
-
|
|
54
|
-
// Vector Type
|
|
55
|
-
VECTOR: 3802, // JSONB (closest equivalent without extensions)
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export type SnowflakeConnectionConfig = {
|
|
59
|
-
account: string;
|
|
60
|
-
username: string;
|
|
61
|
-
password: string;
|
|
62
|
-
database: string;
|
|
63
|
-
warehouse: string;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
export async function runQuerySnowflake(
|
|
67
|
-
sql: string,
|
|
68
|
-
connection: snowflake.Connection,
|
|
69
|
-
): Promise<QuillQueryResults> {
|
|
70
|
-
const results = await new Promise((resolve, reject) => {
|
|
71
|
-
connection.execute({
|
|
72
|
-
sqlText: sql,
|
|
73
|
-
complete: (err, stmt, rows) => {
|
|
74
|
-
if (err) {
|
|
75
|
-
reject(err);
|
|
76
|
-
return { success: false, message: err.message };
|
|
77
|
-
} else {
|
|
78
|
-
resolve({
|
|
79
|
-
rows,
|
|
80
|
-
fields: stmt.getColumns().map((col) => ({
|
|
81
|
-
name: col.getName(),
|
|
82
|
-
dataTypeID: POSTGRES_SNOWFLAKE_MAP[col.getType().toUpperCase()],
|
|
83
|
-
})),
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
return results as QuillQueryResults;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export async function getSchemasSnowflake(
|
|
93
|
-
connection: snowflake.Connection,
|
|
94
|
-
): Promise<string[]> {
|
|
95
|
-
const sql = `SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
|
|
96
|
-
WHERE SCHEMA_NAME != 'INFORMATION_SCHEMA'`;
|
|
97
|
-
const results = await runQuerySnowflake(sql, connection);
|
|
98
|
-
return results.rows.map((row) => row.SCHEMA_NAME);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export async function getTablesBySchemaSnowflake(
|
|
102
|
-
connection: snowflake.Connection,
|
|
103
|
-
schemaNames: string[],
|
|
104
|
-
): Promise<{ tableName: string; schemaName: string }[]> {
|
|
105
|
-
const allColumns = await Promise.all(
|
|
106
|
-
schemaNames.map(async (schema) => {
|
|
107
|
-
const query = `SELECT
|
|
108
|
-
TABLE_NAME as "tableName",
|
|
109
|
-
TABLE_SCHEMA as "schemaName"
|
|
110
|
-
FROM INFORMATION_SCHEMA.TABLES
|
|
111
|
-
WHERE TABLE_SCHEMA = '${schema}';
|
|
112
|
-
`;
|
|
113
|
-
const results = await runQuerySnowflake(query, connection);
|
|
114
|
-
return results.rows.map((row) => {
|
|
115
|
-
return { tableName: row.tableName, schemaName: row.schemaName };
|
|
116
|
-
});
|
|
117
|
-
}),
|
|
118
|
-
);
|
|
119
|
-
return allColumns.flat();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export async function getColumnsByTableSnowflake(
|
|
123
|
-
connection: snowflake.Connection,
|
|
124
|
-
schemaName: string,
|
|
125
|
-
tableName: string,
|
|
126
|
-
): Promise<string[]> {
|
|
127
|
-
const sql = `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${schemaName}' AND TABLE_NAME = '${tableName}'`;
|
|
128
|
-
const results = await runQuerySnowflake(sql, connection);
|
|
129
|
-
return results.rows.map((row) => row.COLUMN_NAME);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function formatSnowflakeConfig(
|
|
133
|
-
connectionString: string,
|
|
134
|
-
): SnowflakeConnectionConfig {
|
|
135
|
-
const parsed = new URL(connectionString);
|
|
136
|
-
return {
|
|
137
|
-
account: parsed.hostname,
|
|
138
|
-
username: parsed.username,
|
|
139
|
-
password: parsed.password,
|
|
140
|
-
database: parsed.pathname.split("/")[1],
|
|
141
|
-
warehouse: parsed.pathname.split("/")[2],
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export function connectToSnowflake(
|
|
146
|
-
config: SnowflakeConnectionConfig,
|
|
147
|
-
): snowflake.Connection {
|
|
148
|
-
const connection = snowflake.createConnection({
|
|
149
|
-
...config,
|
|
150
|
-
clientSessionKeepAlive: true,
|
|
151
|
-
});
|
|
152
|
-
connection.connect((err) => {
|
|
153
|
-
if (err) {
|
|
154
|
-
console.error(`Failed to connect to Snowflake: ${err.message}`);
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
return connection;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export async function disconnectFromSnowflake(
|
|
161
|
-
connection: snowflake.Connection,
|
|
162
|
-
) {
|
|
163
|
-
connection.destroy((err) => {
|
|
164
|
-
if (err) {
|
|
165
|
-
console.error(`Failed to disconnect from Snowflake: ${err.message}`);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export async function getForeignKeysSnowflake(
|
|
171
|
-
connection: snowflake.Connection,
|
|
172
|
-
schemaName: string,
|
|
173
|
-
tableName: string,
|
|
174
|
-
primaryKey: string,
|
|
175
|
-
): Promise<string[]> {
|
|
176
|
-
const depluralizedTableName = depluralize(tableName);
|
|
177
|
-
let sql = `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
|
|
178
|
-
WHERE TABLE_SCHEMA = '${schemaName}'
|
|
179
|
-
AND TABLE_NAME = '${schemaName}'
|
|
180
|
-
and TABLE_NAME != '${tableName}'
|
|
181
|
-
and (COLUMN_NAME = '${primaryKey}'
|
|
182
|
-
or COLUMN_NAME = '${depluralizedTableName}_${primaryKey}'
|
|
183
|
-
or COLUMN_NAME = '${depluralizedTableName}${capitalize(primaryKey)}')`;
|
|
184
|
-
const results = await runQuerySnowflake(sql, connection);
|
|
185
|
-
let foreignKeysString = results.rows.map((key) => {
|
|
186
|
-
return key.COLUMN_NAME;
|
|
187
|
-
});
|
|
188
|
-
foreignKeysString = foreignKeysString.filter(
|
|
189
|
-
(key) => key !== "id" && key !== "_id_",
|
|
190
|
-
);
|
|
191
|
-
foreignKeysString = [...new Set(foreignKeysString)];
|
|
192
|
-
if (foreignKeysString.length === 0) {
|
|
193
|
-
sql = `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
|
|
194
|
-
WHERE TABLE_SCHEMA = '${schemaName}'
|
|
195
|
-
AND TABLE_NAME = '${schemaName}'
|
|
196
|
-
and TABLE_NAME != '${tableName}'
|
|
197
|
-
and (COLUMN_NAME like '${depluralizedTableName}%'
|
|
198
|
-
or column_name like '%\\_id'
|
|
199
|
-
or column_name like '%Id'
|
|
200
|
-
or COLUMN_NAME like '%\\_${primaryKey}'
|
|
201
|
-
or COLUMN_NAME like '%${capitalize(primaryKey)}')`;
|
|
202
|
-
const results = await runQuerySnowflake(sql, connection);
|
|
203
|
-
foreignKeysString = results.rows.map((key) => {
|
|
204
|
-
return key.COLUMN_NAME;
|
|
205
|
-
});
|
|
206
|
-
foreignKeysString = [...new Set(foreignKeysString)];
|
|
207
|
-
}
|
|
208
|
-
return foreignKeysString;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
export async function getSchemaColumnInfoSnowflake(
|
|
212
|
-
connection: snowflake.Connection,
|
|
213
|
-
schemaName: string,
|
|
214
|
-
tableNames: { tableName: string; schemaName: string }[],
|
|
215
|
-
) {
|
|
216
|
-
const allColumns = await Promise.all(
|
|
217
|
-
tableNames.map(async (tableName) => {
|
|
218
|
-
const query = `SELECT
|
|
219
|
-
COLUMN_NAME as "columnName", DATA_TYPE as "dataType"
|
|
220
|
-
FROM INFORMATION_SCHEMA.COLUMNS
|
|
221
|
-
WHERE TABLE_SCHEMA = '${tableName.schemaName}' AND TABLE_NAME = '${tableName.tableName}';
|
|
222
|
-
`;
|
|
223
|
-
const results = await runQuerySnowflake(query, connection);
|
|
224
|
-
return {
|
|
225
|
-
tableName: `${tableName.schemaName}.${tableName.tableName}`,
|
|
226
|
-
displayName: `${tableName.schemaName}.${tableName.tableName}`,
|
|
227
|
-
columns: results.rows.map((row) => {
|
|
228
|
-
const postgresType = POSTGRES_SNOWFLAKE_MAP[row.dataType];
|
|
229
|
-
return {
|
|
230
|
-
columnName: row.columnName,
|
|
231
|
-
displayName: row.columnName,
|
|
232
|
-
dataTypeID: postgresType,
|
|
233
|
-
fieldType: row.dataType,
|
|
234
|
-
};
|
|
235
|
-
}),
|
|
236
|
-
};
|
|
237
|
-
}),
|
|
238
|
-
);
|
|
239
|
-
return allColumns;
|
|
240
|
-
}
|