sombra 0.3.2

High-performance graph database with ACID transactions, single-file storage, and bindings for Rust, TypeScript, and Python
Documentation
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
# Node.js Binding Guide

This guide covers using Sombra from Node.js and TypeScript, including installation, basic operations, async patterns, and integration with the Node.js ecosystem.

## Installation

### From npm (Recommended)

```bash
npm install sombra
```

### From Source

```bash
git clone https://github.com/sombra-db/sombra
cd sombra
npm install
npm run build
npm pack
npm install ./sombra-*.tgz
```

### Development Installation

```bash
git clone https://github.com/sombra-db/sombra
cd sombra
npm install
npm run build
npm link
```

## TypeScript Support

Sombra provides full TypeScript definitions out of the box:

```typescript
import { GraphDB, Node, Edge, Transaction, PropertyValue } from 'sombra';

// Full type safety and IDE support
const db: GraphDB = new GraphDB('example.db');
```

## Quick Start

### TypeScript

```typescript
import { GraphDB, PropertyValue } from 'sombra';

async function main() {
    // Open or create a database
    const db = new GraphDB('example.db');
    
    try {
        // Start a transaction
        const tx = db.beginTransaction();
        
        // Create nodes
        const alice = await tx.createNode('Person', {
            name: 'Alice',
            age: 30,
            email: 'alice@example.com'
        });
        
        const bob = await tx.createNode('Person', {
            name: 'Bob',
            age: 25,
            email: 'bob@example.com'
        });
        
        // Create an edge
        await tx.createEdge(alice, bob, 'KNOWS', {
            since: 2020,
            relationship: 'friend'
        });
        
        // Commit the transaction
        await tx.commit();
        
        console.log('Graph created successfully!');
    } catch (error) {
        console.error('Error:', error);
    }
}

main().catch(console.error);
```

### JavaScript

```javascript
const { GraphDB } = require('sombra');

async function main() {
    // Open or create a database
    const db = new GraphDB('example.db');
    
    try {
        // Start a transaction
        const tx = db.beginTransaction();
        
        // Create nodes
        const alice = await tx.createNode('Person', {
            name: 'Alice',
            age: 30,
            email: 'alice@example.com'
        });
        
        const bob = await tx.createNode('Person', {
            name: 'Bob',
            age: 25,
            email: 'bob@example.com'
        });
        
        // Create an edge
        await tx.createEdge(alice, bob, 'KNOWS', {
            since: 2020,
            relationship: 'friend'
        });
        
        // Commit the transaction
        await tx.commit();
        
        console.log('Graph created successfully!');
    } catch (error) {
        console.error('Error:', error);
    }
}

main().catch(console.error);
```

## Basic Operations

### Working with Nodes

```typescript
import { GraphDB, PropertyValue } from 'sombra';

const db = new GraphDB('social.db');
const tx = db.beginTransaction();

// Create a node with various property types
const user = await tx.createNode('User', {
    username: 'john_doe',
    email: 'john@example.com',
    active: true,
    created_at: new Date('2024-01-15'),
    score: 95.5,
    tags: ['developer', 'javascript', 'typescript']
});

// Get node by ID
const node = await tx.getNode(user.id);
console.log(`Node ${node.id}: ${node.label}`);

// Get node properties
const props = await tx.getNodeProperties(user.id);
console.log(`Name: ${props.name}`);
console.log(`Age: ${props.age}`);
console.log(`Created: ${props.created_at}`);

// Update node properties
await tx.updateNodeProperties(user.id, {
    last_login: new Date(),
    login_count: 5,
    score: 98.2
});

// Find nodes by label
const users = await tx.findNodesByLabel('User');
console.log(`Found ${users.length} users`);

// Find nodes by property
const john = await tx.findNodesByProperty('User', 'username', 'john_doe');
if (john.length > 0) {
    console.log(`Found user: ${john[0].id}`);
}

// Find nodes by property range
const youngUsers = await tx.findNodesByPropertyRange('User', 'age', 18, 25);
console.log(`Found ${youngUsers.length} young users`);

// Find nodes by multiple properties
const engineers = await tx.findNodesByProperties('User', {
    department: 'Engineering',
    level: ['Senior', 'Principal'],
    active: true
});
```

