Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.then(conn => {
console.log(`Creating "${NAME}" database...`)
r.dbList().contains(NAME).run(conn)
.then(dbExists => {
if (dbExists) throw new Error(`"${NAME}" already exists`)
return r.dbCreate(NAME).run(conn)
})
.then(res => {
console.log('Creating "users" table...')
return r.db(NAME).tableCreate('users', {
primaryKey: 'publicKey'
}).run(conn)
})
.then(res => {
// The usernames table is used to quickly ensure unique usernames
console.log('Creating "usernames" table...')
return r.db(NAME).tableCreate('usernames', {
primaryKey: 'username'
}).run(conn)
function createDatabase () {
rethink.dbList().run(connection, (err, dbs) => {
if (err) {
throw new Error('Error getting the list of databases: ', err)
}
if (dbs.indexOf('bkmrkd') === -1) {
rethink.dbCreate('bkmrkd').run(connection, (err, response) => {
if (err) {
throw new Error('Error creating the bkmrkd database: ', err)
}
console.info('Created the bkmrkd database')
})
}
bkmrkd = rethink.db('bkmrkd')
.then(function(conn) {
console.log('_____________________');
console.log("Conection stablished");
console.log("Checking DB...");
r.dbList().run(conn)
.then(function(dbList){
if (dbList.indexOf(config.rethinkdb.db) > -1)
{
initialize(conn);
} else {
console.log("The DB doesn't exist.");
console.log("Initializing DB "+config.rethinkdb.db);
r.dbCreate(config.rethinkdb.db).run(conn)
.then(initialize(conn))
}
})
})
.error(function(error){
const initialize_metadata = (db, conn) =>
r.branch(r.dbList().contains(db), null, r.dbCreate(db)).run(conn)
.then(() =>
Promise.all([ 'hz_collections', 'hz_users_auth', 'hz_groups' ].map((table) =>
r.branch(r.db(db).tableList().contains(table),
{ },
r.db(db).tableCreate(table))
.run(conn))))
.then(() =>
r.db(db).table('hz_collections').wait({ timeout: 30 }).run(conn))
.then(() =>
Promise.all([
r.db(db).tableList().contains('users').not().run(conn).then(() =>
create_collection(db, 'users', conn)),
r.db(db).table('hz_collections')
.insert({ id: 'hz_metadata', version: metadata_version })
.run(conn),
])
async install () {
try {
const databaseName : string = this.config.get( 'database.db' );
const conn = await this.connections.acquire();
try {
const databases = await r.dbList().run( conn );
if ( !databases.includes( databaseName ) ) {
await r.dbCreate( databaseName ).run( conn );
}
await ( r.db( databaseName ) as any ).wait().run( conn );
await this.tables.install();
await this.onInstall.notify();
await ( r.db( databaseName ) as any ).wait().run( conn );
} finally {
await this.connections.release( conn );
}
function getDbList(conn) {
return r.dbList().run(conn);
}
function getDbList(conn) {
return r.dbList().run(conn);
}
async function syncDB() {
const connection = await getConnection({ db: null });
r.branch(
r.dbList().contains(RETHINKDB.DB).not(),
r.dbCreate(RETHINKDB.DB),
null
).run(connection);
await connection.close();
}