I have an application that uses worker_threads
. When the application starts up, I create a connection to the database in the main project, then when I start a thread, is it possible to access that database connection without having to connect to the database in the child thread?
// main.database.ts
export abstract class SqlDatabase {
private static db: Database;
static connect(dbPath: string) {
this.db = new Database(dbPath);
}
mutate(sql: string, ...parameters: string[]) {
const stmt = SqlDatabase.db.prepare(sql);
}
}
// files.database.ts
class FilesDatabase extends SqlDatabase {
someFunction(){}
}
export const files = new FilesDatabase();
// index.ts
SqlDatabase.connect('/path/to/sqlite.db');
Then I have an action which starts up a Worker
process:
export function runThread(file: string, args: { [key: string]: any; }) {
return new Worker(path.resolve(__dirname, './worker.js'), {
workerData: {
aliasModule: file,
...args
}
});
}
In the worker process I have something like this:
import { files } from '../../database/files.database';
files.someFunction();
When running the process I get the following error message:
const stmt = SqlDatabase.db.prepare(sql);
Is there a way that I can get access to this database without having to make another connection in the child?