@farazahmad759/dricup-crud-express v1.0.9
dricup-crud-express
Create quick RESTful APIs using Express + Knex + Objection. A single command dricup-crud-express will generate
- Migration files,
- Model files,
- Controller files, and
- Express Route files
You will save hours of your development.
Use Cases
- Build a Todo App in less than 5 minutes https://farazahmad759.medium.com/build-a-todo-app-in-less-than-5-minutes-in-node-express-ada63d7c54b9
Getting started
You get CRUD APIs in six simple steps.
Step 1
Install the package globally using
npm install -g @farazahmad759/dricup-crud-expressStep 2
Navigate to your Express project's root directory, and create schemas directory in <project_root>/db directory. It is the <project_root>/db/schemas directory where you will create .json schema files.
Your Express project directory structure should look like this
.
├── app.js
├── bin
│ └── www
└── db
└── schemas
├── table_1_schema.json
├── table_2_schema.json
└── table_3_schema.jsonStep 3
Create schema file(s). A sample schema file for users SQL table is included below. Feel free to copy its content and modify as per your needs.
{
"tableName": "users",
"fields": [
{
"title": "name",
"type": "string"
},
{
"title": "username",
"type": "string"
},
{
"title": "email",
"type": "string"
},
{
"title": "password",
"type": "string"
}
]
}Step 4
Run the following command
dricup-crud-expressand then install npm packages using
npm installThe dricup-crud-express command will generate the following files and directories.
db
--- migrations
------ create_table_users.js
--- models
------ users.js
--- controllers
------ users.js
routes
--- users.js
knexfile.js
ecag.config.jsonStep 5
Open knexfile.js and adjust your database credentials. A sample configuration will be like this
client: "mysql",
connection: {
host: "127.0.0.1",
database: "express-test-app",
user: "root",
password: "password",
},
migrations: {
directory: __dirname + "/db/migrations",
},
seeds: {
directory: __dirname + "/db/seeds/development",
},Step 6
In your app.js file, add the following two lines
/** ================
* your app.js file
* ================
*/
// some code
var usersRouter = require('./routes/users');
app.use('/users', usersRouter);
// even deliciuos codeNow start your Express App using npm start, and boom!!! your CRUD API is ready. dricup-crud-express provides the following routes
POST/users --- to create a new userGET/users/:id --- to get a user by IdPUT/users/:id --- to update a user by IdDELETE/users/:id --- to delete a user by IdGET/users --- to get all users (filters by each database column are already created for you)
Capabilities
I have described the capabilities of the
dricup-crud-expressin Build a Todo App in less than 5 minutesThere is much more you can do with it. Visit the
db/directory in your project root and explore the directories inside it, most notably thedb/controllersdirectory.Take a look at the
todoscontroller file created by the package here. The controller file has five methods in it for CRUD operations.- createOne
- getOne
- updateOne
- deleteOne
- getAll
Feel free to modify any of the above methods according to your needs. You can even add more methods to your controllers if you want. dricup-crud-express provides you the boilerplate code for writing API's and thus saves so much of your precious time.
ecag.config.jsonfile in the root of your project defines the paths where the files will be generated, which you can modify it as you want.
What is more?
This is just the starting. Several options will be added very soon to configure libraries such as Sequelize.
Feel free to request features, and I will be delighted to assist you in the issues you experience while using this package.