Giter VIP home page Giter VIP logo

project-sails's Introduction

Sails.js简介及其CRUD

button

1. Sails.js 简介

Sails.js是一个受Ruby on Rails启发的Node.js 模型-视图-控制器(MVC)框架。

Sails.js基于Node.js构建,使用Express来处理HTTP请求,使构建定制的企业级Node.js应用程序变得容易。

今天,我们将开发基本的CRUD(创建,检索,更新,删除)操作。

项目地址:https://github.com/singsing215/project-sails

2. Sails MVC 模型

button

  • 模型层Model:数据库、REST api等。

  • 视图层View:视觉效果、用户界面等。

  • 控制层Controller:各种视图和模型之间的中介,负责处理http请求、访问数据库、将数据填充到视图上等。

2.1 路由(routes)

config\routes.js

module.exports.routes = {
  'GET /rent/viewAllDate': 'RentController.viewAllDate', 
}

当用GET方法请求localhost:1337/rent/viewAllDate时,执行RentController.js里的viewAllDate方法。

2.2 控制器(controller)

api\controllers\RentController.js

我们可以在Sails.js控制器中开发许多动作,下面是显示检索所有数据的操作。

module.exports = {
  viewAllDate: async function (req, res) {
    var models = await Rent.find(); //Rent是自定义的模型,执行Waterline ORM find() 方法 
    return res.view("rent/viewAllData", { //渲染rent目录下的viewAllData.ejs
      rents: models //赋值rents到视图层
    });
  },
}

2.3 模型层(model)

api\models\Rent.js

module.exports = {
  attributes: {
    id: {
      type: "number"
    },
    title: {
      type: "string"
    },
    estate: {
      type: "string"
    },
  }
}

这里指定了每个模型属性的类型。

2.4 视图层(view)

views\Rent\viewAllData.ejs

<table class="table">
  <% rents.forEach( (model) => { %>
    <tr>
      <td>
        <%= model.id %>
      </td>
      <td>
        <%= model.title %>
      </td>
      <td>
        <%= model.estate %>
      </td>
    </tr>
    <% }); %>
</table>

Embedded JavaScript (ejs) 是一种web模板引擎格式,它将数据和模板结合在一起以生成HTML。

<% %>里面的JavaScript会被执行,在<%= %>里面的JavaScript会被执行并将结果添加到HTML上。

效果图:

button

上面的初始8个数据可在config\bootstrap.js创建

module.exports.bootstrap = async function () {
  if (await Rent.count() == 0) {
    await Rent.createEach(
      //初始数据
    );
  }
}

3. Sails基本的CRUD操作

Sails捆绑了一个功能强大的ORM引擎——Waterline,它提供了一个简单的数据访问层。

ORM是对象关系映射(Object Relation Mapping),简单来说就是把关系型数据库映射成对象,完成关系型数据库的操作的技术。

Waterline从数据库中提取所有内容,通过对象和方法进行数据库的操作,如model.create()

如果不使用Sails的ORM引擎Waterline,可考虑使用Express + Mongoose (另一个ORM引擎)

https://www.robinwieruch.de/mongodb-express-setup-tutorial

现在,我们可以在Sails控制器api\controllers\RentController.js中结合Waterline引擎开发许多动作。

3.1 自定义路由

//路由路径:`config\routes.js`
module.exports.routes = {
  'GET /rent/viewAllDate': 'RentController.viewAllDate',  //检索
  'GET /rent/create': 'RentController.create', //创建
  'POST /rent/create': 'RentController.create', //创建
  'GET /rent/update/:id': 'RentController.update', //更新
  'POST /rent/update/:id': 'RentController.update', //更新
  'DELETE /rent/:id': 'RentController.delete', //删除
}

3.2创建model.create()

module.exports = {
  // action - create
  create: async function (req, res) {
    if (req.method == "GET") return res.view("rent/create"); //如果GET方法请求则渲染create.ejs
    if (!req.body.Rent) return res.badRequest("Form-data not received."); //校验表单数据
    await Rent.create(req.body.Rent); //执行Waterline的创建方法 
    return res.ok("Successfully created!");
  },
}

3.3 检索model.find()

module.exports = {
  // action - read all data
  viewAllDate: async function (req, res) {
    var models = await Rent.find(); //执行Waterline检索方法
    return res.view("rent/viewAllData", { //渲染rent目录下的viewAllData.ejs
      rents: models //赋值rents到视图层
    });
  },
}

3.4 更新model.update().set().fetch()

module.exports = {
  // action - update
  update: async function (req, res) {
    if (req.method == "GET") {
      var model = await Rent.findOne(req.params.id);
      if (!model) return res.notFound();
      return res.view("rent/update", {
        rent: model
      }); //如果GET方法请求则渲染update.ejs
    } else {
      if (!req.body.Rent) return res.badRequest("Form-data not received."); //校验表单数据
      var models = await Rent.update(req.params.id)
        .set({
          title: req.body.Rent.title,
          estate: req.body.Rent.estate,
          url: req.body.Rent.url,
        })
        .fetch(); //执行Waterline的更新方法,fetch()用于检索已更新的模型
      if (models.length == 0) return res.notFound(); //校验表单数据
      return res.ok("Record updated");
    }
  },
}

3.5 删除model.destroy().fetch()

module.exports = {
  // action - delete
  delete: async function (req, res) {
    if (req.wantsJSON) { // 检查是否 ajax request
      if (req.method !== "DELETE") return res.forbidden(); //校验Restful的DELETE请求方法
      var models = await Rent.destroy(req.params.id).fetch(); //执行Waterline的删除方法,fetch()用于检索已删除的模型
      if (models.length == 0) return res.notFound(); //校验表单数据
      return res.json({
        message: "Rental information deleted.",
        url: "/"
      }); 
    } else {
      return res.redirect("/"); // for normal request
    }
  },
}

4. 项目描述

1、房屋租赁系统使用MVC框架Sails开发。

2、系统所有的数据库操作由Sails的ORM层(Waterline.js)处理。

3、租赁系统实现租借信息的CRUD,用户有效的租赁操作,以及管理员和用户的权限控制。

4、控制层使用大量async function进行前后端交互,接口符合RESTful API规范。

LICENSE

微信公众号:Open Tech Blog

Github地址:github.com/singsing215/Open-Tech-Blog

点击阅读原文获取最新本篇内容。

qrcode

comp7270-project-react-sails-singsing215

comp7270-project-react-sails-singsing215 created by GitHub Classroom

The overall design of the system interface is simple and elegant. It uses the Bootstrap CSS framework and adopts a responsive web design. It will hide unnecessary components according to the user identity. The back end of the rental system uses Sails built on Node and uses the ejs template engine. The ORM layer (Waterline.js) in Sails handles all database operations for the housing rental system. The rental system uses AJAX and RESTful API technologies, and implement CRUD of rental information as well as effective rental operations for users, and permission control for visitor, user and administrator.

Author: GUO Fusheng 19413238

System admins: { username: "admin", password: 123456 }

Clients: { username: "kenny", password: 123456 }

IMPORTANT NOTE

Install dependencies with npm install

Install Sails with npm install sails -g

Run the following code to start sails lift --models.migrate='drop'

serve with hot reload at localhost:1337

If occur the following error during the first run:

Cannot read property 'columnName' of undefined

You need to delete '.columnName' from:

\node_modules\waterline\lib\waterline\utils\query\forge-stage-three-query.js:591:49

The modified code in line 591 in forge-stage-three-query.js should be

var columnName = model.schema[attrName];

project-sails's People

Contributors

singsing215 avatar

Watchers

 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.