{"id":20671308,"url":"https://github.com/allnulled/mysql2express","last_synced_at":"2026-02-03T04:04:05.062Z","repository":{"id":98546876,"uuid":"242971225","full_name":"allnulled/mysql2express","owner":"allnulled","description":"Generate REST models and controllers automativally for Express web applications from a MySQL database.","archived":false,"fork":false,"pushed_at":"2020-03-08T15:03:59.000Z","size":939,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-21T10:07:15.661Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/allnulled.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-25T10:22:15.000Z","updated_at":"2020-03-08T15:04:02.000Z","dependencies_parsed_at":"2023-05-29T11:50:48.975Z","dependency_job_id":null,"html_url":"https://github.com/allnulled/mysql2express","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/allnulled/mysql2express","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fmysql2express","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fmysql2express/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fmysql2express/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fmysql2express/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/allnulled","download_url":"https://codeload.github.com/allnulled/mysql2express/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fmysql2express/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261103452,"owners_count":23109932,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T20:26:17.991Z","updated_at":"2026-02-03T04:04:05.024Z","avatar_url":"https://github.com/allnulled.png","language":"TSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mysql2express\n\nGenerate REST models and controllers automatically for Express web applications from a MySQL database\n\n## Installation\n\n`$ npm i -g mysql2express`\n\n## Package usage\n\nYou can use any interface: `CLI` or `API`. The same example will be shown in both modes.\n\n### Package usage from CLI\n\n```\nmysql2express\n  --user      my_user\n  --password  my_password\n  --database  my_database\n  --host      localhost\n  --port      3306\n  --output    my_directory\n```\n\n### Package usage from API\n\n```\nrequire(\"mysql2express\")({\n    user:      \"my_user\",\n    password:  \"my_password\",\n    database:  \"my_database\",\n    host:      \"localhost\",\n    port:      3306,\n    output:    process.cwd() + \"/my_directory\"\n}).then(() =\u003e ...).catch(error =\u003e ...);\n```\n\n\n## Generated sources usage\n\nThe generated files will be your tools to accelerate the process of a *REST API* development targeting:\n\n  - `mysql` databases\n  - `node.js`/`express` applications.\n\n\n### The models\n\nThe **models** have nothing to do with `express`.\n\nThey are independent resources that can be overcharged.\n\nThis way, you can customize the behaviour of their *CRUD* operations on a `mysql` database.\n\nHowever, they work standalone, and you do not need to code anything else to start working from `node.js` applications.\n\n\n#### How to use a model\n\n1. Import and instantiate the model you want.\n\n```js\nconst User = require(\"./my_directory/models/UserModel.js\");\nconst user = User.create({...}); // use parameters to override default values of the instance\n```\n\n2. Do any CRUD operation, or retrieve the schema of the model.\n\n2.1. Get the schema:\n\n```js\nconst userSchema = user.schema();\nconsole.log(userSchema);\n```\n\n2.2. Retrieve many items:\n\n```js\nuser.getAll({\n    where:      [[\"id\", \"\u003c\", 10],[\"id\", \"\u003e\", 0]],\n    order:      [[\"id\", \"desc\"],[\"name\", \"desc\"]],\n    fields:     [\"id\", \"name\"],\n    page:       0,\n    quantity:   2,\n    search:     \"Text that must appear in any (selectable) field\"\n}).then(output =\u003e {\n    const { data, totalOfItems } = output;\n    console.log(\"Data:\", data);\n    console.log(\"Total of items to paginate:\", totalOfItems);\n}).catch(error =\u003e {\n    console.log(\"There were errors:\", error);\n});\n```\n\n2.3. Retrieve one item by id:\n\n```js\nuser.getOne(10/*, [\"id\", \"name\"]*/).then(output =\u003e {\n    const { data } = output;\n    console.log(\"Item (or null):\", item);\n}).catch(handler);\n```\n\n2.4. Create one new item:\n\n```js\nuser.postOne({ name: \"username\" }/*, [\"name\"]*/).then(output =\u003e {\n    const { id } = output;\n    console.log(\"New item with id:\", id);\n}).catch(handler);\n```\n\n2.5. Update one item:\n\n```js\nuser.putOne(11, { name: \"custom username\" }/*, [\"name\"]*/).then(output =\u003e {\n    const { changedItems } = output;\n    console.log(\"Items changed:\", changedItems);\n}).catch(handler);\n```\n\n2.6. Delete one item:\n\n```js\nuser.deleteOne(11).then(output =\u003e {\n    const { deletedItems } = output;\n    console.log(\"Items deleted:\", deletedItems);\n}).catch(handler);\n```\n\n### The controllers\n\nThe **controllers** are related to `express`.\n\nThey are dependent resources that can be overcharged, just like the *models* previously explained.\n\nThis way, you can customize the behaviour of their *CRUD* operations on an `express` application.\n\nHowever, they work standalone, and you do not need to code anything else to start working from `express` web applications.\n\n#### How to use a controller\n\nThis is an example of how one can use a controller in an `express` application:\n\n```js\nconst express = require(\"express\");\nconst app = express();\nconst UserController = require(\"./my_directory/controllers/UserController.js\");\n\nUserController.create({...}).mountOn({ app, route: \"/api/v1/user\" });\n```\n\n#### How to use a whole REST API\n\nThis is an example of how one can use a whole REST API in an `express` application:\n\n```js\nconst express = require(\"express\");\nconst app = express();\nconst AllControllers = require(\"./my_directory/AllControllers.js\");\n\nAllControllers.forEach(Controller =\u003e {\n\tController\n\t\t.create({...})\n\t\t.mountOn({\n\t\t\tapp,\n\t\t\troute: \"/api/v1/\" + Controller.REFERENCED_MODEL.MODEL_INFO.table\n\t\t});\n});\n```\n\n### Customization\n\nTo see the options, you can:\n   - navigate the documentation\n   - inspect the classes `models/Model.js` and `controllers/Controller.js`.\n\n### List of generated sources\n\n```\n/{rest-directory}\n  /schema.json             (1)\n  /getConnection.js        (2)\n  /controllers             \n    /Controller.js         (3)\n    /{Table}Controller.js  (4)\n  /models\n    /Model.js              (5)\n    /{Table}Model.js       (6)\n```\n\n#### **(1)** `schema.json`\n\nJSON file containing a general definition of the database.\n\n#### **(2)** `getConnection.js`\n\nJavaScript module that exports a `Promise` which returns the database connection\nused by the models.\n\n#### **(3)** `Controller.js`\n\nJavaScript module that exports the class which every controller extends.\n\n#### **(4)** `{Table}Controller.js`\n\nJavaScript modules that export a different controller per each database table.\n\n#### **(5)** `Model.js`\n\nJavaScript module that exports the class which every model extends.\n\n#### **(6)** `{Table}Model.js`\n\nJavaScript modules that export a different model per each database table.\n\n## License\n\nThis project is licensed under [WTFPL](https://es.wikipedia.org/wiki/WTFPL) or *What The Fuck Public License*, which basically means: **do whatever you want with it**.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallnulled%2Fmysql2express","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fallnulled%2Fmysql2express","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallnulled%2Fmysql2express/lists"}