Compare commits
No commits in common. "91aa37b00398e4088b31dbfee415f4a39fb4c061" and "da1a45e9f0fa805cc6a8a18fd279dcb5f0f62ace" have entirely different histories.
91aa37b003
...
da1a45e9f0
3
index.ts
3
index.ts
@ -1,4 +1,3 @@
|
||||
export { MessageBroker, BrokerOptions } from "./src/MessageBroker.js";
|
||||
export { MariaDB, MariaOptions } from './src/MariaDB.js';
|
||||
export { MongoDB, MongoOptions } from './src/MongoDB.js';
|
||||
export { ObjectId, Document, DeleteResult } from 'mongodb';
|
||||
export { MongoDB, MongoOptions } from './src/MongoDB.js';
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@navy.gif/wrappers",
|
||||
"version": "1.3.13",
|
||||
"version": "1.3.9",
|
||||
"description": "Various wrapper classes I use in my projects",
|
||||
"repository": "https://git.corgi.wtf/Navy.gif/wrappers.git",
|
||||
"author": "Navy.gif",
|
||||
|
@ -50,10 +50,8 @@ type InternalQueueMsg = {
|
||||
queue: string
|
||||
} & InternalMessage
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type Consumer<T = any> = (content: T, msg: ConsumeMessage) => Promise<void>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type Subscriber<T = any> = (content: T, msg: ConsumeMessage) => Promise<void>
|
||||
type Consumer = (content: object, msg: ConsumeMessage) => Promise<void>
|
||||
type Subscriber = (content: object, msg: ConsumeMessage) => Promise<void>
|
||||
|
||||
class MessageBroker {
|
||||
|
||||
@ -204,19 +202,19 @@ class MessageBroker {
|
||||
}
|
||||
|
||||
// Consume queue
|
||||
async consume<T> (queue: string, consumer: Consumer<T>, options: Options.Consume) {
|
||||
async consume (queue: string, consumer: Consumer, options: Options.Consume) {
|
||||
if (!this.#channel)
|
||||
throw new Error('Channel does not exist');
|
||||
|
||||
this.#logger.debug(`Adding queue consumer for ${queue}`);
|
||||
await this._consume<T>(queue, consumer, options);
|
||||
await this._consume(queue, consumer, options);
|
||||
|
||||
const list = this.#consumers.get(queue) ?? [];
|
||||
list.push({ consumer, options });
|
||||
this.#consumers.set(queue, list);
|
||||
}
|
||||
|
||||
private async _consume<T> (queue: string, consumer: Consumer<T>, options: Options.Consume): Promise<void> {
|
||||
private async _consume (queue: string, consumer: Consumer, options: Options.Consume): Promise<void> {
|
||||
if (!this.#channel)
|
||||
return Promise.reject(new Error('Channel doesn\'t exist'));
|
||||
await this.#channel.consume(queue, async (msg: ConsumeMessage) => {
|
||||
@ -227,13 +225,13 @@ class MessageBroker {
|
||||
}
|
||||
|
||||
// Subscribe to exchange, ensures messages aren't lost by binding it to a queue
|
||||
async subscribe<T> (name: string, listener: Subscriber<T>) {
|
||||
async subscribe (name: string, listener: Subscriber) {
|
||||
if (!this.#channel)
|
||||
throw new Error('Channel does not exist');
|
||||
|
||||
this.#logger.debug(`Subscribing to ${name}`);
|
||||
// if (!this.#subscribers.has(name))
|
||||
await this._subscribe<T>(name, listener);
|
||||
await this._subscribe(name, listener);
|
||||
|
||||
const list = this.#subscribers.get(name) ?? [];
|
||||
list.push(listener);
|
||||
@ -241,7 +239,7 @@ class MessageBroker {
|
||||
|
||||
}
|
||||
|
||||
private async _subscribe<T> (name: string, listener: Subscriber<T>): Promise<void> {
|
||||
private async _subscribe (name: string, listener: Subscriber): Promise<void> {
|
||||
if (!this.#channel)
|
||||
return Promise.reject(new Error('Channel doesn\'t exist'));
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { inspect } from "node:util";
|
||||
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification, CreateIndexesOptions } from "mongodb";
|
||||
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification } from "mongodb";
|
||||
import { IServer, ILogger, LoggerClientOptions } from "./interfaces/index.js";
|
||||
|
||||
type Credentials = {
|
||||
@ -333,12 +333,12 @@ class MongoDB {
|
||||
return this.#db.collection(coll);
|
||||
}
|
||||
|
||||
async ensureIndex (collection: string, indices: IndexSpecification = [], options?: CreateIndexesOptions) {
|
||||
async ensureIndex (collection: string, indices: IndexSpecification = []) {
|
||||
if (!this.#db)
|
||||
throw new Error(`MongoDB not connected`);
|
||||
if (!(indices instanceof Array))
|
||||
indices = [ indices ];
|
||||
await this.#db.collection(collection).createIndex(indices, options);
|
||||
await this.#db.collection(collection).createIndex(indices);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,41 +0,0 @@
|
||||
import { MongoDB } from '../build/esm/index.js';
|
||||
|
||||
const mongo = new MongoDB({
|
||||
createLogger: () => {
|
||||
return {
|
||||
debug: console.log,
|
||||
info: console.log,
|
||||
status: console.log,
|
||||
warn: console.log,
|
||||
error: console.error
|
||||
};
|
||||
}
|
||||
}, {
|
||||
credentials: {
|
||||
URI: 'mongodb://127.0.0.1:27017',
|
||||
database: 'framework-proto'
|
||||
},
|
||||
load: true
|
||||
});
|
||||
|
||||
await mongo.init();
|
||||
const user = await mongo.findOne('users', { name: 'navy' });
|
||||
console.log(user, user._id.toString());
|
||||
|
||||
// const query = {
|
||||
// serverType: 'Matchmaking'
|
||||
// };
|
||||
// const [ result ] = await mongo.collection('reservedServers').aggregate([
|
||||
// { $match: query },
|
||||
// { $group: { _id: null, allPlayers: { $push: '$players' } } },
|
||||
// { $project: { _id: 0, allPlayers: { $reduce: { input: '$allPlayers', initialValue: [], in: { $concatArrays: [ '$$value', '$$this' ] } } } } },
|
||||
// { $project: { allPlayers: { $sortArray: { input: '$allPlayers', sortBy: { playerElo: -1 } } } } }
|
||||
// ]).toArray();
|
||||
// console.log(result);
|
||||
|
||||
const result = await mongo.find('locations', {});
|
||||
const ips = result.map(loc => loc.ip);
|
||||
const ranges = new Set(ips.map(ip => ip.split('.').slice(0, 3).join('.')));
|
||||
console.log(ranges);
|
||||
|
||||
await mongo.close();
|
Loading…
Reference in New Issue
Block a user