redis-orm-node
Version:
`redis-orm` is a lightweight Object-Relational Mapping (ORM) library for Node.js.
81 lines • 3.58 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataSource = void 0;
require("reflect-metadata");
const internalStorage_1 = require("./internalStorage");
const Repository_1 = require("./repository/Repository");
class DataSource {
constructor({ client, entities, sync, createIndexes }) {
this.schemas = [];
this.entities = [];
for (let entity of entities) {
const schema = this.createSchema(entity);
internalStorage_1.internalStorage.pushEntityDefinition(entity, {
connection: client, schema
});
this.schemas.push(schema);
}
this.client = client;
this.entities = entities;
this.createIndexes = createIndexes;
this.synchronizeSchemas = sync;
}
createSchema(entity) {
const entityWorkspace = internalStorage_1.internalStorage.getEntityWorkspace(entity);
if (!entityWorkspace.entityOptions)
throw new Error(`Entity is not specified`);
const { entityOptions, propertyMetadata } = entityWorkspace;
const properties = {};
for (let { propertyName, options } of propertyMetadata) {
if (entityOptions.type === "HASH")
//@ts-ignore
properties[propertyName] = Object.fromEntries(Object.entries(options).map(([key, value]) => key == "type" ? [key, value.toUpperCase()] : [key.toUpperCase(), value]));
else
throw new Error(`Entity type: ${entityOptions.type} is not supported in this time.`);
}
return {
name: entityOptions.name,
index: `idx:${entityOptions.name}`,
properties,
options: {
ON: entityOptions.type,
PREFIX: `${entityOptions.name}:`
}
};
}
createIndex(schema) {
return __awaiter(this, void 0, void 0, function* () {
console.log(schema);
let index = yield this.client.ft.info(schema.index).catch(_ => _);
if (index && !this.synchronizeSchemas)
return;
yield this.client.ft.dropIndex(schema.index).catch(_ => _);
yield this.client.ft.create(
//@ts-ignore
schema.index, schema.properties, schema.options);
console.log(`Created Index: ${schema.index}`);
});
}
initialize() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.client.isReady)
throw new Error("Redis client not init");
if (this.createIndexes)
yield Promise.all(this.schemas.map(schema => this.createIndex(schema)));
});
}
getRepository(entity) {
return (0, Repository_1.getRepository)(entity);
}
}
exports.DataSource = DataSource;
//# sourceMappingURL=DataSource.js.map