firestore-queue
Version:
A powerful, scalable queue system built on Google Firestore with time-based indexing, auto-configuration, and connection reuse
142 lines โข 6.15 kB
JavaScript
;
/**
* Auto-Configuration Example
* Shows the simplest possible Fire Queue setup with auto-detection
*/
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.autoConfigExample = autoConfigExample;
exports.showDetectedConfig = showDetectedConfig;
exports.showSearchLocations = showSearchLocations;
const index_1 = require("../index");
async function autoConfigExample() {
console.log('๐ฅ Fire Queue - Auto Configuration');
console.log('==================================\n');
console.log('๐ Method 1: Minimal Setup (Auto-detects everything)');
console.log('----------------------------------------------------');
// Only topic name required! Everything else auto-detected:
// - Finds config/firebase-service-account.json automatically
// - Extracts project_id from the service account file
// - Uses "firequeue" database by default
const { enqueue, consume, shutdown } = await (0, index_1.createReadyQueue)({
topic: 'auto_queue', // Only thing you need to specify!
});
console.log('โ
Queue created with full auto-configuration\n');
console.log('๐ Method 2: Partial Auto-config');
console.log('--------------------------------');
// Specify just what you want to override
const customQueue = await (0, index_1.createReadyQueue)({
topic: 'custom_queue',
dbId: 'my-custom-db', // Override database
// projectId and serviceAccountPath still auto-detected
});
console.log('โ
Queue with custom database but auto-detected credentials\n');
console.log('๐งช Testing the Auto-configured Queue');
console.log('------------------------------------');
// Add some test messages
await enqueue('welcome', { message: 'Hello Auto Config!' });
await enqueue('process', { data: { id: 123, status: 'pending' } });
console.log('๐ Messages added to auto-configured queue');
// Start a simple consumer
await consume('auto-worker', async (messages) => {
for (const msg of messages) {
console.log(`๐ Auto-processing: ${msg.type} - ${JSON.stringify(msg.payload)}`);
await msg.ack();
}
});
console.log('๐ค Auto-consumer started\n');
// Let it process
await new Promise(resolve => setTimeout(resolve, 3000));
console.log('๐งน Cleaning up...');
await shutdown();
await customQueue.shutdown();
console.log('โ
All done with auto-configuration!');
}
async function showDetectedConfig() {
console.log('\n๐ Configuration Auto-Detection Demo');
console.log('====================================\n');
const { extractProjectIdFromServiceAccount, findServiceAccountFile, autoLoadFirebaseConfig } = await Promise.resolve().then(() => __importStar(require('../utils/service-account-helper')));
// Show what gets auto-detected
try {
const serviceAccountPath = findServiceAccountFile();
if (serviceAccountPath) {
console.log(`๐ Auto-detected service account: ${serviceAccountPath}`);
const projectId = extractProjectIdFromServiceAccount(serviceAccountPath);
console.log(`๐ Auto-extracted project ID: ${projectId}`);
const fullConfig = autoLoadFirebaseConfig();
console.log(`โ๏ธ Full auto-config:`, {
projectId: fullConfig.projectId,
serviceAccountPath: fullConfig.serviceAccountPath,
});
}
else {
console.log('โ No service account file found in common locations');
}
}
catch (error) {
console.log(`โ Auto-detection failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
// Demo showing file locations checked
async function showSearchLocations() {
console.log('\n๐ Service Account Search Locations');
console.log('===================================\n');
const commonPaths = [
'config/firebase-service-account.json',
'configs/firebase-service-account.json',
'firebase-service-account.json',
'.firebase/service-account.json',
];
console.log('Fire Queue automatically searches for service account files in:');
commonPaths.forEach((path, index) => {
console.log(` ${index + 1}. ${path}`);
});
console.log('\n๐ก Pro Tip: Place your file in config/firebase-service-account.json for automatic detection!');
}
async function runAll() {
try {
await autoConfigExample();
await showDetectedConfig();
await showSearchLocations();
}
catch (error) {
console.error('โ Error:', error);
}
}
if (require.main === module) {
runAll().catch(console.error);
}
//# sourceMappingURL=auto-config-example.js.map