This repository was archived by the owner on Feb 11, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ import rethinkDB from 'rethinkdb'
2+
3+ export async function createDatabase ( databaseName , connection ) {
4+ let databaseExists = await rethinkDB . dbList ( ) . contains ( databaseName ) . run ( connection ) ;
5+ if ( ! databaseExists ) {
6+ let dbCreationResponse = await rethinkDB . dbCreate ( databaseName ) . run ( connection )
7+
8+ // .do(function(databaseExists) {
9+ // return rethinkDB.branch(
10+ // databaseExists,
11+ // { dbs_created: 0 },
12+ // rethinkDB.dbCreate('webapp')
13+ // );
14+ // })
15+
16+ if ( dbCreationResponse . dbs_created > 0 ) console . log ( `📢 ${ databaseName } database created !` )
17+ } else {
18+ console . log ( `📢📁 ${ databaseName } database already exists !` )
19+ }
20+ }
21+
22+ export async function createTableAndInsertData ( databaseName , databaseData , connection ) {
23+ for ( let tableData of databaseData ) {
24+ await rethinkDB . db ( databaseName ) . tableCreate ( tableData . databaseTableName ) . run ( connection )
25+ . then ( async tableCreationResponse => {
26+ if ( tableCreationResponse . tables_created > 0 ) console . log ( `📢 ${ tableData . databaseTableName } table created.` )
27+ await rethinkDB . db ( databaseName ) . table ( tableData . databaseTableName ) . insert ( tableData . data ) . run ( connection )
28+ . then ( response => {
29+ console . log ( `📢📥 ${ response . inserted } documents inserted to ${ tableData . databaseTableName } .` )
30+ } )
31+ . catch ( error => console . log ( error ) )
32+ } )
33+ . catch ( error => console . log ( `📢 ${ tableData . databaseTableName } table already exists.` ) )
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments