lambdaorm-base
Version:
176 lines • 7.55 kB
JavaScript
"use strict";
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaState = void 0;
const domain_1 = require("../domain");
const path_1 = __importDefault(require("path"));
class SchemaState {
constructor(source, domain, mapping, stage, view, routeService, facade, loadSchema, helper) {
this.source = source;
this.domain = domain;
this.mapping = mapping;
this.stage = stage;
this.view = view;
this.routeService = routeService;
this.facade = facade;
this.loadSchema = loadSchema;
this.helper = helper;
this.originalSchema = this.facade.create();
this.schema = helper.obj.clone(this.originalSchema);
}
initialize(args) {
return __awaiter(this, void 0, void 0, function* () {
const workspace = args.workspace || process.cwd();
const schemaInfo = yield this.facade.read(workspace);
if (schemaInfo === null) {
this.originalSchema = this.facade.create();
if (yield this.helper.fs.isDirectory(workspace)) {
this.schemaPath = path_1.default.join(workspace, 'lambdaORM.yaml');
}
else {
this.schemaPath = workspace;
}
}
else {
this.originalSchema = schemaInfo.schema;
this.schemaPath = schemaInfo.path;
}
this.originalSchema = this.facade.initialize(this.originalSchema, args);
if (this.schemaPath) {
yield this.facade.write(this.originalSchema, this.schemaPath);
}
if (args.dataPath) {
yield this.helper.fs.create(path_1.default.join(workspace, args.dataPath));
}
this.schema = this.toSchema(this.originalSchema);
return this.schema;
});
}
load(source) {
return __awaiter(this, void 0, void 0, function* () {
if (typeof source === 'string') {
const schemaInfo = yield this.facade.read(source);
if (schemaInfo === null) {
throw new domain_1.SchemaError(`Schema: ${source} not found`);
}
this.originalSchema = schemaInfo.schema;
this.schemaPath = schemaInfo.path;
}
else if (typeof source === 'object') {
this.originalSchema = source;
}
else {
throw new domain_1.SchemaError(`Schema: ${source} not supported`);
}
this.schema = this.toSchema(this.originalSchema);
return this.schema;
});
}
introspect(data, name) {
return __awaiter(this, void 0, void 0, function* () {
const schemaData = this.facade.introspect(this.originalSchema, data, name);
if (this.schemaPath) {
yield this.facade.write(this.originalSchema, this.schemaPath);
}
this.schema = this.toSchema(this.originalSchema);
return schemaData;
});
}
match(mappings_1) {
return __awaiter(this, arguments, void 0, function* (mappings, options = { removeEntities: true, removeProperties: true, removeRelations: true }) {
this.facade.match(this.originalSchema, mappings, options);
if (this.schemaPath) {
yield this.facade.write(this.originalSchema, this.schemaPath);
}
this.schema = this.toSchema(this.originalSchema);
});
}
evalSourceRule(rule, info) {
return this.routeService.eval(rule, info);
}
getSource(info, stage) {
return this.routeService.getSource(info, stage);
}
getSchemaSources() {
var _a, _b;
return ((_b = (_a = this.schema.infrastructure) === null || _a === void 0 ? void 0 : _a.sources) === null || _b === void 0 ? void 0 : _b.map(s => ({ name: s.name, dialect: s.dialect }))) || [];
}
getSchemaSource(source) {
const sources = this.getSchemaSources();
if (sources !== undefined) {
return sources.find(s => s.name === source) || { name: source, dialect: 'unknown' };
}
else {
return { name: source, dialect: 'unknown' };
}
}
getSchemaVersion() {
return { version: this.schema.version || '0.0.0' };
}
getSchemaDomain() {
return this.schema.domain;
}
getSchemaEntities() {
return this.schema.domain.entities || [];
}
getSchemaEntity(entity) {
return this.schema.domain.entities.find(e => e.name === entity);
}
getSchemaEnums() {
return this.schema.domain.enums || [];
}
getSchemaEnum(_enum) {
var _a;
return (_a = this.schema.domain.enums) === null || _a === void 0 ? void 0 : _a.find(e => e.name === _enum);
}
getSchemaMappings() {
var _a;
return ((_a = this.schema.infrastructure) === null || _a === void 0 ? void 0 : _a.mappings) || [];
}
getSchemaMapping(mapping) {
if (this.schema.infrastructure === undefined || this.schema.infrastructure.mappings === undefined) {
return undefined;
}
return this.schema.infrastructure.mappings.find(m => m.name === mapping);
}
getSchemaEntityMapping(mapping, entity) {
var _a, _b;
if (this.schema.infrastructure === undefined || this.schema.infrastructure.mappings === undefined) {
return undefined;
}
return (_b = (_a = this.schema.infrastructure.mappings.find(m => m.name === mapping)) === null || _a === void 0 ? void 0 : _a.entities) === null || _b === void 0 ? void 0 : _b.find(e => e.name === entity);
}
getSchemaStages() {
var _a;
return ((_a = this.schema.infrastructure) === null || _a === void 0 ? void 0 : _a.stages) || [];
}
getSchemaStage(stage) {
var _a;
if (this.schema.infrastructure === undefined || this.schema.infrastructure.stages === undefined) {
return undefined;
}
return (_a = this.schema.infrastructure) === null || _a === void 0 ? void 0 : _a.stages.find(s => s.name === stage);
}
getSchemaViews() {
var _a, _b;
return ((_b = (_a = this.schema.infrastructure) === null || _a === void 0 ? void 0 : _a.views) === null || _b === void 0 ? void 0 : _b.map(p => p.name)) || [];
}
toSchema(originalSchema) {
const _schema = this.helper.obj.clone(originalSchema);
this.facade.complete(_schema);
return this.loadSchema.load(_schema);
}
}
exports.SchemaState = SchemaState;
//# sourceMappingURL=state.js.map