@sqlrooms/duckdb 0.0.1 → 0.0.3
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/arrow-utils.d.ts +8 -0
- package/dist/arrow-utils.d.ts.map +1 -0
- package/dist/arrow-utils.js +22 -0
- package/dist/duckdb.d.ts +34 -0
- package/dist/duckdb.d.ts.map +1 -1
- package/dist/duckdb.js +41 -116
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/sql-from.d.ts +18 -0
- package/dist/sql-from.d.ts.map +1 -0
- package/dist/sql-from.js +68 -0
- package/package.json +25 -6
- package/CHANGELOG.md +0 -14
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as arrow from 'apache-arrow';
|
|
2
|
+
/**
|
|
3
|
+
* Converts an Arrow table to a JSON-compatible array of objects
|
|
4
|
+
* @see https://duckdb.org/docs/api/wasm/query.html#arrow-table-to-json
|
|
5
|
+
* @see https://github.com/apache/arrow/issues/37856
|
|
6
|
+
*/
|
|
7
|
+
export declare function arrowTableToJson(table: arrow.Table): Record<string, unknown>[];
|
|
8
|
+
//# sourceMappingURL=arrow-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arrow-utils.d.ts","sourceRoot":"","sources":["../src/arrow-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAEtC;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,KAAK,CAAC,KAAK,GACjB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAQ3B"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts an Arrow table to a JSON-compatible array of objects
|
|
3
|
+
* @see https://duckdb.org/docs/api/wasm/query.html#arrow-table-to-json
|
|
4
|
+
* @see https://github.com/apache/arrow/issues/37856
|
|
5
|
+
*/
|
|
6
|
+
export function arrowTableToJson(table) {
|
|
7
|
+
return table.toArray().map((row) => Object.fromEntries(Object.entries(row).map(([key, value]) => {
|
|
8
|
+
return [key, convertValue(value)];
|
|
9
|
+
})));
|
|
10
|
+
}
|
|
11
|
+
function convertValue(value) {
|
|
12
|
+
if (typeof value === 'bigint') {
|
|
13
|
+
if (value >= Number.MIN_SAFE_INTEGER && value <= Number.MAX_SAFE_INTEGER) {
|
|
14
|
+
return Number(value);
|
|
15
|
+
}
|
|
16
|
+
return String(value);
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === 'number') {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
return String(value);
|
|
22
|
+
}
|
package/dist/duckdb.d.ts
CHANGED
|
@@ -1,15 +1,49 @@
|
|
|
1
|
+
import * as arrow from 'apache-arrow';
|
|
2
|
+
/**
|
|
3
|
+
* Create a table from a query.
|
|
4
|
+
* @param tableName - The name of the table to create.
|
|
5
|
+
* @param query - The query to create the table from.
|
|
6
|
+
* @returns The table that was created.
|
|
7
|
+
*/
|
|
1
8
|
export declare function createTableFromQuery(tableName: string, query: string): Promise<{
|
|
2
9
|
tableName: string;
|
|
3
10
|
rowCount: number;
|
|
4
11
|
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Create a view from a registered file.
|
|
14
|
+
* @param filePath - The path to the file to create the view from.
|
|
15
|
+
* @param schema - The schema to create the view in.
|
|
16
|
+
* @param tableName - The name of the table to create.
|
|
17
|
+
* @param opts - The options to create the view with.
|
|
18
|
+
* @returns The view that was created.
|
|
19
|
+
*/
|
|
5
20
|
export declare function createViewFromRegisteredFile(filePath: string, schema: string, tableName: string, opts?: {
|
|
6
21
|
mode: 'table' | 'view';
|
|
7
22
|
}): Promise<{
|
|
8
23
|
tableName: string;
|
|
9
24
|
rowCount: number;
|
|
10
25
|
}>;
|
|
26
|
+
/**
|
|
27
|
+
* Create a view from a file.
|
|
28
|
+
* @param filePath - The path to the file to create the view from.
|
|
29
|
+
* @param schema - The schema to create the view in.
|
|
30
|
+
* @param tableName - The name of the table to create.
|
|
31
|
+
* @param file - The file to create the view from.
|
|
32
|
+
*/
|
|
11
33
|
export declare function createViewFromFile(filePath: string, schema: string, tableName: string, file: File | Uint8Array): Promise<{
|
|
12
34
|
tableName: string;
|
|
13
35
|
rowCount: number;
|
|
14
36
|
}>;
|
|
37
|
+
/**
|
|
38
|
+
* Create a table from an Arrow table.
|
|
39
|
+
* @param tableName - The name of the table to create.
|
|
40
|
+
* @param arrowTable - The Arrow table to create the table from.
|
|
41
|
+
*/
|
|
42
|
+
export declare function createTableFromArrowTable(tableName: string, data: arrow.Table): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Create a table from an array of objects.
|
|
45
|
+
* @param tableName - The name of the table to create.
|
|
46
|
+
* @param data - The array of objects to create the table from.
|
|
47
|
+
*/
|
|
48
|
+
export declare function createTableFromObjects(tableName: string, data: Record<string, unknown>[]): Promise<void>;
|
|
15
49
|
//# sourceMappingURL=duckdb.d.ts.map
|
package/dist/duckdb.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"duckdb.d.ts","sourceRoot":"","sources":["../src/duckdb.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"duckdb.d.ts","sourceRoot":"","sources":["../src/duckdb.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAGtC;;;;;GAKG;AACH,wBAAsB,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;;;GAU1E;AAED;;;;;;;GAOG;AACH,wBAAsB,4BAA4B,CAChD,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,IAAI,CAAC,EAAE;IAEL,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC;CACxB,GACA,OAAO,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAC,CAAC,CAwBhD;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,IAAI,GAAG,UAAU,GACtB,OAAO,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAC,CAAC,CAgBhD;AAED;;;;GAIG;AACH,wBAAsB,yBAAyB,CAC7C,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,KAAK,CAAC,KAAK,iBAIlB;AAED;;;;GAIG;AACH,wBAAsB,sBAAsB,CAC1C,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,iBAKhC"}
|
package/dist/duckdb.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { DuckDBDataProtocol } from '@duckdb/duckdb-wasm';
|
|
2
2
|
import { escapeVal, getColValAsNumber, getDuckDb } from './useDuckDb';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import { sqlFrom } from './sql-from';
|
|
4
|
+
/**
|
|
5
|
+
* Create a table from a query.
|
|
6
|
+
* @param tableName - The name of the table to create.
|
|
7
|
+
* @param query - The query to create the table from.
|
|
8
|
+
* @returns The table that was created.
|
|
9
|
+
*/
|
|
6
10
|
export async function createTableFromQuery(tableName, query) {
|
|
7
11
|
const { conn } = await getDuckDb();
|
|
8
12
|
const rowCount = getColValAsNumber(await conn.query(`CREATE OR REPLACE TABLE main.${tableName} AS (
|
|
@@ -10,20 +14,18 @@ export async function createTableFromQuery(tableName, query) {
|
|
|
10
14
|
)`));
|
|
11
15
|
return { tableName, rowCount };
|
|
12
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a view from a registered file.
|
|
19
|
+
* @param filePath - The path to the file to create the view from.
|
|
20
|
+
* @param schema - The schema to create the view in.
|
|
21
|
+
* @param tableName - The name of the table to create.
|
|
22
|
+
* @param opts - The options to create the view with.
|
|
23
|
+
* @returns The view that was created.
|
|
24
|
+
*/
|
|
13
25
|
export async function createViewFromRegisteredFile(filePath, schema, tableName, opts) {
|
|
14
26
|
const { mode = 'table' } = opts ?? {};
|
|
15
27
|
const { conn } = await getDuckDb();
|
|
16
28
|
const fileNameLower = filePath.toLowerCase();
|
|
17
|
-
// let rowCount: number;
|
|
18
|
-
// if (fileNameLower.endsWith('.json')) {
|
|
19
|
-
// await conn.insertJSONFromPath(filePath, {schema, name: tableName});
|
|
20
|
-
// // TODO: for JSON we can use insertJSONFromPath https://github.com/duckdb/duckdb-wasm/issues/1262
|
|
21
|
-
// // fileNameLower.endsWith('.json') || fileNameLower.endsWith('.ndjson')
|
|
22
|
-
// // ? `read_json_auto(${escapeVal(fileName)})`
|
|
23
|
-
// rowCount = getColValAsNumber(
|
|
24
|
-
// await conn.query(`SELECT COUNT(*) FROM ${schema}.${tableName}`),
|
|
25
|
-
// );
|
|
26
|
-
// } else {
|
|
27
29
|
const quotedFileName = escapeVal(filePath);
|
|
28
30
|
const readFileQuery = fileNameLower.endsWith('.json') ||
|
|
29
31
|
fileNameLower.endsWith('.geojson') ||
|
|
@@ -34,30 +36,21 @@ export async function createViewFromRegisteredFile(filePath, schema, tableName,
|
|
|
34
36
|
: fileNameLower.endsWith('.csv') || fileNameLower.endsWith('.tsv')
|
|
35
37
|
? `read_csv(${quotedFileName}, SAMPLE_SIZE=-1, AUTO_DETECT=TRUE)`
|
|
36
38
|
: quotedFileName;
|
|
37
|
-
// const readFileQuery = fileNameLower.endsWith('.csv')
|
|
38
|
-
// ? `read_csv(${quotedFileName}, SAMPLE_SIZE=-1, AUTO_DETECT=TRUE)`
|
|
39
|
-
// : quotedFileName;
|
|
40
39
|
// TODO: tableName generate
|
|
41
40
|
const rowCount = getColValAsNumber(await conn.query(`CREATE OR REPLACE ${mode} ${schema}.${tableName} AS
|
|
42
41
|
SELECT * FROM ${readFileQuery}`));
|
|
43
42
|
// }
|
|
44
43
|
return { tableName, rowCount };
|
|
45
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Create a view from a file.
|
|
47
|
+
* @param filePath - The path to the file to create the view from.
|
|
48
|
+
* @param schema - The schema to create the view in.
|
|
49
|
+
* @param tableName - The name of the table to create.
|
|
50
|
+
* @param file - The file to create the view from.
|
|
51
|
+
*/
|
|
46
52
|
export async function createViewFromFile(filePath, schema, tableName, file) {
|
|
47
53
|
const duckConn = await getDuckDb();
|
|
48
|
-
// const fileName = file.name;
|
|
49
|
-
// await duckConn.db.dropFile(fileName);
|
|
50
|
-
// await duckConn.db.registerFileHandle(
|
|
51
|
-
// fileName,
|
|
52
|
-
// file,
|
|
53
|
-
// DuckDBDataProtocol.BROWSER_FILEREADER,
|
|
54
|
-
// true,
|
|
55
|
-
// );
|
|
56
|
-
// const tableName = makeTableName(fileName);
|
|
57
|
-
// await duckConn.conn.query(`
|
|
58
|
-
// CREATE OR REPLACE VIEW ${tableName} AS SELECT * FROM '${fileName}'
|
|
59
|
-
// `);
|
|
60
|
-
//const fileName = file.name;
|
|
61
54
|
await duckConn.db.dropFile(filePath);
|
|
62
55
|
if (file instanceof File) {
|
|
63
56
|
await duckConn.db.registerFileHandle(filePath, file, DuckDBDataProtocol.BROWSER_FILEREADER, true);
|
|
@@ -66,91 +59,23 @@ export async function createViewFromFile(filePath, schema, tableName, file) {
|
|
|
66
59
|
await duckConn.db.registerFileBuffer(filePath, file);
|
|
67
60
|
}
|
|
68
61
|
return createViewFromRegisteredFile(filePath, schema, tableName);
|
|
69
|
-
// const res = await duckConn.conn.query(
|
|
70
|
-
// `SELECT count(*) FROM ${inputTableName}`,
|
|
71
|
-
// );
|
|
72
|
-
// const inputRowCount = getColValAsNumber(res, 0);
|
|
73
|
-
// const tableMeta = await duckConn.conn.query(
|
|
74
|
-
// `DESCRIBE TABLE ${inputTableName}`,
|
|
75
|
-
// );
|
|
76
|
-
// const inputTableFields = Array.from(tableMeta).map((row) => ({
|
|
77
|
-
// name: String(row?.column_name),
|
|
78
|
-
// type: String(row?.column_type),
|
|
79
|
-
// }));
|
|
80
|
-
// const nextResult: DataTable = {
|
|
81
|
-
// inputFileName,
|
|
82
|
-
// tableName: inputTableName,
|
|
83
|
-
// rowCount: inputRowCount,
|
|
84
|
-
// // outputRowCount: undefined,
|
|
85
|
-
// columns: inputTableFields,
|
|
86
|
-
// };
|
|
87
|
-
// // setResult(nextResult);
|
|
88
|
-
// return nextResult;
|
|
89
62
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// await duckConn.conn.query(`DROP TABLE IF EXISTS ${inputTableName}`);
|
|
110
|
-
// const readFileQuery = inputFileName.endsWith('.parquet')
|
|
111
|
-
// ? `parquet_scan(${escapeVal(inputFileName)})`
|
|
112
|
-
// : `read_csv(${escapeVal(
|
|
113
|
-
// inputFileName,
|
|
114
|
-
// )}, SAMPLE_SIZE=-1, AUTO_DETECT=TRUE)`;
|
|
115
|
-
// await duckConn.conn.query(
|
|
116
|
-
// `CREATE TABLE ${inputTableName} AS
|
|
117
|
-
// SELECT * FROM ${readFileQuery}`,
|
|
118
|
-
// );
|
|
119
|
-
// const res = await duckConn.conn.query(
|
|
120
|
-
// `SELECT count(*) FROM ${inputTableName}`,
|
|
121
|
-
// );
|
|
122
|
-
// const inputRowCount = getColValAsNumber(res, 0);
|
|
123
|
-
// const tableMeta = await duckConn.conn.query(
|
|
124
|
-
// `DESCRIBE TABLE ${inputTableName}`,
|
|
125
|
-
// );
|
|
126
|
-
// const inputTableFields = Array.from(tableMeta).map((row) => ({
|
|
127
|
-
// name: String(row?.column_name),
|
|
128
|
-
// type: String(row?.column_type),
|
|
129
|
-
// }));
|
|
130
|
-
// const nextResult: CreateTableDropzoneResult = {
|
|
131
|
-
// inputFileName,
|
|
132
|
-
// inputTableName,
|
|
133
|
-
// inputRowCount,
|
|
134
|
-
// // outputRowCount: undefined,
|
|
135
|
-
// inputTableFields,
|
|
136
|
-
// columns: {},
|
|
137
|
-
// };
|
|
138
|
-
// // setResult(nextResult);
|
|
139
|
-
// onTableCreated(inputTableName, nextResult);
|
|
140
|
-
// } catch (e) {
|
|
141
|
-
// console.error(e);
|
|
142
|
-
// onError(e instanceof Error ? e.message : String(e));
|
|
143
|
-
// }
|
|
144
|
-
// }
|
|
145
|
-
// async function maybeDropTable(
|
|
146
|
-
// value: CreateTableDropzoneResult,
|
|
147
|
-
// duckConn: DuckDb,
|
|
148
|
-
// ) {
|
|
149
|
-
// const {inputFileName, inputTableName} = value || {};
|
|
150
|
-
// if (inputFileName) {
|
|
151
|
-
// await duckConn.db.dropFile(inputFileName);
|
|
152
|
-
// }
|
|
153
|
-
// if (inputTableName) {
|
|
154
|
-
// await duckConn.conn.query(`DROP TABLE IF EXISTS ${inputTableName};`);
|
|
155
|
-
// }
|
|
156
|
-
// }
|
|
63
|
+
/**
|
|
64
|
+
* Create a table from an Arrow table.
|
|
65
|
+
* @param tableName - The name of the table to create.
|
|
66
|
+
* @param arrowTable - The Arrow table to create the table from.
|
|
67
|
+
*/
|
|
68
|
+
export async function createTableFromArrowTable(tableName, data) {
|
|
69
|
+
const { conn } = await getDuckDb();
|
|
70
|
+
await conn.insertArrowTable(data, { name: tableName });
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Create a table from an array of objects.
|
|
74
|
+
* @param tableName - The name of the table to create.
|
|
75
|
+
* @param data - The array of objects to create the table from.
|
|
76
|
+
*/
|
|
77
|
+
export async function createTableFromObjects(tableName, data) {
|
|
78
|
+
const { conn } = await getDuckDb();
|
|
79
|
+
const query = sqlFrom(data);
|
|
80
|
+
await conn.query(`CREATE OR REPLACE TABLE ${tableName} AS ${query}`);
|
|
81
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a SQL query that embeds the given data for loading.
|
|
3
|
+
* @param {*} data The dataset as an array of objects.
|
|
4
|
+
* @param {object} [options] Loading options.
|
|
5
|
+
* @param {string[]|Record<string,string>} [options.columns] The columns to include.
|
|
6
|
+
* If not specified, the keys of the first data object are used.
|
|
7
|
+
* @returns {string} SQL query string to load data.
|
|
8
|
+
*/
|
|
9
|
+
export declare function sqlFrom(data: Record<string, unknown>[], { columns, }?: {
|
|
10
|
+
columns?: string[] | Record<string, string>;
|
|
11
|
+
}): string;
|
|
12
|
+
/**
|
|
13
|
+
* Convert a value to a SQL literal.
|
|
14
|
+
* @param {*} value The value to convert.
|
|
15
|
+
* @returns {string} The SQL literal.
|
|
16
|
+
*/
|
|
17
|
+
export declare function literalToSQL(value: unknown): string;
|
|
18
|
+
//# sourceMappingURL=sql-from.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-from.d.ts","sourceRoot":"","sources":["../src/sql-from.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,EAC/B,EACE,OAAsC,GACvC,GAAE;IAAC,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAM,UAsBtD;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,UA2B1C"}
|
package/dist/sql-from.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Adapted from https://github.com/uwdata/mosaic/blob/main/packages/sql/src/load/sql-from.js
|
|
2
|
+
// BSD 3-Clause License Copyright (c) 2023, UW Interactive Data Lab
|
|
3
|
+
/**
|
|
4
|
+
* Create a SQL query that embeds the given data for loading.
|
|
5
|
+
* @param {*} data The dataset as an array of objects.
|
|
6
|
+
* @param {object} [options] Loading options.
|
|
7
|
+
* @param {string[]|Record<string,string>} [options.columns] The columns to include.
|
|
8
|
+
* If not specified, the keys of the first data object are used.
|
|
9
|
+
* @returns {string} SQL query string to load data.
|
|
10
|
+
*/
|
|
11
|
+
export function sqlFrom(data, { columns = Object.keys(data?.[0] || {}), } = {}) {
|
|
12
|
+
let keys = [];
|
|
13
|
+
let columnMap;
|
|
14
|
+
if (Array.isArray(columns)) {
|
|
15
|
+
keys = columns;
|
|
16
|
+
columnMap = keys.reduce((m, k) => ({ ...m, [k]: k }), {});
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
columnMap = columns;
|
|
20
|
+
keys = Object.keys(columns);
|
|
21
|
+
}
|
|
22
|
+
if (!keys.length) {
|
|
23
|
+
throw new Error('Can not create table from empty column set.');
|
|
24
|
+
}
|
|
25
|
+
const subq = [];
|
|
26
|
+
for (const datum of data) {
|
|
27
|
+
const sel = keys.map((k) => `${literalToSQL(datum[k])} AS "${columnMap[k]}"`);
|
|
28
|
+
subq.push(`(SELECT ${sel.join(', ')})`);
|
|
29
|
+
}
|
|
30
|
+
return subq.join(' UNION ALL ');
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Convert a value to a SQL literal.
|
|
34
|
+
* @param {*} value The value to convert.
|
|
35
|
+
* @returns {string} The SQL literal.
|
|
36
|
+
*/
|
|
37
|
+
export function literalToSQL(value) {
|
|
38
|
+
switch (typeof value) {
|
|
39
|
+
case 'number':
|
|
40
|
+
return Number.isFinite(value) ? `${value}` : 'NULL';
|
|
41
|
+
case 'string':
|
|
42
|
+
return `'${value.replace(`'`, `''`)}'`;
|
|
43
|
+
case 'boolean':
|
|
44
|
+
return value ? 'TRUE' : 'FALSE';
|
|
45
|
+
default:
|
|
46
|
+
if (value == null) {
|
|
47
|
+
return 'NULL';
|
|
48
|
+
}
|
|
49
|
+
else if (value instanceof Date) {
|
|
50
|
+
const ts = +value;
|
|
51
|
+
if (Number.isNaN(ts))
|
|
52
|
+
return 'NULL';
|
|
53
|
+
const y = value.getUTCFullYear();
|
|
54
|
+
const m = value.getUTCMonth();
|
|
55
|
+
const d = value.getUTCDate();
|
|
56
|
+
return ts === Date.UTC(y, m, d)
|
|
57
|
+
? `DATE '${y}-${m + 1}-${d}'` // utc date
|
|
58
|
+
: `epoch_ms(${ts})`; // timestamp
|
|
59
|
+
}
|
|
60
|
+
else if (value instanceof RegExp) {
|
|
61
|
+
return `'${value.source}'`;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// otherwise rely on string coercion
|
|
65
|
+
return `${value}`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
package/package.json
CHANGED
|
@@ -1,23 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqlrooms/duckdb",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"private": false,
|
|
9
|
+
"author": "Ilya Boyandin <ilya@boyandin.me>",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/sqlrooms/sqlrooms.git"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"!dist/__tests__"
|
|
18
|
+
],
|
|
9
19
|
"publishConfig": {
|
|
10
20
|
"access": "public"
|
|
11
21
|
},
|
|
12
|
-
"
|
|
13
|
-
"@duckdb/duckdb-wasm": "
|
|
14
|
-
"apache-arrow": "
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@duckdb/duckdb-wasm": "^1.29.0",
|
|
24
|
+
"apache-arrow": "^18.1.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@sqlrooms/jest-config": "0.0.3",
|
|
28
|
+
"@types/jest": "^29.5.12",
|
|
29
|
+
"jest": "^29.7.0",
|
|
30
|
+
"ts-jest": "^29.1.2"
|
|
15
31
|
},
|
|
16
32
|
"scripts": {
|
|
17
33
|
"dev": "tsc -w",
|
|
18
34
|
"build": "tsc",
|
|
19
35
|
"lint": "eslint .",
|
|
20
|
-
"typecheck": "tsc --noEmit"
|
|
36
|
+
"typecheck": "tsc --noEmit",
|
|
37
|
+
"typedoc": "typedoc",
|
|
38
|
+
"test": "jest",
|
|
39
|
+
"test:watch": "jest --watch"
|
|
21
40
|
},
|
|
22
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "4269a3efdb5a903cb2498c126e9c36683b3bbae0"
|
|
23
42
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# Change Log
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
-
|
|
6
|
-
## [0.0.1](https://github.com/ilyabo/sqlrooms/compare/v0.0.1-alpha.0...v0.0.1) (2025-01-30)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package @sqlrooms/duckdb
|
|
9
|
-
|
|
10
|
-
## 0.0.1-alpha.0 (2025-01-30)
|
|
11
|
-
|
|
12
|
-
**Note:** Version bump only for package @sqlrooms/duckdb
|
|
13
|
-
|
|
14
|
-
**Note:** Version bump only for package @sqlrooms/duckdb
|