-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic-linkdb-example.js
More file actions
80 lines (67 loc) · 2.4 KB
/
basic-linkdb-example.js
File metadata and controls
80 lines (67 loc) · 2.4 KB
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
#!/usr/bin/env node
/**
* Simple example demonstrating basic LinkDB operations.
*
* This example shows how to:
* - Create links between entities
* - Read links from the database
* - Update existing links
* - Delete links
*/
import LinkDBService from '../src/services/link-db-service.js';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs/promises';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function main() {
// Create a LinkDB service with a temporary database
const dbPath = path.join(__dirname, 'example.links');
const linkdb = new LinkDBService(dbPath);
console.log('=== Basic LinkDB Example ===\n');
try {
// Create a link between two entities
console.log('1. Creating a link between entity 100 and entity 200...');
const link = await linkdb.createLink(100, 200);
console.log(` Created link:`, link);
console.log();
// Create another link
console.log('2. Creating another link between entity 300 and entity 400...');
const link2 = await linkdb.createLink(300, 400);
console.log(` Created link:`, link2);
console.log();
// Read all links
console.log('3. Reading all links from the database...');
const allLinks = await linkdb.readAllLinks();
console.log(` Found ${allLinks.length} links:`);
allLinks.forEach(link => {
console.log(` - Link ${link.id}: ${link.source} -> ${link.target}`);
});
console.log();
// Update a link
console.log('4. Updating the first link to point to entity 500...');
const updatedLink = await linkdb.updateLink(link.id, 100, 500);
console.log(` Updated link:`, updatedLink);
console.log();
// Delete a link
console.log('5. Deleting the second link...');
await linkdb.deleteLink(link2.id);
console.log(' Link deleted');
console.log();
// Verify final state
console.log('6. Final state of the database:');
const finalLinks = await linkdb.readAllLinks();
console.log(` Total links: ${finalLinks.length}`);
finalLinks.forEach(link => {
console.log(` - Link ${link.id}: ${link.source} -> ${link.target}`);
});
// Cleanup
console.log('\nCleaning up example database...');
await fs.unlink(dbPath).catch(() => {});
console.log('Done!');
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
}
main();