Creación de tablas y vista para catálogo
This commit is contained in:
parent
25b68d7645
commit
844cb438a7
@ -9,7 +9,7 @@ import { trace } from "console";
|
||||
import { config } from "../../config";
|
||||
import { app } from "../express/app";
|
||||
import { initLogger } from "../logger";
|
||||
import { insertUsers } from "../sequelize/initData";
|
||||
import { initStructure, insertUsers } from "../sequelize";
|
||||
|
||||
process.env.TZ = "UTC";
|
||||
Settings.defaultLocale = "es-ES";
|
||||
@ -107,8 +107,7 @@ const server: http.Server = http
|
||||
try {
|
||||
//firebirdConn.sync().then(() => {
|
||||
sequelizeConn.sync({ force: false, alter: true }).then(() => {
|
||||
//
|
||||
|
||||
initStructure(sequelizeConn);
|
||||
insertUsers();
|
||||
|
||||
// Launch server
|
||||
|
||||
2
server/src/infrastructure/sequelize/index.ts
Normal file
2
server/src/infrastructure/sequelize/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./initData";
|
||||
export * from "./initStructure";
|
||||
143
server/src/infrastructure/sequelize/initStructure.ts
Normal file
143
server/src/infrastructure/sequelize/initStructure.ts
Normal file
@ -0,0 +1,143 @@
|
||||
const createCatalogTableIfNotExists = async (sequelize) => {
|
||||
try {
|
||||
// Consulta para verificar si la tabla existe
|
||||
const checkTableQuery = `
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'catalog';
|
||||
`;
|
||||
|
||||
const [result] = await sequelize.query(checkTableQuery);
|
||||
const tableExists = result[0]["COUNT(*)"] > 0;
|
||||
|
||||
if (!tableExists) {
|
||||
// Consulta para crear la tabla si no existe
|
||||
const createTableQuery = `
|
||||
CREATE TABLE catalog (
|
||||
id char(36) COLLATE utf8mb4_bin NOT NULL,
|
||||
catalog_name varchar(255) NOT NULL,
|
||||
id_article bigint UNSIGNED NOT NULL,
|
||||
reference varchar(255),
|
||||
points bigint UNSIGNED DEFAULT 0,
|
||||
retail_price bigint,
|
||||
created_at datetime NOT NULL,
|
||||
updated_at datetime NOT NULL,
|
||||
deleted_at datetime,
|
||||
/* Keys */
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE = InnoDB
|
||||
COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
CREATE INDEX catalog_name_idx
|
||||
ON catalog (catalog_name);
|
||||
|
||||
CREATE INDEX id_article_idx
|
||||
ON catalog (id_article);
|
||||
|
||||
CREATE INDEX updated_at_idx
|
||||
ON catalog (updated_at);
|
||||
`;
|
||||
|
||||
await sequelize.query(createTableQuery);
|
||||
console.log('Tabla "catalog" creada con éxito.');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error al crear la tabla "catalog":', error);
|
||||
}
|
||||
};
|
||||
|
||||
const createCatalogTranslationsTableIfNotExists = async (sequelize) => {
|
||||
try {
|
||||
// Consulta para verificar si la tabla existe
|
||||
const checkTableQuery = `
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'catalog_translations';
|
||||
`;
|
||||
|
||||
const [result] = await sequelize.query(checkTableQuery);
|
||||
const tableExists = result[0]["COUNT(*)"] > 0;
|
||||
|
||||
if (!tableExists) {
|
||||
// Consulta para crear la tabla si no existe
|
||||
const createTableQuery = `
|
||||
CREATE TABLE catalog_translations (
|
||||
id varchar(255) NOT NULL,
|
||||
lang_code varchar(2) NOT NULL DEFAULT 'es',
|
||||
description varchar(255),
|
||||
catalog_id char(36) COLLATE utf8mb4_bin,
|
||||
/* Keys */
|
||||
PRIMARY KEY (id),
|
||||
/* Foreign keys */
|
||||
CONSTRAINT catalog_translations_ibfk_1
|
||||
FOREIGN KEY (catalog_id)
|
||||
REFERENCES catalog(id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE
|
||||
) ENGINE = InnoDB;
|
||||
|
||||
CREATE INDEX catalog_id
|
||||
ON catalog_translations
|
||||
(catalog_id);
|
||||
|
||||
CREATE INDEX lang_code_idx
|
||||
ON catalog_translations
|
||||
(lang_code);
|
||||
`;
|
||||
|
||||
await sequelize.query(createTableQuery);
|
||||
console.log('Tabla "catalog_translations" creada con éxito.');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error al crear la tabla "catalog_translations":', error);
|
||||
}
|
||||
};
|
||||
|
||||
const createVCatalogViewIfNotExists = async (sequelize) => {
|
||||
try {
|
||||
// Consulta para verificar si la tabla existe
|
||||
const checkViewQuery = `
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.views
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'v_catalog';
|
||||
`;
|
||||
|
||||
const [result] = await sequelize.query(checkViewQuery);
|
||||
const viewExists = result[0]["COUNT(*)"] > 0;
|
||||
|
||||
if (!viewExists) {
|
||||
// Consulta para crear la tabla si no existe
|
||||
const createViewQuery = `
|
||||
CREATE VIEW v_catalog AS
|
||||
SELECT
|
||||
catalog.id AS id,
|
||||
catalog.catalog_name AS catalog_name,
|
||||
catalog.id_article AS id_article,
|
||||
catalog.reference AS reference,
|
||||
catalog.points AS points,
|
||||
catalog.retail_price AS retail_price,
|
||||
catalog.created_at AS created_at,
|
||||
catalog.updated_at AS updated_at,
|
||||
catalog_translations.lang_code AS lang_code,
|
||||
catalog_translations.description AS description
|
||||
FROM catalog
|
||||
JOIN catalog_translations ON catalog.id = catalog_translations.catalog_id
|
||||
WHERE catalog.deleted_at IS NULL;
|
||||
`;
|
||||
|
||||
await sequelize.query(createViewQuery);
|
||||
console.log('Vista "v_catalog" creada con éxito.');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error al crear la vista "v_catalog":', error);
|
||||
}
|
||||
};
|
||||
|
||||
export const initStructure = async (sequelize) => {
|
||||
createCatalogTableIfNotExists(sequelize);
|
||||
createCatalogTranslationsTableIfNotExists(sequelize);
|
||||
createVCatalogViewIfNotExists(sequelize);
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user