@type-bridge/node 1.5.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 +102 -0
- package/dist/attribute.d.ts +102 -0
- package/dist/attribute.js +176 -0
- package/dist/codec.d.ts +36 -0
- package/dist/codec.js +274 -0
- package/dist/flags.d.ts +67 -0
- package/dist/flags.js +123 -0
- package/dist/generator/cardinality.d.ts +15 -0
- package/dist/generator/cardinality.js +28 -0
- package/dist/generator/index.d.ts +44 -0
- package/dist/generator/index.js +64 -0
- package/dist/generator/naming.d.ts +64 -0
- package/dist/generator/naming.js +69 -0
- package/dist/generator/renderAttributes.d.ts +22 -0
- package/dist/generator/renderAttributes.js +153 -0
- package/dist/generator/renderEntities.d.ts +39 -0
- package/dist/generator/renderEntities.js +273 -0
- package/dist/generator/renderPackage.d.ts +22 -0
- package/dist/generator/renderPackage.js +33 -0
- package/dist/generator/renderRelations.d.ts +36 -0
- package/dist/generator/renderRelations.js +395 -0
- package/dist/iid.d.ts +5 -0
- package/dist/iid.js +20 -0
- package/dist/index.d.ts +469 -0
- package/dist/index.js +451 -0
- package/dist/manager.d.ts +51 -0
- package/dist/manager.js +282 -0
- package/dist/model.d.ts +261 -0
- package/dist/model.js +481 -0
- package/dist/native.d.ts +14 -0
- package/dist/native.js +125 -0
- package/dist/parser.d.ts +106 -0
- package/dist/parser.js +20 -0
- package/dist/query.d.ts +109 -0
- package/dist/query.js +240 -0
- package/dist/type-bridge-node-1.5.0.tgz +0 -0
- package/package.json +53 -0
- package/type_bridge_node.linux-x64-gnu.node +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# @type-bridge/node
|
|
2
|
+
|
|
3
|
+
Node.js NAPI facade for the shared type-bridge Rust ORM runtime.
|
|
4
|
+
|
|
5
|
+
This package is a boundary layer only. Descriptor validation, query
|
|
6
|
+
construction, CRUD execution, hydration, and transaction behavior live in
|
|
7
|
+
`type-bridge-orm`; the Node package loads the native module, marshals
|
|
8
|
+
JavaScript values, and exposes TypeScript-friendly wrappers.
|
|
9
|
+
|
|
10
|
+
## Build
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm run build
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The package entry is `index.js`, with declarations emitted to
|
|
17
|
+
`dist/index.d.ts`.
|
|
18
|
+
|
|
19
|
+
For local smoke runs after `cargo build -p type-bridge-node`, copy the built
|
|
20
|
+
library to a `.node` file or point the loader at one:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
TYPE_BRIDGE_NODE_NATIVE_PATH=/path/to/type_bridge_node.node npm run smoke:package
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Public Surface
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import {
|
|
30
|
+
DescriptorRegistry,
|
|
31
|
+
Marshalling,
|
|
32
|
+
RustDatabase,
|
|
33
|
+
long,
|
|
34
|
+
string,
|
|
35
|
+
} from "@type-bridge/node";
|
|
36
|
+
|
|
37
|
+
const registry = new DescriptorRegistry();
|
|
38
|
+
const person = registry.registerEntity({
|
|
39
|
+
type_name: "person",
|
|
40
|
+
is_abstract: false,
|
|
41
|
+
parent_type: null,
|
|
42
|
+
owned_attributes: [
|
|
43
|
+
{
|
|
44
|
+
field_name: "name",
|
|
45
|
+
attr_name: "person-name",
|
|
46
|
+
value_type: "string",
|
|
47
|
+
annotations: ["Key"],
|
|
48
|
+
is_optional: false,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
field_name: "age",
|
|
52
|
+
attr_name: "age",
|
|
53
|
+
value_type: "long",
|
|
54
|
+
annotations: [],
|
|
55
|
+
is_optional: true,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const attrs = { name: string("Alice"), age: long(42n) };
|
|
61
|
+
new Marshalling().entityAttributes(person, attrs);
|
|
62
|
+
|
|
63
|
+
const db = RustDatabase.connect("127.0.0.1:1729", "example");
|
|
64
|
+
const manager = db.entityManager(person);
|
|
65
|
+
manager.insert(attrs);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Descriptor Shape
|
|
69
|
+
|
|
70
|
+
Descriptors use the canonical Rust serde shape:
|
|
71
|
+
|
|
72
|
+
- Entity descriptor: `type_name`, `is_abstract`, `parent_type`, and
|
|
73
|
+
`owned_attributes`.
|
|
74
|
+
- Relation descriptor: entity fields plus `roles`.
|
|
75
|
+
- Owned attributes: `field_name` is the JavaScript-facing field key,
|
|
76
|
+
`attr_name` is the TypeDB attribute label, `value_type` is one TypeDB
|
|
77
|
+
primitive value type, `annotations` carries `Key`, `Unique`, or `Card`, and
|
|
78
|
+
`is_optional` mirrors the model field optionality.
|
|
79
|
+
- Relation roles: `role_name`, accepted `player_type_names`, and optional
|
|
80
|
+
`[min, max]` cardinality.
|
|
81
|
+
|
|
82
|
+
## BigInt Rule
|
|
83
|
+
|
|
84
|
+
TypeDB `long` maps to Rust `i64`, so JavaScript `number` is not accepted by
|
|
85
|
+
`long()`. Use `long(9223372036854775807n)`.
|
|
86
|
+
|
|
87
|
+
`longFromNumberUnsafe()` exists only for explicit caller-owned conversions when
|
|
88
|
+
the value is known to fit safely enough for that call site.
|
|
89
|
+
|
|
90
|
+
Runtime rows return `Long` values as decimal strings so no precision is lost
|
|
91
|
+
while crossing JSON.
|
|
92
|
+
|
|
93
|
+
## Supported Runtime Operations
|
|
94
|
+
|
|
95
|
+
The current facade exposes descriptor registration, value marshalling, database
|
|
96
|
+
and transaction handles, entity and relation managers, insert/put/update/get,
|
|
97
|
+
batch insert/put, count, aggregate, group-by aggregate, relation role-player
|
|
98
|
+
filters, and IID deletes.
|
|
99
|
+
|
|
100
|
+
The facade does not expose a TypeScript query compiler or a direct TypeDB
|
|
101
|
+
driver. Use `npm run scope:probe` to verify the Node crate stays inside that
|
|
102
|
+
boundary.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { AttributeSchemaEntry, ValueType } from "./index.js";
|
|
2
|
+
import { AggregateSpec, ComparisonExpr, SortExpr } from "./query.js";
|
|
3
|
+
declare const attributeBrand: unique symbol;
|
|
4
|
+
/**
|
|
5
|
+
* Base class for a branded attribute value. `Value` is the wrapped runtime
|
|
6
|
+
* value; `Brand` is the schema attribute name carried as a phantom type so that
|
|
7
|
+
* distinct attribute classes are nominally distinct at compile time. Instances
|
|
8
|
+
* are immutable; equality is by `(constructor, value)`.
|
|
9
|
+
*/
|
|
10
|
+
export declare abstract class Attribute<Value, Brand extends string> {
|
|
11
|
+
readonly value: Value;
|
|
12
|
+
readonly [attributeBrand]: Brand;
|
|
13
|
+
constructor(value: Value);
|
|
14
|
+
equals(other: unknown): boolean;
|
|
15
|
+
toString(): string;
|
|
16
|
+
}
|
|
17
|
+
/** Comparison helpers available on every attribute class. */
|
|
18
|
+
type ComparisonStatics<Value, Brand extends string> = {
|
|
19
|
+
eq(value: Attribute<Value, Brand>): ComparisonExpr;
|
|
20
|
+
ne(value: Attribute<Value, Brand>): ComparisonExpr;
|
|
21
|
+
gt(value: Attribute<Value, Brand>): ComparisonExpr;
|
|
22
|
+
gte(value: Attribute<Value, Brand>): ComparisonExpr;
|
|
23
|
+
lt(value: Attribute<Value, Brand>): ComparisonExpr;
|
|
24
|
+
lte(value: Attribute<Value, Brand>): ComparisonExpr;
|
|
25
|
+
};
|
|
26
|
+
/** Sort-key helpers available on every attribute class. */
|
|
27
|
+
type OrderStatics = {
|
|
28
|
+
asc(): SortExpr;
|
|
29
|
+
desc(): SortExpr;
|
|
30
|
+
};
|
|
31
|
+
/** String-matching helpers, available only on string-valued attribute classes. */
|
|
32
|
+
type StringStatics = {
|
|
33
|
+
contains(value: string): ComparisonExpr;
|
|
34
|
+
like(value: string): ComparisonExpr;
|
|
35
|
+
startsWith(value: string): ComparisonExpr;
|
|
36
|
+
endsWith(value: string): ComparisonExpr;
|
|
37
|
+
};
|
|
38
|
+
/** Reduce helpers, available only on numeric attribute classes (long/double/decimal). */
|
|
39
|
+
type AggregateStatics = {
|
|
40
|
+
sum(): AggregateSpec;
|
|
41
|
+
avg(): AggregateSpec;
|
|
42
|
+
min(): AggregateSpec;
|
|
43
|
+
max(): AggregateSpec;
|
|
44
|
+
median(): AggregateSpec;
|
|
45
|
+
std(): AggregateSpec;
|
|
46
|
+
};
|
|
47
|
+
export type AttributeTypeParent = {
|
|
48
|
+
readonly attrName: string;
|
|
49
|
+
readonly attributeSchema?: AttributeSchemaEntry;
|
|
50
|
+
readonly attributeSchemaEntries?: readonly AttributeSchemaEntry[];
|
|
51
|
+
} | string | null;
|
|
52
|
+
export interface AttributeTypeOptions {
|
|
53
|
+
readonly parent?: AttributeTypeParent;
|
|
54
|
+
readonly abstract?: boolean;
|
|
55
|
+
readonly independent?: boolean;
|
|
56
|
+
readonly regex?: string | null;
|
|
57
|
+
readonly values?: readonly string[] | null;
|
|
58
|
+
readonly range?: readonly [string | null, string | null] | null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The abstract base class returned by an `attr.*(name)` factory: an abstract
|
|
62
|
+
* `Attribute` constructor branded by `Name`, plus the static `attrName` and
|
|
63
|
+
* wire `valueType`. Users extend it (`class Name extends attr.String("name") {}`);
|
|
64
|
+
* they never name this type directly.
|
|
65
|
+
*/
|
|
66
|
+
export type AttributeBase<Value, Brand extends string> = (abstract new (value: Value) => Attribute<Value, Brand>) & {
|
|
67
|
+
readonly attrName: Brand;
|
|
68
|
+
readonly valueType: ValueType;
|
|
69
|
+
readonly attributeSchema: AttributeSchemaEntry;
|
|
70
|
+
readonly attributeSchemaEntries: readonly AttributeSchemaEntry[];
|
|
71
|
+
};
|
|
72
|
+
/** Attribute base with comparison + sort helpers (every value type). */
|
|
73
|
+
export type ComparableAttributeBase<Value, Brand extends string> = AttributeBase<Value, Brand> & ComparisonStatics<Value, Brand> & OrderStatics;
|
|
74
|
+
/** Attribute base for string values: comparison + sort + string matching. */
|
|
75
|
+
export type StringAttributeBase<Brand extends string> = ComparableAttributeBase<string, Brand> & StringStatics;
|
|
76
|
+
/** Attribute base for numeric values: comparison + sort + reduce helpers. */
|
|
77
|
+
export type NumericAttributeBase<Value, Brand extends string> = ComparableAttributeBase<Value, Brand> & AggregateStatics;
|
|
78
|
+
type ComparableFactory<Value> = <const Name extends string>(name: Name, options?: AttributeTypeOptions) => ComparableAttributeBase<Value, Name>;
|
|
79
|
+
type StringFactory = <const Name extends string>(name: Name, options?: AttributeTypeOptions) => StringAttributeBase<Name>;
|
|
80
|
+
type NumericFactory<Value> = <const Name extends string>(name: Name, options?: AttributeTypeOptions) => NumericAttributeBase<Value, Name>;
|
|
81
|
+
/**
|
|
82
|
+
* Attribute base-class factories, one per TypeDB value type. Each call returns a
|
|
83
|
+
* branded base to extend: the mandatory `name` is both the schema `attr_name`
|
|
84
|
+
* and the compile-time brand, so distinct names produce non-interchangeable
|
|
85
|
+
* types. The `attr.*` namespace avoids shadowing the `String`/`Boolean`/`Date`
|
|
86
|
+
* JS globals. `Integer` wraps `bigint` and maps to the `long` wire type. String
|
|
87
|
+
* bases gain `contains`/`like`/`startsWith`/`endsWith`; numeric bases gain
|
|
88
|
+
* `sum`/`avg`/`min`/`max`/`median`/`std`; every base gains the comparison and
|
|
89
|
+
* sort helpers used to build typed queries.
|
|
90
|
+
*/
|
|
91
|
+
export declare const attr: {
|
|
92
|
+
readonly String: StringFactory;
|
|
93
|
+
readonly Integer: NumericFactory<bigint>;
|
|
94
|
+
readonly Double: NumericFactory<number>;
|
|
95
|
+
readonly Boolean: ComparableFactory<boolean>;
|
|
96
|
+
readonly Date: ComparableFactory<string>;
|
|
97
|
+
readonly DateTime: ComparableFactory<string>;
|
|
98
|
+
readonly DateTimeTZ: ComparableFactory<string>;
|
|
99
|
+
readonly Decimal: NumericFactory<string>;
|
|
100
|
+
readonly Duration: ComparableFactory<string>;
|
|
101
|
+
};
|
|
102
|
+
export {};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.attr = exports.Attribute = void 0;
|
|
4
|
+
const codec_js_1 = require("./codec.js");
|
|
5
|
+
const query_js_1 = require("./query.js");
|
|
6
|
+
/**
|
|
7
|
+
* Base class for a branded attribute value. `Value` is the wrapped runtime
|
|
8
|
+
* value; `Brand` is the schema attribute name carried as a phantom type so that
|
|
9
|
+
* distinct attribute classes are nominally distinct at compile time. Instances
|
|
10
|
+
* are immutable; equality is by `(constructor, value)`.
|
|
11
|
+
*/
|
|
12
|
+
class Attribute {
|
|
13
|
+
value;
|
|
14
|
+
constructor(value) {
|
|
15
|
+
this.value = value;
|
|
16
|
+
}
|
|
17
|
+
equals(other) {
|
|
18
|
+
return (other instanceof Attribute &&
|
|
19
|
+
other.constructor === this.constructor &&
|
|
20
|
+
other.value === this.value);
|
|
21
|
+
}
|
|
22
|
+
toString() {
|
|
23
|
+
const rendered = typeof this.value === "string" ? JSON.stringify(this.value) : String(this.value);
|
|
24
|
+
return `${this.constructor.name}(${rendered})`;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.Attribute = Attribute;
|
|
28
|
+
function comparisonStatics(name, valueType) {
|
|
29
|
+
const build = (operator) => (value) => new query_js_1.ComparisonExpr(name, operator, (0, codec_js_1.lowerAttributeValue)(valueType, value.value));
|
|
30
|
+
return {
|
|
31
|
+
eq: build("eq"),
|
|
32
|
+
ne: build("neq"),
|
|
33
|
+
gt: build("gt"),
|
|
34
|
+
gte: build("gte"),
|
|
35
|
+
lt: build("lt"),
|
|
36
|
+
lte: build("lte"),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function orderStatics(name) {
|
|
40
|
+
return {
|
|
41
|
+
asc: () => new query_js_1.SortExpr(name, "Asc"),
|
|
42
|
+
desc: () => new query_js_1.SortExpr(name, "Desc"),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function stringStatics(name) {
|
|
46
|
+
const build = (operator) => (value) => new query_js_1.ComparisonExpr(name, operator, { value_type: "string", value });
|
|
47
|
+
return {
|
|
48
|
+
contains: build("contains"),
|
|
49
|
+
like: build("like"),
|
|
50
|
+
startsWith: build("starts_with"),
|
|
51
|
+
endsWith: build("ends_with"),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function aggregateStatics(name) {
|
|
55
|
+
// The reduce variable must be a valid identifier, so attribute names with `-`
|
|
56
|
+
// (kebab-case TypeDB type names) are sanitized to `_` for the wire `result_key`.
|
|
57
|
+
// The user-facing result key keeps the original attribute name. `avg` lowers to
|
|
58
|
+
// TypeDB's `mean`.
|
|
59
|
+
const wireName = name.replace(/[^A-Za-z0-9_]/g, "_");
|
|
60
|
+
const spec = (resultPrefix, fn) => () => new query_js_1.AggregateSpec({ result_key: `${resultPrefix}_${wireName}`, function: fn, attr_name: name }, `${resultPrefix}_${name}`);
|
|
61
|
+
return {
|
|
62
|
+
sum: spec("sum", "sum"),
|
|
63
|
+
avg: spec("avg", "mean"),
|
|
64
|
+
min: spec("min", "min"),
|
|
65
|
+
max: spec("max", "max"),
|
|
66
|
+
median: spec("median", "median"),
|
|
67
|
+
std: spec("std", "std"),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// Build the branded base class and attach the statics whose category was chosen
|
|
71
|
+
// by the factory. The statics objects are type-checked individually; the final
|
|
72
|
+
// assertion only states that `Object.assign` placed them on the constructor.
|
|
73
|
+
function namedAttribute(name, valueType, options) {
|
|
74
|
+
const attributeSchema = buildAttributeSchema(name, valueType, options);
|
|
75
|
+
const attributeSchemaEntries = buildAttributeSchemaEntries(attributeSchema, options);
|
|
76
|
+
class NamedAttribute extends Attribute {
|
|
77
|
+
static attrName = name;
|
|
78
|
+
static valueType = valueType;
|
|
79
|
+
static attributeSchema = attributeSchema;
|
|
80
|
+
static attributeSchemaEntries = attributeSchemaEntries;
|
|
81
|
+
}
|
|
82
|
+
return NamedAttribute;
|
|
83
|
+
}
|
|
84
|
+
function buildAttributeSchema(attrName, valueType, options) {
|
|
85
|
+
const entry = { attr_name: attrName, value_type: valueType };
|
|
86
|
+
if (options === undefined)
|
|
87
|
+
return entry;
|
|
88
|
+
if (options.parent !== undefined) {
|
|
89
|
+
entry.parent_type = parentTypeName(options.parent);
|
|
90
|
+
}
|
|
91
|
+
if (options.abstract !== undefined) {
|
|
92
|
+
entry.is_abstract = options.abstract;
|
|
93
|
+
}
|
|
94
|
+
if (options.independent !== undefined) {
|
|
95
|
+
entry.is_independent = options.independent;
|
|
96
|
+
}
|
|
97
|
+
if (options.regex !== undefined) {
|
|
98
|
+
entry.regex = options.regex;
|
|
99
|
+
}
|
|
100
|
+
if (options.values !== undefined) {
|
|
101
|
+
entry.allowed_values = options.values == null ? null : [...options.values];
|
|
102
|
+
}
|
|
103
|
+
if (options.range !== undefined) {
|
|
104
|
+
entry.range = options.range == null ? null : [options.range[0], options.range[1]];
|
|
105
|
+
}
|
|
106
|
+
return entry;
|
|
107
|
+
}
|
|
108
|
+
function buildAttributeSchemaEntries(entry, options) {
|
|
109
|
+
const parent = options?.parent;
|
|
110
|
+
const entries = [];
|
|
111
|
+
if (parent !== undefined && parent !== null && typeof parent !== "string") {
|
|
112
|
+
const parentEntries = parent.attributeSchemaEntries ??
|
|
113
|
+
(parent.attributeSchema === undefined ? [] : [parent.attributeSchema]);
|
|
114
|
+
entries.push(...parentEntries.map(copyAttributeSchemaEntry));
|
|
115
|
+
}
|
|
116
|
+
entries.push(copyAttributeSchemaEntry(entry));
|
|
117
|
+
return entries;
|
|
118
|
+
}
|
|
119
|
+
function copyAttributeSchemaEntry(entry) {
|
|
120
|
+
const copy = { ...entry };
|
|
121
|
+
if (entry.allowed_values !== undefined) {
|
|
122
|
+
copy.allowed_values = entry.allowed_values === null ? null : [...entry.allowed_values];
|
|
123
|
+
}
|
|
124
|
+
if (entry.range !== undefined) {
|
|
125
|
+
copy.range = entry.range === null ? null : [entry.range[0], entry.range[1]];
|
|
126
|
+
}
|
|
127
|
+
return copy;
|
|
128
|
+
}
|
|
129
|
+
function parentTypeName(parent) {
|
|
130
|
+
if (parent == null || typeof parent === "string") {
|
|
131
|
+
return parent;
|
|
132
|
+
}
|
|
133
|
+
return parent.attrName;
|
|
134
|
+
}
|
|
135
|
+
function makeComparableFactory(valueType) {
|
|
136
|
+
return (name, options) => {
|
|
137
|
+
const cls = namedAttribute(name, valueType, options);
|
|
138
|
+
Object.assign(cls, comparisonStatics(name, valueType), orderStatics(name));
|
|
139
|
+
return cls;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function makeStringFactory() {
|
|
143
|
+
return (name, options) => {
|
|
144
|
+
const cls = namedAttribute(name, "string", options);
|
|
145
|
+
Object.assign(cls, comparisonStatics(name, "string"), orderStatics(name), stringStatics(name));
|
|
146
|
+
return cls;
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function makeNumericFactory(valueType) {
|
|
150
|
+
return (name, options) => {
|
|
151
|
+
const cls = namedAttribute(name, valueType, options);
|
|
152
|
+
Object.assign(cls, comparisonStatics(name, valueType), orderStatics(name), aggregateStatics(name));
|
|
153
|
+
return cls;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Attribute base-class factories, one per TypeDB value type. Each call returns a
|
|
158
|
+
* branded base to extend: the mandatory `name` is both the schema `attr_name`
|
|
159
|
+
* and the compile-time brand, so distinct names produce non-interchangeable
|
|
160
|
+
* types. The `attr.*` namespace avoids shadowing the `String`/`Boolean`/`Date`
|
|
161
|
+
* JS globals. `Integer` wraps `bigint` and maps to the `long` wire type. String
|
|
162
|
+
* bases gain `contains`/`like`/`startsWith`/`endsWith`; numeric bases gain
|
|
163
|
+
* `sum`/`avg`/`min`/`max`/`median`/`std`; every base gains the comparison and
|
|
164
|
+
* sort helpers used to build typed queries.
|
|
165
|
+
*/
|
|
166
|
+
exports.attr = {
|
|
167
|
+
String: makeStringFactory(),
|
|
168
|
+
Integer: makeNumericFactory("long"),
|
|
169
|
+
Double: makeNumericFactory("double"),
|
|
170
|
+
Boolean: makeComparableFactory("boolean"),
|
|
171
|
+
Date: makeComparableFactory("date"),
|
|
172
|
+
DateTime: makeComparableFactory("datetime"),
|
|
173
|
+
DateTimeTZ: makeComparableFactory("datetime-tz"),
|
|
174
|
+
Decimal: makeNumericFactory("decimal"),
|
|
175
|
+
Duration: makeComparableFactory("duration"),
|
|
176
|
+
};
|
package/dist/codec.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type AttributeInput, type AttributeValue, type DynamicEntityRow, type OwnedAttributeDescriptor, type RuntimeAttributeValue, type ValueType } from "./index.js";
|
|
2
|
+
import { type Attribute } from "./attribute.js";
|
|
3
|
+
import type { AttributeClass, EntitySchema, RelationSchema } from "./model.js";
|
|
4
|
+
export declare class TypedCodecError extends Error {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Unwrap a branded `Attribute` instance to its plain primitive value. For a
|
|
9
|
+
* list field, unwrap each element of the array element-wise. Mirrors Python
|
|
10
|
+
* `Entity._unwrap_value` (`type_bridge/models/entity.py:329-335`).
|
|
11
|
+
*
|
|
12
|
+
* This is the serialization-only path (no query language, no DB crossing). The
|
|
13
|
+
* canonical plain-dict value encodings are:
|
|
14
|
+
* - `long` / i64 → `bigint`
|
|
15
|
+
* - `string`, `double`, `boolean` → JS native equivalents
|
|
16
|
+
* - `decimal`, `date`, `datetime`, `datetime-tz`, `duration` → string (as
|
|
17
|
+
* stored in the attribute's `.value` field, matching `expected-canonical.json`)
|
|
18
|
+
*/
|
|
19
|
+
export declare function attributeToPlain(value: Attribute<unknown, string>): unknown;
|
|
20
|
+
export declare function attributeToPlain(value: Attribute<unknown, string>[]): unknown[];
|
|
21
|
+
/**
|
|
22
|
+
* Wrap a plain primitive (or array of plain primitives) into a branded
|
|
23
|
+
* `Attribute` (or `Attribute[]`) using the given attribute class constructor.
|
|
24
|
+
* Mirrors the `new attrType(value)` brand-construction pattern in
|
|
25
|
+
* `hydrateAttributeValue` (`codec.ts:192-224`).
|
|
26
|
+
*
|
|
27
|
+
* Used by `fromDict` to re-brand plain dict values back into typed instances.
|
|
28
|
+
*/
|
|
29
|
+
export declare function plainToAttribute(attrType: AttributeClass, value: unknown, fieldName: string, isList: boolean): Attribute<unknown, string> | Attribute<unknown, string>[];
|
|
30
|
+
export declare function lowerAttributes(instance: object, schema: EntitySchema | RelationSchema): AttributeInput;
|
|
31
|
+
export declare function lowerFilters(filters: Record<string, Attribute<unknown, string>> | null | undefined, schema: EntitySchema | RelationSchema): Record<string, AttributeValue> | null;
|
|
32
|
+
export declare function hydrateAttributes(row: Pick<DynamicEntityRow, "attributes">, schema: EntitySchema | RelationSchema): Record<string, Attribute<unknown, string> | Attribute<unknown, string>[]>;
|
|
33
|
+
export declare function hydrateAttributeEntries(entries: readonly (readonly [string, RuntimeAttributeValue])[], schema: EntitySchema | RelationSchema): Record<string, Attribute<unknown, string> | Attribute<unknown, string>[]>;
|
|
34
|
+
export declare function runtimeAttributeValueFromUnknown(value: unknown, valueType?: ValueType): RuntimeAttributeValue;
|
|
35
|
+
export declare function keyAttributeDescriptor(schema: EntitySchema): OwnedAttributeDescriptor | null;
|
|
36
|
+
export declare function lowerAttributeValue(valueType: ValueType, value: unknown): AttributeValue;
|
package/dist/codec.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypedCodecError = void 0;
|
|
4
|
+
exports.attributeToPlain = attributeToPlain;
|
|
5
|
+
exports.plainToAttribute = plainToAttribute;
|
|
6
|
+
exports.lowerAttributes = lowerAttributes;
|
|
7
|
+
exports.lowerFilters = lowerFilters;
|
|
8
|
+
exports.hydrateAttributes = hydrateAttributes;
|
|
9
|
+
exports.hydrateAttributeEntries = hydrateAttributeEntries;
|
|
10
|
+
exports.runtimeAttributeValueFromUnknown = runtimeAttributeValueFromUnknown;
|
|
11
|
+
exports.keyAttributeDescriptor = keyAttributeDescriptor;
|
|
12
|
+
exports.lowerAttributeValue = lowerAttributeValue;
|
|
13
|
+
const index_js_1 = require("./index.js");
|
|
14
|
+
class TypedCodecError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "TypedCodecError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.TypedCodecError = TypedCodecError;
|
|
21
|
+
function attributeToPlain(value) {
|
|
22
|
+
if (Array.isArray(value)) {
|
|
23
|
+
return value.map((element) => element.value);
|
|
24
|
+
}
|
|
25
|
+
return value.value;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Wrap a plain primitive (or array of plain primitives) into a branded
|
|
29
|
+
* `Attribute` (or `Attribute[]`) using the given attribute class constructor.
|
|
30
|
+
* Mirrors the `new attrType(value)` brand-construction pattern in
|
|
31
|
+
* `hydrateAttributeValue` (`codec.ts:192-224`).
|
|
32
|
+
*
|
|
33
|
+
* Used by `fromDict` to re-brand plain dict values back into typed instances.
|
|
34
|
+
*/
|
|
35
|
+
function plainToAttribute(attrType, value, fieldName, isList) {
|
|
36
|
+
if (isList) {
|
|
37
|
+
if (!Array.isArray(value)) {
|
|
38
|
+
throw new TypedCodecError(`List field "${fieldName}" must be an array in fromDict input`);
|
|
39
|
+
}
|
|
40
|
+
return value.map((element) => {
|
|
41
|
+
if (element === null || element === undefined) {
|
|
42
|
+
throw new TypedCodecError(`List field "${fieldName}" contains null/undefined element`);
|
|
43
|
+
}
|
|
44
|
+
return new attrType(element);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return new attrType(value);
|
|
48
|
+
}
|
|
49
|
+
function lowerAttributes(instance, schema) {
|
|
50
|
+
const lowered = {};
|
|
51
|
+
const source = instance;
|
|
52
|
+
for (const [fieldName, spec] of Object.entries(schema)) {
|
|
53
|
+
const value = source[fieldName];
|
|
54
|
+
if (value === undefined) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (isListFieldSpec(spec)) {
|
|
58
|
+
// A list field value is Attr[] — lower each element to its plain wire value
|
|
59
|
+
// and pass the resulting array. The NAPI insert parser already accepts a JS
|
|
60
|
+
// array per field and lowers it to repeated (attr_name, value) tuples
|
|
61
|
+
// (crates/node/src/lib.rs, the `value.as_array()` branch).
|
|
62
|
+
if (!Array.isArray(value)) {
|
|
63
|
+
throw new TypedCodecError(`List field "${fieldName}" must be an array`);
|
|
64
|
+
}
|
|
65
|
+
lowered[fieldName] = value.map((element) => {
|
|
66
|
+
if (!isAttributeInstance(element)) {
|
|
67
|
+
throw new TypedCodecError(`List field "${fieldName}" elements must be Attribute instances`);
|
|
68
|
+
}
|
|
69
|
+
return lowerAttributeValue(spec.attrType.valueType, element.value);
|
|
70
|
+
});
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (!isFieldSpec(spec)) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (!isAttributeInstance(value)) {
|
|
77
|
+
throw new TypedCodecError(`Field "${fieldName}" must be an Attribute instance`);
|
|
78
|
+
}
|
|
79
|
+
lowered[fieldName] = lowerAttributeValue(spec.attrType.valueType, value.value);
|
|
80
|
+
}
|
|
81
|
+
return lowered;
|
|
82
|
+
}
|
|
83
|
+
function lowerFilters(filters, schema) {
|
|
84
|
+
if (filters == null) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const lowered = {};
|
|
88
|
+
for (const [fieldName, value] of Object.entries(filters)) {
|
|
89
|
+
const spec = schema[fieldName];
|
|
90
|
+
if (!isFieldSpec(spec)) {
|
|
91
|
+
throw new TypedCodecError(`Unknown attribute filter field "${fieldName}"`);
|
|
92
|
+
}
|
|
93
|
+
lowered[fieldName] = lowerAttributeValue(spec.attrType.valueType, value.value);
|
|
94
|
+
}
|
|
95
|
+
return lowered;
|
|
96
|
+
}
|
|
97
|
+
function hydrateAttributes(row, schema) {
|
|
98
|
+
return hydrateAttributeEntries(row.attributes, schema);
|
|
99
|
+
}
|
|
100
|
+
function hydrateAttributeEntries(entries, schema) {
|
|
101
|
+
const fieldsByAttrName = fieldSpecsByAttrName(schema);
|
|
102
|
+
const hydrated = {};
|
|
103
|
+
for (const [attrName, value] of entries) {
|
|
104
|
+
const field = fieldsByAttrName.get(attrName);
|
|
105
|
+
if (field === undefined) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const hydrated_value = hydrateAttributeValue(field.spec.attrType, value);
|
|
109
|
+
if (field.isList) {
|
|
110
|
+
// Collect repeated-name tuples into a typed Attr[] for list fields.
|
|
111
|
+
// TypeDB returns multi-value attributes as repeated [attr_name, value] pairs
|
|
112
|
+
// with the same attr_name. Each pair is one element of the list.
|
|
113
|
+
const existing = hydrated[field.fieldName];
|
|
114
|
+
if (Array.isArray(existing)) {
|
|
115
|
+
existing.push(hydrated_value);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
hydrated[field.fieldName] = [hydrated_value];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
// Scalar field: single value, same behavior as before (no change).
|
|
123
|
+
hydrated[field.fieldName] = hydrated_value;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return hydrated;
|
|
127
|
+
}
|
|
128
|
+
function runtimeAttributeValueFromUnknown(value, valueType) {
|
|
129
|
+
if (!isRuntimeAttributeValue(value)) {
|
|
130
|
+
if (valueType !== undefined) {
|
|
131
|
+
return runtimeAttributeValueFromPrimitive(value, valueType);
|
|
132
|
+
}
|
|
133
|
+
throw new TypedCodecError("Role-player attribute value has an unknown runtime tag");
|
|
134
|
+
}
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
function keyAttributeDescriptor(schema) {
|
|
138
|
+
for (const [fieldName, spec] of Object.entries(schema)) {
|
|
139
|
+
if (!isFieldSpec(spec)) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const annotations = spec.flags.annotations;
|
|
143
|
+
if (annotations.includes("Key")) {
|
|
144
|
+
return {
|
|
145
|
+
field_name: fieldName,
|
|
146
|
+
attr_name: spec.attrType.attrName,
|
|
147
|
+
value_type: spec.attrType.valueType,
|
|
148
|
+
annotations: ["Key"],
|
|
149
|
+
is_optional: false,
|
|
150
|
+
is_ordered: false,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
function lowerAttributeValue(valueType, value) {
|
|
157
|
+
switch (valueType) {
|
|
158
|
+
case "string":
|
|
159
|
+
return (0, index_js_1.string)(requireType(value, "string", valueType));
|
|
160
|
+
case "long":
|
|
161
|
+
return (0, index_js_1.long)(requireType(value, "bigint", valueType));
|
|
162
|
+
case "double":
|
|
163
|
+
return (0, index_js_1.double)(requireType(value, "number", valueType));
|
|
164
|
+
case "boolean":
|
|
165
|
+
return (0, index_js_1.boolean)(requireType(value, "boolean", valueType));
|
|
166
|
+
case "date":
|
|
167
|
+
return (0, index_js_1.date)(requireType(value, "string", valueType));
|
|
168
|
+
case "datetime":
|
|
169
|
+
return (0, index_js_1.datetime)(requireType(value, "string", valueType));
|
|
170
|
+
case "datetime-tz":
|
|
171
|
+
return (0, index_js_1.datetimetz)(requireType(value, "string", valueType));
|
|
172
|
+
case "decimal":
|
|
173
|
+
return (0, index_js_1.decimal)(requireType(value, "string", valueType));
|
|
174
|
+
case "duration":
|
|
175
|
+
return (0, index_js_1.duration)(requireType(value, "string", valueType));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function hydrateAttributeValue(attrType, value) {
|
|
179
|
+
if ("String" in value) {
|
|
180
|
+
return new attrType(value.String);
|
|
181
|
+
}
|
|
182
|
+
if ("Long" in value) {
|
|
183
|
+
return new attrType(BigInt(value.Long));
|
|
184
|
+
}
|
|
185
|
+
if ("Double" in value) {
|
|
186
|
+
return new attrType(value.Double);
|
|
187
|
+
}
|
|
188
|
+
if ("Boolean" in value) {
|
|
189
|
+
return new attrType(value.Boolean);
|
|
190
|
+
}
|
|
191
|
+
if ("Date" in value) {
|
|
192
|
+
return new attrType(value.Date);
|
|
193
|
+
}
|
|
194
|
+
if ("DateTime" in value) {
|
|
195
|
+
return new attrType(value.DateTime);
|
|
196
|
+
}
|
|
197
|
+
if ("DateTimeTZ" in value) {
|
|
198
|
+
return new attrType(value.DateTimeTZ);
|
|
199
|
+
}
|
|
200
|
+
if ("Decimal" in value) {
|
|
201
|
+
return new attrType(value.Decimal);
|
|
202
|
+
}
|
|
203
|
+
if ("Duration" in value) {
|
|
204
|
+
return new attrType(value.Duration);
|
|
205
|
+
}
|
|
206
|
+
throw new TypedCodecError("Unknown runtime attribute value tag");
|
|
207
|
+
}
|
|
208
|
+
function fieldSpecsByAttrName(schema) {
|
|
209
|
+
const fields = new Map();
|
|
210
|
+
for (const [fieldName, spec] of Object.entries(schema)) {
|
|
211
|
+
if (isListFieldSpec(spec)) {
|
|
212
|
+
fields.set(spec.attrType.attrName, { fieldName, spec, isList: true });
|
|
213
|
+
}
|
|
214
|
+
else if (isFieldSpec(spec)) {
|
|
215
|
+
fields.set(spec.attrType.attrName, { fieldName, spec, isList: false });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return fields;
|
|
219
|
+
}
|
|
220
|
+
function isFieldSpec(spec) {
|
|
221
|
+
return spec !== undefined && spec.kind === "field";
|
|
222
|
+
}
|
|
223
|
+
function isListFieldSpec(spec) {
|
|
224
|
+
return spec !== undefined && spec.kind === "list-field";
|
|
225
|
+
}
|
|
226
|
+
function isAttributeInstance(value) {
|
|
227
|
+
return typeof value === "object" && value !== null && "value" in value;
|
|
228
|
+
}
|
|
229
|
+
function isRuntimeAttributeValue(value) {
|
|
230
|
+
if (typeof value !== "object" || value === null) {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
const record = value;
|
|
234
|
+
return (typeof record.String === "string" ||
|
|
235
|
+
typeof record.Long === "string" ||
|
|
236
|
+
typeof record.Double === "number" ||
|
|
237
|
+
typeof record.Boolean === "boolean" ||
|
|
238
|
+
typeof record.Date === "string" ||
|
|
239
|
+
typeof record.DateTime === "string" ||
|
|
240
|
+
typeof record.DateTimeTZ === "string" ||
|
|
241
|
+
typeof record.Decimal === "string" ||
|
|
242
|
+
typeof record.Duration === "string");
|
|
243
|
+
}
|
|
244
|
+
function runtimeAttributeValueFromPrimitive(value, valueType) {
|
|
245
|
+
switch (valueType) {
|
|
246
|
+
case "string":
|
|
247
|
+
return { String: requireType(value, "string", valueType) };
|
|
248
|
+
case "long":
|
|
249
|
+
if (typeof value === "number" && Number.isInteger(value)) {
|
|
250
|
+
return { Long: value.toString() };
|
|
251
|
+
}
|
|
252
|
+
return { Long: requireType(value, "string", valueType) };
|
|
253
|
+
case "double":
|
|
254
|
+
return { Double: requireType(value, "number", valueType) };
|
|
255
|
+
case "boolean":
|
|
256
|
+
return { Boolean: requireType(value, "boolean", valueType) };
|
|
257
|
+
case "date":
|
|
258
|
+
return { Date: requireType(value, "string", valueType) };
|
|
259
|
+
case "datetime":
|
|
260
|
+
return { DateTime: requireType(value, "string", valueType) };
|
|
261
|
+
case "datetime-tz":
|
|
262
|
+
return { DateTimeTZ: requireType(value, "string", valueType) };
|
|
263
|
+
case "decimal":
|
|
264
|
+
return { Decimal: requireType(value, "string", valueType) };
|
|
265
|
+
case "duration":
|
|
266
|
+
return { Duration: requireType(value, "string", valueType) };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function requireType(value, expected, valueType) {
|
|
270
|
+
if (typeof value !== expected) {
|
|
271
|
+
throw new TypedCodecError(`${valueType} attribute expected ${expected} value`);
|
|
272
|
+
}
|
|
273
|
+
return value;
|
|
274
|
+
}
|