1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Graph facade for WASM bindings.
//!
//! Provides a JavaScript-friendly wrapper around the core `crate::storage::Graph`.
use std::sync::Arc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsError;
use crate::storage::cow::Graph as InnerGraph;
use crate::storage::GraphStorage;
use crate::wasm::traversal::Traversal;
use crate::wasm::types::{
create_edge_js, create_vertex_js, js_array_to_vertex_ids, js_to_edge_id, js_to_properties,
js_to_value, js_to_vertex_id,
};
#[cfg(any(feature = "gql", feature = "gremlin"))]
use crate::wasm::types::{value_to_js, values_to_js_array};
/// An in-memory property graph database.
///
/// @example
/// ```typescript
/// const graph = new Graph();
/// const alice = graph.addVertex('person', { name: 'Alice', age: 30n });
/// const bob = graph.addVertex('person', { name: 'Bob', age: 25n });
/// graph.addEdge(alice, bob, 'knows', { since: 2020n });
/// ```
#[wasm_bindgen]
pub struct Graph {
inner: Arc<InnerGraph>,
}
#[wasm_bindgen]
impl Graph {
/// Create a new empty graph.
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
inner: Arc::new(InnerGraph::new()),
}
}
// =========================================================================
// Vertex Operations
// =========================================================================
/// Add a vertex with a label and properties.
///
/// @param label - The vertex label (e.g., 'person', 'product')
/// @param properties - Key-value properties (optional)
/// @returns The new vertex's ID as bigint
#[wasm_bindgen(js_name = "addVertex")]
pub fn add_vertex(&self, label: &str, properties: JsValue) -> Result<u64, JsError> {
let props = js_to_properties(properties)?;
let id = self.inner.add_vertex(label, props);
Ok(id.0)
}
/// Get a vertex by ID.
///
/// @param id - The vertex ID
/// @returns The vertex object, or undefined if not found
#[wasm_bindgen(js_name = "getVertex")]
pub fn get_vertex(&self, id: JsValue) -> Result<JsValue, JsError> {
let vertex_id = js_to_vertex_id(id)?;
let snapshot = self.inner.snapshot();
match snapshot.get_vertex(vertex_id) {
Some(v) => create_vertex_js(v.id, &v.label, &v.properties),
None => Ok(JsValue::UNDEFINED),
}
}
/// Remove a vertex and all its edges.
///
/// @param id - The vertex ID to remove
/// @returns true if removed, false if not found
#[wasm_bindgen(js_name = "removeVertex")]
pub fn remove_vertex(&self, id: JsValue) -> Result<bool, JsError> {
let vertex_id = js_to_vertex_id(id)?;
match self.inner.remove_vertex(vertex_id) {
Ok(_) => Ok(true),
Err(crate::error::StorageError::VertexNotFound(_)) => Ok(false),
Err(e) => Err(JsError::from(e)),
}
}
/// Set a property on a vertex.
///
/// @param id - The vertex ID
/// @param key - Property name
/// @param value - Property value
/// @throws If vertex not found
#[wasm_bindgen(js_name = "setVertexProperty")]
pub fn set_vertex_property(
&self,
id: JsValue,
key: &str,
value: JsValue,
) -> Result<(), JsError> {
let vertex_id = js_to_vertex_id(id)?;
let val = js_to_value(value)?;
self.inner
.set_vertex_property(vertex_id, key, val)
.map_err(JsError::from)
}
/// Remove a property from a vertex.
///
/// @param id - The vertex ID
/// @param key - Property name to remove
/// @returns true if removed, false if property didn't exist or vertex not found
#[wasm_bindgen(js_name = "removeVertexProperty")]
pub fn remove_vertex_property(&self, id: JsValue, key: &str) -> Result<bool, JsError> {
let vertex_id = js_to_vertex_id(id)?;
// Get snapshot and check if vertex exists with the property
let snapshot = self.inner.snapshot();
if let Some(vertex) = snapshot.get_vertex(vertex_id) {
if vertex.properties.contains_key(key) {
// Set to null to "remove" - alternatively we could add a proper remove method
// For now, this is a workaround since remove_vertex_property doesn't exist
self.inner
.set_vertex_property(vertex_id, key, crate::value::Value::Null)
.map_err(|e| JsError::new(&e.to_string()))?;
Ok(true)
} else {
Ok(false) // Property doesn't exist
}
} else {
Err(JsError::new(&format!("Vertex not found: {}", vertex_id.0)))
}
}
// =========================================================================
// Edge Operations
// =========================================================================
/// Add an edge between two vertices.
///
/// @param from - Source vertex ID
/// @param to - Target vertex ID
/// @param label - The edge label (e.g., 'knows', 'purchased')
/// @param properties - Key-value properties (optional)
/// @returns The new edge's ID as bigint
/// @throws If source or target vertex not found
#[wasm_bindgen(js_name = "addEdge")]
pub fn add_edge(
&self,
from: JsValue,
to: JsValue,
label: &str,
properties: JsValue,
) -> Result<u64, JsError> {
let from_id = js_to_vertex_id(from)?;
let to_id = js_to_vertex_id(to)?;
let props = js_to_properties(properties)?;
self.inner
.add_edge(from_id, to_id, label, props)
.map(|id| id.0)
.map_err(JsError::from)
}
/// Get an edge by ID.
///
/// @param id - The edge ID
/// @returns The edge object, or undefined if not found
#[wasm_bindgen(js_name = "getEdge")]
pub fn get_edge(&self, id: JsValue) -> Result<JsValue, JsError> {
let edge_id = js_to_edge_id(id)?;
let snapshot = self.inner.snapshot();
match snapshot.get_edge(edge_id) {
Some(e) => create_edge_js(e.id, &e.label, e.src, e.dst, &e.properties),
None => Ok(JsValue::UNDEFINED),
}
}
/// Remove an edge.
///
/// @param id - The edge ID to remove
/// @returns true if removed, false if not found
#[wasm_bindgen(js_name = "removeEdge")]
pub fn remove_edge(&self, id: JsValue) -> Result<bool, JsError> {
let edge_id = js_to_edge_id(id)?;
match self.inner.remove_edge(edge_id) {
Ok(_) => Ok(true),
Err(crate::error::StorageError::EdgeNotFound(_)) => Ok(false),
Err(e) => Err(JsError::from(e)),
}
}
/// Set a property on an edge.
///
/// @param id - The edge ID
/// @param key - Property name
/// @param value - Property value
/// @throws If edge not found
#[wasm_bindgen(js_name = "setEdgeProperty")]
pub fn set_edge_property(&self, id: JsValue, key: &str, value: JsValue) -> Result<(), JsError> {
let edge_id = js_to_edge_id(id)?;
let val = js_to_value(value)?;
self.inner
.set_edge_property(edge_id, key, val)
.map_err(JsError::from)
}
/// Remove a property from an edge.
///
/// @param id - The edge ID
/// @param key - Property name to remove
/// @returns true if removed, false if property didn't exist or edge not found
#[wasm_bindgen(js_name = "removeEdgeProperty")]
pub fn remove_edge_property(&self, id: JsValue, key: &str) -> Result<bool, JsError> {
let edge_id = js_to_edge_id(id)?;
// Get snapshot and check if edge exists with the property
let snapshot = self.inner.snapshot();
if let Some(edge) = snapshot.get_edge(edge_id) {
if edge.properties.contains_key(key) {
// Set to null to "remove" - alternatively we could add a proper remove method
// For now, this is a workaround since remove_edge_property doesn't exist
self.inner
.set_edge_property(edge_id, key, crate::value::Value::Null)
.map_err(|e| JsError::new(&e.to_string()))?;
Ok(true)
} else {
Ok(false) // Property doesn't exist
}
} else {
Err(JsError::new(&format!("Edge not found: {}", edge_id.0)))
}
}
// =========================================================================
// Graph Statistics
// =========================================================================
/// Get the total number of vertices.
#[wasm_bindgen(js_name = "vertexCount")]
pub fn vertex_count(&self) -> u64 {
self.inner.vertex_count()
}
/// Get the total number of edges.
#[wasm_bindgen(js_name = "edgeCount")]
pub fn edge_count(&self) -> u64 {
self.inner.edge_count()
}
/// Clear all vertices and edges from the graph.
///
/// Note: This is a no-op placeholder - true clear is not yet implemented.
pub fn clear(&self) {
// COW graph doesn't have a clear method yet
// For now, this is a no-op. Users should create a new Graph instead.
// TODO: Implement clear() in the inner graph
}
// =========================================================================
// Traversal Source Steps
// =========================================================================
/// Start a traversal from all vertices, or specific vertices by ID.
///
/// @param ids - Optional vertex IDs to start from (as bigint array)
/// @returns A traversal over vertices
///
/// @example
/// ```typescript
/// // All vertices
/// graph.V().hasLabel('person').values('name').toList();
///
/// // Specific vertices
/// graph.V([aliceId, bobId]).out('knows').toList();
/// ```
#[wasm_bindgen(js_name = "V")]
pub fn v(&self, ids: Option<js_sys::Array>) -> Result<Traversal, JsError> {
match ids {
Some(js_ids) => {
let vertex_ids = js_array_to_vertex_ids(js_ids.into())?;
Ok(Traversal::from_vertex_ids(
Arc::clone(&self.inner),
vertex_ids,
))
}
None => Ok(Traversal::from_all_vertices(Arc::clone(&self.inner))),
}
}
/// Start a traversal from all edges, or specific edges by ID.
///
/// @param ids - Optional edge IDs to start from (as bigint array)
/// @returns A traversal over edges
#[wasm_bindgen(js_name = "E")]
pub fn e(&self, ids: Option<js_sys::Array>) -> Result<Traversal, JsError> {
match ids {
Some(js_ids) => {
let edge_ids = crate::wasm::types::js_array_to_edge_ids(js_ids.into())?;
Ok(Traversal::from_edge_ids(Arc::clone(&self.inner), edge_ids))
}
None => Ok(Traversal::from_all_edges(Arc::clone(&self.inner))),
}
}
/// Inject values into a traversal.
///
/// @param values - Values to inject (as array)
pub fn inject(&self, values: JsValue) -> Result<Traversal, JsError> {
let vals = crate::wasm::types::js_array_to_values(values)?;
Ok(Traversal::from_injected_values(
Arc::clone(&self.inner),
vals,
))
}
// =========================================================================
// Serialization
// =========================================================================
/// Export the graph to a GraphSON JSON string.
///
/// @returns GraphSON 3.0 formatted JSON string
#[cfg(feature = "graphson")]
#[wasm_bindgen(js_name = "toGraphSON")]
pub fn to_graphson(&self) -> Result<String, JsError> {
let snapshot = self.inner.snapshot();
crate::graphson::to_string(&snapshot)
.map_err(|e| JsError::new(&format!("GraphSON export error: {}", e)))
}
/// Import graph data from a GraphSON JSON string.
///
/// @param json - GraphSON 3.0 formatted JSON string
/// @returns Import statistics
#[cfg(feature = "graphson")]
#[wasm_bindgen(js_name = "fromGraphSON")]
pub fn from_graphson(&self, json: &str) -> Result<JsValue, JsError> {
// Parse the GraphSON and import into a new graph
let imported = crate::graphson::from_str(json)
.map_err(|e| JsError::new(&format!("GraphSON import error: {}", e)))?;
// Copy vertices and edges from imported graph to this graph
let imported_snap = imported.snapshot();
let mut vertices_imported: u64 = 0;
let mut edges_imported: u64 = 0;
// Import vertices
for v in imported_snap.all_vertices() {
self.inner.add_vertex(&v.label, v.properties.clone());
vertices_imported += 1;
}
// Import edges
for e in imported_snap.all_edges() {
if self
.inner
.add_edge(e.src, e.dst, &e.label, e.properties.clone())
.is_ok()
{
edges_imported += 1;
}
}
// Create result object
let result = js_sys::Object::new();
js_sys::Reflect::set(
&result,
&"verticesImported".into(),
&js_sys::BigInt::from(vertices_imported).into(),
)
.map_err(|_| JsError::new("Failed to set verticesImported"))?;
js_sys::Reflect::set(
&result,
&"edgesImported".into(),
&js_sys::BigInt::from(edges_imported).into(),
)
.map_err(|_| JsError::new("Failed to set edgesImported"))?;
js_sys::Reflect::set(&result, &"warnings".into(), &js_sys::Array::new().into())
.map_err(|_| JsError::new("Failed to set warnings"))?;
Ok(result.into())
}
// =========================================================================
// GQL Query Language
// =========================================================================
/// Execute a GQL query string.
///
/// @param query - GQL query string
/// @returns Query results as an array
/// @throws If query parsing or execution fails
///
/// @example
/// ```typescript
/// const results = graph.gql(`
/// MATCH (p:person)-[:knows]->(friend)
/// WHERE p.name = 'Alice'
/// RETURN friend.name
/// `);
/// ```
#[cfg(feature = "gql")]
pub fn gql(&self, query: &str) -> Result<JsValue, JsError> {
let results = self
.inner
.gql(query)
.map_err(|e| JsError::new(&format!("GQL error: {}", e)))?;
values_to_js_array(results)
}
// =========================================================================
// Gremlin Query Language
// =========================================================================
/// Execute a Gremlin query string.
///
/// @param query - Gremlin query string
/// @returns Query results (array, single value, boolean, or string depending on terminal step)
///
/// @example
/// ```typescript
/// const names = graph.gremlin("g.V().hasLabel('person').values('name').toList()");
/// const path = graph.gremlin("g.V(1n).shortestPath(5n).toList()");
/// ```
#[cfg(feature = "gremlin")]
pub fn gremlin(&self, query: &str) -> Result<JsValue, JsError> {
use crate::gremlin::{ExecutionResult, compile, parse};
use crate::traversal::GraphTraversalSource;
let snapshot = self.inner.snapshot();
// Parse the Gremlin query
let ast = parse(query)
.map_err(|e| JsError::new(&format!("Gremlin parse error: {}", e)))?;
// Compile with the snapshot
let g = GraphTraversalSource::from_snapshot(&snapshot);
let compiled = compile(&ast, &g)
.map_err(|e| JsError::new(&format!("Gremlin compile error: {}", e)))?;
// Execute and convert result
let result = compiled.execute();
match result {
ExecutionResult::List(values) => values_to_js_array(values),
ExecutionResult::Single(Some(value)) => value_to_js(&value),
ExecutionResult::Single(None) => Ok(JsValue::NULL),
ExecutionResult::Set(set) => {
let values: Vec<_> = set.into_iter().collect();
values_to_js_array(values)
}
ExecutionResult::Bool(b) => Ok(JsValue::from_bool(b)),
ExecutionResult::Unit => Ok(JsValue::NULL),
ExecutionResult::Explain(s) => Ok(JsValue::from_str(&s)),
}
}
}
impl Default for Graph {
fn default() -> Self {
Self::new()
}
}
// Internal accessor for the Arc<InnerGraph>
impl Graph {
/// Get the inner graph (for internal use).
#[allow(dead_code)]
pub(crate) fn inner(&self) -> &Arc<InnerGraph> {
&self.inner
}
}