d3x0r / gun-db

sqlite storage adapter for Gun database (javascript)
45 stars 3 forks source link

sqlite db does not syncing #4

Closed siddhudhangar closed 5 years ago

siddhudhangar commented 5 years ago

i tried to sync sqlite db to another sqlite db but it was not syncing.

this is my code. * sqlite_client1.js *** var vfs = require( "sack.vfs" ); var vol = vfs.Volume( "MountName", "vfsFile.dat" );

var Gun = require('gun'); require('gun-db');

var peers = [ 'http://localhost:8081/gun', 'http://localhost:8082/gun', ] // Create a new gun instance var gun = Gun(peers);

var gun = Gun({ file: false // turn off pesky file.js data.json default , db: { file: "gun.db", exclusive : false // default } }); var data = gun.get('data').put({name: 'abc', age: 41});

function dumpDatabase() { var db = vfs.Sqlite( "gun.db" ); //var db = vfs.Sqlite( "$sack@Mount$gun.db" ); var tables = db.do( "select tbl_name from sqlite_master where type='table'" ); console.log( "Tables:", tables ); var records = db.do( "select * from record" ); console.log( "records:" ); records.forEach( rec=>{ console.log( s:${rec.soul} f:${rec.field} v:${rec.value} r:${rec.relation} s:${rec.state} ); } ); }

dumpDatabase()


* sqlite_client2.js *** var vfs = require( "sack.vfs" ); var vol = vfs.Volume( "MountName", "vfsFile.dat" );

var Gun = require('gun'); require('gun-db');

var peers = [ 'http://localhost:8081/gun', 'http://localhost:8082/gun', ] // Create a new gun instance var gun = Gun(peers);

var gun = Gun({ file: false // turn off pesky file.js data.json default , db: { file: "gun2.db", exclusive : false // default } }); var data = gun.get('data').put({name: 'xyz', age: 28});

function dumpDatabase() { var db = vfs.Sqlite( "gun2.db" ); //var db = vfs.Sqlite( "$sack@Mount$gun.db" ); var tables = db.do( "select tbl_name from sqlite_master where type='table'" ); console.log( "Tables:", tables ); var records = db.do( "select * from record" ); console.log( "records:" ); records.forEach( rec=>{ console.log( s:${rec.soul} f:${rec.field} v:${rec.value} r:${rec.relation} s:${rec.state} ); } ); }

dumpDatabase()


server1.js

// http is a standard library // built into node. var http = require('http'); //var express = require('express'); // Create a new server instance. var server = http.createServer();

// Our GUN setup from the last example. var Gun = require('gun'); //var app = express(); //app.use(Gun.serve).use(express.static(__dirname)); var gun = Gun({web: server});

// Start the server on port 8080. server.listen(8081, function () { console.log('Server listening on http://localhost:8081/gun') })


server2.js

// http is a standard library // built into node. var http = require('http'); //var express = require('express'); // Create a new server instance. var server = http.createServer();

// Our GUN setup from the last example. var Gun = require('gun'); //var app = express(); //app.use(Gun.serve).use(express.static(__dirname)); var gun = Gun({web: server});

// Start the server on port 8082. server.listen(8082, function () { console.log('Server listening on http://localhost:8082/gun') })


i am sharing the four files, which i used to sync sqlite db using GUN DB. i ran all files using node command i.e node sqlite_client1.js and node server1.js etc. so am i making any mistake while writing a code. kindly check above code and please tell me.

d3x0r commented 5 years ago

Okay. 2 things. 1) you're creating a instance of gun with peers, and then creating a different one without peers and just the database. Those don't talk or know about each other..

var Gun = new Gun( peers );
var GUn = new Gun( ... );

2) peers don't just automatically sync. 'upstream' (peers) get data that is pushed, but if another node is connected to that one, it won't actually get any data without doing some sort of map() to pull the information in.

siddhudhangar commented 5 years ago

can you share me a doc to how i can sync sqlite db between two peers using node js?

siddhudhangar commented 5 years ago

var gun = Gun({ file: false // turn off pesky file.js data.json default , db: { file: "gun.db", exclusive : false }, peers: ["http://localhost:8081/gun"] });

Is it correct that i am defining peers inside Gun function ?

d3x0r commented 5 years ago

both instances that use sqlite

// Create a new gun instance
var gun = Gun(peers);

var gun = Gun({
file: false // turn off pesky file.js data.json default
, db: {
file: "gun2.db",
exclusive : false // default
}
, peers: peers  //+++ ?
});
var data = gun.get('data').put({name: 'xyz', age: 28});
siddhudhangar commented 5 years ago

First Instance code :

var vfs = require( "sack.vfs" ); var vol = vfs.Volume( "MountName", "vfsFile.dat" );

var Gun = require('gun'); require('gun-db');

var peers = [ 'http://localhost:8081/gun', ] // Create a new gun instance var gun = Gun(peers);

var gun = Gun({ file: false // turn off pesky file.js data.json default , db: { file: "s1.db", exclusive : false }, peers: peers });

var data = gun.get('data').put({name: 'xyz', age: 41});

function dumpDatabase() { var db = vfs.Sqlite( "s1.db" ); //var db = vfs.Sqlite( "$sack@Mount$gun.db" ); var tables = db.do( "select tbl_name from sqlite_master where type='table'" ); console.log( "Tables:", tables ); var records = db.do( "select * from record" ); console.log( "records:" ); records.forEach( rec=>{ console.log( s:${rec.soul} f:${rec.field} v:${rec.value} r:${rec.relation} s:${rec.state} ); } ); }

dumpDatabase()

Second instance code

var vfs = require( "sack.vfs" ); var vol = vfs.Volume( "MountName", "vfsFile.dat" );

var Gun = require('gun'); require('gun-db');

var peers = [ 'http://localhost:8081/gun', ] // Create a new gun instance var gun = Gun(peers);

var gun = Gun({ file: false // turn off pesky file.js data.json default , db: { file: "s2.db", exclusive : false }, peers: peers });

var data = gun.get('data').put({name: 'abc', age: 52});

function dumpDatabase() { var db = vfs.Sqlite( "s2.db" ); //var db = vfs.Sqlite( "$sack@Mount$gun.db" ); var tables = db.do( "select tbl_name from sqlite_master where type='table'" ); console.log( "Tables:", tables ); var records = db.do( "select * from record" ); console.log( "records:" ); records.forEach( rec=>{ console.log( s:${rec.soul} f:${rec.field} v:${rec.value} r:${rec.relation} s:${rec.state} ); } ); }

server.js:

// http is a standard library // built into node. var http = require('http'); //var express = require('express'); // Create a new server instance. var server = http.createServer();

// Our GUN setup from the last example. var Gun = require('gun'); //var app = express(); //app.use(Gun.serve).use(express.static(__dirname)); var gun = Gun({web: server});

// Start the server on port 8080. server.listen(8081, function () { console.log('Server listening on http://localhost:8081/gun') })

log data:

15:14:27.481|7FA5AF526740~node@Last native error code: 0 15:14:27.481|7FA5AF526740~node@odbc flags = [Connected] [] [] [] [Native SQLite] [] 15:14:27.481|7FA5AF526740~node@Collection(s)... 15:14:27.481|7FA5AF526740~node@odbc = 0x2ce5c84 15:14:27.481|7FA5AF526740~node@odbc->db = 0x2ce5ef8 15:14:27.481|7FA5AF526740~node@Last native error code: 0 15:14:27.481|7FA5AF526740~node@odbc flags = [Connected] [] [] [] [Native SQLite] [] 15:14:27.481|7FA5AF526740~node@Collection(s)... 15:14:27.481|7FA5AF526740~node@---------- 15:14:27.481|7FA5AF526740~node@ flags: Result String Dynamic QueryResult Auto more 15:14:27.481|7FA5AF526740~node@ Command: select tbl_name from sqlite_master where type='table' 15:14:27.481|7FA5AF526740~node@ Result: (null) 15:14:27.481|7FA5AF526740~node@ Err Info: (null) 15:14:27.681|7FA5AF526740~node@Sqlite3 Err: (5) database is locked in "select tbl_name from sqlite_master where type='table'" 15:14:27.681|7FA5AF526740~node@wait for lock... 15:14:27.681|7FA5AF526740~node@odbc = 0x2bd7914 15:14:27.681|7FA5AF526740~node@odbc->db = 0x2bdf408 15:14:27.681|7FA5AF526740~node@Last native error code: 0 15:14:27.681|7FA5AF526740~node@odbc flags = [Connected] [] [] [] [Native SQLite] [] 15:14:27.681|7FA5AF526740~node@Collection(s)... 15:14:27.682|7FA5AF526740~node@odbc = 0x2c67ba4 15:14:27.682|7FA5AF526740~node@odbc->db = 0x2c61a18 15:14:27.682|7FA5AF526740~node@Last native error code: 0 15:14:27.682|7FA5AF526740~node@odbc flags = [Connected] [] [] [] [Native SQLite] [] 15:14:27.682|7FA5AF526740~node@Collection(s)... 15:14:27.682|7FA5AF526740~node@odbc = 0x2c97c94 15:14:27.682|7FA5AF526740~node@odbc->db = 0x2c97f08 15:14:27.682|7FA5AF526740~node@Last native error code: 0 15:14:27.682|7FA5AF526740~node@odbc flags = [Connected] [] [] [] [Native SQLite] [] 15:14:27.682|7FA5AF526740~node@Collection(s)... 15:14:27.682|7FA5AF526740~node@odbc = 0x2ce5c84 15:14:27.682|7FA5AF526740~node@odbc->db = 0x2ce5ef8 15:14:27.682|7FA5AF526740~node@Last native error code: 0 15:14:27.682|7FA5AF526740~node@odbc flags = [Connected] [] [] [] [Native SQLite] [] 15:14:27.682|7FA5AF526740~node@Collection(s)... 15:14:27.682|7FA5AF526740~node@---------- 15:14:27.682|7FA5AF526740~node@ flags: Result String Dynamic QueryResult Auto more 15:14:27.682|7FA5AF526740~node@ Command: select tbl_name from sqlite_master where type='table' 15:14:27.682|7FA5AF526740~node@ Result: (null) 15:14:27.682|7FA5AF526740~node@ Err Info: (null)

still sqlite db not syncing . is it correct ?

I import gun-db module in react js i.e import 'gun-db', then it gives me following error i.e TypeError: sack.JSON6 is undefined. i attached some screenshot for your reference. Screenshot from 2019-03-22 15-07-07 Screenshot from 2019-03-22 16-21-06

d3x0r commented 5 years ago

Okay... have some time to test that out. Looks kinda like something broke.... it's strange that you would get that sql error log on connections...

Hello wonderful person! :) Thanks for using GUN, feel free to ask for help on https://gitter.im/amark/gun and ask StackOverflow questions tagged with 'gun'!
node-webcrypto-ossl and text-encoding may not be included by default, please add it to your package.json!
Tables: [ { tbl_name: 'record' } ]
records:
s:data f:name v:"xyz" r:null s:1553277846938
s:data f:age v:41 r:null s:1553277846938

is what I get....

The sack.JSON6 webpack issue is probably because sack.vfs is a native code module...

d3x0r commented 5 years ago

server radata after first (age=41)

+0#"data.
+1#"age:"+41>+1553278274954
+1#"name:""xyz>+1553278274954

server radata after 2 (age=52)

+0#"data.
+1#"age:"+52>+1553278302559
+1#"name:""abc>+1553278302559

on client1 and client2 you have no mapping or outstanding gets to get updates from the server... adding a line like

var data = gun.get('data').on((val,field)=>{console.log( field,' = ', val );dumpDatabase()});

// log----
data  =  { _:
   { '#': 'data',
     '>': { name: 1553278472582, age: 1553278472582 } },
  name: 'xyz',
  age: 41 }
Tables: [ { tbl_name: 'record' } ]
records:
s:data f:name v:"xyz" r:null s:1553278472582
s:data f:age v:41 r:null s:1553278472582
Tables: [ { tbl_name: 'record' } ]
records:
s:data f:name v:"xyz" r:null s:1553278472582
s:data f:age v:41 r:null s:1553278472582
data  =  { _:
   { '#': 'data',
     '>': { name: 1553278476809, age: 1553278476809 } },
  name: 'abc',
  age: 52 }
Tables: [ { tbl_name: 'record' } ]
records:
s:data f:age v:41 r:null s:1553278472582
s:data f:name v:"abc" r:null s:1553278476809

Okay the database isn't updated until a time after that on event fires..

var data = gun.get('data').on((val,field)=>{console.log( field,' = ', val );setTimeout( dumpDatabase, 100 )});

--- log after client2 runs...

data  =  { _:
   { '#': 'data',
     '>': { name: 1553278556999, age: 1553278556999 } },
  name: 'abc',
  age: 52 }
11:15:57.047|50F8000072D4~Database busy, waiting...
Tables: [ { tbl_name: 'record' } ]
records:
s:data f:age v:52 r:null s:1553278556999
s:data f:name v:"abc" r:null s:1553278556999
d3x0r commented 5 years ago

If there is a mis-sync between

node_modules/sack.vfs and node_modules/gun-db/node_modules/sack.vfs

it could cause some of the above errors...

could instead require( 'gun-db/node_modules/sack.vfs' ); ? or is there only one?

d3x0r commented 5 years ago

https://gist.github.com/d3x0r/4171911d252e3ffa53375e810a6e8e57 is the codes I use...

test-server.js is the server test3.js is client1 and test3-b.js is client2

some of the requires were changed to use the local versions I had...

You still had two 'new Gun()' calls in the examples... but that didn't seem to matter.

siddhudhangar commented 5 years ago

it is working now. thanks for your help.

d3x0r commented 5 years ago

glad you got it working. Gun is definitely a horse of another color :)