npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-crud-gen

v1.0.1

Published

express-crud-gen automates the creation of RESTful routes and controllers for your Express application. This package enables you to easily set up CRUD (Create, Read, Update, Delete) operations with minimal configuration, allowing you to focus on your appl

Downloads

7

Readme

Express CRUD AUTO

express-crud-gen automates the creation of RESTful routes and controllers for your Express application. This package enables you to easily set up CRUD (Create, Read, Update, Delete) operations with minimal configuration, allowing you to focus on your application logic rather than boilerplate code.

Installation

To install the package, run:

npm install express path mysql express-crud-gen

Usage

Step 1: Define Your Models

Create a directory named models at the root of your repository. Inside this directory, create a models array with the desired structure:

const models = [
    {
        name: 'users',
        fields: [
            { name: 'name', type: 'VARCHAR(100)' },
            { name: 'email', type: 'VARCHAR(100)' },
            { name: 'age', type: 'INT' }
        ]
    },
    {
        name: 'products',
        fields: [
            { name: 'title', type: 'VARCHAR(255)' },
            { name: 'description', type: 'TEXT' },
            { name: 'price', type: 'DECIMAL(10, 2)' }
        ]
    }
];
module.exports=models

Step 2: Connect to the Database Create a directory named config at the root of your repository. Inside this directory, file named db.js, handle your MySQL connection as follows:

const mysql = require('mysql');

let db;

const connectDB = (config) => {
    db = mysql.createConnection(config);
    db.connect((err) => {
        if (err) {
            console.error('Error connecting to the database:', err.stack);
            return;
        }
        console.log('Connected to MySQL database');
    });
};

const getDB = () => {
    if (!db) {
        throw new Error('Database not connected. Call connectDB first.');
    }
    return db;
};

module.exports = {
    connectDB,
    getDB
};

Step 3: Set Up Your Application

In app.js, set up your Express app and use the generator:

// app.js
const express = require('express');
const crudGenerator = require('express-crud-gen');
const models = require("./models/models")
const { connectDB, getDB } = require("./config/db")
const path = require("path")
const app = express();
app.use(express.json());
// MySQL database configuration
const dbConfig = {
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'expressdb'
};
connectDB(dbConfig)
// Generate CRUD routes for the models and ensure tables are created
crudGenerator(getDB(),models, { baseDir: __dirname });
const routesDir = path.join(__dirname, 'routes');
models.forEach(model => {
    const routePath = path.join(routesDir, `${model.name}Routes.js`);
    console.log(`/api/${model.name}`);
    
    app.use(`/api/${model.name}`, require(routePath));
});
app.listen(3000, () => {
    console.log('Server running on port 3000');
});

RUN app

node app.js

Conclusion

With express-crud-gen, you can quickly build a RESTful API and manage your database models with ease. This tool saves you time and effort, allowing you to focus on the ultimate functionality of your application. Delve into the customization options for even more control over your CRUD operations!

License

MIT