### Working with Edges

```typescript
// Create an edge
const followEdge = await tx.createEdge(alice, bob, 'FOLLOWS', {
    since: new Date('2021-01-01'),
    strength: 0.8,
    interactions: 42
});

// Get edges from a node
const outgoing = await tx.getOutgoingEdges(alice.id);
const incoming = await tx.getIncomingEdges(bob.id);

console.log(`Alice follows ${outgoing.length} users`);
console.log(`Bob has ${incoming.length} followers`);

// Get edge properties
const edgeProps = await tx.getEdgeProperties(followEdge.id);
console.log(`Follow strength: ${edgeProps.strength}`);
console.log(`Interactions: ${edgeProps.interactions}`);

// Update edge properties
await tx.updateEdgeProperties(followEdge.id, {
    strength: 0.9,
    last_interaction: new Date(),
    interactions: 50
});

// Delete an edge
await tx.deleteEdge(followEdge.id);
```

### Graph Traversal

```typescript
// Basic traversal
const friends = await tx.traverse()
    .fromNode(alice.id)
    .outgoing('KNOWS')
    .collect();

console.log(`Alice's friends: ${friends.map(f => f.id)}`);

// Multi-hop traversal
const friendsOfFriends = await tx.traverse()
    .fromNode(alice.id)
    .outgoing('KNOWS')
    .outgoing('KNOWS')
    .collect();

console.log(`Friends of friends: ${friendsOfFriends.map(f => f.id)}`);

// Traversal with filters
const activeFriends = await tx.traverse()
    .fromNode(alice.id)
    .outgoing('KNOWS')
    .filter(async (node) => {
        const props = await tx.getNodeProperties(node.id);
        return props.active === true;
    })
    .collect();

console.log(`Active friends: ${activeFriends.map(f => f.id)}`);

// Bidirectional traversal
const mutualFriends = await tx.traverse()
    .fromNode(alice.id)
    .outgoing('KNOWS')
    .incoming('KNOWS')
    .filter(async (node) => node.id === bob.id)
    .collect();

// Traversal with property access
const skilledFriends = await tx.traverse()
    .fromNode(alice.id)
    .outgoing('KNOWS')
    .filter(async (node) => {
        const props = await tx.getNodeProperties(node.id);
        return props.skills && props.skills.includes('typescript');
    })
    .collect();
```

## Advanced Patterns

### Async/Await Patterns

```typescript
// Sequential operations
async function createUserWithFriends(userName: string, friendNames: string[]) {
    const tx = db.beginTransaction();
    
    try {
        const user = await tx.createNode('User', { name: userName });
        
        for (const friendName of friendNames) {
            const friend = await tx.createNode('User', { name: friendName });
            await tx.createEdge(user, friend, 'FRIENDS', {});
        }
        
        await tx.commit();
        return user;
    } catch (error) {
        await tx.rollback();
        throw error;
    }
}

// Parallel operations
async function createManyUsers(userData: Array<{name: string, email: string}>) {
    const tx = db.beginTransaction();
    
    try {
        // Create all users in parallel
        const userPromises = userData.map(data => 
            tx.createNode('User', data)
        );
        const users = await Promise.all(userPromises);
        
        await tx.commit();
        return users;
    } catch (error) {
        await tx.rollback();
        throw error;
    }
}
```

### Error Handling

```typescript
import { GraphError, NodeNotFoundError, TransactionError } from 'sombra';

async function safeOperation() {
    try {
        const db = new GraphDB('/invalid/path/db.db');
        // ... operations
    } catch (error) {
        if (error instanceof GraphError) {
            console.error(`Database error: ${error.message}`);
            console.error(`Error code: ${error.code}`);
        } else {
            console.error(`Unexpected error: ${error}`);
        }
    }
}

