Giter VIP home page Giter VIP logo

learn-orm's Introduction

Code Express ORM MariaDB

Express-based REST API server with ORM and MariaDB/MySQL.


Preparation

Database Installation

macOS:

brew install mariadb
brew services start mariadb

Ubuntu:

Installation and Configuration

Install dependencies:

yarn

Run setup script:

yarn setup
# this will copy .env.schema to .env

Then edit .env contents in your editor:

DB_USERNAME=yourusername
DB_PASSWORD=yourpassword
DB_NAME=yourdatabase
DB_HOST=localhost
DB_PORT=3306
DB_DIALECT=mysql

Create the yourdatabase (change this) database to your own database server.

Run migrate script to migrate the tables and seed data into the database.

yarn migrate

Running

Development

yarn dev

Production

yarn start

Extra Information

REST API Endpoints

Endpoint HTTP Description
/ GET Get root API
/users GET Get all users
/users/:id GET Get one user by id
/users POST Create new user
/users/:id PUT Update one user by id
/users/:id DELETE Delete one user by id
/users DELETE Delete all users

Request body example

{
  "email": "[email protected]",
  "password": "yourpassword",
  "username": "yourusername",
  "name": "Your Full Name"
}

Data Schema

Data Schema

Users

{
  "id": 0,
  "email": "",
  "password": "",
  "salt": "",
  "username": "",
  "name": ""
}

Tasks

{
  "id": 0,
  "user_id": 0,
  "text": ""
}

How to Use Sequelize

Follow this official guide: http://docs.sequelizejs.com/manual/tutorial/migrations.html

Install sequelize dependencies in your project.

yarn add sequelize mysql2 mariadb sqlite3

Use sequelize-cli to initialize and configure the project.

# install sequelize-cli globally
yarn global add sequelize-cli sequelize mysql2 mariadb sqlite3

# so you can use it anywhere
sequelize init

# change config.json to config.js

# change '/../config/config.js' in models/index.js

# configure config.js based on your database settings
# change database name, username, password, host, port, dialect

# generate model via cli
sequelize model:generate --name User --attributes username:string,email:string

# edit migrations file
# migrations/20180000000000-create-user.js

# edit models file
# models/user.js

# do the migration from the configuration to the actual database
sequelize db:migrate

# generate seeder via cli
sequelize seed:generate --name demo-users

# edit seeders file
# seeders/20180000000000-demo-users.js

# do the seeding from the configuration to the actual database
sequelize db:seed:all

How to Integrate with Express

Change the server.listen code block.

server.listen(port, function() {
  console.log('Express server listening on port ' + server.address().port);
});
server.on('error', onError);
server.on('listening', onListening);

Into this, to be wrapped with models.sequelize.

const models = require('./models');

// ...

models.sequelize.sync().then(function() {
  server.listen(port, function() {
    console.log('Express server listening on port ' + server.address().port);
    debug('Express server listening on port ' + server.address().port);
  });
  server.on('error', onError);
  server.on('listening', onListening);
});

Use the model from anywhere. For instance, in your controller functions.

const models = require('../../models');

// ...

models.User.findAll()
  .then(users => {
    res.send({
      users
    });
  })
  .catch(error => {
    res.status(400).send({
      error
    });
  });

Run express server as usual.

Database Dump

How to backup/export & restore/import database from/to a file.

Export:

mysqldump yourdatabase --single-transaction --user=yourusername -p > yourfile.sql

Import:

mysql yourdatabase --user=yourusername -p < yourfile.sql

learn-orm's People

Contributors

ariebrainware avatar fullstuck avatar gunturkh avatar mhaidarhanif avatar

Watchers

 avatar

learn-orm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.