Skip to content

Commit 0b18d27

Browse files
committed
Bump version, update typings and refactor browser extensions
1 parent aade044 commit 0b18d27

27 files changed

Lines changed: 353 additions & 663 deletions

README.md

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ options:
146146

147147
### `.version`() -> `VersionInfo`
148148

149-
Returns an object with version strings for all bundled libraries: `spatialite`, `sqlite`, `geos`, `proj`, `rttopo`, `spl.js`.
149+
Returns an object with version strings for all bundled libraries: `spatialite`, `sqlite`, `geos`, `proj`, `rttopo`, `spl`.
150150

151151
### `.terminate`()
152152

@@ -258,42 +258,56 @@ Free result memory. Called automatically after accessing result properties, but
258258

259259
Sometimes you want to run code inside the WebWorker. With this API you can supply additional functions to extend the `SPL` and `DB` APIs executed inside the WebWorker.
260260

261+
Extension functions are available via the `ex` namespace: `db.ex.myFn()` and `spl.ex.myFn()`.
262+
261263
Example: https://jvail.github.io/spl.js/examples/extensions.html
262264

265+
### TypeScript Types
266+
267+
Two extension types are available:
268+
269+
- `DbExtension` - extends the database API, functions receive `DbSync` as first argument
270+
- `SplExtension` - extends the SPL API, functions receive `SplSync` as first argument
271+
272+
```ts
273+
import type { DbExtension, SplExtension } from 'spl.js';
274+
```
275+
263276
### Example Code
264277

265278
```js
266-
const extensions = [
267-
{
268-
extends: 'db',
269-
fns: {
270-
'tables': db => db.exec('SELECT name FROM sqlite_master WHERE type=\'table\''),
271-
'master': (db, type) => db.exec('SELECT name FROM sqlite_master WHERE type=?', [type])
272-
}
273-
},
274-
{
275-
extends: 'spl',
276-
fns: {
277-
'spatialite_version': spl => {
278-
const db = spl.db();
279-
const version = db.exec('SELECT spatialite_version()').get.first;
280-
db.close();
281-
return version;
282-
}
279+
/** @type {import('spl.js').DbExtension} */
280+
const dbExtension = {
281+
extends: 'db',
282+
fns: {
283+
tables: db => db.exec('SELECT name FROM sqlite_master WHERE type=\'table\''),
284+
master: (db, type) => db.exec('SELECT name FROM sqlite_master WHERE type=?', [type])
285+
}
286+
};
287+
288+
/** @type {import('spl.js').SplExtension} */
289+
const splExtension = {
290+
extends: 'spl',
291+
fns: {
292+
spatialite_version: spl => {
293+
const db = spl.db();
294+
const version = db.exec('SELECT spatialite_version()').get.first;
295+
db.close();
296+
return version;
283297
}
284298
}
285-
];
299+
};
286300

287-
const spl = await SPL({}, extensions);
301+
const spl = await SPL({}, [dbExtension, splExtension]);
288302
const db = await spl.db()
289303
.read(`
290304
CREATE TABLE hello (world);
291305
CREATE VIEW hello_view AS SELECT * FROM hello;
292306
`);
293307

294-
console.assert(await db.tables().get.first === 'hello');
295-
console.assert(await db.master('view').get.first === 'hello_view');
296-
console.assert(await spl.spatialite_version() === '5.1.1-rc0');
308+
console.assert(await db.ex.tables().get.first === 'hello');
309+
console.assert(await db.ex.master('view').get.first === 'hello_view');
310+
console.assert(await spl.ex.spatialite_version() === '5.1.1-rc0');
297311
```
298312

299313
## Building and Testing

dist/index.d.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ export type Spl = {
6969
*/
7070
fs: SplFs;
7171
};
72-
import type { SplOptions } from './typedefs.js';
73-
import type { ResultData } from './typedefs.js';
74-
import type { VersionInfo } from './typedefs.js';
72+
export type { SplOptions } from './typedefs.js';
73+
export type { ResultData } from './typedefs.js';
74+
export type { VersionInfo } from './typedefs.js';

dist/index.d.ts

