RethinkDB foreign-key simulation

Apr 21, 06:34 AM

https://rethinkdb.com/docs/sql-to-reql/javascript/
https://rethinkdb.com/docs/data-modeling/
https://www.youtube.com/watch?v=vJtDNRsUozk

================
2016-04-21 from Jorge’s example:

//r.db(‘test’).table(‘Cities’).delete()
//r.db(‘test’).tableCreate(‘States’)
//r.db(‘test’).tableCreate(‘Cities’)
//r.db(‘test’).tableCreate(‘State_to_Cities’)
/*
r.db(‘test’).table(‘States’).insert([
{ ‘id’ : 1, ‘name’ : ‘California’},
{ ‘id’ : 2, ‘name’ : ‘Oregon’}])

r.db(‘test’).table(‘Cities’).insert([
{ ‘id’ : 1, ‘name’ : ‘Portland’},
{ ‘id’ : 2, ‘name’ : ‘Salem’},
{ ‘id’ : 3, ‘name’ : ‘San Francisco’},
{ ‘id’ : 4, ‘name’ : ‘Mountain View’}])

r.db(‘test’).table(‘State_to_Cities’).insert([ { ‘state’ : 1, ‘city’ : 3 } , { ‘state’ : 1, ‘city’ : 4 }, { ‘state’ : 2, ‘city’ : 1 }, { ‘state’ : 2, ‘city’ : 2 } ])

r.db(“test”).table(“States”).get(1).merge( { “Cities”: r.db(“test”).table(“Cities”).getAll( r.args(r.db(“test”).table(“State_to_Cities”).getAll(1, {index: “states”})(“city”).coerceTo(‘array’)) ).coerceTo(‘array’) })
/*** old python example:”
r.db(‘test’).table(‘States’) .get(StateId).merge({ ‘Cities’ : r.db(‘test’).table(‘Cities’) .getAll(r.args(r.db(‘test’).table(‘State_to_Cities’) .getAll(stateId, index=“state”)[“city”] ))
})
****/

Jorge’s example of an embedded city:

var db = r.db(‘test’);
db.tableCreate(‘States’);
db.table(‘States’).insert([ { ‘id’ : 1, ‘name’ : ‘California’, cities: [ {name: ‘San Francisco’},{name:‘Mountain View’}] } , { ‘id’ : 2, ‘name’ : ‘Oregon’ , cities: [ {name: ‘Portaland’ },{name:‘Salem’ }] } ]);
db.table(‘States’).filter({id:1});

r.db(‘test’).table(‘States’).insert({‘name’:‘Washington’, ‘cities’: [] })

r.db(‘test’).table(‘States’) .get(“ef628142-2cac-4b18-aa7a-7de346a09123”) .update({‘cities’: r.row(‘cities’).append({‘name’:‘Tacoma’}) });


