Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

Commit 4aff74a

Browse files
author
Dr. Safi
committed
Update moved database models into common utility folder.
1 parent 2c97f46 commit 4aff74a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)