Sails.js Model / ORM introduce
http://sailsjs.org/
sails 0.10 beta
• sails-0.10.0-reference	

• .done() vs. .exec()	

• Model Associations	

• sails console	

• promises in ...
sails upgrade 0.10 beta
• npm i sails@beta -g	

• npm i sails@beta -save	

• npm i sails-mysql@beta -save	

• npm i sails-...
• config/connections.js • addconfig/models.js
DB Setting
Why you need ORM ?
How Create Table?	

traditional way
SHOW CREATE TABLE `post`;	

!
ALTER TABLE `post` ADD `title`VARCHAR(100) NULL DEFAULT NULL AFTER `id`;	

!
ALTER TABLE `po...
MySQL
SELECT * FROM `post` WHERE `id` = '1' LIMIT 1;	

MySQL
MySQL
Terrible happened	

• DB schema modify	

• Multi tables join	

• Migrate Table or Database	

• Change adapter	

• more ...
Can we controller
Model from Code???
ORM
Objective Orient Model
Object Orented
adpters
one of best practices
Sql & Nosql
SQL vs. MongoDB
Waterline
Sails.js ORM module
Model
• file path -> api/models	

• Capital naming	

• Set schema and rule.	

• ORM like	

• you can use multi-adapter
Model Associations
• sails 0.10 new Feature	

• Many-to-Many	

• One-to-Many	

• One-to-One
Disk database (default)
Adapter support
• MySQL	

• MongoDB	

• Redis	

• ...	

• http://sailsjs.org/#!documentation/models
Adapter config
• config/connections.js • addconfig/models.js
DB Setting
Attribute type
• string	

• text	

• integer	

• float	

• date	

• time	

• datetime	

• boolean	

• binary	

• array	

• ...
Custom Defined 	

Instance Methods
example, api/models/Post.js
example, api/models/Post.js
generate a table and column
Auto migration
example2, api/models/Post.js
insert a attribute in Post model
Before and After
Lifecycle Callbacks
sails model
define Model
Associations
One to One
One to Many
Controller and Model
example, controller call Post model
example, controller call Post model
model name
example, controller call Post model
model
behavior
example, controller call Post model
new data
example, controller call Post model
success
callback 	

& 	

response
Model document
http://sailsjs.org/#!documentation/models
• User.create({name:"smlsun"}).exec(console.log);	

• Post.create({title:"post", content:"body", user:
1}).exec(console.lo...
Sails.js Model / ORM introduce
Sails.js Model / ORM introduce
Upcoming SlideShare
Loading in …5
×

Sails.js Model / ORM introduce

68,678 views

Published on

Sails.js Model / ORM introduce

Published in: Technology, Business
1 Comment
24 Likes
Statistics
Notes
  • How about many-to-many dude? I'm still confused about that.. could yo give me a sample console to exec create record in database about this association. I've already tried this but failed:
    Post.create({title: 'example title',owners:[{1,2}]}).exec(console.log)

    I defined in Post an Category model like this:
    Category Model:
    posts: {
    collection: 'post',
    via: 'owners'
    }

    Post Model :
    owners: {
    collection: 'category',
    via: 'posts',
    dominant: true
    }

    Help me, please! Thanks.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
68,678
On SlideShare
0
From Embeds
0
Number of Embeds
318
Actions
Shares
0
Downloads
158
Comments
1
Likes
24
Embeds 0
No embeds

No notes for slide

Sails.js Model / ORM introduce

  1. 1. Sails.js Model / ORM introduce
  2. 2. http://sailsjs.org/
  3. 3. sails 0.10 beta • sails-0.10.0-reference • .done() vs. .exec() • Model Associations • sails console • promises in core
  4. 4. sails upgrade 0.10 beta • npm i sails@beta -g • npm i sails@beta -save • npm i sails-mysql@beta -save • npm i sails-mongo@beta -save
  5. 5. • config/connections.js • addconfig/models.js DB Setting
  6. 6. Why you need ORM ?
  7. 7. How Create Table? traditional way
  8. 8. SHOW CREATE TABLE `post`; ! ALTER TABLE `post` ADD `title`VARCHAR(100) NULL DEFAULT NULL AFTER `id`; ! ALTER TABLE `post` ADD `content` TEXT DEFAULT NULL AFTER `title`; MySQL
  9. 9. MySQL
  10. 10. SELECT * FROM `post` WHERE `id` = '1' LIMIT 1; MySQL
  11. 11. MySQL
  12. 12. Terrible happened • DB schema modify • Multi tables join • Migrate Table or Database • Change adapter • more ...
  13. 13. Can we controller Model from Code???
  14. 14. ORM Objective Orient Model
  15. 15. Object Orented
  16. 16. adpters
  17. 17. one of best practices
  18. 18. Sql & Nosql
  19. 19. SQL vs. MongoDB
  20. 20. Waterline Sails.js ORM module
  21. 21. Model • file path -> api/models • Capital naming • Set schema and rule. • ORM like • you can use multi-adapter
  22. 22. Model Associations • sails 0.10 new Feature • Many-to-Many • One-to-Many • One-to-One
  23. 23. Disk database (default)
  24. 24. Adapter support • MySQL • MongoDB • Redis • ... • http://sailsjs.org/#!documentation/models
  25. 25. Adapter config
  26. 26. • config/connections.js • addconfig/models.js DB Setting
  27. 27. Attribute type • string • text • integer • float • date • time • datetime • boolean • binary • array • json
  28. 28. Custom Defined Instance Methods
  29. 29. example, api/models/Post.js
  30. 30. example, api/models/Post.js generate a table and column
  31. 31. Auto migration
  32. 32. example2, api/models/Post.js insert a attribute in Post model
  33. 33. Before and After
  34. 34. Lifecycle Callbacks sails model
  35. 35. define Model Associations
  36. 36. One to One
  37. 37. One to Many
  38. 38. Controller and Model
  39. 39. example, controller call Post model
  40. 40. example, controller call Post model model name
  41. 41. example, controller call Post model model behavior
  42. 42. example, controller call Post model new data
  43. 43. example, controller call Post model success callback & response
  44. 44. Model document http://sailsjs.org/#!documentation/models
  45. 45. • User.create({name:"smlsun"}).exec(console.log); • Post.create({title:"post", content:"body", user: 1}).exec(console.log); • Post.create({title:"post another", content:"body", user: 1}).exec(console.log); • Post.find().exec(console.log); • Post.find().populate("user").exec(console.log); • User.find().populate("posts").exec(console.log); • Profile.create({email: "smlsun@gmail.com", user: 1}).exec(console.log) • Profile.find().populate("user").exec(console.log);

×