The GreyCode.js framework is designed to streamline the development of applications using Sequelize and Express.js. This document provides an overview of the folder structure, its purpose, and how to work with it effectively. While the structure is customizable, adhering to the default layout ensures smoother functionality.
Installation
To install the GreyCode.js framework, use the following command:
curl -L -o greycodejs-0.0.2.tgz https://github.com/kculz/greycodejs/raw/main/greycodejs-0.0.1.tgz
After downloading the file, you can use tar to extract it to a desired directory:
tar -xvzf greycodejs-0.0.2.tgz --strip-components=1 -C ./desired-directory
Install dependencies and run app:
npm install
gray.js run --watch
Here is the default folder structure of a GreyCode.js project:
bin/ # Command-line entry points for the application
config/ # Configuration files (e.g., database settings)
controllers/ # Controller files for handling business logic
core/ # Core framework utilities and functionalities
middlewares/ # Middleware functions for request/response handling
models/ # Sequelize models for database interaction
node_modules/ # Node.js dependencies
public/ # Public assets (e.g., static files like images, CSS, JS)
routes/ # Route definitions for mapping URLs to controllers
seeds/ # Seed files for populating the database with initial data
templates/ # Templates for generating code or HTML
.env # Environment variable configuration
app.js # Main application entry point
package.json # Node.js package configuration
README.md # Documentation for the project
- Contains scripts for starting and managing the application.
- Example: Custom server startup scripts.
- Holds configuration files.
database.js
: Contains database connection settings (required).
- Contains controller files for handling application logic.
- Each controller corresponds to a specific model or feature and includes CRUD operations by default.
- Example:
UserController.js
for user-related operations.
- Core functionality of the framework, including essential utilities and base classes.
- Developers can extend the framework by adding custom functionality here.
- Contains middleware functions to process requests and responses.
- Example: Authentication, logging, or validation middleware.
- Contains Sequelize model definitions.
- Each file represents a table in the database.
- Example:
User.js
defines the schema and associations for theUser
table.
- Stores static assets like images, CSS, and JavaScript files.
- These files are served directly to the client.
- Contains route files that map endpoints to controllers.
- Example:
user.js
defines routes for user-related operations.
- Contains seed files for populating the database with initial or test data.
- Example:
user-seed.json
holds seed data for theUser
table.
- Contains templates for generating models, controllers, and routes.
- These templates can be customized to fit your specific requirements.
- Environment variables for the application.
- Example: Database credentials, port numbers.
- A sample environment configuration file for reference.
- The main entry point for the application.
- Initializes the server, middleware, and routes.
- Node.js dependencies and scripts.
- Add new dependencies or scripts as needed for the project.
- Documentation about the project.
- Provide information on how to set up and run the application.
Run the following command to start the application:
gray.js run [--watch]
- Use the
--watch
flag to enablenodemon
for automatic restarts on file changes.
Create a new model using the CLI:
gray.js create-model <name>
The generated model will appear in the /models
directory. Customize its fields and associations as needed.
Generate a controller for handling logic:
gray.js create-controller <name>
Controllers are stored in the /controllers
directory. Use these to define your application's business logic.
Generate a route file:
gray.js create-route <name>
Routes are stored in the /routes
directory and link HTTP endpoints to controller methods.
Create or apply seed data:
gray.js make-seed <model> [--count <number>] [--seed]
Use seed files in the /seeds
directory to populate your database.
You can modify the folder structure to suit your needs. For example:
- Rename or reorganize directories.
- Add new directories for additional features, such as
/services
for business logic or/tests
for unit tests.
Update your configuration files and scripts to reflect the changes.
-
Separation of Concerns:
- Keep controllers focused on application logic.
- Use middleware for cross-cutting concerns like authentication and validation.
-
Modularity:
- Group related files (e.g., models, controllers, routes) by feature.
-
Environment Variables:
- Store sensitive information in the
.env
file.
- Store sensitive information in the
-
Version Control:
- Commit the
.env.example
file, but exclude.env
using.gitignore
.
- Commit the
This structure is designed to be flexible and scalable, making it suitable for small projects as well as large applications. Modify it as needed to fit your specific use case!
When running commands via cli.js, you might run into permissions issues if the cli.js file doesn't have the correct execution permissions. Here's how to resolve that:
- Give Execute Permissions to
cli.js
If you’re getting a permission error when trying to run the script, make sure the file is executable by running:
chmod +x path/to/greycodejs/bin/cli.js