Initial content
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.BaseRepo = void 0;
|
||||
const sequelize_1 = require("sequelize");
|
||||
const config_1 = require("../../config");
|
||||
const models_1 = require("./models");
|
||||
const fs_1 = require("fs");
|
||||
const config = (0, config_1.getConfig)("catalog:orm_repo");
|
||||
const logging = config.logging
|
||||
? { logging: console.log, logQueryParameters: true }
|
||||
: { logging: false };
|
||||
class BaseRepo {
|
||||
sequelize;
|
||||
constructor() {
|
||||
this.sequelize = new sequelize_1.Sequelize({ ...config.settings, ...logging });
|
||||
this.initModelsAndDatabase();
|
||||
}
|
||||
async initModelsAndDatabase() {
|
||||
(0, models_1.initializeModels)(this.sequelize);
|
||||
if (config.reset_db) {
|
||||
await this.sequelize.drop();
|
||||
await this.sequelize.sync();
|
||||
await this.addSeedData();
|
||||
}
|
||||
else {
|
||||
await this.sequelize.sync();
|
||||
}
|
||||
}
|
||||
async addSeedData() {
|
||||
const data = JSON.parse((0, fs_1.readFileSync)(config.seed_file).toString());
|
||||
await this.sequelize.transaction(async (transaction) => {
|
||||
await models_1.SupplierModel.bulkCreate(data.suppliers, { transaction });
|
||||
await models_1.CategoryModel.bulkCreate(data.categories, { transaction });
|
||||
await models_1.ProductModel.bulkCreate(data.products, { transaction });
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.BaseRepo = BaseRepo;
|
||||
@@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CatalogRepoImpl = void 0;
|
||||
const core_1 = require("./core");
|
||||
const queries_1 = require("./queries");
|
||||
const storage_1 = require("./storage");
|
||||
const RepoWithQueries = (0, queries_1.AddQueries)(core_1.BaseRepo);
|
||||
const CompleteRepo = (0, storage_1.AddStorage)(RepoWithQueries);
|
||||
exports.CatalogRepoImpl = CompleteRepo;
|
||||
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.initializeCatalogModels = void 0;
|
||||
const sequelize_1 = require("sequelize");
|
||||
const catalog_models_1 = require("./catalog_models");
|
||||
const primaryKey = {
|
||||
id: { type: sequelize_1.DataTypes.INTEGER, autoIncrement: true, primaryKey: true }
|
||||
};
|
||||
const initializeCatalogModels = (sequelize) => {
|
||||
catalog_models_1.ProductModel.init({
|
||||
...primaryKey,
|
||||
name: { type: sequelize_1.DataTypes.STRING },
|
||||
description: { type: sequelize_1.DataTypes.STRING },
|
||||
price: { type: sequelize_1.DataTypes.DECIMAL(10, 2) }
|
||||
}, { sequelize });
|
||||
catalog_models_1.CategoryModel.init({
|
||||
...primaryKey,
|
||||
name: { type: sequelize_1.DataTypes.STRING }
|
||||
}, { sequelize });
|
||||
catalog_models_1.SupplierModel.init({
|
||||
...primaryKey,
|
||||
name: { type: sequelize_1.DataTypes.STRING }
|
||||
}, { sequelize });
|
||||
catalog_models_1.ProductModel.belongsTo(catalog_models_1.CategoryModel, { foreignKey: "categoryId", as: "category" });
|
||||
catalog_models_1.ProductModel.belongsTo(catalog_models_1.SupplierModel, { foreignKey: "supplierId", as: "supplier" });
|
||||
catalog_models_1.CategoryModel.hasMany(catalog_models_1.ProductModel, { foreignKey: "categoryId", as: "products" });
|
||||
catalog_models_1.SupplierModel.hasMany(catalog_models_1.ProductModel, { foreignKey: "supplierId", as: "products" });
|
||||
};
|
||||
exports.initializeCatalogModels = initializeCatalogModels;
|
||||
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.SupplierModel = exports.CategoryModel = exports.ProductModel = void 0;
|
||||
const sequelize_1 = require("sequelize");
|
||||
class ProductModel extends sequelize_1.Model {
|
||||
}
|
||||
exports.ProductModel = ProductModel;
|
||||
class CategoryModel extends sequelize_1.Model {
|
||||
}
|
||||
exports.CategoryModel = CategoryModel;
|
||||
class SupplierModel extends sequelize_1.Model {
|
||||
}
|
||||
exports.SupplierModel = SupplierModel;
|
||||
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.initializeModels = exports.SupplierModel = exports.CategoryModel = exports.ProductModel = void 0;
|
||||
const catalog_helpers_1 = require("./catalog_helpers");
|
||||
var catalog_models_1 = require("./catalog_models");
|
||||
Object.defineProperty(exports, "ProductModel", { enumerable: true, get: function () { return catalog_models_1.ProductModel; } });
|
||||
Object.defineProperty(exports, "CategoryModel", { enumerable: true, get: function () { return catalog_models_1.CategoryModel; } });
|
||||
Object.defineProperty(exports, "SupplierModel", { enumerable: true, get: function () { return catalog_models_1.SupplierModel; } });
|
||||
const initializeModels = (sequelize) => {
|
||||
(0, catalog_helpers_1.initializeCatalogModels)(sequelize);
|
||||
};
|
||||
exports.initializeModels = initializeModels;
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AddQueries = void 0;
|
||||
const models_1 = require("./models");
|
||||
function AddQueries(Base) {
|
||||
return class extends Base {
|
||||
getProducts() {
|
||||
return models_1.ProductModel.findAll({
|
||||
include: [
|
||||
{ model: models_1.SupplierModel, as: "supplier" },
|
||||
{ model: models_1.CategoryModel, as: "category" }
|
||||
],
|
||||
raw: true, nest: true
|
||||
});
|
||||
}
|
||||
getCategories() {
|
||||
return models_1.CategoryModel.findAll({
|
||||
raw: true, nest: true
|
||||
});
|
||||
}
|
||||
getSuppliers() {
|
||||
return models_1.SupplierModel.findAll({
|
||||
raw: true, nest: true
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
exports.AddQueries = AddQueries;
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AddStorage = void 0;
|
||||
const models_1 = require("./models");
|
||||
function AddStorage(Base) {
|
||||
return class extends Base {
|
||||
storeProduct(p) {
|
||||
return this.sequelize.transaction(async (transaction) => {
|
||||
if (p.category) {
|
||||
p.category = await this.storeCategory(p.category);
|
||||
}
|
||||
if (p.supplier) {
|
||||
p.supplier = await this.storeSupplier(p.supplier);
|
||||
}
|
||||
const [stored] = await models_1.ProductModel.upsert({
|
||||
id: p.id, name: p.name, description: p.description,
|
||||
price: p.price, categoryId: p.category?.id,
|
||||
supplierId: p.supplier?.id
|
||||
}, { transaction });
|
||||
return stored;
|
||||
});
|
||||
}
|
||||
async storeCategory(c, transaction) {
|
||||
const [stored] = await models_1.CategoryModel.upsert({
|
||||
id: c.id, name: c.name
|
||||
}, { transaction });
|
||||
return stored;
|
||||
}
|
||||
async storeSupplier(s, transaction) {
|
||||
const [stored] = await models_1.SupplierModel.upsert({
|
||||
id: s.id, name: s.name
|
||||
}, { transaction });
|
||||
return stored;
|
||||
}
|
||||
};
|
||||
}
|
||||
exports.AddStorage = AddStorage;
|
||||
Reference in New Issue
Block a user