async function transactionWithRetry<T>(
    operation: (tx: Transaction) => Promise<T>,
    maxRetries: number = 3
): Promise<T> {
    for (let attempt = 1; attempt <= maxRetries; attempt++) {
        const tx = db.beginTransaction();
        
        try {
            const result = await operation(tx);
            await tx.commit();
            return result;
        } catch (error) {
            await tx.rollback();
            
            if (attempt === maxRetries) {
                throw error;
            }
            
            // Exponential backoff
            const delay = Math.pow(2, attempt) * 100;
            await new Promise(resolve => setTimeout(resolve, delay));
        }
    }
    
    throw new Error('Max retries exceeded');
}

// Usage
const result = await transactionWithRetry(async (tx) => {
    const user = await tx.createNode('User', { name: 'Alice' });
    return user;
});
```

### Connection Pooling

```typescript
import { GraphDB } from 'sombra';

class DatabasePool {
    private pools: GraphDB[] = [];
    private available: number[] = [];
    private inUse: Set<number> = new Set();
    
    constructor(private dbPath: string, private poolSize: number = 5) {
        this.initializePool();
    }
    
    private initializePool() {
        for (let i = 0; i < this.poolSize; i++) {
            this.pools.push(new GraphDB(this.dbPath));
            this.available.push(i);
        }
    }
    
    async getConnection(): Promise<{ db: GraphDB, release: () => void }> {
        return new Promise((resolve, reject) => {
            const checkAvailable = () => {
                if (this.available.length > 0) {
                    const index = this.available.pop()!;
                    this.inUse.add(index);
                    
                    resolve({
                        db: this.pools[index],
                        release: () => {
                            this.inUse.delete(index);
                            this.available.push(index);
                        }
                    });
                } else {
                    setTimeout(checkAvailable, 10);
                }
            };
            
            checkAvailable();
        });
    }
    
    async closeAll() {
        for (const db of this.pools) {
            await db.close();
        }
    }
}

// Usage
const pool = new DatabasePool('production.db', 10);

async function processUser(userData: any) {
    const { db, release } = await pool.getConnection();
    
    try {
        const tx = db.beginTransaction();
        const user = await tx.createNode('User', userData);
        await tx.commit();
        return user;
    } finally {
        release();
    }
}
```

### Batch Operations

```typescript
async function bulkInsertUsers(userData: Array<{name: string, email: string}>) {
    const batchSize = 1000;
    const results: Node[] = [];
    
    for (let i = 0; i < userData.length; i += batchSize) {
        const batch = userData.slice(i, i + batchSize);
        const tx = db.beginTransaction();
        
        try {
            const batchResults = await Promise.all(
                batch.map(data => tx.createNode('User', data))
            );
            
            await tx.commit();
            results.push(...batchResults);
        } catch (error) {
            await tx.rollback();
            throw error;
        }
    }
    
    return results;
}

// Usage
const users = [
    { name: 'User1', email: 'user1@example.com' },
    { name: 'User2', email: 'user2@example.com' },
    // ... more users
];

const insertedUsers = await bulkInsertUsers(users);
console.log(`Inserted ${insertedUsers.length} users`);
```

## Integration with Node.js Ecosystem

### Express.js Integration

```typescript
import express from 'express';
import { GraphDB } from 'sombra';

const app = express();
app.use(express.json());

const db = new GraphDB('social_network.db');

