UNPKG

@gftdcojp/ksqldb-orm

Version:

ksqldb-orm - Server-Side TypeScript ORM for ksqlDB with enterprise security extensions

166 lines 6.01 kB
"use strict"; /** * Schema 定義層 - defineSchema() で TypeScript 型→Avro Schema 生成&登録 */ 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.defineSchema = defineSchema; exports.getSchema = getSchema; exports.getAllSchemas = getAllSchemas; exports.clearSchemaRegistry = clearSchemaRegistry; exports.generateDDLFromSchema = generateDDLFromSchema; exports.createStreamFromSchema = createStreamFromSchema; /** * スキーマ定義のレジストリ */ const schemaRegistry = new Map(); /** * スキーマ定義関数 - 設計案の通り(トピック設定拡張版) */ function defineSchema(name, fields, topicConfig) { // Avro Schema を生成 const avroFields = Object.entries(fields).map(([fieldName, fieldType]) => { const definition = fieldType.getDefinition(); const avroField = { name: fieldName, type: fieldType.toAvroType(), }; // デフォルト値があれば追加 if (definition.default !== undefined) { avroField.default = definition.default; } return avroField; }); const avroSchema = { type: 'record', name, fields: avroFields, }; // デフォルトのトピック設定を適用 const defaultTopicConfig = { topicName: topicConfig?.topicName || name.toLowerCase(), valueFormat: topicConfig?.valueFormat || 'JSON', partitions: topicConfig?.partitions || 3, replicas: topicConfig?.replicas || 1, retentionMs: topicConfig?.retentionMs !== undefined ? topicConfig.retentionMs : -1, // デフォルトinfinity cleanupPolicy: topicConfig?.cleanupPolicy || 'delete', keyField: topicConfig?.keyField, additionalSettings: topicConfig?.additionalSettings || {}, }; const schemaDefinition = { name, fields, avroSchema, topicConfig: defaultTopicConfig, }; // レジストリに登録 schemaRegistry.set(name, schemaDefinition); return schemaDefinition; } /** * 登録済みスキーマを取得 */ function getSchema(name) { return schemaRegistry.get(name); } /** * 全スキーマを取得 */ function getAllSchemas() { return Array.from(schemaRegistry.values()); } /** * スキーマレジストリをクリア(テスト用) */ function clearSchemaRegistry() { schemaRegistry.clear(); } /** * スキーマ定義からDDL文を生成する関数 */ function generateDDLFromSchema(schemaName, streamType = 'STREAM') { const schema = getSchema(schemaName); if (!schema) { throw new Error(`Schema not found: ${schemaName}`); } // フィールド定義を生成 const fieldDefinitions = Object.entries(schema.fields).map(([fieldName, fieldType]) => { const definition = fieldType.getDefinition(); return `${fieldName} ${definition.type.toUpperCase()}`; }).join(',\n '); // WITH句の設定を生成 const topicConfig = schema.topicConfig; const withSettings = { 'kafka_topic': topicConfig.topicName, 'value_format': topicConfig.valueFormat, 'partitions': topicConfig.partitions.toString(), 'replicas': topicConfig.replicas.toString(), }; // retention設定を追加 if (topicConfig.retentionMs !== undefined) { withSettings['retention.ms'] = topicConfig.retentionMs.toString(); } // cleanup.policy設定を追加 if (topicConfig.cleanupPolicy) { withSettings['cleanup.policy'] = topicConfig.cleanupPolicy; } // キーフィールドがある場合は追加 if (topicConfig.keyField) { withSettings['key'] = topicConfig.keyField; } // 追加設定をマージ Object.assign(withSettings, topicConfig.additionalSettings); // WITH句を構築 const withClause = Object.entries(withSettings) .map(([key, value]) => ` '${key}'='${value}'`) .join(',\n'); const ddl = ` CREATE ${streamType} ${schemaName} ( ${fieldDefinitions} ) WITH ( ${withClause} );`; return ddl; } /** * スキーマ定義を使ってストリーム/テーブルを作成 */ async function createStreamFromSchema(schemaName, streamType = 'STREAM') { const ddl = generateDDLFromSchema(schemaName, streamType); // DDL実行(既にinfinityが設定されているのでauto-enhancementは無効化) const { executeDDLWithOptions } = await Promise.resolve().then(() => __importStar(require('./ksqldb-client'))); return executeDDLWithOptions(ddl, { disableAutoInfinityRetention: true }); } //# sourceMappingURL=schema.js.map