fix: Move database init into Db
class
This commit is contained in:
parent
be5228b053
commit
20dd82c79e
@ -6,10 +6,27 @@ interface Messages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Db {
|
class Db {
|
||||||
private client: cassandra.Client;
|
private client: cassandra.Client = new cassandra.Client({
|
||||||
|
contactPoints: ['localhost'],
|
||||||
|
localDataCenter: 'datacenter1',
|
||||||
|
});
|
||||||
|
|
||||||
constructor(client: cassandra.Client) {
|
// Initalize and connect
|
||||||
this.client = client;
|
async init() {
|
||||||
|
try {
|
||||||
|
await this.client.connect();
|
||||||
|
} catch (e) {
|
||||||
|
console.log(`Error connecting to DB: ${e as Error}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.client.execute(`CREATE KEYSPACE IF NOT EXISTS users WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`);
|
||||||
|
await this.client.execute(`CREATE KEYSPACE IF NOT EXISTS channels WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(`Error generating keyspaces: ${e as Error}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Channel Method
|
// Create Channel Method
|
||||||
@ -76,29 +93,8 @@ class Db {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new cassandra.Client({
|
const db = new Db();
|
||||||
contactPoints: ['localhost'],
|
await db.init();
|
||||||
localDataCenter: 'datacenter1',
|
|
||||||
});
|
|
||||||
|
|
||||||
// Connect to Cassandra/ScyllaDB and create
|
|
||||||
// the necessary tables, keyspaces, etc.
|
|
||||||
try {
|
|
||||||
await client.connect();
|
|
||||||
} catch (e) {
|
|
||||||
console.log(`Error connecting to DB: ${e as Error}`);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await client.execute(`CREATE KEYSPACE IF NOT EXISTS users WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`);
|
|
||||||
await client.execute(`CREATE KEYSPACE IF NOT EXISTS channels WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`);
|
|
||||||
} catch (e) {
|
|
||||||
console.log(`Error generating keyspaces: ${e as Error}`);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const db = new Db(client);
|
|
||||||
await db.createChannel('general');
|
await db.createChannel('general');
|
||||||
|
|
||||||
export { db, type Messages };
|
export { db, type Messages };
|
||||||
|
Loading…
Reference in New Issue
Block a user