fastdb4ts 0.0.1
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 +270 -0
- package/dist/column.d.ts +23 -0
- package/dist/column.js +118 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.js +24 -0
- package/dist/feature.d.ts +25 -0
- package/dist/feature.js +167 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +9 -0
- package/dist/orm.d.ts +27 -0
- package/dist/orm.js +185 -0
- package/dist/schema.d.ts +23 -0
- package/dist/schema.js +93 -0
- package/dist/serializer.d.ts +5 -0
- package/dist/serializer.js +616 -0
- package/dist/table.d.ts +22 -0
- package/dist/table.js +91 -0
- package/dist/types.d.ts +45 -0
- package/dist/types.js +115 -0
- package/dist/wasm/fastdb4ts.d.ts +5 -0
- package/dist/wasm/fastdb4ts.js +2 -0
- package/dist/wasm/fastdb4ts.wasm +0 -0
- package/dist/wasm-loader.d.ts +146 -0
- package/dist/wasm-loader.js +21 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# fastdb4ts
|
|
2
|
+
|
|
3
|
+
Browser-oriented TypeScript bindings for `fastdb`, powered by WebAssembly.
|
|
4
|
+
|
|
5
|
+
`fastdb4ts` brings the `fastdb` storage model to TypeScript with a browser-first API centered around typed schemas, structured table access, binary buffer transport, and graph serialization.
|
|
6
|
+
|
|
7
|
+
## What this package provides
|
|
8
|
+
|
|
9
|
+
- **Typed schemas**
|
|
10
|
+
- model records with `Feature` subclasses and `defineSchema(...)`
|
|
11
|
+
- **Structured table access**
|
|
12
|
+
- use `ORM`, `TableDefn`, `Table`, and column accessors
|
|
13
|
+
- **Binary buffer roundtrips**
|
|
14
|
+
- import/export databases as `Uint8Array` / `ArrayBuffer`
|
|
15
|
+
- **Graph serialization**
|
|
16
|
+
- use `FastSerializer` for nested features, lists, and cyclic references
|
|
17
|
+
- **Shared native semantics**
|
|
18
|
+
- backed by the same C++ core as `fastdb4py`
|
|
19
|
+
|
|
20
|
+
## Current scope
|
|
21
|
+
|
|
22
|
+
This package currently targets the browser/WebAssembly use case.
|
|
23
|
+
|
|
24
|
+
Included:
|
|
25
|
+
|
|
26
|
+
- browser-first WASM runtime
|
|
27
|
+
- `Feature` + `defineSchema(...)`
|
|
28
|
+
- `ORM`, `TableDefn`, `Table`, column access
|
|
29
|
+
- `FastSerializer`
|
|
30
|
+
- `Uint8Array` / `ArrayBuffer` import/export
|
|
31
|
+
|
|
32
|
+
Not included in the current TS package:
|
|
33
|
+
|
|
34
|
+
- shared-memory IPC
|
|
35
|
+
- direct filesystem persistence APIs
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install fastdb4ts
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
If you are working from the repository source instead of npm:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
bash ts/build-wasm.sh
|
|
47
|
+
npm --prefix ts/fastdb4ts run build
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Quick start
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import {
|
|
54
|
+
F64,
|
|
55
|
+
Feature,
|
|
56
|
+
ORM,
|
|
57
|
+
TableDefn,
|
|
58
|
+
defineSchema,
|
|
59
|
+
initFastdb,
|
|
60
|
+
} from 'fastdb4ts';
|
|
61
|
+
|
|
62
|
+
await initFastdb();
|
|
63
|
+
|
|
64
|
+
class Point extends Feature {
|
|
65
|
+
static schema = defineSchema({
|
|
66
|
+
x: F64,
|
|
67
|
+
y: F64,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const db = ORM.truncate([new TableDefn(Point, 3)]);
|
|
72
|
+
const table = db.table(Point);
|
|
73
|
+
|
|
74
|
+
table.column.x.fill([1.5, 2.5, 3.5]);
|
|
75
|
+
table.column.y.fill([4.5, 5.5, 6.5]);
|
|
76
|
+
|
|
77
|
+
console.log(table.get(1).x); // 2.5
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Defining schemas
|
|
81
|
+
|
|
82
|
+
Unlike `fastdb4py`, the TypeScript package uses explicit schema definitions rather than Python annotations.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import {
|
|
86
|
+
F64,
|
|
87
|
+
Feature,
|
|
88
|
+
I32,
|
|
89
|
+
STR,
|
|
90
|
+
defineSchema,
|
|
91
|
+
listOf,
|
|
92
|
+
ref,
|
|
93
|
+
} from 'fastdb4ts';
|
|
94
|
+
|
|
95
|
+
class Point extends Feature {
|
|
96
|
+
static schema = defineSchema({
|
|
97
|
+
x: F64,
|
|
98
|
+
y: F64,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
class Line extends Feature {
|
|
103
|
+
static schema = defineSchema({
|
|
104
|
+
id: I32,
|
|
105
|
+
name: STR,
|
|
106
|
+
points: listOf(Point),
|
|
107
|
+
first: ref(Point),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Common field helpers:
|
|
113
|
+
|
|
114
|
+
- `U8`, `U16`, `U32`, `I32`
|
|
115
|
+
- `F32`, `F64`
|
|
116
|
+
- `STR`, `WSTR`
|
|
117
|
+
- `BYTES`
|
|
118
|
+
- `ref(TargetFeature)`
|
|
119
|
+
- `listOf(...)`
|
|
120
|
+
|
|
121
|
+
## Working with tables
|
|
122
|
+
|
|
123
|
+
### Fixed-size tables
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { F64, Feature, ORM, TableDefn, defineSchema, initFastdb } from 'fastdb4ts';
|
|
127
|
+
|
|
128
|
+
await initFastdb();
|
|
129
|
+
|
|
130
|
+
class Particle extends Feature {
|
|
131
|
+
static schema = defineSchema({
|
|
132
|
+
x: F64,
|
|
133
|
+
y: F64,
|
|
134
|
+
vx: F64,
|
|
135
|
+
vy: F64,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const db = ORM.truncate([new TableDefn(Particle, 4)]);
|
|
140
|
+
const table = db.table(Particle);
|
|
141
|
+
|
|
142
|
+
table.column.x.fill([0.0, 1.0, 2.0, 3.0]);
|
|
143
|
+
table.column.y.fill([10.0, 20.0, 30.0, 40.0]);
|
|
144
|
+
table.column.vx.fill([0.1, 0.1, 0.2, 0.2]);
|
|
145
|
+
table.column.vy.fill([0.0, 0.0, 0.0, 0.0]);
|
|
146
|
+
|
|
147
|
+
console.log(table.get(2).x); // 2.0
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Buffer roundtrip
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
const bytes = db.toBuffer();
|
|
154
|
+
const copy = ORM.fromBuffer(bytes);
|
|
155
|
+
const copyTable = copy.table(Particle);
|
|
156
|
+
|
|
157
|
+
console.log(copyTable.get(2).x); // 2.0
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## FastSerializer
|
|
161
|
+
|
|
162
|
+
`FastSerializer` is the graph-oriented layer for feature graphs that are more naturally represented as nested objects than flat tables.
|
|
163
|
+
|
|
164
|
+
Supported scenarios include:
|
|
165
|
+
|
|
166
|
+
- nested features
|
|
167
|
+
- scalar lists
|
|
168
|
+
- string lists
|
|
169
|
+
- feature references
|
|
170
|
+
- cyclic references
|
|
171
|
+
- numeric-list columnar auxiliary layers
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import {
|
|
175
|
+
F64,
|
|
176
|
+
FastSerializer,
|
|
177
|
+
Feature,
|
|
178
|
+
I32,
|
|
179
|
+
defineSchema,
|
|
180
|
+
initFastdb,
|
|
181
|
+
listOf,
|
|
182
|
+
} from 'fastdb4ts';
|
|
183
|
+
|
|
184
|
+
await initFastdb();
|
|
185
|
+
|
|
186
|
+
class Point extends Feature {
|
|
187
|
+
static schema = defineSchema({ x: F64, y: F64 });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
class Line extends Feature {
|
|
191
|
+
static schema = defineSchema({
|
|
192
|
+
id: I32,
|
|
193
|
+
points: listOf(Point),
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const line = new Line({
|
|
198
|
+
id: 42,
|
|
199
|
+
points: [new Point({ x: 1.0, y: 2.0 }), new Point({ x: 3.0, y: 4.0 })],
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const bytes = FastSerializer.dumps(line);
|
|
203
|
+
const copy = FastSerializer.loads(bytes, Line);
|
|
204
|
+
|
|
205
|
+
console.log(copy.points[1].y); // 4.0
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Cycles are preserved:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { Feature, I32, FastSerializer, defineSchema, initFastdb, ref } from 'fastdb4ts';
|
|
212
|
+
|
|
213
|
+
await initFastdb();
|
|
214
|
+
|
|
215
|
+
class Node extends Feature {
|
|
216
|
+
static schema = defineSchema({
|
|
217
|
+
val: I32,
|
|
218
|
+
next: ref(() => Node),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const a = new Node({ val: 1 });
|
|
223
|
+
const b = new Node({ val: 2 });
|
|
224
|
+
a.next = b;
|
|
225
|
+
b.next = a;
|
|
226
|
+
|
|
227
|
+
const copy = FastSerializer.loads(FastSerializer.dumps(a), Node);
|
|
228
|
+
console.log(copy.next.next === copy); // true
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Build
|
|
232
|
+
|
|
233
|
+
From the repository root:
|
|
234
|
+
|
|
235
|
+
```bash
|
|
236
|
+
npm --prefix ts/fastdb4ts run build
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
To rebuild the WASM module as well:
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
bash ts/build-wasm.sh
|
|
243
|
+
npm --prefix ts/fastdb4ts run build
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Tests
|
|
247
|
+
|
|
248
|
+
Pure TypeScript tests live under `tests/ts/`.
|
|
249
|
+
|
|
250
|
+
Run them from the repository root:
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
npm run test:ts
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
Serializer interop validation is also available:
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
npm --prefix ts/fastdb4ts run test:serializer:interop
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Repository documentation
|
|
263
|
+
|
|
264
|
+
For fuller project documentation, see:
|
|
265
|
+
|
|
266
|
+
- repository overview: [`README.md`](../../README.md)
|
|
267
|
+
- TypeScript/WASM binding guide: [`ts/README.md`](../README.md)
|
|
268
|
+
- C++ core guide: [`fastcarto/README.md`](../../fastcarto/README.md)
|
|
269
|
+
- Python binding guide: [`python/README.md`](../../python/README.md)
|
|
270
|
+
|
package/dist/column.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { FastdbModule, WxLayerTableHandle } from './wasm-loader.js';
|
|
2
|
+
export declare class StridedColumn {
|
|
3
|
+
readonly length: number;
|
|
4
|
+
readonly fieldIndex: number;
|
|
5
|
+
readonly byteOffset: number;
|
|
6
|
+
readonly stride: number;
|
|
7
|
+
readonly kind: number;
|
|
8
|
+
private readonly module;
|
|
9
|
+
private readonly layer;
|
|
10
|
+
private readonly basePtr;
|
|
11
|
+
constructor(module: FastdbModule, layer: WxLayerTableHandle, fieldIndex: number, kind: number);
|
|
12
|
+
get(index: number): number;
|
|
13
|
+
set(index: number, value: number): void;
|
|
14
|
+
fill(values: ArrayLike<number>): void;
|
|
15
|
+
toArray(): TypedArray;
|
|
16
|
+
forEach(fn: (value: number, index: number) => void): void;
|
|
17
|
+
private getElementOffset;
|
|
18
|
+
private normalizeIndex;
|
|
19
|
+
private getArrayConstructor;
|
|
20
|
+
private getDataView;
|
|
21
|
+
}
|
|
22
|
+
type TypedArray = Uint8Array | Uint16Array | Uint32Array | Int32Array | Float32Array | Float64Array;
|
|
23
|
+
export {};
|
package/dist/column.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { FastdbRuntimeError } from './errors.js';
|
|
2
|
+
export class StridedColumn {
|
|
3
|
+
length;
|
|
4
|
+
fieldIndex;
|
|
5
|
+
byteOffset;
|
|
6
|
+
stride;
|
|
7
|
+
kind;
|
|
8
|
+
module;
|
|
9
|
+
layer;
|
|
10
|
+
basePtr;
|
|
11
|
+
constructor(module, layer, fieldIndex, kind) {
|
|
12
|
+
this.module = module;
|
|
13
|
+
this.layer = layer;
|
|
14
|
+
this.fieldIndex = fieldIndex;
|
|
15
|
+
this.kind = kind;
|
|
16
|
+
this.length = layer.getFeatureCount();
|
|
17
|
+
this.byteOffset = layer.getFieldOffset(fieldIndex);
|
|
18
|
+
this.stride = layer.getFeatureByteSize();
|
|
19
|
+
if (this.length > 0) {
|
|
20
|
+
const feature = layer.tryGetFeatureAt(0);
|
|
21
|
+
this.basePtr = feature.getAddress() + this.byteOffset;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this.basePtr = 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
get(index) {
|
|
28
|
+
const offset = this.getElementOffset(index);
|
|
29
|
+
const view = this.getDataView();
|
|
30
|
+
if (this.kind === this.module.ftU8 || this.kind === this.module.ftU8n) {
|
|
31
|
+
return view.getUint8(offset);
|
|
32
|
+
}
|
|
33
|
+
if (this.kind === this.module.ftU16 || this.kind === this.module.ftU16n) {
|
|
34
|
+
return view.getUint16(offset, true);
|
|
35
|
+
}
|
|
36
|
+
if (this.kind === this.module.ftU32) {
|
|
37
|
+
return view.getUint32(offset, true);
|
|
38
|
+
}
|
|
39
|
+
if (this.kind === this.module.ftI32) {
|
|
40
|
+
return view.getInt32(offset, true);
|
|
41
|
+
}
|
|
42
|
+
if (this.kind === this.module.ftF32) {
|
|
43
|
+
return view.getFloat32(offset, true);
|
|
44
|
+
}
|
|
45
|
+
if (this.kind === this.module.ftF64) {
|
|
46
|
+
return view.getFloat64(offset, true);
|
|
47
|
+
}
|
|
48
|
+
throw new FastdbRuntimeError(`Field index ${this.fieldIndex} is not a numeric column.`);
|
|
49
|
+
}
|
|
50
|
+
set(index, value) {
|
|
51
|
+
const feature = this.layer.tryGetFeatureAt(this.normalizeIndex(index));
|
|
52
|
+
if (this.kind === this.module.ftF32 ||
|
|
53
|
+
this.kind === this.module.ftF64 ||
|
|
54
|
+
this.kind === this.module.ftU8n ||
|
|
55
|
+
this.kind === this.module.ftU16n) {
|
|
56
|
+
feature.setFieldDouble(this.fieldIndex, value);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
feature.setFieldInt(this.fieldIndex, Math.trunc(value));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
fill(values) {
|
|
63
|
+
if (values.length !== this.length) {
|
|
64
|
+
throw new FastdbRuntimeError(`Column fill length mismatch: expected ${this.length}, got ${values.length}.`);
|
|
65
|
+
}
|
|
66
|
+
for (let i = 0; i < this.length; i += 1) {
|
|
67
|
+
this.set(i, values[i] ?? 0);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
toArray() {
|
|
71
|
+
const ctor = this.getArrayConstructor();
|
|
72
|
+
const out = new ctor(this.length);
|
|
73
|
+
for (let i = 0; i < this.length; i += 1) {
|
|
74
|
+
out[i] = this.get(i);
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
forEach(fn) {
|
|
79
|
+
for (let i = 0; i < this.length; i += 1) {
|
|
80
|
+
fn(this.get(i), i);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
getElementOffset(index) {
|
|
84
|
+
const normalized = this.normalizeIndex(index);
|
|
85
|
+
return this.basePtr + normalized * this.stride;
|
|
86
|
+
}
|
|
87
|
+
normalizeIndex(index) {
|
|
88
|
+
const normalized = index < 0 ? this.length + index : index;
|
|
89
|
+
if (normalized < 0 || normalized >= this.length) {
|
|
90
|
+
throw new FastdbRuntimeError(`Column index ${index} is out of range [0, ${this.length}).`);
|
|
91
|
+
}
|
|
92
|
+
return normalized;
|
|
93
|
+
}
|
|
94
|
+
getArrayConstructor() {
|
|
95
|
+
if (this.kind === this.module.ftU8 || this.kind === this.module.ftU8n) {
|
|
96
|
+
return Uint8Array;
|
|
97
|
+
}
|
|
98
|
+
if (this.kind === this.module.ftU16 || this.kind === this.module.ftU16n) {
|
|
99
|
+
return Uint16Array;
|
|
100
|
+
}
|
|
101
|
+
if (this.kind === this.module.ftU32) {
|
|
102
|
+
return Uint32Array;
|
|
103
|
+
}
|
|
104
|
+
if (this.kind === this.module.ftI32) {
|
|
105
|
+
return Int32Array;
|
|
106
|
+
}
|
|
107
|
+
if (this.kind === this.module.ftF32) {
|
|
108
|
+
return Float32Array;
|
|
109
|
+
}
|
|
110
|
+
if (this.kind === this.module.ftF64) {
|
|
111
|
+
return Float64Array;
|
|
112
|
+
}
|
|
113
|
+
throw new FastdbRuntimeError(`Field index ${this.fieldIndex} is not a numeric column.`);
|
|
114
|
+
}
|
|
115
|
+
getDataView() {
|
|
116
|
+
return new DataView(this.module.HEAPU8.buffer);
|
|
117
|
+
}
|
|
118
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class FastdbError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class FastdbSchemaError extends FastdbError {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class FastdbRuntimeError extends FastdbError {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class FastdbUsageError extends FastdbError {
|
|
11
|
+
constructor(message: string);
|
|
12
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export class FastdbError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'FastdbError';
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class FastdbSchemaError extends FastdbError {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'FastdbSchemaError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class FastdbRuntimeError extends FastdbError {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = 'FastdbRuntimeError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class FastdbUsageError extends FastdbError {
|
|
20
|
+
constructor(message) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = 'FastdbUsageError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type ClassSchema } from './schema.js';
|
|
2
|
+
import type { WxDatabaseBuildHandle, WxDatabaseHandle, WxFeatureHandle } from './wasm-loader.js';
|
|
3
|
+
declare const PROXY_SYMBOL: unique symbol;
|
|
4
|
+
export type FeatureDatabaseHandle = WxDatabaseHandle | WxDatabaseBuildHandle;
|
|
5
|
+
export interface FeatureClass<T extends Feature = Feature> {
|
|
6
|
+
new (): T;
|
|
7
|
+
name: string;
|
|
8
|
+
schema: unknown;
|
|
9
|
+
}
|
|
10
|
+
type FeatureInitialValues<T extends Feature> = Partial<Record<keyof T & string, unknown>>;
|
|
11
|
+
export declare class Feature {
|
|
12
|
+
_origin: WxFeatureHandle | null;
|
|
13
|
+
_db: FeatureDatabaseHandle | null;
|
|
14
|
+
_cache: Record<string, unknown> | null;
|
|
15
|
+
_schema: ClassSchema;
|
|
16
|
+
[PROXY_SYMBOL]?: this;
|
|
17
|
+
constructor(initialValues?: Record<string, unknown>);
|
|
18
|
+
get fixed(): boolean;
|
|
19
|
+
_getCache(): Record<string, unknown>;
|
|
20
|
+
static mapFrom<T extends Feature>(this: FeatureClass<T>, db: FeatureDatabaseHandle, origin: WxFeatureHandle | null): T;
|
|
21
|
+
}
|
|
22
|
+
export declare function createFeature<T extends Feature>(ctor: FeatureClass<T>, initialValues?: FeatureInitialValues<T>): T;
|
|
23
|
+
export declare function mapFeatureFrom<T extends Feature>(ctor: FeatureClass<T>, db: FeatureDatabaseHandle, origin: WxFeatureHandle | null): T;
|
|
24
|
+
export declare function wrapFeature<T extends Feature>(feature: T): T;
|
|
25
|
+
export {};
|
package/dist/feature.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { FastdbRuntimeError, FastdbSchemaError, FastdbUsageError, } from './errors.js';
|
|
2
|
+
import { getClassSchema, getFieldDefinition, } from './schema.js';
|
|
3
|
+
import { isNumericField, isRefField, } from './types.js';
|
|
4
|
+
const PROXY_SYMBOL = Symbol('fastdb4ts.proxy');
|
|
5
|
+
export class Feature {
|
|
6
|
+
_origin = null;
|
|
7
|
+
_db = null;
|
|
8
|
+
_cache = null;
|
|
9
|
+
_schema;
|
|
10
|
+
[PROXY_SYMBOL];
|
|
11
|
+
constructor(initialValues) {
|
|
12
|
+
this._schema = getClassSchema(this.constructor);
|
|
13
|
+
if (initialValues && Object.keys(initialValues).length > 0) {
|
|
14
|
+
this._cache = { ...initialValues };
|
|
15
|
+
}
|
|
16
|
+
return wrapFeature(this);
|
|
17
|
+
}
|
|
18
|
+
get fixed() {
|
|
19
|
+
return this._origin !== null;
|
|
20
|
+
}
|
|
21
|
+
_getCache() {
|
|
22
|
+
if (this._cache === null) {
|
|
23
|
+
this._cache = {};
|
|
24
|
+
}
|
|
25
|
+
return this._cache;
|
|
26
|
+
}
|
|
27
|
+
static mapFrom(db, origin) {
|
|
28
|
+
return mapFeatureFrom(this, db, origin);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function createFeature(ctor, initialValues) {
|
|
32
|
+
const feature = new ctor();
|
|
33
|
+
if (initialValues && Object.keys(initialValues).length > 0) {
|
|
34
|
+
feature._cache = { ...initialValues };
|
|
35
|
+
}
|
|
36
|
+
return wrapFeature(feature);
|
|
37
|
+
}
|
|
38
|
+
export function mapFeatureFrom(ctor, db, origin) {
|
|
39
|
+
const feature = new ctor();
|
|
40
|
+
feature._db = db;
|
|
41
|
+
feature._origin = origin;
|
|
42
|
+
return wrapFeature(feature);
|
|
43
|
+
}
|
|
44
|
+
export function wrapFeature(feature) {
|
|
45
|
+
const existingProxy = feature[PROXY_SYMBOL];
|
|
46
|
+
if (existingProxy) {
|
|
47
|
+
return existingProxy;
|
|
48
|
+
}
|
|
49
|
+
const proxy = new Proxy(feature, {
|
|
50
|
+
get(target, prop, receiver) {
|
|
51
|
+
if (typeof prop !== 'string' || prop.startsWith('_')) {
|
|
52
|
+
return Reflect.get(target, prop, receiver);
|
|
53
|
+
}
|
|
54
|
+
const def = getFieldDefinition(target._schema, prop);
|
|
55
|
+
if (!def) {
|
|
56
|
+
return Reflect.get(target, prop, receiver);
|
|
57
|
+
}
|
|
58
|
+
return readField(target, def);
|
|
59
|
+
},
|
|
60
|
+
set(target, prop, value, receiver) {
|
|
61
|
+
if (typeof prop !== 'string' || prop.startsWith('_')) {
|
|
62
|
+
return Reflect.set(target, prop, value, receiver);
|
|
63
|
+
}
|
|
64
|
+
const def = getFieldDefinition(target._schema, prop);
|
|
65
|
+
if (!def) {
|
|
66
|
+
return Reflect.set(target, prop, value, receiver);
|
|
67
|
+
}
|
|
68
|
+
writeField(target, def, value);
|
|
69
|
+
return true;
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
feature[PROXY_SYMBOL] = proxy;
|
|
73
|
+
return proxy;
|
|
74
|
+
}
|
|
75
|
+
function readField(feature, def) {
|
|
76
|
+
const cache = feature._cache;
|
|
77
|
+
if (cache && def.name in cache) {
|
|
78
|
+
return cache[def.name];
|
|
79
|
+
}
|
|
80
|
+
if (!feature.fixed || feature._origin === null) {
|
|
81
|
+
const value = createDefaultValue(def);
|
|
82
|
+
feature._getCache()[def.name] = value;
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
return readMappedField(feature, def);
|
|
86
|
+
}
|
|
87
|
+
function writeField(feature, def, value) {
|
|
88
|
+
if (!feature.fixed || feature._origin === null) {
|
|
89
|
+
feature._getCache()[def.name] = value;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
writeMappedField(feature, def, value);
|
|
93
|
+
}
|
|
94
|
+
function readMappedField(feature, def) {
|
|
95
|
+
const origin = feature._origin;
|
|
96
|
+
if (!origin) {
|
|
97
|
+
throw new FastdbRuntimeError('Feature is not mapped to a WASM origin.');
|
|
98
|
+
}
|
|
99
|
+
const entry = def.entry;
|
|
100
|
+
switch (entry.kind) {
|
|
101
|
+
case 'bool':
|
|
102
|
+
return origin.getFieldAsInt(def.index) !== 0;
|
|
103
|
+
case 'u8':
|
|
104
|
+
case 'u16':
|
|
105
|
+
case 'u32':
|
|
106
|
+
case 'i32':
|
|
107
|
+
return origin.getFieldAsInt(def.index);
|
|
108
|
+
case 'u8n':
|
|
109
|
+
case 'u16n':
|
|
110
|
+
case 'f32':
|
|
111
|
+
case 'f64':
|
|
112
|
+
return origin.getFieldAsFloat(def.index);
|
|
113
|
+
case 'str':
|
|
114
|
+
case 'wstr':
|
|
115
|
+
return origin.getFieldAsString(def.index);
|
|
116
|
+
case 'bytes':
|
|
117
|
+
return origin.geometryView();
|
|
118
|
+
case 'ref':
|
|
119
|
+
return feature._cache?.[def.name] ?? null;
|
|
120
|
+
default:
|
|
121
|
+
throw new FastdbSchemaError(`Unsupported field kind: ${entry.kind}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function writeMappedField(feature, def, value) {
|
|
125
|
+
const origin = feature._origin;
|
|
126
|
+
if (!origin) {
|
|
127
|
+
throw new FastdbRuntimeError('Feature is not mapped to a WASM origin.');
|
|
128
|
+
}
|
|
129
|
+
const entry = def.entry;
|
|
130
|
+
if (isNumericField(entry)) {
|
|
131
|
+
if (entry.kind === 'f32' || entry.kind === 'f64' || entry.kind === 'u8n' || entry.kind === 'u16n') {
|
|
132
|
+
origin.setFieldDouble(def.index, Number(value));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (entry.kind === 'bool') {
|
|
136
|
+
origin.setFieldInt(def.index, value ? 1 : 0);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
origin.setFieldInt(def.index, Math.trunc(Number(value)));
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (isRefField(entry)) {
|
|
143
|
+
writeRefField(feature, def, entry, value);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
throw new FastdbUsageError(`Direct writes to db-mapped field "${def.name}" of type "${entry.kind}" are not supported yet.`);
|
|
147
|
+
}
|
|
148
|
+
function writeRefField(feature, def, entry, value) {
|
|
149
|
+
if (value === null || value === undefined) {
|
|
150
|
+
feature._getCache()[def.name] = null;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (!(value instanceof Feature)) {
|
|
154
|
+
throw new FastdbUsageError(`Field "${def.name}" expects a Feature reference, got "${typeof value}".`);
|
|
155
|
+
}
|
|
156
|
+
if (entry.target && !(value instanceof entry.target)) {
|
|
157
|
+
throw new FastdbUsageError(`Field "${def.name}" expects a reference to "${entry.target.name}".`);
|
|
158
|
+
}
|
|
159
|
+
feature._getCache()[def.name] = value;
|
|
160
|
+
}
|
|
161
|
+
function createDefaultValue(def) {
|
|
162
|
+
const entry = def.entry;
|
|
163
|
+
if (isRefField(entry) && def.target) {
|
|
164
|
+
return createFeature(def.target);
|
|
165
|
+
}
|
|
166
|
+
return entry.createDefault();
|
|
167
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { getFastdbModule, initFastdb } from './wasm-loader.js';
|
|
2
|
+
export { Feature, createFeature, mapFeatureFrom, wrapFeature, } from './feature.js';
|
|
3
|
+
export { ORM, TableDefn, } from './orm.js';
|
|
4
|
+
export { Table } from './table.js';
|
|
5
|
+
export { StridedColumn } from './column.js';
|
|
6
|
+
export { FastSerializer } from './serializer.js';
|
|
7
|
+
export { defineSchema, getClassSchema, } from './schema.js';
|
|
8
|
+
export { BOOL, BYTES, F32, F64, I32, REF, STR, U8, U8N, U16, U16N, U32, WSTR, listOf, ref, } from './types.js';
|
|
9
|
+
export { FastdbError, FastdbRuntimeError, FastdbSchemaError, FastdbUsageError, } from './errors.js';
|
|
10
|
+
export type { ChunkView, FieldDefView, FastdbModule, FastdbModuleFactory, WxDatabaseBuildHandle, WxDatabaseHandle, WxFeatureHandle, WxLayerTableBuildHandle, WxLayerTableHandle, WxMemoryStreamHandle, } from './wasm-loader.js';
|
|
11
|
+
export type { ClassSchema, SchemaDefinition, SchemaFieldDefinition, } from './schema.js';
|
|
12
|
+
export type { FeatureClass, FeatureDatabaseHandle, } from './feature.js';
|
|
13
|
+
export type { FieldKind, FieldTypeDef, ListFieldDef, ListItemDef, RefFieldDef, SchemaEntry, TypedArrayConstructor, TypedArrayInstance, } from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { getFastdbModule, initFastdb } from './wasm-loader.js';
|
|
2
|
+
export { Feature, createFeature, mapFeatureFrom, wrapFeature, } from './feature.js';
|
|
3
|
+
export { ORM, TableDefn, } from './orm.js';
|
|
4
|
+
export { Table } from './table.js';
|
|
5
|
+
export { StridedColumn } from './column.js';
|
|
6
|
+
export { FastSerializer } from './serializer.js';
|
|
7
|
+
export { defineSchema, getClassSchema, } from './schema.js';
|
|
8
|
+
export { BOOL, BYTES, F32, F64, I32, REF, STR, U8, U8N, U16, U16N, U32, WSTR, listOf, ref, } from './types.js';
|
|
9
|
+
export { FastdbError, FastdbRuntimeError, FastdbSchemaError, FastdbUsageError, } from './errors.js';
|
package/dist/orm.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Feature, type FeatureClass } from './feature.js';
|
|
2
|
+
import { Table } from './table.js';
|
|
3
|
+
export declare class TableDefn<T extends Feature> {
|
|
4
|
+
readonly featureType: FeatureClass<T>;
|
|
5
|
+
readonly capacity: number;
|
|
6
|
+
readonly name?: string;
|
|
7
|
+
constructor(featureType: FeatureClass<T>, capacity: number, name?: string);
|
|
8
|
+
}
|
|
9
|
+
export declare class ORM {
|
|
10
|
+
private readonly module;
|
|
11
|
+
private origin;
|
|
12
|
+
private readonly tableMap;
|
|
13
|
+
private constructor();
|
|
14
|
+
get fixed(): boolean;
|
|
15
|
+
static create(): ORM;
|
|
16
|
+
static truncate(defns: TableDefn<Feature>[]): ORM;
|
|
17
|
+
static fromBuffer(data: Uint8Array | ArrayBuffer): ORM;
|
|
18
|
+
push<T extends Feature>(feature: T, tableName?: string): void;
|
|
19
|
+
combine(): void;
|
|
20
|
+
toBuffer(): Uint8Array;
|
|
21
|
+
table<T extends Feature>(featureType: FeatureClass<T>, name?: string): Table<T>;
|
|
22
|
+
private getBuildOrigin;
|
|
23
|
+
private defineTableFields;
|
|
24
|
+
private ensureTruncateCompatible;
|
|
25
|
+
private copyChunkToBytes;
|
|
26
|
+
private disposeBuildTables;
|
|
27
|
+
}
|