@cereusdb/standard 0.2.0 → 0.4.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 +103 -3
- package/dist/external.d.ts +293 -0
- package/dist/external.js +347 -0
- package/dist/index.d.ts +199 -4
- package/dist/index.js +219 -4
- package/dist/storage.d.ts +64 -0
- package/dist/storage.js +211 -0
- package/dist/wasm/cereusdb-external.d.ts +220 -0
- package/dist/wasm/cereusdb-external.js +1428 -0
- package/dist/wasm/cereusdb.d.ts +82 -4
- package/dist/wasm/cereusdb.js +277 -28
- package/dist/wasm/cereusdb_bg.wasm +0 -0
- package/dist/wasm/cereusdb_bg.wasm.d.ts +23 -3
- package/dist/wasm/env_shim.js +52 -4
- package/package.json +5 -1
- /package/dist/wasm/snippets/{cereusdb-object-store-3803cd296f83ecfa → cereusdb-object-store-6ee0e8370430462c}/inline0.js +0 -0
package/dist/external.js
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import init, { CereusDB as WasmCereusDB } from './wasm/cereusdb-external.js';
|
|
2
|
+
import { OPFSStorageBackend } from './storage.js';
|
|
3
|
+
export { MemoryStorageBackend, OPFSStorageBackend, } from './storage.js';
|
|
4
|
+
/** MIME type of GeoParquet exports. */
|
|
5
|
+
export const PARQUET_MIME_TYPE = 'application/vnd.apache.parquet';
|
|
6
|
+
/**
|
|
7
|
+
* Save a file with the browser's download mechanism. Needs a DOM `document`
|
|
8
|
+
* (the main thread); it does not work in Web Workers or Node.js.
|
|
9
|
+
*/
|
|
10
|
+
export function downloadFile(filename, data, mimeType = 'application/octet-stream') {
|
|
11
|
+
if (typeof document === 'undefined') {
|
|
12
|
+
throw new Error('downloadFile() needs a browser document; pass CereusDB.create({ onExport }) to handle exports elsewhere');
|
|
13
|
+
}
|
|
14
|
+
const url = URL.createObjectURL(new Blob([data], { type: mimeType }));
|
|
15
|
+
const link = document.createElement('a');
|
|
16
|
+
link.href = url;
|
|
17
|
+
link.download = filename;
|
|
18
|
+
link.style.display = 'none';
|
|
19
|
+
document.body.appendChild(link);
|
|
20
|
+
try {
|
|
21
|
+
link.click();
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
link.remove();
|
|
25
|
+
// Revoking right away can cancel the download in some browsers.
|
|
26
|
+
setTimeout(() => URL.revokeObjectURL(url), 60000);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const QUERY_PATTERN = /^\s*(select|with|values|\()/i;
|
|
30
|
+
function resolveExportHandler(option) {
|
|
31
|
+
if (option === false) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
if (option !== undefined) {
|
|
35
|
+
return option;
|
|
36
|
+
}
|
|
37
|
+
return typeof document !== 'undefined' ? downloadFile : undefined;
|
|
38
|
+
}
|
|
39
|
+
function defaultExportFilename(queryOrTable) {
|
|
40
|
+
if (QUERY_PATTERN.test(queryOrTable)) {
|
|
41
|
+
return 'export.parquet';
|
|
42
|
+
}
|
|
43
|
+
const name = queryOrTable.trim().split('.').pop()?.replace(/^"|"$/g, '');
|
|
44
|
+
return `${name || 'export'}.parquet`;
|
|
45
|
+
}
|
|
46
|
+
function resolveStorageBackends(options) {
|
|
47
|
+
const backends = {};
|
|
48
|
+
for (const [scheme, backend] of Object.entries(options ?? {})) {
|
|
49
|
+
if (backend) {
|
|
50
|
+
backends[scheme.toLowerCase()] = backend;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (options?.opfs === undefined && OPFSStorageBackend.isSupported()) {
|
|
54
|
+
backends.opfs = new OPFSStorageBackend();
|
|
55
|
+
}
|
|
56
|
+
return backends;
|
|
57
|
+
}
|
|
58
|
+
function alterTableClause(operation) {
|
|
59
|
+
if ('renameTo' in operation) {
|
|
60
|
+
return `RENAME TO ${operation.renameTo}`;
|
|
61
|
+
}
|
|
62
|
+
if ('addColumn' in operation) {
|
|
63
|
+
const column = operation.addColumn;
|
|
64
|
+
const notNull = column.notNull ? ' NOT NULL' : '';
|
|
65
|
+
const defaultValue = column.default !== undefined ? ` DEFAULT ${column.default}` : '';
|
|
66
|
+
const ifNotExists = operation.ifNotExists ? 'IF NOT EXISTS ' : '';
|
|
67
|
+
return `ADD COLUMN ${ifNotExists}${column.name} ${column.type}${notNull}${defaultValue}`;
|
|
68
|
+
}
|
|
69
|
+
if ('dropColumn' in operation) {
|
|
70
|
+
const columns = Array.isArray(operation.dropColumn)
|
|
71
|
+
? operation.dropColumn
|
|
72
|
+
: [operation.dropColumn];
|
|
73
|
+
const ifExists = operation.ifExists ? 'IF EXISTS ' : '';
|
|
74
|
+
return columns.map((column) => `DROP COLUMN ${ifExists}${column}`).join(', ');
|
|
75
|
+
}
|
|
76
|
+
return `RENAME COLUMN ${operation.renameColumn.from} TO ${operation.renameColumn.to}`;
|
|
77
|
+
}
|
|
78
|
+
function toUint8Array(data) {
|
|
79
|
+
if (ArrayBuffer.isView(data)) {
|
|
80
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
81
|
+
}
|
|
82
|
+
return new Uint8Array(data);
|
|
83
|
+
}
|
|
84
|
+
function normalizeRasterFormat(format) {
|
|
85
|
+
const normalized = format.trim().toLowerCase();
|
|
86
|
+
if (normalized === 'geotiff' || normalized === 'tiff') {
|
|
87
|
+
return normalized;
|
|
88
|
+
}
|
|
89
|
+
throw new Error(`Unsupported raster format: ${format}`);
|
|
90
|
+
}
|
|
91
|
+
export class CereusDB {
|
|
92
|
+
constructor(inner) {
|
|
93
|
+
this.storageBackends = {};
|
|
94
|
+
this.inner = inner;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Create and initialize a new CereusDB instance.
|
|
98
|
+
* This loads the WASM module and initializes the query engine.
|
|
99
|
+
*/
|
|
100
|
+
static async create(options) {
|
|
101
|
+
const source = options?.wasmSource ?? options?.wasmUrl;
|
|
102
|
+
if (source === undefined) {
|
|
103
|
+
await init();
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
await init({ module_or_path: source });
|
|
107
|
+
}
|
|
108
|
+
const inner = WasmCereusDB.create();
|
|
109
|
+
const db = new CereusDB(inner);
|
|
110
|
+
if (options?.objectStores !== undefined) {
|
|
111
|
+
db.registerObjectStores(options.objectStores);
|
|
112
|
+
}
|
|
113
|
+
db.exportHandler = resolveExportHandler(options?.onExport);
|
|
114
|
+
if (db.exportHandler !== undefined) {
|
|
115
|
+
inner.register_export_handler(db.exportHandler);
|
|
116
|
+
}
|
|
117
|
+
db.storageBackends = resolveStorageBackends(options?.storage);
|
|
118
|
+
for (const [scheme, backend] of Object.entries(db.storageBackends)) {
|
|
119
|
+
inner.register_storage_backend(scheme, backend);
|
|
120
|
+
}
|
|
121
|
+
for (const location of options?.attach ?? []) {
|
|
122
|
+
await db.createDatabase(location, { ifNotExists: true });
|
|
123
|
+
}
|
|
124
|
+
return db;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Execute a SQL query and return results as Arrow IPC bytes.
|
|
128
|
+
*/
|
|
129
|
+
async sql(query) {
|
|
130
|
+
return await this.inner.sql(query);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Execute a SQL query and return results as JSON.
|
|
134
|
+
*/
|
|
135
|
+
async sqlJSON(query) {
|
|
136
|
+
const json = await this.inner.sql_json(query);
|
|
137
|
+
return JSON.parse(json);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Register a remote Parquet file as a table.
|
|
141
|
+
* The server must support CORS.
|
|
142
|
+
*/
|
|
143
|
+
async registerRemoteParquet(name, url) {
|
|
144
|
+
await this.inner.register_remote_parquet(name, url);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Register browser-backed object stores for ranged and listing reads.
|
|
148
|
+
*/
|
|
149
|
+
registerObjectStores(config) {
|
|
150
|
+
this.objectStoreApi().register_object_stores(config);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Register a remote Parquet object or prefix through DataFusion's listing table path.
|
|
154
|
+
*/
|
|
155
|
+
async registerParquetTable(name, url, options = {}) {
|
|
156
|
+
await this.objectStoreApi().register_parquet_table(name, url, options);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Register a local file (from File API / drag-and-drop) as a table.
|
|
160
|
+
* Currently supports Parquet, GeoJSON, and GeoTIFF rasters.
|
|
161
|
+
*/
|
|
162
|
+
async registerFile(name, file) {
|
|
163
|
+
const buffer = new Uint8Array(await file.arrayBuffer());
|
|
164
|
+
const ext = file.name.split('.').pop()?.toLowerCase();
|
|
165
|
+
if (ext === 'parquet' || ext === 'geoparquet') {
|
|
166
|
+
await this.inner.register_parquet_buffer(name, buffer);
|
|
167
|
+
}
|
|
168
|
+
else if (ext === 'geojson' || ext === 'json') {
|
|
169
|
+
const text = new TextDecoder().decode(buffer);
|
|
170
|
+
this.inner.register_geojson(name, text);
|
|
171
|
+
}
|
|
172
|
+
else if (ext === 'tif' || ext === 'tiff') {
|
|
173
|
+
this.registerRaster(name, buffer, 'geotiff');
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
throw new Error(`Unsupported file format: .${ext}`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Register a GeoJSON object or string as a table.
|
|
181
|
+
*/
|
|
182
|
+
registerGeoJSON(name, geojson) {
|
|
183
|
+
const str = typeof geojson === 'string' ? geojson : JSON.stringify(geojson);
|
|
184
|
+
this.inner.register_geojson(name, str);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Register a raster buffer as a single-column raster table.
|
|
188
|
+
* Requires the full GDAL-enabled package build.
|
|
189
|
+
*/
|
|
190
|
+
registerRaster(name, data, format) {
|
|
191
|
+
this.inner.register_raster_buffer(name, normalizeRasterFormat(format), toUint8Array(data));
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Register a GeoTIFF buffer as a single-column raster table.
|
|
195
|
+
* Requires the full GDAL-enabled package build.
|
|
196
|
+
*/
|
|
197
|
+
registerGeoTIFF(name, data) {
|
|
198
|
+
this.registerRaster(name, data, 'geotiff');
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Drop a table. The table is removed immediately; for tables of persistent
|
|
202
|
+
* databases the returned promise resolves once the change is written.
|
|
203
|
+
*/
|
|
204
|
+
dropTable(name) {
|
|
205
|
+
this.inner.drop_table(name);
|
|
206
|
+
return this.inner.flush();
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Create a persistent database, for example `opfs://mydb`, and attach it
|
|
210
|
+
* as a catalog. Equivalent to `CREATE DATABASE 'opfs://mydb'`.
|
|
211
|
+
* Resolves to the database name.
|
|
212
|
+
*/
|
|
213
|
+
async createDatabase(location, options = {}) {
|
|
214
|
+
return await this.inner.attach_database(location, options.name, true, options.ifNotExists ?? false);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Open an existing persistent database. Equivalent to
|
|
218
|
+
* `ATTACH 'opfs://mydb' [AS name]`. Resolves to the database name.
|
|
219
|
+
*/
|
|
220
|
+
async attachDatabase(location, options = {}) {
|
|
221
|
+
return await this.inner.attach_database(location, options.name, false, options.ifNotExists ?? false);
|
|
222
|
+
}
|
|
223
|
+
/** Write pending changes and close a persistent database. Equivalent to `DETACH name`. */
|
|
224
|
+
async detachDatabase(name, options = {}) {
|
|
225
|
+
await this.inner.detach_database(name, options.ifExists ?? false);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Drop a database by name or location. Persistent databases are deleted
|
|
229
|
+
* from storage. Equivalent to `DROP DATABASE`.
|
|
230
|
+
*/
|
|
231
|
+
async dropDatabase(nameOrLocation, options = {}) {
|
|
232
|
+
await this.inner.drop_database(nameOrLocation, options.ifExists ?? false);
|
|
233
|
+
}
|
|
234
|
+
/** Set the default database (and schema). Equivalent to `USE database[.schema]`. */
|
|
235
|
+
async useDatabase(database, schema) {
|
|
236
|
+
await this.inner.sql_json(schema === undefined ? `USE ${database}` : `USE ${database}.${schema}`);
|
|
237
|
+
}
|
|
238
|
+
/** Attached databases plus stored databases that are not attached. */
|
|
239
|
+
async listDatabases() {
|
|
240
|
+
const rows = (await this.sqlJSON('SELECT database_name, storage, location FROM cereusdb_databases()'));
|
|
241
|
+
const databases = rows.map((row) => ({
|
|
242
|
+
name: row.database_name,
|
|
243
|
+
storage: row.storage,
|
|
244
|
+
location: row.location ?? null,
|
|
245
|
+
attached: true,
|
|
246
|
+
}));
|
|
247
|
+
for (const scheme of Object.keys(this.storageBackends)) {
|
|
248
|
+
const locations = (await this.inner.list_database_locations(scheme));
|
|
249
|
+
for (const location of locations) {
|
|
250
|
+
if (!databases.some((database) => database.location === location)) {
|
|
251
|
+
databases.push({ name: null, storage: scheme, location, attached: false });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return databases;
|
|
256
|
+
}
|
|
257
|
+
/** Describe all databases, schemas, tables and views. */
|
|
258
|
+
async catalog() {
|
|
259
|
+
return JSON.parse(await this.inner.catalog_json());
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Create a table from column definitions or a query. `name` may be
|
|
263
|
+
* qualified (`mydb.public.cities`).
|
|
264
|
+
*/
|
|
265
|
+
async createTable(name, definition, options = {}) {
|
|
266
|
+
const orReplace = options.orReplace ? 'OR REPLACE ' : '';
|
|
267
|
+
const ifNotExists = options.ifNotExists ? 'IF NOT EXISTS ' : '';
|
|
268
|
+
const body = 'as' in definition
|
|
269
|
+
? `AS ${definition.as}`
|
|
270
|
+
: `(${Object.entries(definition.columns)
|
|
271
|
+
.map(([column, type]) => `${column} ${type}`)
|
|
272
|
+
.join(', ')})`;
|
|
273
|
+
await this.inner.sql_json(`CREATE ${orReplace}TABLE ${ifNotExists}${name} ${body}`);
|
|
274
|
+
}
|
|
275
|
+
/** Rename a table or add, drop, or rename a column. */
|
|
276
|
+
async alterTable(name, operation) {
|
|
277
|
+
await this.inner.sql_json(`ALTER TABLE ${name} ${alterTableClause(operation)}`);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Insert Arrow IPC data (stream or file format, for example from
|
|
281
|
+
* `tableToIPC()` of apache-arrow) into a table. Columns are matched by
|
|
282
|
+
* name; missing columns are filled with NULL. Resolves to the number of
|
|
283
|
+
* inserted rows.
|
|
284
|
+
*/
|
|
285
|
+
async insertArrow(table, data) {
|
|
286
|
+
return await this.inner.insert_arrow(table, toUint8Array(data));
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Export a table or query result as a GeoParquet (1.1) file. Geometry and
|
|
290
|
+
* geography columns are described in the file's `geo` metadata.
|
|
291
|
+
*
|
|
292
|
+
* @param queryOrTable A query (`SELECT ...`, `WITH ...`) or a table name.
|
|
293
|
+
*/
|
|
294
|
+
async exportGeoParquet(queryOrTable, options = {}) {
|
|
295
|
+
const query = QUERY_PATTERN.test(queryOrTable)
|
|
296
|
+
? queryOrTable
|
|
297
|
+
: `SELECT * FROM ${queryOrTable}`;
|
|
298
|
+
return await this.inner.export_geoparquet(query, options);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Export a table or query result as GeoParquet and pass it to the export
|
|
302
|
+
* handler: a browser download by default (see `onExport` in
|
|
303
|
+
* {@link CereusDBOptions}). Resolves to the file name.
|
|
304
|
+
*
|
|
305
|
+
* @param queryOrTable A query (`SELECT ...`, `WITH ...`) or a table name.
|
|
306
|
+
*/
|
|
307
|
+
async downloadGeoParquet(queryOrTable, options = {}) {
|
|
308
|
+
const { filename, ...exportOptions } = options;
|
|
309
|
+
const handler = this.exportHandler;
|
|
310
|
+
if (handler === undefined) {
|
|
311
|
+
throw new Error('No export handler: downloads need a browser document; pass CereusDB.create({ onExport }) elsewhere');
|
|
312
|
+
}
|
|
313
|
+
const data = await this.exportGeoParquet(queryOrTable, exportOptions);
|
|
314
|
+
const name = filename ?? defaultExportFilename(queryOrTable);
|
|
315
|
+
await handler(name, data, PARQUET_MIME_TYPE);
|
|
316
|
+
return name;
|
|
317
|
+
}
|
|
318
|
+
/** Rewrite all tables of a persistent database into one segment each. */
|
|
319
|
+
async compactDatabase(name) {
|
|
320
|
+
await this.inner.compact_database(name);
|
|
321
|
+
}
|
|
322
|
+
/** Write pending changes of persistent databases (for example after registerGeoJSON()). */
|
|
323
|
+
async flush() {
|
|
324
|
+
await this.inner.flush();
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* List the tables and views of all databases. By default only the table
|
|
328
|
+
* names are returned, which is ambiguous when databases contain tables with
|
|
329
|
+
* the same name; pass `{ qualified: true }` for `database.schema.table`
|
|
330
|
+
* names, or use {@link CereusDB.catalog} for full details.
|
|
331
|
+
*/
|
|
332
|
+
tables(options = {}) {
|
|
333
|
+
return options.qualified ? this.inner.qualified_tables() : this.inner.tables();
|
|
334
|
+
}
|
|
335
|
+
/** Version string. */
|
|
336
|
+
version() {
|
|
337
|
+
return this.inner.version();
|
|
338
|
+
}
|
|
339
|
+
objectStoreApi() {
|
|
340
|
+
const api = this.inner;
|
|
341
|
+
if (typeof api.register_object_stores !== 'function' ||
|
|
342
|
+
typeof api.register_parquet_table !== 'function') {
|
|
343
|
+
throw new Error('Browser object stores are not available in this CereusDB build');
|
|
344
|
+
}
|
|
345
|
+
return api;
|
|
346
|
+
}
|
|
347
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type StorageBackend } from './storage.js';
|
|
2
|
+
export { MemoryStorageBackend, OPFSStorageBackend, type OPFSStorageBackendOptions, type StorageBackend, } from './storage.js';
|
|
1
3
|
export interface QueryResult {
|
|
2
4
|
/** Raw JSON data parsed from query */
|
|
3
5
|
data: Record<string, unknown>[];
|
|
@@ -15,7 +17,127 @@ export interface CereusDBOptions {
|
|
|
15
17
|
wasmSource?: RequestInfo | URL | Response | BufferSource | WebAssembly.Module | Promise<Response>;
|
|
16
18
|
/** Browser-backed object stores to register at startup. */
|
|
17
19
|
objectStores?: ObjectStoreRegistryConfig;
|
|
20
|
+
/**
|
|
21
|
+
* Storage backends for persistent databases, by URL scheme. `opfs` defaults
|
|
22
|
+
* to an {@link OPFSStorageBackend} when the browser supports OPFS; pass
|
|
23
|
+
* `false` to disable it.
|
|
24
|
+
*/
|
|
25
|
+
storage?: StorageOptions;
|
|
26
|
+
/**
|
|
27
|
+
* Persistent databases to open at startup, for example `['opfs://mydb']`.
|
|
28
|
+
* Each is created if it does not exist yet.
|
|
29
|
+
*/
|
|
30
|
+
attach?: string[];
|
|
31
|
+
/**
|
|
32
|
+
* Receives files written by `COPY ... TO '<file>'` and
|
|
33
|
+
* {@link CereusDB.downloadGeoParquet}. Defaults to {@link downloadFile}
|
|
34
|
+
* (a browser download) when a DOM `document` is available; pass `false` to
|
|
35
|
+
* disable exports or a function to handle them yourself (for example in a
|
|
36
|
+
* Web Worker or in Node.js).
|
|
37
|
+
*/
|
|
38
|
+
onExport?: ExportHandler | false;
|
|
39
|
+
}
|
|
40
|
+
/** Receives an exported file. */
|
|
41
|
+
export type ExportHandler = (filename: string, data: Uint8Array, mimeType: string) => void | Promise<void>;
|
|
42
|
+
/** MIME type of GeoParquet exports. */
|
|
43
|
+
export declare const PARQUET_MIME_TYPE = "application/vnd.apache.parquet";
|
|
44
|
+
/**
|
|
45
|
+
* Save a file with the browser's download mechanism. Needs a DOM `document`
|
|
46
|
+
* (the main thread); it does not work in Web Workers or Node.js.
|
|
47
|
+
*/
|
|
48
|
+
export declare function downloadFile(filename: string, data: Uint8Array, mimeType?: string): void;
|
|
49
|
+
export interface StorageOptions {
|
|
50
|
+
opfs?: StorageBackend | false;
|
|
51
|
+
[scheme: string]: StorageBackend | false | undefined;
|
|
52
|
+
}
|
|
53
|
+
export interface CreateDatabaseOptions {
|
|
54
|
+
/** Database (catalog) name. Defaults to the lowercased name in the location URL. */
|
|
55
|
+
name?: string;
|
|
56
|
+
/** Open the database if it already exists instead of failing. */
|
|
57
|
+
ifNotExists?: boolean;
|
|
58
|
+
}
|
|
59
|
+
export interface AttachDatabaseOptions {
|
|
60
|
+
/** Database (catalog) name. Defaults to the lowercased name in the location URL. */
|
|
61
|
+
name?: string;
|
|
62
|
+
/** Succeed if the database is already attached under this name. */
|
|
63
|
+
ifNotExists?: boolean;
|
|
64
|
+
}
|
|
65
|
+
export interface DatabaseListing {
|
|
66
|
+
/** Catalog name, or `null` for stored databases that are not attached. */
|
|
67
|
+
name: string | null;
|
|
68
|
+
/** `memory` or the storage scheme, for example `opfs`. */
|
|
69
|
+
storage: string;
|
|
70
|
+
/** Location URL for persistent databases. */
|
|
71
|
+
location: string | null;
|
|
72
|
+
attached: boolean;
|
|
73
|
+
}
|
|
74
|
+
export interface CatalogColumn {
|
|
75
|
+
name: string;
|
|
76
|
+
/** Arrow data type. */
|
|
77
|
+
type: string;
|
|
78
|
+
nullable: boolean;
|
|
79
|
+
/** Arrow extension type, for example `geoarrow.wkb` for geometry columns. */
|
|
80
|
+
extension?: string;
|
|
81
|
+
}
|
|
82
|
+
export interface CatalogTable {
|
|
83
|
+
name: string;
|
|
84
|
+
type: 'BASE TABLE' | 'VIEW' | 'LOCAL TEMPORARY';
|
|
85
|
+
columns: CatalogColumn[];
|
|
86
|
+
}
|
|
87
|
+
export interface CatalogSchema {
|
|
88
|
+
name: string;
|
|
89
|
+
tables: CatalogTable[];
|
|
90
|
+
}
|
|
91
|
+
export interface CatalogDatabase {
|
|
92
|
+
name: string;
|
|
93
|
+
storage: string;
|
|
94
|
+
location: string | null;
|
|
95
|
+
schemas: CatalogSchema[];
|
|
96
|
+
}
|
|
97
|
+
/** Columns as `{ name: 'SQL type' }`, or a query for CREATE TABLE ... AS. */
|
|
98
|
+
export type CreateTableDefinition = {
|
|
99
|
+
columns: Record<string, string>;
|
|
100
|
+
} | {
|
|
101
|
+
as: string;
|
|
102
|
+
};
|
|
103
|
+
export interface GeoParquetExportOptions {
|
|
104
|
+
/** Parquet compression codec. Defaults to `zstd`. */
|
|
105
|
+
compression?: 'zstd' | 'snappy' | 'lz4' | 'gzip' | 'brotli' | 'uncompressed';
|
|
106
|
+
/** Maximum number of rows per row group. */
|
|
107
|
+
rowGroupSize?: number;
|
|
108
|
+
}
|
|
109
|
+
export interface DownloadGeoParquetOptions extends GeoParquetExportOptions {
|
|
110
|
+
/**
|
|
111
|
+
* File name. Defaults to `<table>.parquet` for a table and
|
|
112
|
+
* `export.parquet` for a query.
|
|
113
|
+
*/
|
|
114
|
+
filename?: string;
|
|
18
115
|
}
|
|
116
|
+
export interface CreateTableOptions {
|
|
117
|
+
ifNotExists?: boolean;
|
|
118
|
+
orReplace?: boolean;
|
|
119
|
+
}
|
|
120
|
+
export type AlterTableOperation = {
|
|
121
|
+
renameTo: string;
|
|
122
|
+
} | {
|
|
123
|
+
addColumn: {
|
|
124
|
+
name: string;
|
|
125
|
+
/** SQL type, for example `VARCHAR` or `DOUBLE`. */
|
|
126
|
+
type: string;
|
|
127
|
+
notNull?: boolean;
|
|
128
|
+
/** SQL expression used for existing rows and as column default. */
|
|
129
|
+
default?: string;
|
|
130
|
+
};
|
|
131
|
+
ifNotExists?: boolean;
|
|
132
|
+
} | {
|
|
133
|
+
dropColumn: string | string[];
|
|
134
|
+
ifExists?: boolean;
|
|
135
|
+
} | {
|
|
136
|
+
renameColumn: {
|
|
137
|
+
from: string;
|
|
138
|
+
to: string;
|
|
139
|
+
};
|
|
140
|
+
};
|
|
19
141
|
export type RasterFormat = 'geotiff' | 'tiff';
|
|
20
142
|
export type ObjectStoreProvider = 'http' | 's3' | 'gcs' | 'azure';
|
|
21
143
|
export interface ObjectStoreRegistryConfig {
|
|
@@ -42,6 +164,8 @@ export interface RegisterParquetTableOptions {
|
|
|
42
164
|
}
|
|
43
165
|
export declare class CereusDB {
|
|
44
166
|
private inner;
|
|
167
|
+
private storageBackends;
|
|
168
|
+
private exportHandler?;
|
|
45
169
|
private constructor();
|
|
46
170
|
/**
|
|
47
171
|
* Create and initialize a new CereusDB instance.
|
|
@@ -88,10 +212,81 @@ export declare class CereusDB {
|
|
|
88
212
|
* Requires the full GDAL-enabled package build.
|
|
89
213
|
*/
|
|
90
214
|
registerGeoTIFF(name: string, data: BufferSource): void;
|
|
91
|
-
/**
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Drop a table. The table is removed immediately; for tables of persistent
|
|
217
|
+
* databases the returned promise resolves once the change is written.
|
|
218
|
+
*/
|
|
219
|
+
dropTable(name: string): Promise<void>;
|
|
220
|
+
/**
|
|
221
|
+
* Create a persistent database, for example `opfs://mydb`, and attach it
|
|
222
|
+
* as a catalog. Equivalent to `CREATE DATABASE 'opfs://mydb'`.
|
|
223
|
+
* Resolves to the database name.
|
|
224
|
+
*/
|
|
225
|
+
createDatabase(location: string, options?: CreateDatabaseOptions): Promise<string>;
|
|
226
|
+
/**
|
|
227
|
+
* Open an existing persistent database. Equivalent to
|
|
228
|
+
* `ATTACH 'opfs://mydb' [AS name]`. Resolves to the database name.
|
|
229
|
+
*/
|
|
230
|
+
attachDatabase(location: string, options?: AttachDatabaseOptions): Promise<string>;
|
|
231
|
+
/** Write pending changes and close a persistent database. Equivalent to `DETACH name`. */
|
|
232
|
+
detachDatabase(name: string, options?: {
|
|
233
|
+
ifExists?: boolean;
|
|
234
|
+
}): Promise<void>;
|
|
235
|
+
/**
|
|
236
|
+
* Drop a database by name or location. Persistent databases are deleted
|
|
237
|
+
* from storage. Equivalent to `DROP DATABASE`.
|
|
238
|
+
*/
|
|
239
|
+
dropDatabase(nameOrLocation: string, options?: {
|
|
240
|
+
ifExists?: boolean;
|
|
241
|
+
}): Promise<void>;
|
|
242
|
+
/** Set the default database (and schema). Equivalent to `USE database[.schema]`. */
|
|
243
|
+
useDatabase(database: string, schema?: string): Promise<void>;
|
|
244
|
+
/** Attached databases plus stored databases that are not attached. */
|
|
245
|
+
listDatabases(): Promise<DatabaseListing[]>;
|
|
246
|
+
/** Describe all databases, schemas, tables and views. */
|
|
247
|
+
catalog(): Promise<CatalogDatabase[]>;
|
|
248
|
+
/**
|
|
249
|
+
* Create a table from column definitions or a query. `name` may be
|
|
250
|
+
* qualified (`mydb.public.cities`).
|
|
251
|
+
*/
|
|
252
|
+
createTable(name: string, definition: CreateTableDefinition, options?: CreateTableOptions): Promise<void>;
|
|
253
|
+
/** Rename a table or add, drop, or rename a column. */
|
|
254
|
+
alterTable(name: string, operation: AlterTableOperation): Promise<void>;
|
|
255
|
+
/**
|
|
256
|
+
* Insert Arrow IPC data (stream or file format, for example from
|
|
257
|
+
* `tableToIPC()` of apache-arrow) into a table. Columns are matched by
|
|
258
|
+
* name; missing columns are filled with NULL. Resolves to the number of
|
|
259
|
+
* inserted rows.
|
|
260
|
+
*/
|
|
261
|
+
insertArrow(table: string, data: BufferSource): Promise<number>;
|
|
262
|
+
/**
|
|
263
|
+
* Export a table or query result as a GeoParquet (1.1) file. Geometry and
|
|
264
|
+
* geography columns are described in the file's `geo` metadata.
|
|
265
|
+
*
|
|
266
|
+
* @param queryOrTable A query (`SELECT ...`, `WITH ...`) or a table name.
|
|
267
|
+
*/
|
|
268
|
+
exportGeoParquet(queryOrTable: string, options?: GeoParquetExportOptions): Promise<Uint8Array>;
|
|
269
|
+
/**
|
|
270
|
+
* Export a table or query result as GeoParquet and pass it to the export
|
|
271
|
+
* handler: a browser download by default (see `onExport` in
|
|
272
|
+
* {@link CereusDBOptions}). Resolves to the file name.
|
|
273
|
+
*
|
|
274
|
+
* @param queryOrTable A query (`SELECT ...`, `WITH ...`) or a table name.
|
|
275
|
+
*/
|
|
276
|
+
downloadGeoParquet(queryOrTable: string, options?: DownloadGeoParquetOptions): Promise<string>;
|
|
277
|
+
/** Rewrite all tables of a persistent database into one segment each. */
|
|
278
|
+
compactDatabase(name: string): Promise<void>;
|
|
279
|
+
/** Write pending changes of persistent databases (for example after registerGeoJSON()). */
|
|
280
|
+
flush(): Promise<void>;
|
|
281
|
+
/**
|
|
282
|
+
* List the tables and views of all databases. By default only the table
|
|
283
|
+
* names are returned, which is ambiguous when databases contain tables with
|
|
284
|
+
* the same name; pass `{ qualified: true }` for `database.schema.table`
|
|
285
|
+
* names, or use {@link CereusDB.catalog} for full details.
|
|
286
|
+
*/
|
|
287
|
+
tables(options?: {
|
|
288
|
+
qualified?: boolean;
|
|
289
|
+
}): string[];
|
|
95
290
|
/** Version string. */
|
|
96
291
|
version(): string;
|
|
97
292
|
private objectStoreApi;
|