kitedb 0.2.2

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.
@@ -0,0 +1,210 @@
1
+ /**
2
+ * Schema Definition API for RayDB
3
+ *
4
+ * Provides type-safe schema builders for defining graph nodes and edges.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { node, edge, prop, optional } from '@ray-db/core'
9
+ *
10
+ * const User = node('user', {
11
+ * key: (id: string) => `user:${id}`,
12
+ * props: {
13
+ * name: prop.string('name'),
14
+ * email: prop.string('email'),
15
+ * age: optional(prop.int('age')),
16
+ * },
17
+ * })
18
+ *
19
+ * const knows = edge('knows', {
20
+ * since: prop.int('since'),
21
+ * })
22
+ * ```
23
+ */
24
+ /** Property type identifiers */
25
+ export type PropType = 'string' | 'int' | 'float' | 'bool' | 'vector' | 'any';
26
+ /** Property specification */
27
+ export interface PropSpec {
28
+ /** Property type */
29
+ type: PropType;
30
+ /** Whether this property is optional */
31
+ optional?: boolean;
32
+ /** Default value for this property */
33
+ default?: unknown;
34
+ }
35
+ /**
36
+ * Property type builders.
37
+ *
38
+ * Use these to define typed properties on nodes and edges.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const name = prop.string('name') // required string
43
+ * const age = optional(prop.int('age')) // optional int
44
+ * const score = prop.float('score') // required float
45
+ * const active = prop.bool('active') // required bool
46
+ * const embedding = prop.vector('embedding', 1536) // vector with dimensions
47
+ * ```
48
+ */
49
+ export declare const prop: {
50
+ /**
51
+ * String property.
52
+ * Stored as UTF-8 strings.
53
+ */
54
+ string: (_name: string) => PropSpec;
55
+ /**
56
+ * Integer property.
57
+ * Stored as 64-bit signed integers.
58
+ */
59
+ int: (_name: string) => PropSpec;
60
+ /**
61
+ * Float property.
62
+ * Stored as 64-bit IEEE 754 floats.
63
+ */
64
+ float: (_name: string) => PropSpec;
65
+ /**
66
+ * Boolean property.
67
+ */
68
+ bool: (_name: string) => PropSpec;
69
+ /**
70
+ * Vector property for embeddings.
71
+ * Stored as Float32 arrays.
72
+ *
73
+ * @param _name - Property name
74
+ * @param _dimensions - Vector dimensions (for documentation/validation)
75
+ */
76
+ vector: (_name: string, _dimensions?: number) => PropSpec;
77
+ /**
78
+ * Any property (schema-less).
79
+ * Accepts any value type.
80
+ */
81
+ any: (_name: string) => PropSpec;
82
+ };
83
+ /**
84
+ * Mark a property as optional.
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const age = optional(prop.int('age'))
89
+ * ```
90
+ */
91
+ export declare function optional<T extends PropSpec>(spec: T): T;
92
+ /**
93
+ * Set a default value for a property.
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * const status = withDefault(prop.string('status'), 'active')
98
+ * ```
99
+ */
100
+ export declare function withDefault<T extends PropSpec>(spec: T, value: unknown): T;
101
+ /** Key generation strategy */
102
+ export interface KeySpec {
103
+ /** Key generation kind */
104
+ kind: 'prefix' | 'template' | 'parts';
105
+ /** Key prefix (for all kinds) */
106
+ prefix?: string;
107
+ /** Template string with {field} placeholders (for 'template' kind) */
108
+ template?: string;
109
+ /** Field names to concatenate (for 'parts' kind) */
110
+ fields?: string[];
111
+ /** Separator between parts (for 'parts' kind, default ':') */
112
+ separator?: string;
113
+ }
114
+ /** Node type specification */
115
+ export interface NodeSpec {
116
+ /** Node type name (must be unique per database) */
117
+ name: string;
118
+ /** Key generation specification */
119
+ key?: KeySpec;
120
+ /** Property definitions */
121
+ props?: Record<string, PropSpec>;
122
+ }
123
+ /** Configuration for node() */
124
+ export interface NodeConfig<K extends string = string> {
125
+ /**
126
+ * Key generator function or key specification.
127
+ *
128
+ * If a function is provided, it will be analyzed to extract the key prefix.
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * // Function form - prefix is extracted automatically
133
+ * key: (id: string) => `user:${id}`
134
+ *
135
+ * // Object form - explicit specification
136
+ * key: { kind: 'prefix', prefix: 'user:' }
137
+ * key: { kind: 'template', template: 'user:{org}:{id}' }
138
+ * key: { kind: 'parts', fields: ['org', 'id'], separator: ':' }
139
+ * ```
140
+ */
141
+ key?: ((arg: K) => string) | KeySpec;
142
+ /** Property definitions */
143
+ props?: Record<string, PropSpec>;
144
+ }
145
+ /**
146
+ * Define a node type with properties.
147
+ *
148
+ * Creates a node definition that can be used for all node operations
149
+ * (insert, update, delete, query).
150
+ *
151
+ * @param name - The node type name (must be unique)
152
+ * @param config - Node configuration with key function and properties
153
+ * @returns A NodeSpec that can be passed to ray()
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * const User = node('user', {
158
+ * key: (id: string) => `user:${id}`,
159
+ * props: {
160
+ * name: prop.string('name'),
161
+ * email: prop.string('email'),
162
+ * age: optional(prop.int('age')),
163
+ * },
164
+ * })
165
+ *
166
+ * // With template key
167
+ * const OrgUser = node('org_user', {
168
+ * key: { kind: 'template', template: 'org:{org}:user:{id}' },
169
+ * props: {
170
+ * name: prop.string('name'),
171
+ * },
172
+ * })
173
+ * ```
174
+ */
175
+ export declare function node<K extends string = string>(name: string, config?: NodeConfig<K>): NodeSpec;
176
+ /** Edge type specification */
177
+ export interface EdgeSpec {
178
+ /** Edge type name (must be unique per database) */
179
+ name: string;
180
+ /** Property definitions */
181
+ props?: Record<string, PropSpec>;
182
+ }
183
+ /**
184
+ * Define an edge type with optional properties.
185
+ *
186
+ * Creates an edge definition that can be used for all edge operations
187
+ * (link, unlink, query). Edges are directional and can have properties.
188
+ *
189
+ * @param name - The edge type name (must be unique)
190
+ * @param props - Optional property definitions
191
+ * @returns An EdgeSpec that can be passed to ray()
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * // Edge with properties
196
+ * const knows = edge('knows', {
197
+ * since: prop.int('since'),
198
+ * weight: optional(prop.float('weight')),
199
+ * })
200
+ *
201
+ * // Edge without properties
202
+ * const follows = edge('follows')
203
+ * ```
204
+ */
205
+ export declare function edge(name: string, props?: Record<string, PropSpec>): EdgeSpec;
206
+ /** @deprecated Use `node()` instead */
207
+ export declare const defineNode: typeof node;
208
+ /** @deprecated Use `edge()` instead */
209
+ export declare const defineEdge: typeof edge;
210
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../ts/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAMH,gCAAgC;AAChC,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,KAAK,GAAG,OAAO,GAAG,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAA;AAE7E,6BAA6B;AAC7B,MAAM,WAAW,QAAQ;IACvB,oBAAoB;IACpB,IAAI,EAAE,QAAQ,CAAA;IACd,wCAAwC;IACxC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,sCAAsC;IACtC,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAMD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,IAAI;IACf;;;OAGG;oBACa,MAAM,KAAG,QAAQ;IAEjC;;;OAGG;iBACU,MAAM,KAAG,QAAQ;IAE9B;;;OAGG;mBACY,MAAM,KAAG,QAAQ;IAEhC;;OAEG;kBACW,MAAM,KAAG,QAAQ;IAE/B;;;;;;OAMG;oBACa,MAAM,gBAAgB,MAAM,KAAG,QAAQ;IAEvD;;;OAGG;iBACU,MAAM,KAAG,QAAQ;CAC/B,CAAA;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,QAAQ,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,CAEvD;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,QAAQ,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,CAE1E;AAMD,8BAA8B;AAC9B,MAAM,WAAW,OAAO;IACtB,0BAA0B;IAC1B,IAAI,EAAE,QAAQ,GAAG,UAAU,GAAG,OAAO,CAAA;IACrC,iCAAiC;IACjC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,sEAA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AO,CAAA;AAE9B,uCAAuC;AACvC,eAAO,MAAM,UAAU,aAAO,CAAA"}
package/dist/schema.js ADDED
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ /**
3
+ * Schema Definition API for RayDB
4
+ *
5
+ * Provides type-safe schema builders for defining graph nodes and edges.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { node, edge, prop, optional } from '@ray-db/core'
10
+ *
11
+ * const User = node('user', {
12
+ * key: (id: string) => `user:${id}`,
13
+ * props: {
14
+ * name: prop.string('name'),
15
+ * email: prop.string('email'),
16
+ * age: optional(prop.int('age')),
17
+ * },
18
+ * })
19
+ *
20
+ * const knows = edge('knows', {
21
+ * since: prop.int('since'),
22
+ * })
23
+ * ```
24
+ */
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.defineEdge = exports.defineNode = exports.prop = void 0;
27
+ exports.optional = optional;
28
+ exports.withDefault = withDefault;
29
+ exports.node = node;
30
+ exports.edge = edge;
31
+ // =============================================================================
32
+ // Property Builders
33
+ // =============================================================================
34
+ /**
35
+ * Property type builders.
36
+ *
37
+ * Use these to define typed properties on nodes and edges.
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const name = prop.string('name') // required string
42
+ * const age = optional(prop.int('age')) // optional int
43
+ * const score = prop.float('score') // required float
44
+ * const active = prop.bool('active') // required bool
45
+ * const embedding = prop.vector('embedding', 1536) // vector with dimensions
46
+ * ```
47
+ */
48
+ exports.prop = {
49
+ /**
50
+ * String property.
51
+ * Stored as UTF-8 strings.
52
+ */
53
+ string: (_name) => ({ type: 'string' }),
54
+ /**
55
+ * Integer property.
56
+ * Stored as 64-bit signed integers.
57
+ */
58
+ int: (_name) => ({ type: 'int' }),
59
+ /**
60
+ * Float property.
61
+ * Stored as 64-bit IEEE 754 floats.
62
+ */
63
+ float: (_name) => ({ type: 'float' }),
64
+ /**
65
+ * Boolean property.
66
+ */
67
+ bool: (_name) => ({ type: 'bool' }),
68
+ /**
69
+ * Vector property for embeddings.
70
+ * Stored as Float32 arrays.
71
+ *
72
+ * @param _name - Property name
73
+ * @param _dimensions - Vector dimensions (for documentation/validation)
74
+ */
75
+ vector: (_name, _dimensions) => ({ type: 'vector' }),
76
+ /**
77
+ * Any property (schema-less).
78
+ * Accepts any value type.
79
+ */
80
+ any: (_name) => ({ type: 'any' }),
81
+ };
82
+ /**
83
+ * Mark a property as optional.
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * const age = optional(prop.int('age'))
88
+ * ```
89
+ */
90
+ function optional(spec) {
91
+ return { ...spec, optional: true };
92
+ }
93
+ /**
94
+ * Set a default value for a property.
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * const status = withDefault(prop.string('status'), 'active')
99
+ * ```
100
+ */
101
+ function withDefault(spec, value) {
102
+ return { ...spec, default: value };
103
+ }
104
+ /**
105
+ * Define a node type with properties.
106
+ *
107
+ * Creates a node definition that can be used for all node operations
108
+ * (insert, update, delete, query).
109
+ *
110
+ * @param name - The node type name (must be unique)
111
+ * @param config - Node configuration with key function and properties
112
+ * @returns A NodeSpec that can be passed to ray()
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const User = node('user', {
117
+ * key: (id: string) => `user:${id}`,
118
+ * props: {
119
+ * name: prop.string('name'),
120
+ * email: prop.string('email'),
121
+ * age: optional(prop.int('age')),
122
+ * },
123
+ * })
124
+ *
125
+ * // With template key
126
+ * const OrgUser = node('org_user', {
127
+ * key: { kind: 'template', template: 'org:{org}:user:{id}' },
128
+ * props: {
129
+ * name: prop.string('name'),
130
+ * },
131
+ * })
132
+ * ```
133
+ */
134
+ function node(name, config) {
135
+ if (!config) {
136
+ return { name };
137
+ }
138
+ let keySpec;
139
+ if (typeof config.key === 'function') {
140
+ // Extract prefix from key function by calling it with a test value
141
+ const testKey = config.key('__test__');
142
+ const testIdx = testKey.indexOf('__test__');
143
+ if (testIdx !== -1) {
144
+ const prefix = testKey.slice(0, testIdx);
145
+ keySpec = { kind: 'prefix', prefix };
146
+ }
147
+ else {
148
+ // Couldn't extract prefix, use default
149
+ keySpec = { kind: 'prefix', prefix: `${name}:` };
150
+ }
151
+ }
152
+ else if (config.key) {
153
+ keySpec = config.key;
154
+ }
155
+ return {
156
+ name,
157
+ key: keySpec,
158
+ props: config.props,
159
+ };
160
+ }
161
+ /**
162
+ * Define an edge type with optional properties.
163
+ *
164
+ * Creates an edge definition that can be used for all edge operations
165
+ * (link, unlink, query). Edges are directional and can have properties.
166
+ *
167
+ * @param name - The edge type name (must be unique)
168
+ * @param props - Optional property definitions
169
+ * @returns An EdgeSpec that can be passed to ray()
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * // Edge with properties
174
+ * const knows = edge('knows', {
175
+ * since: prop.int('since'),
176
+ * weight: optional(prop.float('weight')),
177
+ * })
178
+ *
179
+ * // Edge without properties
180
+ * const follows = edge('follows')
181
+ * ```
182
+ */
183
+ function edge(name, props) {
184
+ return { name, props };
185
+ }
186
+ // =============================================================================
187
+ // Aliases for backwards compatibility
188
+ // =============================================================================
189
+ /** @deprecated Use `node()` instead */
190
+ exports.defineNode = node;
191
+ /** @deprecated Use `edge()` instead */
192
+ exports.defineEdge = edge;
193
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../ts/schema.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;AAqFH,4BAEC;AAUD,kCAEC;AAuFD,oBA2BC;AAoCD,oBAEC;AAxOD,gFAAgF;AAChF,oBAAoB;AACpB,gFAAgF;AAEhF;;;;;;;;;;;;;GAaG;AACU,QAAA,IAAI,GAAG;IAClB;;;OAGG;IACH,MAAM,EAAE,CAAC,KAAa,EAAY,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;IAEzD;;;OAGG;IACH,GAAG,EAAE,CAAC,KAAa,EAAY,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;IA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