@uwdata/mosaic-duckdb 0.16.1 → 0.17.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/package.json +6 -6
- package/src/data-server.js +0 -16
- package/tsconfig.json +8 -0
- package/LICENSE +0 -47
- package/src/load/bundle.js +0 -75
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uwdata/mosaic-duckdb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "A Promise-based DuckDB API and Node.js data server.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"duckdb",
|
|
@@ -25,15 +25,15 @@
|
|
|
25
25
|
"to-parquet": "./bin/to-parquet.js"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
|
+
"prebuild": "rimraf dist && mkdir dist",
|
|
28
29
|
"lint": "eslint src test",
|
|
29
30
|
"server": "node bin/run-server.js",
|
|
30
|
-
"test": "vitest run",
|
|
31
|
+
"test": "vitest run && tsc",
|
|
31
32
|
"prepublishOnly": "npm run test && npm run lint"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@uwdata/mosaic-sql": "^0.
|
|
35
|
-
"duckdb": "
|
|
35
|
+
"@uwdata/mosaic-sql": "^0.17.0",
|
|
36
|
+
"duckdb": "~1.2.1",
|
|
36
37
|
"ws": "^8.18.2"
|
|
37
|
-
}
|
|
38
|
-
"gitHead": "e12af9927b0a35a0e3194850ef569d0d24a022ce"
|
|
38
|
+
}
|
|
39
39
|
}
|
package/src/data-server.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import http from 'node:http';
|
|
2
|
-
import path from 'node:path';
|
|
3
2
|
import url from 'node:url';
|
|
4
3
|
import { WebSocketServer } from 'ws';
|
|
5
4
|
import { Cache, cacheKey } from './Cache.js';
|
|
6
|
-
import { createBundle, loadBundle } from './load/bundle.js';
|
|
7
5
|
|
|
8
6
|
const CACHE_DIR = '.mosaic/cache';
|
|
9
|
-
const BUNDLE_DIR = '.mosaic/bundle';
|
|
10
7
|
|
|
11
8
|
export function dataServer(db, {
|
|
12
9
|
cache = true,
|
|
@@ -121,19 +118,6 @@ export function queryHandler(db, queryCache) {
|
|
|
121
118
|
// JSON response format
|
|
122
119
|
res.json(await retrieve(query, sql => db.query(sql)));
|
|
123
120
|
break;
|
|
124
|
-
case 'create-bundle':
|
|
125
|
-
// Create a named bundle of precomputed resources
|
|
126
|
-
await createBundle(
|
|
127
|
-
db, queryCache, query.queries,
|
|
128
|
-
path.resolve(BUNDLE_DIR, query.name)
|
|
129
|
-
);
|
|
130
|
-
res.done();
|
|
131
|
-
break;
|
|
132
|
-
case 'load-bundle':
|
|
133
|
-
// Load a named bundle of precomputed resources
|
|
134
|
-
await loadBundle(db, queryCache, path.resolve(BUNDLE_DIR, query.name));
|
|
135
|
-
res.done();
|
|
136
|
-
break;
|
|
137
121
|
default:
|
|
138
122
|
res.error(`Unrecognized command: ${type}`, 400);
|
|
139
123
|
}
|
package/tsconfig.json
ADDED
package/LICENSE
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
BSD 3-Clause License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023, UW Interactive Data Lab
|
|
4
|
-
|
|
5
|
-
Redistribution and use in source and binary forms, with or without
|
|
6
|
-
modification, are permitted provided that the following conditions are met:
|
|
7
|
-
|
|
8
|
-
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
-
list of conditions and the following disclaimer.
|
|
10
|
-
|
|
11
|
-
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
-
this list of conditions and the following disclaimer in the documentation
|
|
13
|
-
and/or other materials provided with the distribution.
|
|
14
|
-
|
|
15
|
-
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
-
contributors may be used to endorse or promote products derived from
|
|
17
|
-
this software without specific prior written permission.
|
|
18
|
-
|
|
19
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
-
|
|
30
|
-
---
|
|
31
|
-
|
|
32
|
-
Portions of this software are derived from Observable Plot, which is released
|
|
33
|
-
under the ISC license.
|
|
34
|
-
|
|
35
|
-
Copyright 2020-2023 Observable, Inc.
|
|
36
|
-
|
|
37
|
-
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
38
|
-
with or without fee is hereby granted, provided that the above copyright notice
|
|
39
|
-
and this permission notice appear in all copies.
|
|
40
|
-
|
|
41
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
42
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
43
|
-
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
44
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
45
|
-
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
46
|
-
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
47
|
-
THIS SOFTWARE.
|
package/src/load/bundle.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { cacheKey } from '../Cache.js';
|
|
4
|
-
|
|
5
|
-
async function retrieve(db, cache, sql, type) {
|
|
6
|
-
const key = cacheKey(sql, type);
|
|
7
|
-
const cached = cache.get(key);
|
|
8
|
-
if (cached) return cached;
|
|
9
|
-
switch (type) {
|
|
10
|
-
case 'arrow':
|
|
11
|
-
return db.arrowBuffer(sql);
|
|
12
|
-
case 'json':
|
|
13
|
-
return JSON.stringify(await db.query(sql));
|
|
14
|
-
default:
|
|
15
|
-
throw new Error(`Unsupported query type: ${type}`);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export async function createBundle(db, cache, queries, dir) {
|
|
20
|
-
const describe_re = /^DESCRIBE /;
|
|
21
|
-
const pragma_re = /^PRAGMA /;
|
|
22
|
-
const view_re = /^CREATE( TEMP| TEMPORARY)? VIEW/;
|
|
23
|
-
const table_re = /^CREATE( TEMP| TEMPORARY)? TABLE( IF NOT EXISTS)? ([^\s]+)/;
|
|
24
|
-
|
|
25
|
-
const manifest = { tables: [], queries: [] };
|
|
26
|
-
|
|
27
|
-
await fs.mkdir(dir, { recursive: true });
|
|
28
|
-
|
|
29
|
-
const querySet = new Set(queries);
|
|
30
|
-
for (const query of querySet) {
|
|
31
|
-
const sql = typeof query === 'string' ? query : query.sql;
|
|
32
|
-
if (query.alias) {
|
|
33
|
-
const table = query.alias;
|
|
34
|
-
const file = path.resolve(dir, `${table}.parquet`);
|
|
35
|
-
await db.exec(`COPY (${sql}) TO '${file}' (FORMAT PARQUET)`);
|
|
36
|
-
manifest.tables.push(table);
|
|
37
|
-
} else if (sql.startsWith('CREATE ')) {
|
|
38
|
-
// table or view
|
|
39
|
-
if (view_re.test(sql)) continue; // ignore views
|
|
40
|
-
const table = sql.match(table_re)?.[3];
|
|
41
|
-
const file = path.resolve(dir, `${table}.parquet`);
|
|
42
|
-
await db.exec(`${sql}`);
|
|
43
|
-
await db.exec(`COPY ${table} TO '${file}' (FORMAT PARQUET)`);
|
|
44
|
-
manifest.tables.push(table);
|
|
45
|
-
} else if (!pragma_re.test(sql)) {
|
|
46
|
-
// select query
|
|
47
|
-
const type = describe_re.test(sql) ? 'json' : 'arrow';
|
|
48
|
-
const key = cacheKey(sql, type);
|
|
49
|
-
const result = await retrieve(db, cache, sql, type);
|
|
50
|
-
await fs.writeFile(path.resolve(dir, key), result);
|
|
51
|
-
manifest.queries.push(key);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
await fs.writeFile(path.resolve(dir, 'bundle.json'), JSON.stringify(manifest, 0, 2));
|
|
56
|
-
return manifest;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export async function loadBundle(db, cache, dir) {
|
|
60
|
-
const manifest = JSON.parse(await fs.readFile(path.resolve(dir, 'bundle.json')));
|
|
61
|
-
|
|
62
|
-
// load precomputed query results into the cache
|
|
63
|
-
for (const key of manifest.queries) {
|
|
64
|
-
const file = path.resolve(dir, key);
|
|
65
|
-
const json = path.extname(file) === '.json';
|
|
66
|
-
const data = await fs.readFile(file);
|
|
67
|
-
cache.set(key, json ? JSON.parse(data) : data);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// load precomputed tables into the database
|
|
71
|
-
for (const table of manifest.tables) {
|
|
72
|
-
const file = path.resolve(dir, `${table}.parquet`);
|
|
73
|
-
await db.exec(`CREATE TABLE IF NOT EXISTS ${table} AS SELECT * FROM '${file}'`);
|
|
74
|
-
}
|
|
75
|
-
}
|