mirror of
https://github.com/djhackersdev/minime.git
synced 2026-09-12 20:45:30 -05:00
Add database utility functions
This commit is contained in:
30
src/db.ts
Normal file
30
src/db.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { randomBytes } from "crypto";
|
||||
import { Pool, PoolClient } from "pg";
|
||||
|
||||
const pool = new Pool();
|
||||
|
||||
export function connect(): Promise<PoolClient> {
|
||||
return pool.connect();
|
||||
}
|
||||
|
||||
export function generateId(): bigint {
|
||||
const buf = randomBytes(8);
|
||||
|
||||
buf[0] &= 0x7f; // Force number to be non-negative
|
||||
|
||||
// Let's not depend on Node v12 for the sake of 3 LoC just yet.
|
||||
|
||||
const hi = buf.readUInt32BE(0);
|
||||
const lo = buf.readUInt32BE(4);
|
||||
|
||||
return (BigInt(hi) << 32n) | BigInt(lo);
|
||||
}
|
||||
|
||||
/** Generate a random 32-bit ID for use in external protocol messages */
|
||||
export function generateExtId(): number {
|
||||
const buf = randomBytes(4);
|
||||
|
||||
buf[0] &= 0x7f; // Force number to be non-negative
|
||||
|
||||
return buf.readUInt32BE(0);
|
||||
}
|
||||
Reference in New Issue
Block a user