lambdaorm-base
Version:
131 lines • 5.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InitializeSchema = void 0;
const domain_1 = require("../../domain");
class InitializeSchema {
// eslint-disable-next-line no-useless-constructor
constructor(schemaService) {
this.schemaService = schemaService;
}
initialize(schema, args) {
var _a, _b;
let source;
let mapping;
if (schema.infrastructure === undefined) {
mapping = { name: 'default', entities: [] };
source = { name: 'default', dialect: args.dialect || domain_1.Dialect.MySQL, mapping: mapping.name, connection: args.connection };
schema.infrastructure = { sources: [source], mappings: [{ name: 'default', entities: [] }], stages: [] };
}
else {
if (!schema.infrastructure.mappings || schema.infrastructure.mappings.length === 0) {
mapping = { name: 'default', entities: [] };
schema.infrastructure.mappings = [mapping];
}
else {
mapping = schema.infrastructure.mappings[0];
}
if (!schema.infrastructure.sources) {
source = { name: 'default', dialect: args.dialect || domain_1.Dialect.MySQL, mapping: schema.infrastructure.mappings[0].name, connection: args.connection };
schema.infrastructure.sources = [];
}
if (args.source && schema.infrastructure.sources && schema.infrastructure.sources.length > 1) {
source = (_a = schema.infrastructure) === null || _a === void 0 ? void 0 : _a.sources.find(p => p.name === args.source);
if (source === undefined) {
throw Error(`source ${args.source} not found`);
}
}
else if (((_b = schema.infrastructure) === null || _b === void 0 ? void 0 : _b.sources.length) === 1) {
source = schema.infrastructure.sources[0];
}
}
// if the mapping does not exist it creates it
if (source && mapping && schema.infrastructure.mappings) {
if (source.mapping === undefined) {
if (schema.infrastructure && schema.infrastructure.mappings.length > 0) {
source.mapping = schema.infrastructure.mappings[0].name;
}
else {
source.mapping = mapping.name;
}
}
}
// update the connection if applicable
if (source) {
if (args.connection) {
source.connection = args.connection;
}
else if (!source.connection) {
source.connection = this.defaultConnection(source.dialect);
}
}
// if the stage does not exist, create it
if (schema.infrastructure.stages === undefined) {
schema.infrastructure.stages = [];
}
if (source && schema.infrastructure.stages.length === 0) {
schema.infrastructure.stages.push({ name: 'default', sources: [{ name: source.name }] });
}
return schema;
}
defaultConnection(dialect) {
switch (dialect) {
case domain_1.Dialect.MySQL:
return {
host: 'localhost',
port: 3306,
user: 'test',
password: 'test',
database: 'test',
multipleStatements: true,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
};
case domain_1.Dialect.MariaDB:
return {
host: 'localhost',
port: 3306,
user: 'test',
password: 'test',
database: 'test',
multipleStatements: true,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
};
case domain_1.Dialect.SQLjs:
return { database: 'default' };
case domain_1.Dialect.PostgreSQL:
return {
host: 'localhost',
port: 5432,
username: 'test',
password: 'test',
database: 'test'
};
case domain_1.Dialect.SqlServer:
return {
server: 'localhost',
authentication: { type: 'default', options: { userName: 'sa', password: 'Admin12345' } },
options: { encrypt: false, database: 'tempDb' }
};
case domain_1.Dialect.Oracle:
return {
host: 'localhost',
username: 'system',
password: 'oracle',
port: 1521,
sid: 'xe.oracle.docker'
};
case domain_1.Dialect.MongoDB:
return {
url: 'mongodb://@localhost:27017',
database: 'test'
};
default:
throw new Error(`dialect: ${dialect} not supported`);
}
}
}
exports.InitializeSchema = InitializeSchema;
//# sourceMappingURL=initialize.js.map