firestore-queue
Version:
A powerful, scalable queue system built on Google Firestore with time-based indexing, auto-configuration, and connection reuse
140 lines • 4.78 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FireQueueManager = void 0;
exports.setupFireQueue = setupFireQueue;
const simple_setup_1 = require("./simple-setup");
const admin = __importStar(require("firebase-admin"));
/**
* FireQueue Manager for reusable configurations and connections
* Provides a standard way to initialize and manage multiple queues
*/
class FireQueueManager {
constructor(config) {
this.queues = new Map();
this.baseConfig = {
projectId: config.projectId,
serviceAccountPath: config.serviceAccountPath,
firestoreInstance: config.firestoreInstance,
dbId: config.dbId || 'firequeue',
};
this.firestoreInstance = config.firestoreInstance;
}
/**
* Configure the manager with connection details
* This is the standard initialization method
*/
static async configure(config) {
const manager = new FireQueueManager(config);
// Initialize Firestore instance if not provided
if (!config.firestoreInstance && config.serviceAccountPath) {
if (admin.apps.length === 0) {
const serviceAccount = require(config.serviceAccountPath);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
projectId: config.projectId,
});
}
manager.firestoreInstance = new admin.firestore.Firestore({
projectId: config.projectId,
databaseId: config.dbId || 'firequeue',
});
}
return manager;
}
/**
* Create a new queue with the manager's base configuration
*/
async createQueue(topic, options) {
const queueConfig = {
...this.baseConfig,
...options,
topic,
firestoreInstance: this.firestoreInstance,
};
const queueWrapper = await (0, simple_setup_1.createReadyQueue)(queueConfig);
this.queues.set(topic, queueWrapper);
return queueWrapper;
}
/**
* Get an existing queue by topic name
*/
getQueue(topic) {
return this.queues.get(topic);
}
/**
* List all managed queues
*/
listQueues() {
return Array.from(this.queues.keys());
}
/**
* Get the shared Firestore instance
*/
getFirestoreInstance() {
return this.firestoreInstance;
}
/**
* Shutdown all managed queues
*/
async shutdown() {
const shutdownPromises = Array.from(this.queues.values()).map(q => q.shutdown());
await Promise.all(shutdownPromises);
this.queues.clear();
}
/**
* Get metrics for all queues
*/
async getAllMetrics() {
const metrics = {};
for (const [topic, queue] of this.queues) {
try {
metrics[topic] = await queue.getMetrics();
}
catch (error) {
metrics[topic] = { error: error instanceof Error ? error.message : String(error) };
}
}
return metrics;
}
}
exports.FireQueueManager = FireQueueManager;
/**
* Simplified factory function for the most common use case
*/
async function setupFireQueue(config) {
return FireQueueManager.configure(config);
}
//# sourceMappingURL=queue-manager.js.map