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/dist/orm.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { getClassSchema } from './schema.js';
|
|
2
|
+
import { FastdbRuntimeError, FastdbUsageError } from './errors.js';
|
|
3
|
+
import { Table } from './table.js';
|
|
4
|
+
import { getInitializedFastdbModule } from './wasm-loader.js';
|
|
5
|
+
export class TableDefn {
|
|
6
|
+
featureType;
|
|
7
|
+
capacity;
|
|
8
|
+
name;
|
|
9
|
+
constructor(featureType, capacity, name) {
|
|
10
|
+
this.featureType = featureType;
|
|
11
|
+
this.capacity = capacity;
|
|
12
|
+
this.name = name;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export class ORM {
|
|
16
|
+
module;
|
|
17
|
+
origin;
|
|
18
|
+
tableMap = new Map();
|
|
19
|
+
constructor(origin, module) {
|
|
20
|
+
this.origin = origin;
|
|
21
|
+
this.module = module;
|
|
22
|
+
}
|
|
23
|
+
get fixed() {
|
|
24
|
+
return 'bufferView' in this.origin;
|
|
25
|
+
}
|
|
26
|
+
static create() {
|
|
27
|
+
const module = getInitializedFastdbModule();
|
|
28
|
+
const origin = new module.WxDatabaseBuild();
|
|
29
|
+
origin.begin('');
|
|
30
|
+
return new ORM(origin, module);
|
|
31
|
+
}
|
|
32
|
+
static truncate(defns) {
|
|
33
|
+
const orm = ORM.create();
|
|
34
|
+
const build = orm.getBuildOrigin();
|
|
35
|
+
for (const defn of defns) {
|
|
36
|
+
if (defn.capacity <= 0) {
|
|
37
|
+
throw new FastdbUsageError('Table capacity must be positive.');
|
|
38
|
+
}
|
|
39
|
+
orm.ensureTruncateCompatible(defn.featureType);
|
|
40
|
+
const layerName = defn.name ?? defn.featureType.name;
|
|
41
|
+
const table = build.createLayerBegin(layerName);
|
|
42
|
+
table.setGeometryType(orm.module.gtAny, orm.module.cfDefault, false);
|
|
43
|
+
orm.defineTableFields(table, defn.featureType);
|
|
44
|
+
build.createLayerEnd();
|
|
45
|
+
build.truncate(layerName, defn.capacity);
|
|
46
|
+
}
|
|
47
|
+
orm.combine();
|
|
48
|
+
return orm;
|
|
49
|
+
}
|
|
50
|
+
static fromBuffer(data) {
|
|
51
|
+
const module = getInitializedFastdbModule();
|
|
52
|
+
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
53
|
+
const ptr = module._malloc(bytes.byteLength);
|
|
54
|
+
try {
|
|
55
|
+
module.HEAPU8.set(bytes, ptr);
|
|
56
|
+
const db = module.WxDatabase.loadFromHeap(ptr, bytes.byteLength);
|
|
57
|
+
return new ORM(db, module);
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
module._free(ptr);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
push(feature, tableName) {
|
|
64
|
+
const build = this.getBuildOrigin();
|
|
65
|
+
const featureType = feature.constructor;
|
|
66
|
+
const name = tableName ?? featureType.name;
|
|
67
|
+
let table = this.tableMap.get(name);
|
|
68
|
+
let origin;
|
|
69
|
+
if (!table) {
|
|
70
|
+
origin = build.createLayerBegin(name);
|
|
71
|
+
origin.setGeometryType(this.module.gtAny, this.module.cfDefault, false);
|
|
72
|
+
this.defineTableFields(origin, featureType);
|
|
73
|
+
this.tableMap.set(name, new Table(featureType, origin, build, this.module));
|
|
74
|
+
table = this.tableMap.get(name);
|
|
75
|
+
}
|
|
76
|
+
origin = table.origin;
|
|
77
|
+
origin.addFeatureBegin();
|
|
78
|
+
try {
|
|
79
|
+
const schema = getClassSchema(featureType);
|
|
80
|
+
for (const field of schema.fieldList) {
|
|
81
|
+
const value = feature[field.name];
|
|
82
|
+
const kind = field.entry.kind;
|
|
83
|
+
if (kind === 'bool') {
|
|
84
|
+
origin.setFieldInt(field.index, value ? 1 : 0);
|
|
85
|
+
}
|
|
86
|
+
else if (kind === 'u8' ||
|
|
87
|
+
kind === 'u16' ||
|
|
88
|
+
kind === 'u32' ||
|
|
89
|
+
kind === 'i32') {
|
|
90
|
+
origin.setFieldInt(field.index, Math.trunc(Number(value)));
|
|
91
|
+
}
|
|
92
|
+
else if (kind === 'u8n' ||
|
|
93
|
+
kind === 'u16n' ||
|
|
94
|
+
kind === 'f32' ||
|
|
95
|
+
kind === 'f64') {
|
|
96
|
+
origin.setFieldDouble(field.index, Number(value));
|
|
97
|
+
}
|
|
98
|
+
else if (kind === 'str' || kind === 'wstr') {
|
|
99
|
+
origin.setFieldString(field.index, String(value ?? ''));
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
throw new FastdbUsageError(`push() does not support field kind "${kind}" yet.`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
origin.addFeatureEnd();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
combine() {
|
|
111
|
+
const build = this.getBuildOrigin();
|
|
112
|
+
const stream = new this.module.WxMemoryStream();
|
|
113
|
+
build.post(stream);
|
|
114
|
+
const bytes = this.copyChunkToBytes(stream);
|
|
115
|
+
stream.delete();
|
|
116
|
+
this.disposeBuildTables();
|
|
117
|
+
build.delete();
|
|
118
|
+
this.origin = ORM.fromBuffer(bytes).origin;
|
|
119
|
+
this.tableMap.clear();
|
|
120
|
+
}
|
|
121
|
+
toBuffer() {
|
|
122
|
+
if (this.fixed) {
|
|
123
|
+
const db = this.origin;
|
|
124
|
+
const chunk = db.bufferView();
|
|
125
|
+
return this.module.HEAPU8.slice(chunk.data, chunk.data + chunk.size);
|
|
126
|
+
}
|
|
127
|
+
const build = this.getBuildOrigin();
|
|
128
|
+
const stream = new this.module.WxMemoryStream();
|
|
129
|
+
build.post(stream);
|
|
130
|
+
const bytes = this.copyChunkToBytes(stream);
|
|
131
|
+
stream.delete();
|
|
132
|
+
return bytes;
|
|
133
|
+
}
|
|
134
|
+
table(featureType, name) {
|
|
135
|
+
const tableName = name ?? featureType.name;
|
|
136
|
+
const cached = this.tableMap.get(tableName);
|
|
137
|
+
if (cached) {
|
|
138
|
+
return cached;
|
|
139
|
+
}
|
|
140
|
+
if (!this.fixed) {
|
|
141
|
+
throw new FastdbRuntimeError(`Table "${tableName}" is not available until combine() finishes.`);
|
|
142
|
+
}
|
|
143
|
+
const db = this.origin;
|
|
144
|
+
for (let i = 0; i < db.getLayerCount(); i += 1) {
|
|
145
|
+
const layer = db.getLayer(i);
|
|
146
|
+
if (layer.name() === tableName) {
|
|
147
|
+
const table = new Table(featureType, layer, db, this.module);
|
|
148
|
+
this.tableMap.set(tableName, table);
|
|
149
|
+
return table;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
throw new FastdbRuntimeError(`Table "${tableName}" not found.`);
|
|
153
|
+
}
|
|
154
|
+
getBuildOrigin() {
|
|
155
|
+
if (this.fixed) {
|
|
156
|
+
throw new FastdbUsageError('Operation is only available for mutable ORM instances.');
|
|
157
|
+
}
|
|
158
|
+
return this.origin;
|
|
159
|
+
}
|
|
160
|
+
defineTableFields(table, featureType) {
|
|
161
|
+
const schema = getClassSchema(featureType);
|
|
162
|
+
for (const field of schema.fieldList) {
|
|
163
|
+
table.addField(field.name, field.entry.originType, 0, 1);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
ensureTruncateCompatible(featureType) {
|
|
167
|
+
const schema = getClassSchema(featureType);
|
|
168
|
+
for (const field of schema.fieldList) {
|
|
169
|
+
if (field.entry.kind === 'str' || field.entry.kind === 'wstr' || field.entry.kind === 'bytes') {
|
|
170
|
+
throw new FastdbUsageError(`truncate() does not support variable-length field "${field.name}" of type "${field.entry.kind}".`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
copyChunkToBytes(stream) {
|
|
175
|
+
const chunk = stream.dataView();
|
|
176
|
+
return this.module.HEAPU8.slice(chunk.data, chunk.data + chunk.size);
|
|
177
|
+
}
|
|
178
|
+
disposeBuildTables() {
|
|
179
|
+
for (const table of this.tableMap.values()) {
|
|
180
|
+
if (!table.fixed) {
|
|
181
|
+
table.origin.delete();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type FeatureClassLike, type ListFieldDef, type ListItemDef, type SchemaEntry } from './types.js';
|
|
2
|
+
export interface SchemaDefinition {
|
|
3
|
+
readonly fields: Record<string, SchemaEntry>;
|
|
4
|
+
}
|
|
5
|
+
export interface SchemaFieldDefinition {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly index: number;
|
|
8
|
+
readonly entry: SchemaEntry;
|
|
9
|
+
readonly target?: FeatureClassLike;
|
|
10
|
+
}
|
|
11
|
+
export interface ClassSchema {
|
|
12
|
+
readonly fieldList: readonly SchemaFieldDefinition[];
|
|
13
|
+
readonly fieldMap: ReadonlyMap<string, SchemaFieldDefinition>;
|
|
14
|
+
readonly scalarFieldIds: readonly number[];
|
|
15
|
+
readonly numericFieldIds: readonly number[];
|
|
16
|
+
readonly refFieldIds: readonly number[];
|
|
17
|
+
readonly listFieldIds: readonly number[];
|
|
18
|
+
}
|
|
19
|
+
export declare function defineSchema(fields: Record<string, SchemaEntry>): SchemaDefinition;
|
|
20
|
+
export declare function getClassSchema(ctor: FeatureClassLike): ClassSchema;
|
|
21
|
+
export declare function getFieldDefinition(schema: ClassSchema, fieldName: string): SchemaFieldDefinition | undefined;
|
|
22
|
+
export declare function resolveFeatureClassLike(target: FeatureClassLike | (() => FeatureClassLike)): FeatureClassLike;
|
|
23
|
+
export declare function resolveListItem(entry: ListFieldDef): Exclude<ListItemDef, (() => FeatureClassLike)>;
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { FastdbSchemaError } from './errors.js';
|
|
2
|
+
import { isListField, isNumericField, isRefField, isScalarField, } from './types.js';
|
|
3
|
+
const SCHEMA_CACHE = new WeakMap();
|
|
4
|
+
export function defineSchema(fields) {
|
|
5
|
+
return Object.freeze({ fields: { ...fields } });
|
|
6
|
+
}
|
|
7
|
+
export function getClassSchema(ctor) {
|
|
8
|
+
const cached = SCHEMA_CACHE.get(ctor);
|
|
9
|
+
if (cached) {
|
|
10
|
+
return cached;
|
|
11
|
+
}
|
|
12
|
+
const rawSchema = ctor.schema;
|
|
13
|
+
if (!rawSchema || typeof rawSchema !== 'object' || rawSchema.fields === undefined) {
|
|
14
|
+
throw new FastdbSchemaError(`Feature class "${ctor.name}" is missing a static schema. Use defineSchema({...}).`);
|
|
15
|
+
}
|
|
16
|
+
const fieldList = [];
|
|
17
|
+
const fieldMap = new Map();
|
|
18
|
+
const scalarFieldIds = [];
|
|
19
|
+
const numericFieldIds = [];
|
|
20
|
+
const refFieldIds = [];
|
|
21
|
+
const listFieldIds = [];
|
|
22
|
+
let index = 0;
|
|
23
|
+
for (const [name, entry] of Object.entries(rawSchema.fields)) {
|
|
24
|
+
if (!name || name.startsWith('_')) {
|
|
25
|
+
throw new FastdbSchemaError(`Feature class "${ctor.name}" contains an invalid field name "${name}".`);
|
|
26
|
+
}
|
|
27
|
+
const target = resolveRefTarget(entry);
|
|
28
|
+
const def = Object.freeze({
|
|
29
|
+
name,
|
|
30
|
+
index,
|
|
31
|
+
entry,
|
|
32
|
+
target,
|
|
33
|
+
});
|
|
34
|
+
fieldList.push(def);
|
|
35
|
+
fieldMap.set(name, def);
|
|
36
|
+
if (isScalarField(entry)) {
|
|
37
|
+
scalarFieldIds.push(index);
|
|
38
|
+
}
|
|
39
|
+
if (isNumericField(entry)) {
|
|
40
|
+
numericFieldIds.push(index);
|
|
41
|
+
}
|
|
42
|
+
if (isRefField(entry)) {
|
|
43
|
+
refFieldIds.push(index);
|
|
44
|
+
}
|
|
45
|
+
if (isListField(entry)) {
|
|
46
|
+
listFieldIds.push(index);
|
|
47
|
+
}
|
|
48
|
+
index += 1;
|
|
49
|
+
}
|
|
50
|
+
const schema = Object.freeze({
|
|
51
|
+
fieldList: Object.freeze(fieldList),
|
|
52
|
+
fieldMap,
|
|
53
|
+
scalarFieldIds: Object.freeze(scalarFieldIds),
|
|
54
|
+
numericFieldIds: Object.freeze(numericFieldIds),
|
|
55
|
+
refFieldIds: Object.freeze(refFieldIds),
|
|
56
|
+
listFieldIds: Object.freeze(listFieldIds),
|
|
57
|
+
});
|
|
58
|
+
SCHEMA_CACHE.set(ctor, schema);
|
|
59
|
+
return schema;
|
|
60
|
+
}
|
|
61
|
+
function resolveRefTarget(entry) {
|
|
62
|
+
if (!isRefField(entry) || entry.target === undefined) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const target = entry.target;
|
|
66
|
+
if (typeof target !== 'function') {
|
|
67
|
+
return target;
|
|
68
|
+
}
|
|
69
|
+
if (target.prototype && target.prototype.constructor === target) {
|
|
70
|
+
return target;
|
|
71
|
+
}
|
|
72
|
+
const resolved = target();
|
|
73
|
+
return resolved;
|
|
74
|
+
}
|
|
75
|
+
export function getFieldDefinition(schema, fieldName) {
|
|
76
|
+
return schema.fieldMap.get(fieldName);
|
|
77
|
+
}
|
|
78
|
+
export function resolveFeatureClassLike(target) {
|
|
79
|
+
if (typeof target !== 'function') {
|
|
80
|
+
return target;
|
|
81
|
+
}
|
|
82
|
+
if (target.prototype && target.prototype.constructor === target) {
|
|
83
|
+
return target;
|
|
84
|
+
}
|
|
85
|
+
return target();
|
|
86
|
+
}
|
|
87
|
+
export function resolveListItem(entry) {
|
|
88
|
+
const item = entry.item;
|
|
89
|
+
if (typeof item !== 'function') {
|
|
90
|
+
return item;
|
|
91
|
+
}
|
|
92
|
+
return resolveFeatureClassLike(item);
|
|
93
|
+
}
|