agrs-sequelize-sdk 1.0.5 → 1.0.7

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 CHANGED
@@ -1 +1,179 @@
1
- # agrs-sequelize
1
+ # agrs-sequelize SDK
2
+
3
+ The `agrs-sequelize` SDK provides a structured way to manage your database models using Sequelize ORM. It allows you to easily add, update, and manage models, with support for publishing updates to NPM and pushing changes to GitHub.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Prerequisites](#prerequisites)
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [Adding or Modifying Models](#adding-or-modifying-models)
11
+ - [Model Loading](#model-loading)
12
+ - [Publishing Changes](#publishing-changes)
13
+ - [Using PowerShell (Windows)](#using-powershell-windows)
14
+ - [Using Bash (Linux/macOS)](#using-bash-linuxmacos)
15
+ - [Script Details](#script-details)
16
+ - [run.ps1 (PowerShell Script for Windows)](#runps1-powershell-script-for-windows)
17
+ - [run.sh (Bash Script for Linux/macOS)](#runsh-bash-script-for-linuxmacos)
18
+ - [Example Model File](#example-model-file)
19
+ - [Versioning](#versioning)
20
+ - [Troubleshooting](#troubleshooting)
21
+ - [License](#license)
22
+
23
+ ## Prerequisites
24
+
25
+ Ensure the following dependencies are installed on your system:
26
+
27
+ - **Node.js**: [Download Node.js](https://nodejs.org/)
28
+ - **Sequelize**: Used for database interaction.
29
+ - **Git**: Required for version control and publishing changes to GitHub.
30
+ - **npm**: For managing and publishing packages.
31
+ - **PowerShell** (Windows) or **Bash** (Linux/macOS): For running automation scripts.
32
+
33
+ ## Installation
34
+
35
+ 1. **Clone the Repository:**
36
+
37
+ ```bash
38
+ git clone https://github.com/your-repo/agrs-sequelize.git
39
+ cd agrs-sequelize
40
+ ```
41
+
42
+ 2. **Install Dependencies:**
43
+
44
+ ```bash
45
+ npm install
46
+ ```
47
+
48
+
49
+ # Usage
50
+
51
+ ## Adding or Modifying Models
52
+ To add or modify models in the agrs-sequelize SDK:
53
+
54
+ 1. **Create or Edit a Model:**
55
+
56
+ - Add a new model file inside the models directory or edit an existing one.
57
+ - Each model file should export a function that initializes the model with Sequelize.
58
+
59
+ ```javascript
60
+ // models/YourModel.js
61
+ module.exports = (sequelize, DataTypes) => {
62
+ const YourModel = sequelize.define('YourModel', {
63
+ fieldName: {
64
+ type: DataTypes.STRING,
65
+ allowNull: false,
66
+ },
67
+ });
68
+
69
+ // Define associations (optional)
70
+ YourModel.associate = (models) => {
71
+ YourModel.hasMany(models.OtherModel);
72
+ };
73
+
74
+ return YourModel;
75
+ };
76
+ ```
77
+
78
+ 2. **Define Associations (if required):**
79
+
80
+ Use the associate method to define relationships with other models.
81
+ Associations like hasMany, belongsTo, etc., are supported.
82
+ ### Model Loading
83
+ Models are automatically loaded when initializing Sequelize.
84
+ The index.js file ensures all models in the models directory are properly loaded and associated.
85
+
86
+ ## Publishing Changes
87
+ After modifying or adding models, publish the changes to NPM and GitHub.
88
+
89
+ ### Using PowerShell (Windows)
90
+ Open PowerShell as Administrator.
91
+
92
+ Run the Script:
93
+
94
+ ```powershell
95
+ ./run.ps1
96
+ ```
97
+ The script will:
98
+
99
+ - Check for jq installation.
100
+ - Bump the version in package.json.
101
+ - Publish to NPM.
102
+ - Commit and push changes to GitHub.
103
+
104
+ ### Using Bash (Linux/macOS)
105
+ Open a Bash Terminal.
106
+
107
+ Run the Script:
108
+
109
+ ```bash
110
+ ./run.sh
111
+ ```
112
+ The script performs the same tasks as the PowerShell script, tailored for Bash environments.
113
+
114
+ ## Script Details
115
+ ### run.ps1 (PowerShell Script for Windows)
116
+ Automates the publishing process:
117
+
118
+ - Ensures jq is installed.
119
+ - Increments the version in package.json.
120
+ - Publishes the package to NPM.
121
+ - Commits and pushes changes to GitHub.
122
+
123
+ ### run.sh (Bash Script for Linux/macOS)
124
+ Provides similar functionality:
125
+
126
+ - Checks for jq installation.
127
+ - Bumps the package version.
128
+ = Publishes to NPM.
129
+ = Commits and pushes to GitHub.
130
+
131
+ ## Example Model File
132
+ ```javascript
133
+ Copy code
134
+ // models/YourModel.js
135
+ module.exports = (sequelize, DataTypes) => {
136
+ const YourModel = sequelize.define('YourModel', {
137
+ name: {
138
+ type: DataTypes.STRING,
139
+ allowNull: false,
140
+ },
141
+ });
142
+
143
+ // Define associations (optional)
144
+ YourModel.associate = (models) => {
145
+ YourModel.hasMany(models.OtherModel);
146
+ };
147
+
148
+ return YourModel;
149
+ };
150
+ ```
151
+ In this example:
152
+
153
+ YourModel has a name field.
154
+ It has a hasMany association with OtherModel.
155
+ ## Versioning
156
+ Versioning is handled automatically:
157
+
158
+ - The scripts increment the version in package.json.
159
+ - Follows semantic versioning (major, minor, patch).
160
+ - Updated versions are published to NPM and committed to the repository.
161
+
162
+ ## Troubleshooting
163
+ - ### Permission Issues:
164
+
165
+ - Linux/macOS: Ensure scripts have execute permissions (chmod +x run.sh).
166
+ - ### NPM Login Errors:
167
+
168
+ - The script will prompt for NPM login if not already logged in.
169
+ - Verify NPM account credentials.
170
+
171
+ - ### Missing Dependencies:
172
+
173
+ - The script checks for jq and installs it if missing.
174
+ ## License
175
+ This project is licensed under the MIT License. See the LICENSE file for details.
176
+
177
+
178
+
179
+
package/models/Folders.js CHANGED
@@ -1,37 +1,116 @@
1
+ const Files = require('./Files');
1
2
  module.exports = (sequelize, DataTypes) => {
2
- const Folders = sequelize.define("Folders", {
3
- id: {
4
- type: DataTypes.STRING,
5
- primaryKey: true,
6
- },
7
- ParentFolder: {
8
- type: DataTypes.STRING, // Self-referencing folder ID
9
- allowNull: true,
10
- },
11
- ParentFolders: {
12
- type: DataTypes.ARRAY(DataTypes.STRING), // Array of folder IDs
13
- allowNull: true,
14
- },
15
- name: {
16
- type: DataTypes.STRING,
17
- allowNull: false,
18
- },
19
- FilesCount: {
20
- type: DataTypes.INTEGER,
21
- allowNull: true,
22
- defaultValue: 0,
3
+ const Folders = sequelize.define("Folders", {
4
+ id: {
5
+ type: DataTypes.STRING,
6
+ primaryKey: true,
7
+ },
8
+ ParentFolder: {
9
+ type: DataTypes.STRING, // Self-referencing folder ID
10
+ allowNull: true,
11
+ },
12
+ ParentFolders: {
13
+ type: DataTypes.ARRAY(DataTypes.STRING), // Array of folder IDs
14
+ allowNull: true,
15
+ },
16
+ name: {
17
+ type: DataTypes.STRING,
18
+ allowNull: false,
19
+ },
20
+ FilesCount: {
21
+ type: DataTypes.INTEGER,
22
+ allowNull: true,
23
+ defaultValue: 0,
24
+ }
25
+ });
26
+
27
+ Folders.associate = function (models) {
28
+ // One-to-many: A folder can have many files
29
+ Folders.hasMany(models.Files, { foreignKey: "ParentFolder", sourceKey: "id" });
30
+
31
+ // Self-referencing: A folder can have subfolders
32
+ Folders.hasMany(models.Folders, { foreignKey: "ParentFolder", sourceKey: "id" });
33
+ Folders.belongsTo(models.Folders, { foreignKey: "ParentFolder", targetKey: "id" });
34
+ };
35
+
36
+ // Function to count files and subfolders for each parent folder in ParentFolders array
37
+ Folders.countFilesAndSubfolders = async function (parentFolderIds) {
38
+ try {
39
+ // Loop through each parent folder ID
40
+ for (const parentFolderId of parentFolderIds) {
41
+ // Find the parent folder by ID
42
+ const parentFolder = await Folders.findOne({ where: { id: parentFolderId } });
43
+
44
+ if (!parentFolder) {
45
+ console.warn(`Folder with ID ${parentFolderId} not found.`);
46
+ continue;
47
+ }
48
+
49
+ // Count the number of files directly under this folder
50
+ const fileCount = await Files.count({
51
+ where: { ParentFolder: parentFolder.id }
52
+ });
53
+
54
+ // Count the number of subfolders directly under this folder
55
+ const subfolderCount = await Folders.count({
56
+ where: { ParentFolder: parentFolder.id }
57
+ });
58
+
59
+ // Total count is files + subfolders
60
+ const totalFilesCount = fileCount + subfolderCount;
61
+
62
+ // Update the FilesCount field in the parent folder
63
+ await parentFolder.update({ FilesCount: totalFilesCount });
23
64
  }
24
- });
25
-
26
- Folders.associate = function (models) {
27
- // One-to-many: A folder can have many files
28
- Folders.hasMany(models.Files, { foreignKey: "ParentFolder", sourceKey: "id" });
29
-
30
- // Self-referencing: A folder can have subfolders
31
- Folders.hasMany(models.Folders, { foreignKey: "ParentFolder", sourceKey: "id" });
32
- Folders.belongsTo(models.Folders, { foreignKey: "ParentFolder", targetKey: "id" });
33
- };
34
-
35
- return Folders;
65
+ } catch (error) {
66
+ console.error(error);
67
+ throw error;
68
+ }
36
69
  };
37
-
70
+
71
+ // Add a hook that triggers after a folder is created, updated, or deleted
72
+ Folders.addHook('afterCreate', async (folder, options) => {
73
+ if (folder.ParentFolders && folder.ParentFolders.length > 0) {
74
+ await Folders.countFilesAndSubfolders(folder.ParentFolders);
75
+ }
76
+ });
77
+
78
+ Folders.addHook('afterDestroy', async (folder, options) => {
79
+ if (folder.ParentFolders && folder.ParentFolders.length > 0) {
80
+ await Folders.countFilesAndSubfolders(folder.ParentFolders);
81
+ }
82
+ });
83
+
84
+ Folders.addHook('afterUpdate', async (folder, options) => {
85
+ if (folder.ParentFolders && folder.ParentFolders.length > 0) {
86
+ await Folders.countFilesAndSubfolders(folder.ParentFolders);
87
+ }
88
+ });
89
+
90
+ // Add hooks for Files
91
+ Files.addHook('afterCreate', async (file, options) => {
92
+ if (file.ParentFolders && file.ParentFolders.length > 0) {
93
+ await Folders.countFilesAndSubfolders(file.ParentFolders);
94
+ } else if (file.ParentFolder) {
95
+ await Folders.countFilesAndSubfolders([file.ParentFolder]);
96
+ }
97
+ });
98
+
99
+ Files.addHook('afterDestroy', async (file, options) => {
100
+ if (file.ParentFolders && file.ParentFolders.length > 0) {
101
+ await Folders.countFilesAndSubfolders(file.ParentFolders);
102
+ } else if (file.ParentFolder) {
103
+ await Folders.countFilesAndSubfolders([file.ParentFolder]);
104
+ }
105
+ });
106
+
107
+ Files.addHook('afterUpdate', async (file, options) => {
108
+ if (file.ParentFolders && file.ParentFolders.length > 0) {
109
+ await Folders.countFilesAndSubfolders(file.ParentFolders);
110
+ } else if (file.ParentFolder) {
111
+ await Folders.countFilesAndSubfolders([file.ParentFolder]);
112
+ }
113
+ });
114
+
115
+ return Folders;
116
+ };
package/package.json CHANGED
@@ -1 +1,21 @@
1
- {"name":"agrs-sequelize-sdk","version":"1.0.5","main":"index.js","scripts":{"start":"node index.js","sync":"node services/sequelizeService.js","test":"echo \"Error: no test specified\" \u0026\u0026 exit 1"},"keywords":[],"author":"","license":"ISC","description":"","dependencies":{"pg":"^8.13.0","pg-hstore":"^2.3.4","sequelize":"^6.37.4"}}
1
+ {
2
+ "name": "agrs-sequelize-sdk",
3
+ "version": "1.0.7",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "start": "node index.js",
7
+ "sync": "node services/sequelizeService.js",
8
+ "test": "echo \"Error: no test specified\" \u0026\u0026 exit 1"
9
+ },
10
+ "keywords": [
11
+
12
+ ],
13
+ "author": "",
14
+ "license": "ISC",
15
+ "description": "",
16
+ "dependencies": {
17
+ "pg": "^8.13.0",
18
+ "pg-hstore": "^2.3.4",
19
+ "sequelize": "^6.37.4"
20
+ }
21
+ }
package/run.ps1 CHANGED
@@ -72,9 +72,9 @@ if ($patch -eq 99) {
72
72
 
73
73
  $newVersion = "$major.$minor.$patch"
74
74
 
75
- # Update the version in package.json
75
+ # Update the version in package.json while maintaining indentation
76
76
  $packageJson.version = $newVersion
77
- $packageJson | ConvertTo-Json -Compress | Set-Content -Path "package.json"
77
+ $packageJson | ConvertTo-Json -Depth 100 | Set-Content -Path "package.json"
78
78
 
79
79
  Write-Host "Version updated from $currentVersion to $newVersion"
80
80
 
package/run.bat DELETED
@@ -1,105 +0,0 @@
1
- @echo off
2
- setlocal
3
-
4
- :: Check if running as administrator
5
- net session >nul 2>&1
6
- if %ERRORLEVEL% neq 0 (
7
- echo This script must be run as an administrator.
8
- echo Please right-click on the script and select 'Run as Administrator'.
9
- pause
10
- exit /b 1
11
- )
12
-
13
- :: Check if jq is installed
14
- where jq >nul 2>nul
15
- if %ERRORLEVEL% neq 0 (
16
- echo jq not found. Downloading jq manually...
17
- powershell -Command "Invoke-WebRequest -Uri https://github.com/stedolan/jq/releases/download/jq-1.6/jq-win64.exe -OutFile %TEMP%\jq.exe"
18
- if exist %TEMP%\jq.exe (
19
- move /y %TEMP%\jq.exe %SystemRoot%\System32\jq.exe >nul
20
- if %ERRORLEVEL% neq 0 (
21
- echo Failed to move jq to System32. Please run this script as administrator.
22
- exit /b 1
23
- )
24
- echo jq installed manually.
25
- ) else (
26
- echo Failed to download jq. Please install it manually.
27
- exit /b 1
28
- )
29
- )
30
-
31
- :: Step 1: Check if already logged in to NPM
32
- npm whoami >nul 2>nul
33
- if %ERRORLEVEL% neq 0 (
34
- echo Not logged in. Logging in to NPM...
35
- npm login
36
- if %ERRORLEVEL% neq 0 (
37
- echo NPM login failed. Exiting...
38
- exit /b 1
39
- )
40
- ) else (
41
- for /f %%i in ('npm whoami') do set USERNAME=%%i
42
- echo Already logged in to NPM as %USERNAME%
43
- )
44
-
45
- :: Step 2: Increment version in package.json
46
- for /f "tokens=*" %%i in ('jq -r ".version" package.json') do set current_version=%%i
47
-
48
- if "%current_version%"=="" (
49
- echo Could not find version in package.json
50
- exit /b 1
51
- )
52
-
53
- :: Split version into major, minor, patch
54
- for /f "tokens=1-3 delims=." %%a in ("%current_version%") do (
55
- set major=%%a
56
- set minor=%%b
57
- set patch=%%c
58
- )
59
-
60
- :: Increment version numbers with three-digit logic
61
- if "%patch%"=="99" (
62
- set patch=0
63
- if "%minor%"=="99" (
64
- set minor=0
65
- set /a major+=1
66
- ) else (
67
- set /a minor+=1
68
- )
69
- ) else (
70
- set /a patch+=1
71
- )
72
-
73
- set new_version=%major%.%minor%.%patch%
74
-
75
- :: Update the version in package.json using jq
76
- jq --arg new_version "%new_version%" ".version = $new_version" package.json > temp.json && move /y temp.json package.json
77
-
78
- if %ERRORLEVEL% neq 0 (
79
- echo Failed to update package.json. Exiting...
80
- exit /b 1
81
- )
82
-
83
- echo Version updated from %current_version% to %new_version%
84
-
85
- :: Step 3: Publish the package to NPM
86
- npm publish
87
- if %ERRORLEVEL% neq 0 (
88
- echo NPM publish failed. Exiting...
89
- exit /b 1
90
- )
91
-
92
- :: Step 4: Commit the changes and push to GitHub
93
- git add .
94
- git commit -m "Bump version to %new_version%"
95
- git push origin %cd:~2,100%
96
-
97
- if %ERRORLEVEL% neq 0 (
98
- echo Failed to push changes to GitHub. Exiting...
99
- exit /b 1
100
- )
101
-
102
- echo Version %new_version% published and changes pushed to GitHub successfully!
103
-
104
- endlocal
105
- exit /b 0