@@ -354,82 +354,114 @@ following syntax:
354
354
355
355
### Relations between models
356
356
357
+ #### belongsTo
358
+
359
+ A ` belongsTo ` relation sets up a one-to-one connection with another model, such
360
+ that each instance of the declaring model "belongs to" one instance of the other
361
+ model. For example, if your application includes customers and orders, and each order
362
+ can be placed by exactly one customer.
363
+
364
+ ![ belongsTo] ( belongs-to.png " belongsTo ")
365
+
366
+ var Order = ds.createModel('Order', {
367
+ customerId: Number,
368
+ orderDate: Date
369
+ });
370
+
371
+ var Customer = ds.createModel('Customer', {
372
+ name: String
373
+ });
374
+
375
+ Order.belongsTo(Customer);
376
+
377
+
378
+ The code above basically says Order has a reference called ` customer ` to User using
379
+ the ` customerId ` property of Order as the foreign key. Now we can access the customer
380
+ in one of the following styles:
381
+
382
+
383
+ order.customer(callback); // Get the customer for the order
384
+ order.customer(); // Get the customer for the order synchronously
385
+ order.customer(customer); // Set the customer for the order
386
+
387
+
357
388
#### hasMany
358
389
359
390
A ` hasMany ` relation builds a one-to-many connection with another model. You'll
360
391
often find this relation on the "other side" of a ` belongsTo ` relation. This
361
392
relation indicates that each instance of the model has zero or more instances
362
- of another model. For example, in an application containing users and posts, a
363
- user has zero or more posts. For example,
364
-
365
- // setup relationships
366
- User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
367
- // creates instance methods:
368
- // user.posts(conds)
369
- // user.posts.build(data) // like new Post({userId: user.id});
370
- // user.posts.create(data) // build and save
393
+ of another model. For example, in an application containing customers and orders, a
394
+ customer has zero or more orders.
371
395
372
- Define all necessary stuff for ` one to many` relation:
396
+ ![ hasMany ] ( has- many.png " hasMany ")
373
397
374
- - foreign key in ` many ` model
375
- - named scope in ` one ` model
376
-
377
- Example:
398
+ var Order = ds.createModel('Order', {
399
+ customerId: Number,
400
+ orderDate: Date
401
+ });
378
402
379
- var Book = db.define('Book');
380
- var Chapter = db.define('Chapters');
403
+ var Customer = ds.createModel('Customer', {
404
+ name: String
405
+ });
381
406
382
- // Style 1
383
- Book.hasMany(Chapter, {as: 'chapters'});
407
+ Customer.hasMany(Order, {as: 'orders', foreignKey: 'customerId'});
384
408
385
- // Style 2
386
- Book.hasMany('chapters', {model: Chapter, foreignKey: 'chapter_id'});
387
409
388
410
389
411
Scope methods created on the base model by hasMany allows to build, create and
390
412
query instances of other class. For example,
391
413
392
- Book.create(function(err, book) {
393
- // using 'chapters' scope for build:
394
- var c = book.chapters.build({name: 'Chapter 1'});
395
- // same as:
396
- c = new Chapter({name: 'Chapter 1', bookId: book.id});
397
- // using 'chapters' scope for create:
398
- book.chapters.create();
399
- // same as:
400
- Chapter.create({bookId: book.id});
401
-
402
- // using scope for querying:
403
- book.chapters(function() {/* all chapters with bookId = book.id */ });
404
- book.chapters({where: {name: 'test'}, function(err, chapters) {
405
- // all chapters with bookId = book.id and name = 'test'
406
- });
414
+ customer.orders(filter, callback); // Find orders for the customer
415
+ customer.orders.build(data); // Build a new order
416
+ customer.orders.create(data, callback); // Create a new order for the customer
417
+ customer.orders.destroyAll(callback); // Remove all orders for the customer
418
+ customer.orders.findById(orderId, callback); // Find an order by id
419
+ customer.orders.destroy(orderId, callback); // Delete and order by id
407
420
408
421
422
+ #### hasMany through
409
423
410
- #### belongsTo
411
- A ` belongsTo ` relation sets up a one-to-one connection with another model, such
412
- that each instance of the declaring model "belongs to" one instance of the other
413
- model. For example, if your application includes users and posts, and each post
414
- can be written by exactly one user.
424
+ A ` hasMany through ` relation is often used to set up a many-to-many connection with another model. This relation
425
+ indicates that the declaring model can be matched with zero or more instances of another model by proceeding through
426
+ a third model. For example, consider a medical practice where patients make appointments to see physicians. The
427
+ relevant association declarations could look like this:
415
428
416
- Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
429
+ ![ hasManyThrough ] ( has-many-through.png " hasManyThrough ")
417
430
418
- The code above basically says Post has a reference called ` author ` to User using
419
- the ` userId ` property of Post as the foreign key. Now we can access the author
420
- in one of the following styles:
431
+ var Physician = ds.createModel('Physician', {name: String});
432
+ var Patient = ds.createModel('Patient', {name: String});
433
+ var Appointment = ds.createModel('Appointment', {
434
+ physicianId: Number,
435
+ patientId: Number,
436
+ appointmentDate: Date
437
+ });
438
+
439
+ Appointment.belongsTo(Patient);
440
+ Appointment.belongsTo(Physician);
421
441
442
+ Physician.hasMany(Patient, {through: Appointment});
443
+ Patient.hasMany(Physician, {through: Appointment});
444
+
445
+ Now the Physician model has a virtual property called ` patients ` :
446
+
447
+ physician.patients(filter, callback); // Find patients for the physician
448
+ physician.patients.build(data); // Build a new patient
449
+ physician.patients.create(data, callback); // Create a new patient for the physician
450
+ physician.patients.destroyAll(callback); // Remove all patients for the physician
451
+ physician.patients.add(patient, callback); // Add an patient to the physician
452
+ physician.patients.remove(patient, callback); // Remove an patient from the physician
453
+ physician.patients.findById(patientId, callback); // Find an patient by id
422
454
423
- post.author(callback); // Get the User object for the post author asynchronously
424
- post.author(); // Get the User object for the post author synchronously
425
- post.author(user) // Set the author to be the given user
426
455
427
456
#### hasAndBelongsToMany
457
+
428
458
A ` hasAndBelongsToMany ` relation creates a direct many-to-many connection with
429
459
another model, with no intervening model. For example, if your application
430
460
includes users and groups, with each group having many users and each user
431
461
appearing in many groups, you could declare the models this way,
432
462
463
+ ![ hasAndBelongsToMany] ( has-and-belongs-to-many.png " hasAndBelongsToMany ")
464
+
433
465
User.hasAndBelongsToMany('groups', {model: Group, foreignKey: 'groupId'});
434
466
user.groups(callback); // get groups of the user
435
467
user.groups.create(data, callback); // create a new group and connect it with the user
0 commit comments