Node.js bindings to the NogDB library
We rely on node-gyp.
- first step : Install NogDB library.
- second step : Clone repo and npm install.
git clone https://github.com/nogdb/nogdb.js.git
cd nogdb.js
npm install
[Note] : You need python2.7 for node-gyp
- First step In file javascript require nogdb in this form.
const nogdb = require("./build/Release/nogdb_js");- Second step you must be create Database Context before use another function like this.
// "mygraph.db" is database path.
const ctx = new nogdb.Context("mygraph.db");- Third step And create Transaction for use Database operations like this.
// Transaction has 2 mode "READ_WRITE" for modifired database and "READ_ONLY" can't modified database
var txn = new nogdb.Txn(ctx,"READ_WRITE");In "READ_WRITE" mode you can call
txn.commit();after complete the transaction for affect to database.
-
Fourth step you can use Database operations .
-
ClassOperations from nogdb.Class -
PropertyOperations from nogdb.Property -
DatabaseOperations from nogdb.Db -
VertexOperations from nogdb.Vertex -
EdgeOperations from nogdb.Edge -
Traverse(Graph) Operations from nogdb.Traverse
-
See more Document
- Create classes VERTEX
Wordsfor words, VERTEXInitialWordsfor starting words and EDGEWordLinksfor linking words
const nogdb = require("./build/Release/nogdb_js");
// Create database context pointing to file 'mygraph.db'
const ctx = new nogdb.Context("mygraph.db");
// Create READ_WRITE transaction
var txn = new nogdb.Txn(ctx,"READ_WRITE");
// Create vertex 'Words'
nogdb.Class.create(txn,"Words","VERTEX");
// Create vertex 'InitialWords'
nogdb.Class.createExtend(txn,"InitialWords","Words");
// Create edge 'WordLinks'
nogdb.Class.create(txn,"WordLinks","EDGE");- Create Property
messagedwithin VERTEXWords
const nogdb = require("./build/Release/nogdb_js");
const ctx = new nogdb.Context("mygraph.db");
var txn = new nogdb.Txn(ctx,"READ_WRITE");
// create property 'string' within vertex 'Words' (also applied to 'InitialWords')
nogdb.Property.add(txn, "Words", "messaged", "TEXT");- Create vertices for
InitialWordsandWords. And create link between them.
const nogdb = require("./build/Release/nogdb_js");
const ctx = new nogdb.Context("mygraph.db");
var txn = new nogdb.Txn(ctx,"READ_WRITE");
// Create prototypes of vertices
hello = new nogdb.Record();
world = new nogdb.Record();
hello.set("messaged", "Hello");
world.set("messaged", ", World.");
// Create vertices to db with created prototypes
vHello = nogdb.Vertex.create(txn, "InitialWords", hello);
vWorld = nogdb.Vertex.create(txn, "Words", world);
// Create link between both vertices
nogdb.Edge.create(txn, "WordLinks", vHello, vWorld);
// Do a transaction commit
txn.commit();- Get Initial Word, go to another word through edge, print string for every word passed.
const nogdb = require("./build/Release/nogdb_js");
const ctx = new nogdb.Context("mygraph.db");
// Create READ_ONLY transaction
var txn = new nogdb.Txn(ctx,"READ_ONLY");
// Get initial word from 'InitialWords'
word1 = nogdb.Vertex.get(txn, "InitialWords");
// Get property 'string' from vertex and print to screen
str_vertex = word1[0].record.value.messaged.toText;
// Get out edge from record
edge = nogdb.Vertex.getOutEdge(txn,word1[0].recordDescriptor);
// Get destination vertex from edge
word2 = nogdb.Edge.getDst(txn,edge[0].recordDescriptor);
// Get property 'string' from another vertex and print to screen
str_another = word2.record.value.messaged.toText;
// Print show result
console.log(str_vertex + str_another);- This is what final
hello_world.jslooks like
// hello_world.js
const nogdb = require("./build/Release/nogdb_js");
// Create database context pointing to file 'mygraph.db'
const ctx = new nogdb.Context("mygraph.db");
/* Write data */
// Create READ_WRITE transaction
var txn = new nogdb.Txn(ctx,"READ_WRITE");
// Create vertex 'Words'
nogdb.Class.create(txn,"Words","VERTEX");
// Create vertex 'InitialWords'
nogdb.Class.createExtend(txn,"InitialWords","Words");
// Create edge 'WordLinks'
nogdb.Class.create(txn,"WordLinks","EDGE");
// create property 'string' within vertex 'Words' (also applied to 'InitialWords')
nogdb.Property.add(txn, "Words", "messaged", "TEXT");
// Create prototypes of vertices
hello = new nogdb.Record();
world = new nogdb.Record();
hello.set("messaged", "Hello");
world.set("messaged", ", World.");
// Create vertices to db with created prototypes
vHello = nogdb.Vertex.create(txn, "InitialWords", hello);
vWorld = nogdb.Vertex.create(txn, "Words", world);
// Create link between both vertices
nogdb.Edge.create(txn, "WordLinks", vHello, vWorld);
// Do a transaction commit
txn.commit();
/* Read data */
// Create READ_ONLY transaction
txn = new nogdb.Txn(ctx,"READ_ONLY");
// Get initial word from 'InitialWords'
word1 = nogdb.Vertex.get(txn, "InitialWords");
// Get property 'string' from vertex and print to screen
str_vertex = word1[0].record.value.messaged.toText;
// Get out edge from record
edge = nogdb.Vertex.getOutEdge(txn,word1[0].recordDescriptor);
// Get destination vertex from edge
word2 = nogdb.Edge.getDst(txn,edge[0].recordDescriptor);
// Get property 'string' from another vertex and print to screen
str_another = word2.record.value.messaged.toText;
// Print result
console.log(str_vertex + str_another);- Run
$ node hello_world.js
Output : Hello, World.