Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sails #72

Open
magicdawn opened this issue May 6, 2016 · 6 comments
Open

Sails #72

magicdawn opened this issue May 6, 2016 · 6 comments
Labels

Comments

@magicdawn
Copy link
Owner

new framework again ~

@magicdawn
Copy link
Owner Author

magicdawn commented May 8, 2016

Blueprints

Blueprints Actions

就是说一个 FooController & Foo model 的存在的话, FooController 里的一些方法(find, create, update, destroy, populate, add and remove) 就隐式存在了, 不用手写了. 但是你可以去 override 这些方法实现覆盖.

Blueprints Routes

route type

  • RESTful routes: 需要有一个 controller & model. 对应 /:modelIdentity or /:modelIdentity/:id. 根据 HTTP method 不同做出不同响应
  • Shortcut routes: 在 RESTful routes的基础上, 将 method 搬到 path 中, 如:
    • /user/create?name=joe 创建用户
    • /user/update/1?name=mike updates user #1
    • 特点: 在开发的时候是比较方便的, 生产环境中会被禁用
  • Action routes: 即是 controller 中的 action 方法可以自动的被 map 为 route, 例如 FooController.bar() 方法可以通过 /foo/bar 访问

@magicdawn magicdawn added the node label May 8, 2016
@magicdawn
Copy link
Owner Author

magicdawn commented May 10, 2016

Models

def

  • attributes
    • { type, columnName, defaultsTo, primaryKey, required }
  • tableName
  • connection 使用哪个数据库连接
  • autoCreatedAt / autoUpdatedAt : 默认 true

methods

FooModel.

  • create
  • find
  • update
  • destroy

attribute methods or instance methods

  • toJSON
  • save
  • destroy
  • validate

同时可定义其他 attribute methods, 可引用 this.idattribute

toJSON() & toObject

  • toObject() 是会删除所有的实例方法
  • toJSON() 是仅仅克隆一个 model record

The real power of toJSON relies on the fact every model instance sent out via res.json is first passed through toJSON. Instead of writing custom code for every controller action that uses a particular model (including the "out of the box" blueprints), you can manipulate outgoing records by simply overriding the default toJSON function in your model. You would use this to keep private data like email addresses and passwords from being sent back to every client.

This is an instance method. Currently, instance methods ARE NOT TRANSACTIONAL. Because of this, it is recommended that you use the equivalent model method instead.

在 res.json(xxx) 的时候, record 会自动调用 toJSON, 实际用处在这里, 常用的是要覆盖这个实例方法自己去实现.

query

http://sailsjs.org/documentation/concepts/models-and-orm/query-language

Model.find({
    where: {
    name: 'foo'
  },
  limit: 10,
  skip: 0,
  sort: 'id'
})

当没有限制条件时, 可直接将 para.where 作为 para 传参

  • key-pairs { name: 'foo' }
  • in / not in { name: ['foo', 'bar'] } / { name: { '!': ['foo', 'bar'] } }
  • modified pairs { name: { contains: 'abc' } }

modifiers

  • < / lessThan
  • <= / lessThanOrEqual
  • > / greaterThan
  • >= / greaterThanOrEqual
  • ! / not
  • like
  • contains
  • startsWith
  • endsWith

@magicdawn
Copy link
Owner Author

magicdawn commented May 19, 2016

config

// config/abc.js

exports.def = {
  foo: 'bar'
};

然后用 sails.config.def 取, 值为 { foo: 'bar' }

环境变量

sails_a__b__c=d 
// sails.config.a.b.c = d

@magicdawn
Copy link
Owner Author

magicdawn commented May 30, 2016

action

sails 的 action 是可以传 next, 官方说, 要么响应, 要么返回一个错误, 可以 next(e)
而且用浏览器访问 & 直接 curl 结果不一样

  • 浏览器的话, expect text/html, 看到的是 html
  • curl, 看到的是 json

这样 https://github.com/magicdawn/sails-boat 就没有意义了
直接使用https://github.com/express-modern 即可.

progress

  1. next(e)
  2. https://github.com/balderdashy/sails/blob/v0.12.4/lib/router/bindDefaultHandlers.js#L23-L44
  3. 进入 res.negotiate(e)
  4. 一般进入 res.serverError, 不要将 log.level 设置为 silent, 因为请求会输出 500 + 空内容 + console没输出

@magicdawn
Copy link
Owner Author

magicdawn commented Jun 28, 2016

log

http://sailsjs.org/documentation/concepts/logging#?log-levels

默认生成 level = debug, 可产生输出的方法有 debug / warn / error

启动的时候 sails_log__level=verbose log.level 默认是 info, 可通过环境变量改变

@magicdawn
Copy link
Owner Author

bootstrap / model

在 bootstrap 中可以修改连接字符串, 然后做下 Hack

// 重载 orm
sails.emit('hook:orm:reload');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant