Open jrf0110 opened 11 years ago
In addition, if a table has as hasOne
relationship, we can safely assume that something like this:
dirac.register({
name: 'users'
, hasMany: [ 'groups' ]
, hasOne: [ 'profile' ]
, schema: {
/* ... */
}
});
dirac.insert({
email: 'bob@bob.com'
, password: 'password'
, profile: {
firstName: 'Bob'
, lastName: 'Bobbo'
}
});
So, we can extract the profile object because it matches the hasOne relationship, and do two inserts in a transaction. One for the user and for the profile.
The purpose of this is to automatically create junction tables if necessary and reason about the relationship between tables. If for instance we have a
hasOne
relationship, we can safely say get the corresponding records in the the hasOne tables on each query. That is, add a join to whatever queries we're performing. In addition, if there are updates to be made, we can treat a document, say a user with user groups embedded in it, as atomic and when the users array changes, we know how to update related tables.We can automatically create junction tables for
hasMany
relationships like this:In the case of
manyToMany
, the junction table schema is the same. We'll just need to detect when a many-to-many relationship exists and only create one version. That is, do not create bothbooks_authors
andauthors_books
when authors specifieshasMany: [ 'books' ]
and books specifies `hasMany: [ 'authors' ]``