UNPKG

query-2jz

Version:

Query-2jz: A GraphQL alternative with faster performance and simpler use

307 lines 11.3 kB
"use strict"; 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 __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Query2jzEdgeHandler = exports.Query2jzEdgeRuntime = exports.Query2jzTypeGenerator = exports.Query2jzOptimisticEngine = exports.Query2jzRealtime = exports.Query2jzCache = exports.Query2jzResolver = exports.Query2jz = void 0; const resolver_1 = require("./core/resolver"); const cache_1 = require("./core/cache"); const realtime_1 = require("./core/realtime"); const optimistic_1 = require("./core/optimistic"); const typegen_1 = require("./core/typegen"); const edge_1 = require("./core/edge"); const fastify_1 = __importDefault(require("fastify")); /** * Main Query-2jz Engine - GraphQL alternative with faster performance and simpler use */ class Query2jz { config; models; resolver; cache; realtime; optimistic; typeGenerator; edgeRuntime; server; isInitialized = false; constructor(config, models = []) { this.config = config; this.models = models; } /** * Initialize Query-2jz engine */ async initialize() { if (this.isInitialized) return; console.log('Initializing Query-2jz engine...'); // Initialize core components this.resolver = new resolver_1.Query2jzResolver(this.config, this.models); this.cache = new cache_1.Query2jzCache(this.config); this.optimistic = new optimistic_1.Query2jzOptimisticEngine(); this.typeGenerator = new typegen_1.Query2jzTypeGenerator(this.models); // Initialize real-time if enabled if (this.config.realtime?.enabled) { this.realtime = new realtime_1.Query2jzRealtime(this.config); } // Initialize edge runtime if enabled if (this.config.edge?.enabled) { this.edgeRuntime = new edge_1.Query2jzEdgeRuntime(this.config); await this.edgeRuntime.initialize(); } this.isInitialized = true; console.log('Query-2jz engine initialized successfully'); } /** * Start the Query-2jz server */ async start(port = 3000) { await this.ensureInitialized(); this.server = (0, fastify_1.default)({ logger: true, trustProxy: true }); // Register Query-2jz routes await this.server.register(this.createQuery2jzRoutes.bind(this)); // Initialize real-time if enabled if (this.realtime) { await this.realtime.initialize(this.server.server); } // Start server await this.server.listen({ port, host: '0.0.0.0' }); console.log(`Query-2jz server running on port ${port}`); } /** * Stop the Query-2jz server */ async stop() { if (this.server) { await this.server.close(); } if (this.realtime) { await this.realtime.cleanup(); } if (this.edgeRuntime) { await this.edgeRuntime.cleanup(); } console.log('Query-2jz server stopped'); } /** * Execute a query */ async query(query, modelName) { await this.ensureInitialized(); if (this.edgeRuntime) { return await this.edgeRuntime.executeQuery(query, modelName); } return await this.resolver.executeQuery(query, modelName); } /** * Execute a mutation */ async mutate(mutation, modelName) { await this.ensureInitialized(); if (this.edgeRuntime) { return await this.edgeRuntime.executeMutation(mutation, modelName); } return await this.resolver.executeMutation(mutation, modelName); } /** * Subscribe to live query updates */ subscribe(query, modelName, callback) { if (!this.realtime) { console.warn('Real-time is not enabled'); return null; } return this.realtime.subscribe(query, modelName, callback); } /** * Unsubscribe from live query updates */ unsubscribe(subscriptionId) { if (this.realtime) { this.realtime.unsubscribe(subscriptionId); } } /** * Generate TypeScript types */ generateTypes(outputDir) { if (!this.typeGenerator) { throw new Error('Query-2jz not initialized'); } if (outputDir) { this.typeGenerator = new typegen_1.Query2jzTypeGenerator(this.models, outputDir); } this.typeGenerator.generateTypes(); console.log(`Types generated in ${outputDir || './generated'}`); } /** * Add a model */ addModel(model) { this.models.push(model); if (this.resolver) { this.resolver.addModel(model); } if (this.typeGenerator) { this.typeGenerator = new typegen_1.Query2jzTypeGenerator(this.models); } } /** * Remove a model */ removeModel(modelName) { this.models = this.models.filter(m => m.name !== modelName); if (this.resolver) { this.resolver.removeModel(modelName); } if (this.typeGenerator) { this.typeGenerator = new typegen_1.Query2jzTypeGenerator(this.models); } } /** * Get all models */ getModels() { return [...this.models]; } /** * Get model by name */ getModel(modelName) { return this.models.find(m => m.name === modelName); } /** * Get Query-2jz statistics */ getStats() { return { initialized: this.isInitialized, models: this.models.length, cache: this.cache?.getStats(), realtime: this.realtime?.getStats(), optimistic: this.optimistic?.getStats(), edge: this.edgeRuntime?.getStats() }; } /** * Create Query-2jz routes for Fastify */ async createQuery2jzRoutes(fastify, options) { // Health check fastify.get('/health', async (request, reply) => { return { status: 'healthy', timestamp: new Date().toISOString() }; }); // Query endpoint fastify.get('/api/query-2jz/:model', async (request, reply) => { const { model } = request.params; const query = request.query; try { const result = await this.query(query, model); // Set cache headers if (result.meta?.etag) { reply.header('ETag', result.meta.etag); } if (result.meta?.lastModified) { reply.header('Last-Modified', result.meta.lastModified); } reply.header('Cache-Control', 'public, max-age=300'); return result; } catch (error) { reply.code(500); return { error: 'Query execution failed', message: error.message }; } }); // Mutation endpoint fastify.post('/api/query-2jz/:model', async (request, reply) => { const { model } = request.params; const mutation = request.body; try { const result = await this.mutate(mutation, model); reply.header('Cache-Control', 'no-cache'); return result; } catch (error) { reply.code(500); return { error: 'Mutation execution failed', message: error.message }; } }); // Real-time subscription endpoint if (this.realtime) { fastify.get('/api/query-2jz/:model/subscribe', async (request, reply) => { const { model } = request.params; const query = request.query; // This would handle SSE connections reply.type('text/event-stream'); reply.header('Cache-Control', 'no-cache'); reply.header('Connection', 'keep-alive'); const subscriptionId = this.subscribe(query, model, (data) => { reply.raw.write(`data: ${JSON.stringify(data)}\n\n`); }); request.raw.on('close', () => { if (subscriptionId) { this.unsubscribe(subscriptionId); } }); return reply; }); } // Type generation endpoint fastify.post('/api/query-2jz/generate-types', async (request, reply) => { try { this.generateTypes(); return { message: 'Types generated successfully' }; } catch (error) { reply.code(500); return { error: 'Type generation failed', message: error.message }; } }); // Statistics endpoint fastify.get('/api/query-2jz/stats', async (request, reply) => { return this.getStats(); }); } async ensureInitialized() { if (!this.isInitialized) { await this.initialize(); } } } exports.Query2jz = Query2jz; // Export types and classes __exportStar(require("./types"), exports); var resolver_2 = require("./core/resolver"); Object.defineProperty(exports, "Query2jzResolver", { enumerable: true, get: function () { return resolver_2.Query2jzResolver; } }); var cache_2 = require("./core/cache"); Object.defineProperty(exports, "Query2jzCache", { enumerable: true, get: function () { return cache_2.Query2jzCache; } }); var realtime_2 = require("./core/realtime"); Object.defineProperty(exports, "Query2jzRealtime", { enumerable: true, get: function () { return realtime_2.Query2jzRealtime; } }); var optimistic_2 = require("./core/optimistic"); Object.defineProperty(exports, "Query2jzOptimisticEngine", { enumerable: true, get: function () { return optimistic_2.Query2jzOptimisticEngine; } }); var typegen_2 = require("./core/typegen"); Object.defineProperty(exports, "Query2jzTypeGenerator", { enumerable: true, get: function () { return typegen_2.Query2jzTypeGenerator; } }); var edge_2 = require("./core/edge"); Object.defineProperty(exports, "Query2jzEdgeRuntime", { enumerable: true, get: function () { return edge_2.Query2jzEdgeRuntime; } }); Object.defineProperty(exports, "Query2jzEdgeHandler", { enumerable: true, get: function () { return edge_2.Query2jzEdgeHandler; } }); // Default export exports.default = Query2jz; //# sourceMappingURL=index.js.map