@flutry/sequelize 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +12 -0
- package/dist/main.d.ts +5 -0
- package/dist/main.js +12 -0
- package/dist/models/models.d.ts +8 -0
- package/dist/models/models.js +101 -0
- package/dist/utils/connect.d.ts +8 -0
- package/dist/utils/connect.js +35 -0
- package/dist/utils/databaseWrap.d.ts +9 -0
- package/dist/utils/databaseWrap.js +34 -0
- package/dist/utils/function.d.ts +5 -0
- package/dist/utils/function.js +42 -0
- package/dist/utils/models.d.ts +5 -0
- package/dist/utils/models.js +34 -0
- package/package.json +22 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @flutry/sequelize
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
A fully customizable sequelize integration for Flutry projects, with automatic model loading, database connection, and support for advanced features like associations and hooks.
|
|
6
|
+
|
|
7
|
+
## 🔧 Features
|
|
8
|
+
|
|
9
|
+
- 🔌 Automatically connects to the database using Flutry configuration.
|
|
10
|
+
- 📦 Dynamically loads all sequelize models from the `models/` directory.
|
|
11
|
+
- 🛠️ Built-in support for model synchronization.
|
|
12
|
+
- 🔁 Integrates smoothly with the Flutry dynamic adapters system.
|
|
13
|
+
- 🔍 Enhanced error handling and logging for development.
|
|
14
|
+
|
|
15
|
+
## Access Database Type
|
|
16
|
+
|
|
17
|
+
- mysql
|
|
18
|
+
- mariadb
|
|
19
|
+
|
|
20
|
+
## 📄 Licenc
|
|
21
|
+
|
|
22
|
+
MIT
|
|
23
|
+
|
|
24
|
+
## 🌐 Website
|
|
25
|
+
|
|
26
|
+
The official website of the package: [https://flutry.com/docs/package/sequelize](https://flutry.com/docs/package/sequelize)
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FlutryDatabase = exports.Sequelize = exports.DataTypes = exports.Model = exports.Flutry_Sequelize = void 0;
|
|
4
|
+
//!EXPORTS
|
|
5
|
+
var main_1 = require("./main");
|
|
6
|
+
Object.defineProperty(exports, "Flutry_Sequelize", { enumerable: true, get: function () { return main_1.Flutry_Sequelize; } });
|
|
7
|
+
var sequelize_1 = require("sequelize");
|
|
8
|
+
Object.defineProperty(exports, "Model", { enumerable: true, get: function () { return sequelize_1.Model; } });
|
|
9
|
+
Object.defineProperty(exports, "DataTypes", { enumerable: true, get: function () { return sequelize_1.DataTypes; } });
|
|
10
|
+
Object.defineProperty(exports, "Sequelize", { enumerable: true, get: function () { return sequelize_1.Sequelize; } });
|
|
11
|
+
var databaseWrap_1 = require("./utils/databaseWrap");
|
|
12
|
+
Object.defineProperty(exports, "FlutryDatabase", { enumerable: true, get: function () { return databaseWrap_1.FlutryDatabase; } });
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Flutry_Sequelize = void 0;
|
|
4
|
+
require("dotenv/config");
|
|
5
|
+
const connect_1 = require("./utils/connect");
|
|
6
|
+
class Flutry_Sequelize {
|
|
7
|
+
constructor(logger) {
|
|
8
|
+
Flutry_Sequelize.logger = logger;
|
|
9
|
+
new connect_1.Flutry_Connect();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.Flutry_Sequelize = Flutry_Sequelize;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.Flutry_Models = void 0;
|
|
40
|
+
const main_1 = require("../main");
|
|
41
|
+
const connect_1 = require("../utils/connect");
|
|
42
|
+
const fs_1 = __importDefault(require("fs"));
|
|
43
|
+
const path_1 = __importDefault(require("path"));
|
|
44
|
+
const function_1 = require("../utils/function");
|
|
45
|
+
class Flutry_Models {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.functions = new function_1.Functions();
|
|
48
|
+
this.models = {};
|
|
49
|
+
this.init = async () => {
|
|
50
|
+
await this.initModels(path_1.default.join(process.cwd(), `${(await this.functions.isDevelompent()) ? 'src/models' : 'dist/models'}`), '');
|
|
51
|
+
// Asszociációk beállítása
|
|
52
|
+
for (const modelName of Object.keys(this.models)) {
|
|
53
|
+
const model = this.models[modelName];
|
|
54
|
+
if (typeof model.associate === 'function') {
|
|
55
|
+
model.associate(this.models);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
await connect_1.Flutry_Connect.sequelize.sync({ alter: true });
|
|
60
|
+
main_1.Flutry_Sequelize.logger.info('All models were synchronized successfully.');
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
main_1.Flutry_Sequelize.logger.error('Unable to synchronize models:', error);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
this.initModels = async (dir, basePath) => {
|
|
67
|
+
if (connect_1.Flutry_Connect.sequelize) {
|
|
68
|
+
const files = await fs_1.default.readdirSync(dir);
|
|
69
|
+
for (const file of files) {
|
|
70
|
+
const filePath = path_1.default.join(dir, file);
|
|
71
|
+
const stat = await fs_1.default.statSync(filePath);
|
|
72
|
+
if (stat.isDirectory()) {
|
|
73
|
+
await this.initModels(filePath, path_1.default.join(basePath, file));
|
|
74
|
+
}
|
|
75
|
+
else if (file.endsWith('.model.ts') || file.endsWith('.model.js')) {
|
|
76
|
+
await this.loadModel(filePath);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
this.loadModel = async (filePath) => {
|
|
82
|
+
try {
|
|
83
|
+
const module = await Promise.resolve(`${filePath}`).then(s => __importStar(require(s)));
|
|
84
|
+
const ModelClass = module.default;
|
|
85
|
+
if (ModelClass && typeof ModelClass.initialize === 'function') {
|
|
86
|
+
ModelClass.initialize(connect_1.Flutry_Connect.sequelize);
|
|
87
|
+
// Model neve pl. a class neve vagy fájlnév alapján
|
|
88
|
+
this.models[ModelClass.name] = ModelClass;
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
main_1.Flutry_Sequelize.logger.warn(`Model at ${filePath} does not have an initialize method.`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
main_1.Flutry_Sequelize.logger.error(`Failed to load model at ${filePath}`, error);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
this.init();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.Flutry_Models = Flutry_Models;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Flutry_Connect = void 0;
|
|
4
|
+
const sequelize_1 = require("sequelize");
|
|
5
|
+
const main_1 = require("../main");
|
|
6
|
+
const function_1 = require("./function");
|
|
7
|
+
const models_1 = require("../models/models");
|
|
8
|
+
class Flutry_Connect {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.functions = new function_1.Functions();
|
|
11
|
+
this.init = async () => {
|
|
12
|
+
if (await this.functions.DBConfigVerify()) {
|
|
13
|
+
this.connect();
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
this.connect = async () => {
|
|
17
|
+
Flutry_Connect.sequelize = new sequelize_1.Sequelize(`${process.env.DB_NAME}`, `${process.env.DB_USER}`, `${process.env.DB_PASS}`, {
|
|
18
|
+
host: `${process.env.DB_HOST}`,
|
|
19
|
+
dialect: `${process.env.DB_TYPE}`,
|
|
20
|
+
logging: false,
|
|
21
|
+
timezone: await await this.functions.getTimeZone(),
|
|
22
|
+
});
|
|
23
|
+
try {
|
|
24
|
+
await Flutry_Connect.sequelize.authenticate();
|
|
25
|
+
//Flutry_Sequelize.logger.info('Connection has been established successfully'); //! This line commented for now
|
|
26
|
+
await new models_1.Flutry_Models();
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
main_1.Flutry_Sequelize.logger.error('Unebale to connect to the database');
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
this.init();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.Flutry_Connect = Flutry_Connect;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Model, ModelStatic, FindOptions, CreateOptions, UpdateOptions, DestroyOptions, Utils } from 'sequelize';
|
|
2
|
+
export declare class FlutryDatabase {
|
|
3
|
+
static findAll<T extends Model>(model: ModelStatic<T>, options?: FindOptions): Promise<object[]>;
|
|
4
|
+
static findOne<T extends Model>(model: ModelStatic<T>, options?: FindOptions): Promise<object | null>;
|
|
5
|
+
static create<T extends Model>(model: ModelStatic<T>, values: Utils.MakeNullishOptional<T['_creationAttributes']>, options?: CreateOptions<T['_creationAttributes']>): Promise<object>;
|
|
6
|
+
static update<T extends Model>(model: ModelStatic<T>, values: Partial<T['_creationAttributes']>, options: UpdateOptions<T['_creationAttributes']>): Promise<object[]>;
|
|
7
|
+
static destroy<T extends Model>(model: ModelStatic<T>, options: DestroyOptions<T['_creationAttributes']>): Promise<number>;
|
|
8
|
+
static count<T extends Model>(model: ModelStatic<T>, options?: FindOptions): Promise<number>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FlutryDatabase = void 0;
|
|
4
|
+
class FlutryDatabase {
|
|
5
|
+
static async findAll(model, options) {
|
|
6
|
+
const results = await model.findAll(options);
|
|
7
|
+
return results.map((row) => row.get({ plain: true }));
|
|
8
|
+
}
|
|
9
|
+
static async findOne(model, options) {
|
|
10
|
+
const result = await model.findOne(options);
|
|
11
|
+
return result ? result.get({ plain: true }) : null;
|
|
12
|
+
}
|
|
13
|
+
static async create(model, values, options) {
|
|
14
|
+
const result = await model.create(values, options);
|
|
15
|
+
return result.get({ plain: true });
|
|
16
|
+
}
|
|
17
|
+
static async update(model, values, options) {
|
|
18
|
+
const result = await model.update(values, { ...options, returning: true });
|
|
19
|
+
if (Array.isArray(result)) {
|
|
20
|
+
const [count, updatedRows] = result;
|
|
21
|
+
if (Array.isArray(updatedRows)) {
|
|
22
|
+
return updatedRows.map((row) => row.get({ plain: true }));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
static async destroy(model, options) {
|
|
28
|
+
return await model.destroy(options);
|
|
29
|
+
}
|
|
30
|
+
static async count(model, options) {
|
|
31
|
+
return await model.count(options);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.FlutryDatabase = FlutryDatabase;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Functions = void 0;
|
|
4
|
+
const main_1 = require("../main");
|
|
5
|
+
class Functions {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.DBConfigVerify = async () => {
|
|
8
|
+
const requiredEnvVars = ['DB_NAME', 'DB_USER', 'DB_PASS', 'DB_HOST', 'DB_TYPE'];
|
|
9
|
+
const missingVars = requiredEnvVars.filter((key) => !process.env[key]);
|
|
10
|
+
if (missingVars.length > 0) {
|
|
11
|
+
missingVars.forEach((key) => {
|
|
12
|
+
main_1.Flutry_Sequelize.logger.error(`${key} is not defined`);
|
|
13
|
+
});
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
const validDbTypes = ['mysql', 'mariadb'];
|
|
17
|
+
if (!validDbTypes.includes(process.env.DB_TYPE)) {
|
|
18
|
+
main_1.Flutry_Sequelize.logger.error(`DB_TYPE must be one of: ${validDbTypes.join(', ')}`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
};
|
|
23
|
+
this.getTimeZone = async () => {
|
|
24
|
+
const date = new Date();
|
|
25
|
+
const offsetMinutes = date.getTimezoneOffset();
|
|
26
|
+
const sign = offsetMinutes > 0 ? '-' : '+';
|
|
27
|
+
const offsetHours = String(Math.abs(Math.floor(offsetMinutes / 60))).padStart(2, '0');
|
|
28
|
+
const offsetMinutesFormatted = String(Math.abs(offsetMinutes % 60)).padStart(2, '0');
|
|
29
|
+
const offset = `${sign}${offsetHours}:${offsetMinutesFormatted}`;
|
|
30
|
+
return offset;
|
|
31
|
+
};
|
|
32
|
+
this.isDevelompent = async () => {
|
|
33
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.Functions = Functions;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Flutry_Models = void 0;
|
|
4
|
+
const sequelize_1 = require("sequelize");
|
|
5
|
+
const connect_1 = require("./connect");
|
|
6
|
+
class Flutry_Models {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.models = [];
|
|
9
|
+
this.initmodels = async () => {
|
|
10
|
+
if (connect_1.Flutry_Connect.sequelize) {
|
|
11
|
+
connect_1.Flutry_Connect.sequelize.define('User', {
|
|
12
|
+
id: {
|
|
13
|
+
type: sequelize_1.DataTypes.INTEGER,
|
|
14
|
+
primaryKey: true,
|
|
15
|
+
autoIncrement: true,
|
|
16
|
+
allowNull: false,
|
|
17
|
+
},
|
|
18
|
+
emailId: {
|
|
19
|
+
type: sequelize_1.DataTypes.STRING,
|
|
20
|
+
unique: true,
|
|
21
|
+
allowNull: false,
|
|
22
|
+
},
|
|
23
|
+
age: {
|
|
24
|
+
type: sequelize_1.DataTypes.INTEGER,
|
|
25
|
+
allowNull: false,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
connect_1.Flutry_Connect.sequelize.sync();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
this.initmodels();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.Flutry_Models = Flutry_Models;
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flutry/sequelize",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"dotenv": "^16.5.0",
|
|
15
|
+
"mariadb": "^3.4.2",
|
|
16
|
+
"mysql2": "^3.14.1",
|
|
17
|
+
"sequelize": "^6.37.7"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"typescript": "^5.0.0"
|
|
21
|
+
}
|
|
22
|
+
}
|