UNPKG

@yihuangdb/storage-object

Version:

A Node.js storage object layer library using Redis OM

71 lines 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StorageSchema = void 0; const redis_om_1 = require("redis-om"); class StorageSchema { schema; fields; dataStructure; constructor(entityName, fields, useJSON = true) { this.fields = fields; this.dataStructure = useJSON ? 'JSON' : 'HASH'; const schemaDefinition = this.buildSchemaDefinition(fields); // Use JSON mode by default for better support of nested objects // This requires RedisJSON module to be installed (Redis Stack) this.schema = new redis_om_1.Schema(entityName, schemaDefinition, { dataStructure: this.dataStructure, }); } buildSchemaDefinition(fields) { const definition = {}; for (const [fieldName, fieldConfig] of Object.entries(fields)) { if (typeof fieldConfig === 'string') { definition[fieldName] = this.createFieldDefinition(fieldConfig); } else { definition[fieldName] = this.createFieldDefinition(fieldConfig.type, fieldConfig); } } return definition; } createFieldDefinition(type, options = {}) { const baseDefinition = { type: this.mapFieldType(type) }; if (options.indexed !== undefined) { baseDefinition.indexed = options.indexed; } if (type === 'text' && options.sortable) { baseDefinition.sortable = options.sortable; } if (type === 'text' && options.normalized) { baseDefinition.normalized = options.normalized; } if (type === 'string[]' && options.separator) { baseDefinition.separator = options.separator; } return baseDefinition; } mapFieldType(type) { const typeMap = { 'string': 'string', 'number': 'number', 'boolean': 'boolean', 'date': 'date', 'point': 'point', 'string[]': 'string[]', 'number[]': 'number[]', // Only supported in JSON mode 'text': 'text', }; return typeMap[type] || 'string'; } getSchema() { return this.schema; } getFields() { return this.fields; } isJSON() { return this.dataStructure === 'JSON'; } } exports.StorageSchema = StorageSchema; //# sourceMappingURL=schema.js.map