// Create user endpoint
app.post('/users', async (req, res) => {
    try {
        const { name, email, age } = req.body;
        
        const tx = db.beginTransaction();
        const user = await tx.createNode('User', { name, email, age });
        await tx.commit();
        
        res.status(201).json({ id: user.id, name, email, age });
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Get user endpoint
app.get('/users/:id', async (req, res) => {
    try {
        const userId = parseInt(req.params.id);
        const tx = db.beginTransaction();
        const user = await tx.getNode(userId);
        const properties = await tx.getNodeProperties(userId);
        await tx.commit();
        
        res.json({ id: user.id, label: user.label, properties });
    } catch (error) {
        res.status(404).json({ error: 'User not found' });
    }
});

// Get friends endpoint
app.get('/users/:id/friends', async (req, res) => {
    try {
        const userId = parseInt(req.params.id);
        const tx = db.beginTransaction();
        
        const friends = await tx.traverse()
            .fromNode(userId)
            .outgoing('FRIENDS')
            .collect();
        
        const friendData = await Promise.all(
            friends.map(async (friend) => {
                const props = await tx.getNodeProperties(friend.id);
                return { id: friend.id, ...props };
            })
        );
        
        await tx.commit();
        res.json(friendData);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

app.listen(3000, () => {
    console.log('Server running on port 3000');
});
```

### Stream Processing

```typescript
import { Transform } from 'stream';
import { GraphDB } from 'sombra';

class UserImportStream extends Transform {
    private db: GraphDB;
    private tx: Transaction;
    private batchSize: number = 100;
    private currentBatch: any[] = [];
    
    constructor(dbPath: string) {
        super({ objectMode: true });
        this.db = new GraphDB(dbPath);
        this.tx = this.db.beginTransaction();
    }
    
    async _transform(chunk: any, encoding: string, callback: Function) {
        this.currentBatch.push(chunk);
        
        if (this.currentBatch.length >= this.batchSize) {
            await this.processBatch();
        }
        
        callback();
    }
    
    async _flush(callback: Function) {
        if (this.currentBatch.length > 0) {
            await this.processBatch();
        }
        
        await this.tx.commit();
        callback();
    }
    
    private async processBatch() {
        try {
            await Promise.all(
                this.currentBatch.map(userData => 
                    this.tx.createNode('User', userData)
                )
            );
            
            this.currentBatch = [];
        } catch (error) {
            await this.tx.rollback();
            throw error;
        }
    }
}

// Usage with CSV parsing
import fs from 'fs';
import csv from 'csv-parser';

const userStream = fs.createReadStream('users.csv')
    .pipe(csv())
    .pipe(new UserImportStream('users.db'));

userStream.on('finish', () => {
    console.log('User import completed');
});

userStream.on('error', (error) => {
    console.error('Import failed:', error);
});
```

### Concurrent Read Operations

Sombra supports multiple concurrent read operations using a multi-reader, single-writer concurrency model:

```typescript
import { GraphDB } from 'sombra';

const db = new GraphDB('concurrent.db');

async function concurrentReads() {
    const userId1 = 1;
    const userId2 = 2;
    const userId3 = 3;
    
    const [user1Friends, user2Friends, user3Friends] = await Promise.all([
        db.getOutgoingEdges(userId1),
        db.getOutgoingEdges(userId2),
        db.getOutgoingEdges(userId3)
    ]);
    
    console.log(`User 1 has ${user1Friends.length} friends`);
    console.log(`User 2 has ${user2Friends.length} friends`);
    console.log(`User 3 has ${user3Friends.length} friends`);
}

async function parallelTraversals() {
    const userIds = [1, 2, 3, 4, 5];
    
    const allFriends = await Promise.all(
        userIds.map(userId => 
            db.rangeQuery(userId, userId, 'KNOWS')
        )
    );
    
    allFriends.forEach((friends, idx) => {
        console.log(`User ${userIds[idx]} friends: ${friends.length}`);
    });
}

async function concurrentPropertyReads() {
    const nodeIds = [1, 2, 3, 4, 5, 6, 7, 8];
    
    const properties = await Promise.all(
        nodeIds.map(id => db.getNodeProperties(id))
    );
    
    console.log(`Fetched ${properties.length} node properties concurrently`);
}

concurrentReads();
parallelTraversals();
concurrentPropertyReads();
```

### Worker Threads

```typescript
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
import { GraphDB } from 'sombra';

interface WorkerTask {
    type: 'process_users';
    userData: Array<{name: string, email: string}>;
    dbPath: string;
}

if (!isMainThread) {
    // Worker thread code
    const processUsers = async (task: WorkerTask) => {
        const db = new GraphDB(task.dbPath);
        const tx = db.beginTransaction();
        
        try {
            const results = await Promise.all(
                task.userData.map(data => tx.createNode('User', data))
            );
            
            await tx.commit();
            
            parentPort?.postMessage({
                success: true,
                count: results.length
            });
        } catch (error) {
            await tx.rollback();
            parentPort?.postMessage({
                success: false,
                error: error.message
            });
        }
    };
    
    parentPort?.on('message', (task: WorkerTask) => {
        processUsers(task);
    });
} else {
    // Main thread code
    async function processWithWorkers(userData: Array<{name: string, email: string}>) {
        const numWorkers = 4;
        const chunkSize = Math.ceil(userData.length / numWorkers);
        const workers: Worker[] = [];
        
        for (let i = 0; i < numWorkers; i++) {
            const chunk = userData.slice(i * chunkSize, (i + 1) * chunkSize);
            
            const worker = new Worker(__filename, {
                workerData: {
                    type: 'process_users',
                    userData: chunk,
                    dbPath: 'parallel_users.db'
                }
            });
            
            workers.push(worker);
        }
        
        const results = await Promise.all(
            workers.map(worker => 
                new Promise((resolve) => {
                    worker.on('message', resolve);
                })
            )
        );
        
        console.log('Worker results:', results);
        
        // Clean up workers
        workers.forEach(worker => worker.terminate());
    }
}
```

### GraphQL Integration

```typescript
import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLList, GraphQLID } from 'graphql';
import { GraphDB } from 'sombra';

const db = new GraphDB('graphql.db');

const UserType = new GraphQLObjectType({
    name: 'User',
    fields: {
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        email: { type: GraphQLString },
        friends: {
            type: new GraphQLList(UserType),
            resolve: async (parent) => {
                const tx = db.beginTransaction();
                const friends = await tx.traverse()
                    .fromNode(parent.id)
                    .outgoing('FRIENDS')
                    .collect();
                
                const friendData = await Promise.all(
                    friends.map(async (friend) => {
                        const props = await tx.getNodeProperties(friend.id);
                        return { id: friend.id, ...props };
                    })
                );
                
                await tx.commit();
                return friendData;
            }
        }
    }
});

const QueryType = new GraphQLObjectType({
    name: 'Query',
    fields: {
        user: {
            type: UserType,
            args: {
                id: { type: GraphQLID }
            },
            resolve: async (_, { id }) => {
                const tx = db.beginTransaction();
                const user = await tx.getNode(parseInt(id));
                const properties = await tx.getNodeProperties(parseInt(id));
                await tx.commit();
                
                return { id: user.id, ...properties };
            }
        },
        users: {
            type: new GraphQLList(UserType),
            resolve: async () => {
                const tx = db.beginTransaction();
                const users = await tx.findNodesByLabel('User');
                
                const userData = await Promise.all(
                    users.map(async (user) => {
                        const props = await tx.getNodeProperties(user.id);
                        return { id: user.id, ...props };
                    })
                );
                
                await tx.commit();
                return userData;
            }
        }
    }
});

const schema = new GraphQLSchema({
    query: QueryType
});

export { schema };
```

## Testing

### Jest Testing

```typescript
import { GraphDB } from 'sombra';
import { existsSync, unlinkSync } from 'fs';
import { join } from 'path';

describe('Sombra Operations', () => {
    const testDbPath = join(__dirname, 'test.db');
    
    beforeEach(() => {
        // Clean up test database
        if (existsSync(testDbPath)) {
            unlinkSync(testDbPath);
        }
    });
    
    afterEach(() => {
        // Clean up test database
        if (existsSync(testDbPath)) {
            unlinkSync(testDbPath);
        }
    });
    
    test('create and retrieve node', async () => {
        const db = new GraphDB(testDbPath);
        const tx = db.beginTransaction();
        
        const user = await tx.createNode('User', { name: 'Alice' });
        await tx.commit();
        
        const tx2 = db.beginTransaction();
        const retrieved = await tx2.getNode(user.id);
        const props = await tx2.getNodeProperties(user.id);
        await tx2.commit();
        
        expect(retrieved.label).toBe('User');
        expect(props.name).toBe('Alice');
    });
    
    test('graph traversal', async () => {
        const db = new GraphDB(testDbPath);
        const tx = db.beginTransaction();
        
        const alice = await tx.createNode('User', { name: 'Alice' });
        const bob = await tx.createNode('User', { name: 'Bob' });
        await tx.createEdge(alice, bob, 'KNOWS', {});
        await tx.commit();
        
        const tx2 = db.beginTransaction();
        const friends = await tx2.traverse()
            .fromNode(alice.id)
            .outgoing('KNOWS')
            .collect();
        await tx2.commit();
        
        expect(friends).toHaveLength(1);
        expect(friends[0].id).toBe(bob.id);
    });
});
```

### Integration Testing

```typescript
import { GraphDB } from 'sombra';

describe('Social Network Integration', () => {
    let db: GraphDB;
    
    beforeAll(async () => {
        db = new GraphDB('integration_test.db');
    });
    
    afterAll(async () => {
        await db.close();
    });
    
    test('complete social network scenario', async () => {
        // Create users
        const tx = db.beginTransaction();
        const alice = await tx.createNode('User', { name: 'Alice', age: 30 });
        const bob = await tx.createNode('User', { name: 'Bob', age: 25 });
        const charlie = await tx.createNode('User', { name: 'Charlie', age: 35 });
        
        // Create friendships
        await tx.createEdge(alice, bob, 'FRIENDS', { since: new Date('2020-01-01') });
        await tx.createEdge(bob, charlie, 'FRIENDS', { since: new Date('2021-01-01') });
        await tx.createEdge(alice, charlie, 'FRIENDS', { since: new Date('2022-01-01') });
        await tx.commit();
        
        // Test Alice's friends
        const tx2 = db.beginTransaction();
        const aliceFriends = await tx2.traverse()
            .fromNode(alice.id)
            .outgoing('FRIENDS')
            .collect();
        await tx2.commit();
        
        expect(aliceFriends).toHaveLength(2);
        
        // Test friends of friends
        const tx3 = db.beginTransaction();
        const fofs = await tx3.traverse()
            .fromNode(alice.id)
            .outgoing('FRIENDS')
            .outgoing('FRIENDS')
            .collect();
        await tx3.commit();
        
        expect(fofs.length).toBeGreaterThanOrEqual(2);
    });
});
```

## Performance Optimization

### Connection Management

```typescript
class DatabaseManager {
    private static instance: DatabaseManager;
    private db: GraphDB;
    
    private constructor(dbPath: string) {
        this.db = new GraphDB(dbPath);
    }
    
    static getInstance(dbPath: string): DatabaseManager {
        if (!DatabaseManager.instance) {
            DatabaseManager.instance = new DatabaseManager(dbPath);
        }
        return DatabaseManager.instance;
    }
    
    getDatabase(): GraphDB {
        return this.db;
    }
    
    async close(): Promise<void> {
        await this.db.close();
    }
}

// Usage
const dbManager = DatabaseManager.getInstance('app.db');
const db = dbManager.getDatabase();
```

### Caching Layer

```typescript
import NodeCache from 'node-cache';

class CachedGraphDB {
    private db: GraphDB;
    private cache: NodeCache;
    
    constructor(dbPath: string, cacheTTL: number = 300) {
        this.db = new GraphDB(dbPath);
        this.cache = new NodeCache({ stdTTL: cacheTTL });
    }
    
    async getNodePropertiesCached(nodeId: number): Promise<any> {
        const cacheKey = `node_props_${nodeId}`;
        let props = this.cache.get<any>(cacheKey);
        
        if (!props) {
            const tx = this.db.beginTransaction();
            props = await tx.getNodeProperties(nodeId);
            await tx.commit();
            
            this.cache.set(cacheKey, props);
        }
        
        return props;
    }
    
    invalidateNodeCache(nodeId: number): void {
        const cacheKey = `node_props_${nodeId}`;
        this.cache.del(cacheKey);
    }
    
    async updateNodePropertiesWithCache(
        nodeId: number, 
        properties: any
    ): Promise<void> {
        const tx = this.db.beginTransaction();
        await tx.updateNodeProperties(nodeId, properties);
        await tx.commit();
        
        // Update cache
        const cacheKey = `node_props_${nodeId}`;
        const existingProps = this.cache.get<any>(cacheKey) || {};
        const updatedProps = { ...existingProps, ...properties };
        this.cache.set(cacheKey, updatedProps);
    }
}
```

## Next Steps

- Read the [Getting Started Guide]getting-started.md for basic usage
- Check the [Configuration Guide]configuration.md for performance tuning
- Review the [Operations Guide]operations.md for production deployment
- Browse the [examples]../examples/ directory for complete applications