Lines changed: 42 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,13 @@
1-
declare function _default(options?: SplOptions, extensions?: Extension[]): Promise<Spl>;
1+
declare function _default(options?: SplOptions, extensions?: (DbExtension | SplExtension)[]): Promise<Spl>;
22
export default _default;
3-
/**
4-
* - A value that is both T and awaitable as T
5-
*/
63
export type Thenable<T> = T & PromiseLike<T>;
7-
/**
8-
* Query result accessor (async)
9-
*/
10-
export type Result = {
11-
/**
12-
* - First value of first row
13-
*/
14-
first: Promise<any>;
15-
/**
16-
* - Flat array of all values
17-
*/
18-
flat: Promise<any[]>;
19-
/**
20-
* - Array of row arrays
21-
*/
22-
rows: Promise<any[][]>;
23-
/**
24-
* - Column names
25-
*/
26-
cols: Promise<string[]>;
27-
/**
28-
* - Array of row objects
29-
*/
30-
objs: Promise<any[]>;
31-
/**
32-
* - Synchronous result (transfers ArrayBuffers)
33-
*/
34-
sync: Promise<ResultData>;
35-
/**
36-
* - Free result memory
37-
*/
38-
free: () => Promise<void>;
39-
};
40-
/**
41-
* Database instance
42-
*/
43-
export type Db = {
44-
/**
45-
* - Attach another database
46-
*/
47-
attach: (db: string, schema: string) => Thenable<Db>;
48-
/**
49-
* - Detach a database
50-
*/
51-
detach: (schema: string) => Thenable<Db>;
52-
/**
53-
* - Execute SQL
54-
*/
55-
exec: (sql: string, parameters?: any[] | {
56-
[name: string]: any;
57-
}) => Thenable<Db>;
58-
/**
59-
* - Read and execute a SQL script
60-
*/
61-
read: (sql: string) => Thenable<Db>;
62-
/**
63-
* - Load database from path
64-
*/
65-
load: (src: string) => Thenable<Db>;
66-
/**
67-
* - Save database to ArrayBuffer
68-
*/
69-
save: () => Thenable<Db>;
70-
/**
71-
* - Close the database
72-
*/
73-
close: () => Thenable<Spl>;
74-
/**
75-
* - Get query results
76-
*/
77-
get: Result;
78-
};
79-
/**
80-
* Filesystem operations (Browser)
81-
*/
824
export type SplFs = {
835
/**
84-
* - Mount files to virtual filesystem
6+
* - Mount files
857
*/
868
mount: (path: string, options?: MountOption[]) => Thenable<Spl>;
879
/**
88-
* - Unmount from virtual filesystem
10+
* - Unmount files
8911
*/
9012
unmount: (path: string) => Thenable<Spl>;
9113
/**
@@ -105,18 +27,15 @@ export type SplFs = {
10527
*/
10628
mkdir: (path: string) => Thenable<Spl>;
10729
};
108-
/**
109-
* SPL instance
110-
*/
11130
export type Spl = {
11231
/**
11332
* - Open a database
11433
*/
115-
db: (path?: string | ArrayBuffer) => Thenable<Db>;
34+
db: (path?: string | ArrayBuffer) => Db;
11635
/**
11736
* - Get version info
11837
*/
119-
version: () => Thenable<Spl>;
38+
version: () => Thenable<VersionInfo>;
12039
/**
12140
* - Terminate the WebWorker
12241
*/
@@ -125,8 +44,41 @@ export type Spl = {
12544
* - Filesystem operations
12645
*/
12746
fs: SplFs;
47+
/**
48+
* - User-defined extension functions
49+
*/
50+
ex: {
51+
[x: string]: Function;
52+
};
53+
};
54+
export type Db = {
55+
attach: (db: string, schema: string) => Thenable<Db>;
56+
detach: (schema: string) => Thenable<Db>;
57+
exec: (sql: string, par?: any) => Thenable<Db>;
58+
read: (sql: string) => Thenable<Db>;
59+
load: (src: string) => Thenable<Db>;
60+
save: () => Thenable<Db>;
61+
close: () => Thenable<Spl>;
62+
get: Result;
63+
/**
64+
* - User-defined extension functions
65+
*/
66+
ex: {
67+
[x: string]: Function;
68+
};
69+
};
70+
export type Result = {
71+
first: Promise<any>;
72+
flat: Promise<any[]>;
73+
rows: Promise<any[][]>;
74+
cols: Promise<string[]>;
75+
objs: Promise<any[]>;
76+
sync: Promise<ResultData>;
77+
free: () => Promise<void>;
12878
};
129-
import type { SplOptions } from './typedefs.js';
130-
import type { Extension } from './typedefs.js';
131-
import type { ResultData } from './typedefs.js';
132-
import type { MountOption } from './typedefs.js';
79+
export type { SplOptions } from './typedefs.js';
80+
export type { DbExtension } from './typedefs.js';
81+
export type { SplExtension } from './typedefs.js';
82+
export type { MountOption } from './typedefs.js';
83+
export type { VersionInfo } from './typedefs.js';
84+
export type { ResultData } from './typedefs.js';

dist/index.js

Lines changed: 2 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.mjs

Lines changed: 1 addition & 38 deletions
Large diffs are not rendered by default.

dist/result.d.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

dist/spl-node.d.mts

Lines changed: 0 additions & 4 deletions
This file was deleted.

dist/spl-node.mjs

Lines changed: 0 additions & 1 deletion
This file was deleted.

dist/spl-web.d.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.

dist/spl-web.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)