Update .gitignore, add dist directory
This commit is contained in:
+6
@@ -0,0 +1,6 @@
|
||||
import { type BunMariaDBConfig } from "../types";
|
||||
type Params = {
|
||||
config: BunMariaDBConfig;
|
||||
};
|
||||
export default function grabMariaDBClient({ config, }: Params): Bun.SQL | undefined;
|
||||
export {};
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
import path from "path";
|
||||
import grabDirNames from "../data/grab-dir-names";
|
||||
import {} from "../types";
|
||||
import { SQL } from "bun";
|
||||
/** Reuse one client per database name to avoid exhausting MariaDB connections. */
|
||||
const clientCache = new Map();
|
||||
export default function grabMariaDBClient({ config, }) {
|
||||
const cacheKey = config.db_name || "__default__";
|
||||
const cached = clientCache.get(cacheKey);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
// Prefer the process-wide client when it matches the requested database
|
||||
if (global.MARIADB_CLIENT &&
|
||||
(!global.CONFIG?.db_name || global.CONFIG.db_name === config.db_name)) {
|
||||
clientCache.set(cacheKey, global.MARIADB_CLIENT);
|
||||
return global.MARIADB_CLIENT;
|
||||
}
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
try {
|
||||
const tls = config.ssl_ca
|
||||
? {
|
||||
ca: Bun.file(path.resolve(ROOT_DIR, config.ssl_ca)),
|
||||
rejectUnauthorized: false,
|
||||
}
|
||||
: {
|
||||
rejectUnauthorized: false,
|
||||
};
|
||||
const MariaDBClient = new SQL({
|
||||
hostname: process.env.BUN_MARIADB_SERVER_HOST,
|
||||
username: process.env.BUN_MARIADB_SERVER_USERNAME,
|
||||
password: process.env.BUN_MARIADB_SERVER_PASSWORD,
|
||||
database: config.db_name,
|
||||
port: process.env.BUN_MARIADB_SERVER_PORT
|
||||
? Number(process.env.BUN_MARIADB_SERVER_PORT)
|
||||
: undefined,
|
||||
...config.db_config,
|
||||
tls,
|
||||
adapter: "mariadb",
|
||||
});
|
||||
clientCache.set(cacheKey, MariaDBClient);
|
||||
return MariaDBClient;
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Couldn't grab MariaDB Client => ` + error.message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
import { type BunMariaDBConfig, type BUN_MARIADB_DatabaseSchemaType } from "../types";
|
||||
/**
|
||||
* # Declare Global Variables
|
||||
*/
|
||||
declare global {
|
||||
var CONFIG: BunMariaDBConfig;
|
||||
var DB_SCHEMA: BUN_MARIADB_DatabaseSchemaType;
|
||||
var MARIADB_CLIENT: Bun.SQL;
|
||||
}
|
||||
export default function init(): void;
|
||||
Vendored
+80
@@ -0,0 +1,80 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { AppData } from "../data/app-data";
|
||||
import grabDirNames from "../data/grab-dir-names";
|
||||
import { RequiredENVs, } from "../types";
|
||||
import setMariaDBClient from "./set-mariadb-client";
|
||||
export default function init() {
|
||||
try {
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
const { ConfigFileName } = AppData;
|
||||
const ConfigFilePath = path.join(ROOT_DIR, ConfigFileName);
|
||||
if (!fs.existsSync(ConfigFilePath)) {
|
||||
console.error(`Please create a \`${ConfigFileName}\` file at the root of your project.`);
|
||||
process.exit(1);
|
||||
}
|
||||
const ConfigImport = require(ConfigFilePath);
|
||||
const Config = ConfigImport["default"];
|
||||
if (!Config) {
|
||||
console.error(`No default export from \`${ConfigFilePath}\`. Please export a default module.`);
|
||||
process.exit(1);
|
||||
}
|
||||
if (!Config.db_name) {
|
||||
console.error(`\`db_name\` is required in your config`);
|
||||
process.exit(1);
|
||||
}
|
||||
RequiredENVs.forEach((env_key) => {
|
||||
if (env_key == "BUN_MARIADB_SERVER_PORT" ||
|
||||
env_key == "BUN_MARIADB_SERVER_SSL_KEY_PATH") {
|
||||
return;
|
||||
}
|
||||
if (!process.env[env_key]) {
|
||||
console.error(`\`${env_key}\` env variable is required.`);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
if (!Config.db_dir) {
|
||||
console.error(`\`db_dir\` is required in your config. This directory holds all database related configuration. Also note that a \`${AppData["DbSchemaFileName"]}\` file is also required in this directory to define your database schema`);
|
||||
process.exit(1);
|
||||
}
|
||||
const db_dir = path.resolve(ROOT_DIR, Config.db_dir);
|
||||
if (!fs.existsSync(db_dir)) {
|
||||
fs.mkdirSync(db_dir, { recursive: true });
|
||||
}
|
||||
const DBSchemaFilePath = path.join(db_dir, AppData["DbSchemaFileName"]);
|
||||
if (!fs.existsSync(DBSchemaFilePath)) {
|
||||
console.error(`Please create a schema file at \`${DBSchemaFilePath}\`. Don't forget to export a default module from this file.`);
|
||||
process.exit(1);
|
||||
}
|
||||
const DbSchemaImport = require(DBSchemaFilePath);
|
||||
const DbSchema = DbSchemaImport["default"];
|
||||
const backup_dir = Config.db_backup_dir || AppData["DefaultBackupDirName"];
|
||||
const BackupDir = path.resolve(db_dir, backup_dir);
|
||||
if (!fs.existsSync(BackupDir)) {
|
||||
fs.mkdirSync(BackupDir, { recursive: true });
|
||||
}
|
||||
const ExportDir = path.resolve(db_dir, AppData["DefaultExportDirName"]);
|
||||
if (!fs.existsSync(ExportDir)) {
|
||||
fs.mkdirSync(ExportDir, { recursive: true });
|
||||
}
|
||||
global.CONFIG = Config;
|
||||
global.DB_SCHEMA = DbSchema;
|
||||
if (!global.CONFIG) {
|
||||
console.error(`Couldn't grab global Config.`);
|
||||
process.exit(1);
|
||||
}
|
||||
if (!global.DB_SCHEMA) {
|
||||
console.error(`Couldn't grab Database Schema.`);
|
||||
process.exit(1);
|
||||
}
|
||||
setMariaDBClient({ config: Config });
|
||||
if (!global.MARIADB_CLIENT) {
|
||||
console.error(`Couldn't set MariaDB Client.`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Initialization ERROR => ` + error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
import type { BUN_MARIADB_DatabaseSchemaType } from "../types";
|
||||
type Params = {
|
||||
schema: BUN_MARIADB_DatabaseSchemaType;
|
||||
};
|
||||
export declare function writeLiveSchema({ schema }: Params): void;
|
||||
export declare function readLiveSchema(): BUN_MARIADB_DatabaseSchemaType | undefined;
|
||||
export {};
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
||||
import grabDirNames from "../data/grab-dir-names";
|
||||
import path from "path";
|
||||
const { BUN_MARIADB_LIVE_SCHEMA } = grabDirNames();
|
||||
export function writeLiveSchema({ schema }) {
|
||||
mkdirSync(path.dirname(BUN_MARIADB_LIVE_SCHEMA), { recursive: true });
|
||||
writeFileSync(BUN_MARIADB_LIVE_SCHEMA, JSON.stringify(schema));
|
||||
}
|
||||
export function readLiveSchema() {
|
||||
if (!existsSync(BUN_MARIADB_LIVE_SCHEMA)) {
|
||||
return undefined;
|
||||
}
|
||||
const live_schema = readFileSync(BUN_MARIADB_LIVE_SCHEMA, "utf-8");
|
||||
return JSON.parse(live_schema);
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import { type BunMariaDBConfig } from "../types";
|
||||
type Params = {
|
||||
config: BunMariaDBConfig;
|
||||
};
|
||||
export default function setMariaDBClient({ config }: Params): void;
|
||||
export {};
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
import {} from "../types";
|
||||
import grabMariaDBClient from "./grab-mariadb-client";
|
||||
export default function setMariaDBClient({ config }) {
|
||||
try {
|
||||
const MariaDBClient = grabMariaDBClient({ config });
|
||||
if (!MariaDBClient) {
|
||||
throw new Error(`Couldn't grab MariaDB Client`);
|
||||
}
|
||||
global.MARIADB_CLIENT = MariaDBClient;
|
||||
// const test = await MariaDBClient.unsafe(`SHOW DATABASES`);
|
||||
// if (!test.count) {
|
||||
// console.error(`MariaDBClient Error: Database not ready.`);
|
||||
// console.log(test);
|
||||
// process.exit(1);
|
||||
// }
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`ERROR setting MariaDB Client => ` + error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user