Compare commits
7 Commits
da1a45e9f0
...
91aa37b003
Author | SHA1 | Date | |
---|---|---|---|
91aa37b003 | |||
42c0cd4bcc | |||
37fb7243ad | |||
2f5c28fe80 | |||
99cac7dd07 | |||
878cd9dcb4 | |||
eb1096ea99 |
1
index.ts
1
index.ts
@ -1,3 +1,4 @@
|
||||
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';
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@navy.gif/wrappers",
|
||||
"version": "1.3.9",
|
||||
"version": "1.3.13",
|
||||
"description": "Various wrapper classes I use in my projects",
|
||||
"repository": "https://git.corgi.wtf/Navy.gif/wrappers.git",
|
||||
"author": "Navy.gif",
|
||||
|
@ -50,8 +50,10 @@ type InternalQueueMsg = {
|
||||
queue: string
|
||||
} & InternalMessage
|
||||
|
||||
type Consumer = (content: object, msg: ConsumeMessage) => Promise<void>
|
||||
type Subscriber = (content: object, msg: ConsumeMessage) => Promise<void>
|
||||
// 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>
|
||||
|
||||
class MessageBroker {
|
||||
|
||||
@ -202,19 +204,19 @@ class MessageBroker {
|
||||
}
|
||||
|
||||
// Consume queue
|
||||
async consume (queue: string, consumer: Consumer, options: Options.Consume) {
|
||||
async consume<T> (queue: string, consumer: Consumer<T>, options: Options.Consume) {
|
||||
if (!this.#channel)
|
||||
throw new Error('Channel does not exist');
|
||||
|
||||
this.#logger.debug(`Adding queue consumer for ${queue}`);
|
||||
await this._consume(queue, consumer, options);
|
||||
await this._consume<T>(queue, consumer, options);
|
||||
|
||||
const list = this.#consumers.get(queue) ?? [];
|
||||
list.push({ consumer, options });
|
||||
this.#consumers.set(queue, list);
|
||||
}
|
||||
|
||||
private async _consume (queue: string, consumer: Consumer, options: Options.Consume): Promise<void> {
|
||||
private async _consume<T> (queue: string, consumer: Consumer<T>, 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) => {
|
||||
@ -225,13 +227,13 @@ class MessageBroker {
|
||||
}
|
||||
|
||||
// Subscribe to exchange, ensures messages aren't lost by binding it to a queue
|
||||
async subscribe (name: string, listener: Subscriber) {
|
||||
async subscribe<T> (name: string, listener: Subscriber<T>) {
|
||||
if (!this.#channel)
|
||||
throw new Error('Channel does not exist');
|
||||
|
||||
this.#logger.debug(`Subscribing to ${name}`);
|
||||
// if (!this.#subscribers.has(name))
|
||||
await this._subscribe(name, listener);
|
||||
await this._subscribe<T>(name, listener);
|
||||
|
||||
const list = this.#subscribers.get(name) ?? [];
|
||||
list.push(listener);
|
||||
@ -239,7 +241,7 @@ class MessageBroker {
|
||||
|
||||
}
|
||||
|
||||
private async _subscribe (name: string, listener: Subscriber): Promise<void> {
|
||||
private async _subscribe<T> (name: string, listener: Subscriber<T>): 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 } from "mongodb";
|
||||
import { MongoClient, MongoClientOptions, Db, Document, WithId, ObjectId, Filter, IndexSpecification, CreateIndexesOptions } 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 = []) {
|
||||
async ensureIndex (collection: string, indices: IndexSpecification = [], options?: CreateIndexesOptions) {
|
||||
if (!this.#db)
|
||||
throw new Error(`MongoDB not connected`);
|
||||
if (!(indices instanceof Array))
|
||||
indices = [ indices ];
|
||||
await this.#db.collection(collection).createIndex(indices);
|
||||
await this.#db.collection(collection).createIndex(indices, options);
|
||||
}
|
||||
|
||||
}
|
||||
|
41
tests/testMongo.js
Normal file
41
tests/testMongo.js
Normal file
@ -0,0 +1,41 @@
|
||||
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