Jorge’s example of State with embedded City array:
/*
r.db(‘test’).table(‘States’).insert([ { id : 1 , name: ‘California’ , cities: [ 3,4] }, { id: 2 , name: ‘Oregon’ , cities: [1,2] } ])

r.db(‘test’).table(‘Cities’).insert([ { id: 1, name: ‘Portland’ } ,{ id: 2, name: ‘Salem’ } ,{ id: 3, name: ‘San Jose’ } ,{ id: 4, name: “Mountain View”}
])

r.db( ‘test’ ).table( ‘States’ ) .get( 1 ) .merge({ cities: r.db( ‘test’ ).table( ‘Cities’ ).getAll( r.args( r.row( ‘cities’ ) ) ).coerceTo( ‘array’ ) })

===
trying to join two embedded records:
var db = r.db(‘test’);
db.tableCreate(‘parentOne’);
db.table(‘parentOne’).insert([ {‘id’: 11, name: ‘parent one row one’ , embeddedRecords: [{ keyValue : ‘11aa’, name:‘parent one record one embedded one’} ,{ keyValue : ‘11bb’, name:‘parent one record one embedded two’} ] },{‘id’: 22, name: ‘parent one row two’ , embeddedRecords: [{ keyValue : ‘22aa’, name:‘parent one record two embedded one’} ,{ keyValue : ‘22bb’, name:‘parent one record two embedded two’} ] }
]);
db.tableCreate(‘parentTwo’);
db.table(‘parentTwo’).insert([ {‘id’: 121, name: ‘parent two row one’ , embeddedRecords: [{ keyValue : ‘111aa’, name:‘parent two record one embedded one’, parentOneKey: ‘11aa’} ,{ keyValue : ‘111bb’, name:‘parent two record one embedded two’, parentOneKey: ‘11bb’} ] },{‘id’: 222, name: ‘parent two row two’ , embeddedRecords: [{ keyValue : ‘222aa’, name:‘parent two record two embedded one’, parentOneKey: ‘22aa’} ,{ keyValue : ‘222bb’, name:‘parent two record two embedded two’, parentTwoKey: ‘22bb’} ] }
]);
===

r.db(‘test’).tableCreate(‘firstTable’)
db(‘test’).tableCreate(‘nextTable’)

r.db(‘test’).table(‘firstTable’).insert({ name : ‘firstTable first record’, firstTable_embedded_records: [ { embedded_record : ‘firstTable first embedded record’}, { embedded_record : ‘firstTable second embedded record’} ]
})
r.db(‘test’).table(‘nextTable’).insert({ name : ‘nextTable first record’, nextTable_embedded_record: [ { embedded_record : ‘nextTable first embedded record’ , external_key: ‘firstTable first embedded record’}, { embedded_record : ‘nextTable second embedded record’, external_key: ‘firstTable second embedded record’ } ]
})

r.db(“test”).table(‘firstTable’) .indexCreate ( ‘firstTable_embedded_records’ , r.row(‘firstTable_embedded_records’)(‘embedded_record’) , {multi: true} )

r.db(‘test’).table(‘nextTable’).map(function (nRow)
{ return nRow(‘nextTable_embedded_record’) .eqJoin ( “external_key” , r.db(‘test’).table(‘firstTable’) , {index: ‘firstTable_embedded_records’} ) .zip();
})

======================================
embedded eqJoin:

r.db(‘comptonTransAnlys’).table(‘Context’) .indexCreate(‘Sound_id’)

r.db(‘comptonTransAnlys’).table(‘Language_norms’) .indexCreate(‘Context_id’)

r.db(‘comptonTransAnlys’).table(‘Stimword_position’) .indexCreate(‘Context_id’)
r.db(‘comptonTransAnlys’).table(‘Stimword_position’) .indexCreate(‘Stimword_id’)

r.db(‘comptonTransAnlys’).table(‘Client_master’) .indexCreate(‘Session_names_id’)
r.db(‘comptonTransAnlys’).table(‘Client_master’) .indexCreate(‘Teacher_id’)

r.db(‘comptonTransAnlys’).table(‘Client_context’) .indexCreate(‘Context_id’)
r.db(‘comptonTransAnlys’).table(‘Client_context’) .indexCreate(‘Client_master_id’)
r.db(‘comptonTransAnlys’).table(‘Client_context’) .indexCreate(‘Frequency_list_id’)

r.db(‘comptonTransAnlys’).table(‘Client_stimword’) .indexCreate(‘Client_context_id’)
r.db(‘comptonTransAnlys’).table(‘Client_stimword’) .indexCreate(‘Stimword_position_id’)

r.db(‘comptonTransAnlys’).table(‘Sound’) .eqJoin(‘id’, r.db(‘comptonTransAnlys’).table(‘Context’), {index:‘Sound_id’}).zip() .eqJoin(‘id’, r.db(‘comptonTransAnlys’).table(‘Stimword_position’), {index:‘Context_id’}).zip()

r.db(‘comptonTransAnlys’).table(‘Sound’) .eqJoin(‘id’ , r.db(‘comptonTransAnlys’).table(‘Context’) , {index:‘Sound_id’}).zip() .eqJoin(‘id’ , r.db(‘comptonTransAnlys’).table(‘Stimword_position’) , {index:‘Context_id’}).zip() .eqJoin(‘Stimword_id’ , r.db(‘comptonTransAnlys’).table(‘Stimword’) , {index: ‘id’})

Mark Edwards

,

---

Commenting is closed for this article.

---