GithubHelp home page GithubHelp logo

sailsjs-and-mysql-example's Introduction

SailsJs and Mysql example

Create CRUD with SailsJs and MySql.

Installs

Install Node js

Download Node.Js from this url Node.js.

Node

We check Node.Js, in the command-line tool:

$ npm -v
$ node -v

Check

Install SaislJs

We can see how install Saisl from this url Saisl.js.

To install with the command-line tool:

$ npm -g install sails

Let's check if "Sails" was installed correctly:

$ sails -v

Create a new Sails app

$ cd /you/path/project/
$ sails new crudUser
$ cd crudUser

Now lift the server and after you visit http://localhost:1337/:

$ sails lift

serve

Let's to create model and controller User:

$ sails generate api user

This created two files, api/models/User.js(model) and api/controllers/UserController.js(controller).

Install Mysql if you don't have installed before

Download Mysql from this url MySql.

You can see the documentation for settings Mysql.

CREATE DATABASE test;

Create table of users:

CREATE TABLE `test`.`users` (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `firstName` VARCHAR(45) NOT NULL DEFAULT '',
  `lastName` VARCHAR(45) NOT NULL DEFAULT '',
  `email` VARCHAR(100) NOT NULL DEFAULT '',
  `phone` VARCHAR(30) NOT NULL DEFAULT '',
  `address` VARCHAR(80) NOT NULL DEFAULT '',
  `createdData` DATETIME NOT NULL DEFAULT 0,
  `updateDate` TIMESTAMP NOT NULL DEFAULT 0,
  PRIMARY KEY(`id`)
)
ENGINE = InnoDB
CHARACTER SET utf8;

Let's Integrate Sails and Mysql

MySQL adapter for the Sails framework and Waterline ORM. Install from npm sails-mysql:

$ npm install --save sails-mysql

setting MySql on SailsJs, add the next code into config/connections.js:

  mysqlTest: {
    adapter: 'sails-mysql',
    host: 'localhost',
    user: 'root', //optional
    password: 'root', //optional
    database: 'test' //optional
  }

and change settings on config/models.js:

  connection: 'mysqlTest',
  migrate: 'safe',
  schema: true

Now, modify api/models/User.js:

module.exports = {
  // Name table in database
  tableName: 'users',

  // attributes: types, validations ans defaults values
  attributes: {
    id: {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true
    },
    firstName: {
      type: 'string',
      required: true,
      size: 45
    },
    lastName: {
      type: 'string',
      required: true,
      size: 45
    },
    email: {
      type: 'email',
      unique: true,
      required: true,
      size: 100
    },
    phone: {
      type: 'string',
      required: true
    },
    address: {
      type: 'string',
      required: true,
      size: 30
    },
    createdData: {
      type: 'datetime'
    },
    updateDate: {
      type: 'datetime',
      defaultsTo: function () {
        return new Date();
      }
    }
  },
  // before execute function createsss
  beforeCreate: function (values, cb) {
    values.createdData = new Date();
    cb();
  }
};

sailsjs-and-mysql-example's People

Contributors

miguelcast avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.