mythix-orm 1.0.4 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.biblorc.js ADDED
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ /* global __dirname */
4
+
5
+ const Path = require('path');
6
+
7
+ module.exports = {
8
+ rootDir: __dirname,
9
+ inputDir: Path.resolve(__dirname),
10
+ outputDir: Path.resolve(__dirname, '..', 'mythix-orm.wiki'),
11
+ files: [
12
+ {
13
+ include: /\/lib\/.*\.js$/,
14
+ parser: 'typescript',
15
+ compiler: 'typescript',
16
+ },
17
+ {
18
+ include: /\/docs\/.*\.md$/,
19
+ parser: 'markdown',
20
+ compiler: 'markdown',
21
+ },
22
+ ],
23
+ exclude: [
24
+ /node_modules|\/spec\//
25
+ ],
26
+ generatorOptions: {
27
+ baseURL: './',
28
+ },
29
+ };
@@ -0,0 +1,539 @@
1
+ # Mythix ORM associations
2
+
3
+ Mythix ORM makes associations really easy.
4
+
5
+ I many other ORMs you need to define how two models are related... and what fields are related on those models, and if the relation ship is polymorphic, and if it should load related models while accessing the relation, and if there is a default scope that should be applied, and...
6
+
7
+ Lame.
8
+
9
+ Let's not repeat that design pattern.
10
+
11
+ Instead, in Mythix ORM, you define relationships *with* queries. This means all that garbage you need to manually define in other ORMs is consistently and conveniently defined all in one place: the relationship query itself. This can include a polymorphic relation, a projection and related models to load, a "default scope", and everything else that can be done in other ORMs, in a simple and intuitive interface.
12
+
13
+ For this to work, when you define a relationship using the `Types.Model` or `Types.Models` types, you always define two parameters: 1) The target model of the relationship, and 2) The "query provider", which is a simple method that returns the query for the relationship.
14
+
15
+ ## Getting started
16
+
17
+ Fields in a Mythix ORM model can be either "virtual" or "concrete".
18
+
19
+ Concrete fields are backed by storage (the database), and will have a direct value they can be associated with.
20
+
21
+ Virtual fields are not backed by storage (at least not directly), and instead will dynamically fetch their value.
22
+
23
+ A field in Mythix ORM with be "concrete" or "virtual" simply based on its type. For example, the types `Types.Model` and `Types.Models` will by nature define a virtual field. These types define relations to other tables, and so don't have a 1x1 concrete field in the DB themselves, and instead will pull data based on the relationship defined by the type.
24
+
25
+ Before we get started, there are a few things to keep in mind:
26
+
27
+ 1. `Types.Model` is used for a 1x1 relationship
28
+ 2. `Types.Models` is used for a one-to-many, or a many-to-many relationship
29
+ 3. These two types specify field relationships
30
+ 4. In field relationships, it is required to specify a target model
31
+ 5. All concrete fields must be manually defined on all models. There is no "automagic" or "hidden" fields in `mythix-orm`. Said another way, **you must manually define ALL table columns/model fields, always, without exception**... none will be auto-defined for you. If you want auto-defined fields, such as "createdAt", and "updatedAt" for all your models, then the recommendation is to define a BaseModel that all of your other models inherit from.
32
+
33
+ ## Example #1 - Defining a 1x1 relationship
34
+
35
+ Let's say you want to have a Users table, and have each user have a single Role.
36
+
37
+ This is a 1x1 relationship, with one User to one Role.
38
+
39
+ You could define the association like so:
40
+
41
+ ```javascript
42
+ class User extends Model {
43
+ static fields = {
44
+ 'id': {
45
+ type: Types.UUIDV4,
46
+ defaultValue: Types.UUIDV4.Default.UUIDV4,
47
+ allowNull: false,
48
+ primaryKey: true,
49
+ },
50
+ 'firstName': {
51
+ type: Types.STRING(64),
52
+ allowNull: true,
53
+ index: true,
54
+ },
55
+ 'lastName': {
56
+ type: Types.STRING(64),
57
+ allowNull: true,
58
+ index: true,
59
+ },
60
+ // Define a foreign key relationship
61
+ // to the Roles table, targeting the
62
+ // "id" column of that table.
63
+ 'roleID': {
64
+ type: Types.FOREIGN_KEY('Role:id', {
65
+ onDelete: 'SET NULL',
66
+ onUpdate: 'SET NULL',
67
+ }),
68
+ allowNull: true,
69
+ index: true,
70
+ },
71
+ // This defines a "virtual" field,
72
+ // that will be used to define
73
+ // methods to interact with the role
74
+ 'role': {
75
+ // Relationships are defined by a query.
76
+ // Simply generate the query for the data
77
+ // you want the relationship to interact
78
+ // with, and away you go! The first model
79
+ // specified in the query is the "root"
80
+ // model, and must always match the target
81
+ // model specified in the first argument.
82
+ // "self" is always the origin model instance.
83
+ // So for example, when we want to get the role
84
+ // for this user, we would do a:
85
+ // `await user.getRole()`, so "self" would be
86
+ // "user" (the origin model instance).
87
+ //
88
+ // target query provider
89
+ type: Types.Model('Role', ({ Role, self, userQuery }) => {
90
+ return Role.where.id.EQ(self.roleID).MERGE(userQuery);
91
+ }),
92
+ },
93
+ };
94
+ }
95
+
96
+ // Now let's not forget to define
97
+ // our Role model
98
+ class Role extends Model {
99
+ static fields = {
100
+ 'id': {
101
+ type: Types.UUIDV4,
102
+ defaultValue: Types.UUIDV4.Default.UUIDV4,
103
+ allowNull: false,
104
+ primaryKey: true,
105
+ },
106
+ 'name': {
107
+ type: Types.STRING(64),
108
+ allowNull: false,
109
+ index: true,
110
+ },
111
+ // This defines a "virtual" field,
112
+ // that will be used to define
113
+ // methods to interact with the user
114
+ 'user': {
115
+ // Now we simply define the relationship
116
+ // in reverse, using a simple query.
117
+ type: Types.Model('User', ({ User, self, userQuery }) => {
118
+ return User.where.roleID.EQ(self.id).MERGE(userQuery);
119
+ }),
120
+ },
121
+ };
122
+ }
123
+ ```
124
+
125
+ Let's look at the `User` model `role` field first, which is of the type:
126
+
127
+ ```javascript
128
+ type: Types.Model('Role', ({ Role, self, userQuery }) => {
129
+ return Role.where.id.EQ(self.roleID).MERGE(userQuery);
130
+ }),
131
+ ```
132
+
133
+ This may seem confusing at first, until you understand the pattern here. The pattern is fairly simple. The `Types.Model` is singular (`Models` on the other hand, if used, defines a one-to-many, or many-to-many relationship), so right away we know this is going to fetch a single model. It is a 1x1 relationship. Next, we have the two arguments we provide. It helps a lot to think of these as "target model", and "query provider"... as in, "What is the target model we are interacting with, and how do we interact with it?" It also helps quite a bit to think of this from the perspective of "this model instance", instead of "this table", or "this relationship". The relationship is always from an instance of a model... a single instance (or a single row in the DB, if that helps instead).
134
+
135
+ With this in mind, the above model definitions should make a little more sense:
136
+
137
+ 1. From the perspective of this specific user, what are we targeting?
138
+ 2. We are targeting the `Role` model (the first argument)
139
+ 3. Great! Now, how are we interacting with this model?
140
+ 4. Oh, that is easy, we are interacting with a `Role` model where the `Role.id` equals `user.roleID` (self.roleID).
141
+
142
+ Simple!
143
+
144
+ The last thing you might be wondering is "What is this `userQuery` garbage?". Good question! The last part, the `.MERGE(userQuery)` is simply merging in any user query that might have been passed along with the call. What is a `userQuery`? A user query is a query that the caller provides to any relationship method call. For example, if we made the call `await user.getRole(Role.where.name.EQ('admin'))` then the `userQuery` passed into the query provider would be `Role.where.name.EQ('admin')`. When we merge this into our "default" query, this would then fetch the user's defined role, but only if the `name` attribute of the target `Role` was equal to `"admin"`. `userQuery` generally makes more sense in the context of many-to-many relationships, but can still be quite useful for 1x1 relationships.
145
+
146
+ One thing to note here is that Mythix ORM expects the developer to do something with the `userQuery`. If it is not used by the developer than it will be silently discarded. Why? Because with complex relationships you might want to apply the `userQuery` to some other part of the primary query... or you actually might not want to use it at all... or something else entirely. Allowing the developer to define what happens with `userQuery` might make the code more verbose, but it also allows much more flexibility and power in how it is used.
147
+
148
+
149
+ Let's look at it from the `Role` perspective now.
150
+
151
+ From the perspective of the `Role` it is very similar, except the "query provider" has switched to the `User` model (as the target/root model), where the `roleID` field matches the current `Role` model instance (self) `id` attribute.
152
+
153
+ We have:
154
+
155
+ ```javascript
156
+ type: Types.Model('User', ({ User, self, userQuery }) => {
157
+ return User.where.roleID.EQ(self.id).MERGE(userQuery);
158
+ }),
159
+ ```
160
+
161
+ So now, let's take a look at the process here:
162
+ 1. From the perspective of this specific role, what are we targeting?
163
+ 2. We are targeting the `User` model, and its `roleID` field
164
+ 3. Great! Now, who is providing the value to match against the `User:roleID` field?
165
+ 4. Oh, that is easy, it is me (self), this specific role, and the field to pull the `User:roleID` value from is `Role:id`... as in, this (self) very roles's primary key `id`.
166
+
167
+ One thing that is important to know is that the model name defined in the *first* argument will be the actual model type fetched. So, from the perspective of the user, we specify `Role` as the model in the first argument, so this is the role model type that will be fetched. From the perspective of the role, we specify `User` as the model in the first argument, so this is the model type that will be fetched.
168
+
169
+ ## Injected relationship methods
170
+
171
+ When we use a virtual relationship type, such as `Types.Model` or `Types.Models`, then when a model with these field types is instantiated, the model will have methods injected into it for the specified relationships. For example, above we are creating a `role` relationship to the `Role` model. When we do this, the user model will automatically get a certain set of methods injected to work with this 1x1 relationship.
172
+
173
+ For `Types.Model` (1x1 relationship), these methods are:
174
+ 1. `queryFor`
175
+ 2. `create`
176
+ 3. `get`
177
+ 4. `update`
178
+ 5. `destroy`
179
+ 6. `has`
180
+ 7. `pluck`
181
+
182
+ Now what was just listed are *prefixes* for the injected methods. These methods obviously need to exist for every relationship field on the model, so these prefixes are used in combination with the field name to create these injected methods. In our specific case, `Role` (the `role` field capitalized) will be added to the end of these prefixes, giving us:
183
+ 1. `queryForRole`
184
+ 2. `createRole`
185
+ 3. `getRole`
186
+ 4. `updateRole`
187
+ 5. `destroyRole`
188
+ 6. `hasRole`
189
+ 7. `pluckRole`
190
+
191
+ This is handy, because now from our user we can call any of these methods to interact with the relationship. For example, we could check to see if a user has a role simply by `await user.hasRole()`. Or we could pluck fields from the related `Role` model simply by `await user.pluckRole([ 'name' ])`.
192
+
193
+ For `Types.Models` (many-to-n relationships), these methods are:
194
+ 1. `queryFor`
195
+ 2. `addTo`
196
+ 3. `get`
197
+ 4. `set`
198
+ 5. `removeFrom`
199
+ 6. `destroy`
200
+ 7. `count`
201
+ 8. `pluck`
202
+ 9. `has`
203
+
204
+ As with the singular `Types.Model`, the name of the field is added to the end of each... so if we had many `roles`, these would turn into:
205
+ 1. `queryForRoles`
206
+ 2. `addToRoles`
207
+ 3. `getRoles`
208
+ 4. `setRoles`
209
+ 5. `removeFromRoles`
210
+ 6. `destroyRoles`
211
+ 7. `countRoles`
212
+ 8. `pluckRoles`
213
+ 9. `hasRoles`
214
+
215
+ Most of these injected methods are fairly self-explanatory... however, you might be wondering what the `queryFor` injected methods are used for. Simply put, these methods return the relationship query itself, and do nothing else. This can be really handy if you want to modify the query beyond what you can just do by providing a `userQuery`. For example, you could fetch the `role` relationship query, and modify it:
216
+
217
+ ```javascript
218
+ let query = await user.queryForRole();
219
+
220
+ query = query.AND.Role.name.EQ('admin');
221
+
222
+ let roles = await query.all();
223
+ ```
224
+
225
+ Now you know! When you define a virtual relationship field, these methods will always be injected by default, for every relationship field.
226
+
227
+ Note: One other important thing to note here is that Mythix ORM will *not* overwrite your class methods. If you define a method on your `User` model called `getRole`, then Mythix ORM won't touch that method when it is injecting methods onto the model. For this reason, Mythix ORM *also* injects the same methods but prefixed with an underscore (i.e. `_queryForRole`, `_createRole`, etc...). This will allow developers to easily implement their own methods of the same name as an override, while still being able to access the injected relationship method via the underscore prefix.
228
+
229
+ i.e.:
230
+ ```javascript
231
+ async getRole(...args) {
232
+ let role = await this._getRole(...args);
233
+ // do something with `role`
234
+ return role;
235
+ }
236
+ ```
237
+
238
+ ## Example #2 - Defining a One To Many relationship
239
+
240
+ Okay, so your boss comes in on Monday morning, and yells at you for implementing the previous example, because users only being able to have a single role is "just plain stupid... what were you thinking?"
241
+
242
+ Well, it is hard to argue with the boss, especially since he is correct. Users only being able to have a single role is a just a *wee* bit limiting. So, lets update this so a user can have many roles. To do so, we will need to define a one-to-many relationship.
243
+
244
+ Let's jump right into the code this time:
245
+
246
+ ```javascript
247
+ class User extends Model {
248
+ static fields = {
249
+ 'id': {
250
+ type: Types.UUIDV4,
251
+ defaultValue: Types.UUIDV4.Default.UUIDV4,
252
+ allowNull: false,
253
+ primaryKey: true,
254
+ },
255
+ 'firstName': {
256
+ type: Types.STRING(64),
257
+ allowNull: true,
258
+ index: true,
259
+ },
260
+ 'lastName': {
261
+ type: Types.STRING(64),
262
+ allowNull: true,
263
+ index: true,
264
+ },
265
+ // Let's drop this column, because now it makes
266
+ // no sense to have it live on the User table
267
+ //
268
+ // 'roleID': {
269
+ // type: Types.FOREIGN_KEY('Role:id', {
270
+ // onDelete: 'SET NULL',
271
+ // onUpdate: 'SET NULL',
272
+ // }),
273
+ // allowNull: true,
274
+ // index: true,
275
+ // },
276
+ //
277
+ // Let's change "role" to "roles", because
278
+ // now it will be plural (one to many)
279
+ 'roles': {
280
+ // Now we simply need to change our primary
281
+ // query slightly. We will now be targeting
282
+ // a "userID" column on the Role model.
283
+ //
284
+ // Notice the use of "plural" "Models" here
285
+ type: Types.Models('Role', ({ Role, self, userQuery }) => {
286
+ return Role.where.userID.EQ(self.id).MERGE(userQuery);
287
+ }),
288
+ },
289
+ };
290
+ }
291
+
292
+ class Role extends Model {
293
+ static fields = {
294
+ 'id': {
295
+ type: Types.UUIDV4,
296
+ defaultValue: Types.UUIDV4.Default.UUIDV4,
297
+ allowNull: false,
298
+ primaryKey: true,
299
+ },
300
+ 'name': {
301
+ type: Types.STRING(64),
302
+ allowNull: false,
303
+ index: true,
304
+ },
305
+ // Define a foreign key relationship
306
+ // to the User table, targeting the
307
+ // "id" column of that table.
308
+ 'userID': {
309
+ type: Types.FOREIGN_KEY('User:id', {
310
+ // Before we didn't want to delete
311
+ // a User if we deleted a role, so
312
+ // we set these values to "SET NULL".
313
+ // Now, if we delete a User, it would
314
+ // make sense that we would want to
315
+ // delete all the User's roles... so
316
+ // this time we "CASCADE".
317
+ onDelete: 'CASCADE',
318
+ onUpdate: 'CASCADE',
319
+ }),
320
+ allowNull: false,
321
+ index: true,
322
+ },
323
+ // This is still correctly named, as
324
+ // each role will still only link back
325
+ // to a single user
326
+ 'user': {
327
+ // Now we update this, flipping the
328
+ // relationship. If we store a "userID"
329
+ // on the Role table, then a user can
330
+ // have many roles.
331
+ type: Types.Model('User', ({ User, self, userQuery }) => {
332
+ return User.where.id.EQ(self.userID).MERGE(userQuery);
333
+ }),
334
+ },
335
+ };
336
+ }
337
+ ```
338
+
339
+ Hopefully this makes sense. We simply removed `roleID` from the User model, and moved it over to instead be `userID` on the other side of the relationship. Now a user can have many roles, because instead of each user only defining a single `roleID`, now Roles define a `userID`, and since many roles can define the same `userID`, a user can have many roles.
340
+
341
+ ## A quick aside on Foreign Keys
342
+
343
+ Great! So far so good. Hopefully my reader is still following me. If you don't know what "foreign keys" are, or don't understand the concept behind them, I suggest you go take a moment to read about them. In short, "foreign keys" simultaneously define an index, and one or more constraints. This means the database will disallow certain things. For example, if you specify that a relationship between a user and a role **must** exist, and **must** be valid, then the database will throw an error if you try to add a role without a user.
344
+
345
+ The `Types.FOREIGN_KEY` type allows us to define a foreign key relationship. The first argument is the target model and field. The second argument are simply the "options" for the foreign key.
346
+
347
+ Using the `FOREIGN_KEY` where appropriate is important. It is the **only** way that Mythix ORM knows your models are related. Mythix ORM uses this field type to update related attributes on models during load and store. Without using foreign keys, Mythix ORM will not know that your models are related, and so you might struggle when attempting to store or load related models. For example, because we defined `FOREIGN_KEY` types above for our `Role` model, linking to the `User` model `id` field, when Mythix ORM loads a `Role` model from the database, it will automatically know that it needs to assign the `userID` attribute of the `Role` model to the user's `id` field. This will happen even if the `userID` column is not loaded from the database.
348
+
349
+ The other thing `FOREIGN_KEY` types open up to you as the developer is easier bulk-model creation. For example, the following is completely valid, but **only** when using the `FOREIGN_KEY` type, so that Mythix ORM knows the models are related: `await Role.create({ name: 'admin', user: { firstName: 'Bob', lastName: 'Brown' } });`. What this is doing (even though it is strange, and likely doesn't reflect a real-world example), is creating the user *at the same time* that it creates the role. Mythix ORM is smart enough to understand the relationships, and it knows that in order to create a `Role` model it must first have a `userID`, so it will create and store the specified user first, and then create the role requested using the created user's `id`. It knows that `user: {some value}` is a `User`, because of the target model defined on that virtual field.
350
+
351
+ If the provided value to the role's `user` attribute was instead an already persisted `User` model instance, than Mythix ORM would skip storing the model, and simply pull its `id` for the `userID` attribute of the role.
352
+
353
+ **Important note:** Mythix ORM ignores many-to-n relationships by default with these types of operations. So you *can not* do the reverse, such as: `await User.create({ ..., roles: [ { name: 'admin' } ] })`. This **will not** work. Mythix ORM doesn't know what is desired in this case. What if the user already had some roles? Would they be removed? What if we wanted to just add instead? Because Mythix ORM can not know what you want to do with many-to-n relationships in a case like this, it simply isn't supported. With many-to-n relationships you must be explicit, and generally will use the relationship methods injected by the type itself. For example, instead of the above, you would have to:
354
+
355
+ ```javascript
356
+ let user = await User.create({ firstName: 'Bob', lastName: 'Brown' });
357
+ let role = await user.addToRoles({ name: 'admin' });
358
+ ```
359
+
360
+ This *will* work, because Mythix ORM now understands that you want to *add* to the many-to-n model set.
361
+
362
+ ## Example #3 - Using a "through" table in relationships
363
+
364
+ So far we have just defined relationships between two different tables, our `User` and `Role` tables. What if we instead want to define more that two tables in our relationship? What if, for example, our boss comes back to us on Friday morning and says "Dang, Shayla, okay, well, this situation is slightly better. Now users can have more than one role, but we need to define role information *on the relationship* itself."
365
+
366
+ Back to work! Now the boss wants extra information defined *on the relationship*, not on users, and not on roles, but rather on the relationship between the two. The boss wants us to define if a user, with a specific role, is able to use the front-door of the office, the back-door of the office, or both.
367
+
368
+ Okay, strange request boss... but then again, bosses generally don't do the thinking very well, which is why they hired you, right? Great! Let's get r' done.
369
+
370
+ For this to work, we now need to define a third table. We will call this table `UserRole`. This will define the user id, the role id, and which door the user is allowed to use. This will also mean that we need to remove the `userID` from the `Role` table.
371
+
372
+ So to sum up, we need to do the following:
373
+ 1. Drop the `userID` from the `Role` table
374
+ 2. Create a new table/model called `UserRole`
375
+ 3. Adjust the relationships between the models
376
+
377
+ Let's dig in:
378
+
379
+ ```javascript
380
+ class User extends Model {
381
+ static fields = {
382
+ 'id': {
383
+ type: Types.UUIDV4,
384
+ defaultValue: Types.UUIDV4.Default.UUIDV4,
385
+ allowNull: false,
386
+ primaryKey: true,
387
+ },
388
+ 'firstName': {
389
+ type: Types.STRING(64),
390
+ allowNull: true,
391
+ index: true,
392
+ },
393
+ 'lastName': {
394
+ type: Types.STRING(64),
395
+ allowNull: true,
396
+ index: true,
397
+ },
398
+ 'roles': {
399
+ // Now we update our primary query to
400
+ // go through the UserRole table.
401
+ //
402
+ // Note: Notice how we are .EQ against
403
+ // a query that has no conditions. We
404
+ // ask for `Role.where.id.EQ(UserRole.where.id)`.
405
+ // This is a table join. When you use any
406
+ // conditional operator on a FIELD from
407
+ // another table, without any conditions of
408
+ // its own, then Mythix ORM translates this
409
+ // as a request to join tables.
410
+ type: Types.Models('Role', ({ Role, UserRole, self, userQuery }) => {
411
+ return Role.where.id.EQ(UserRole.where.roleID).AND.UserRole.userID.EQ(self.id).MERGE(userQuery);
412
+ }),
413
+ },
414
+ };
415
+ }
416
+
417
+ class Role extends Model {
418
+ static fields = {
419
+ 'id': {
420
+ type: Types.UUIDV4,
421
+ defaultValue: Types.UUIDV4.Default.UUIDV4,
422
+ allowNull: false,
423
+ primaryKey: true,
424
+ },
425
+ 'name': {
426
+ type: Types.STRING(64),
427
+ allowNull: false,
428
+ index: true,
429
+ },
430
+ 'user': {
431
+ type: Types.Model('User', ({ User, UserRole, self, userQuery }) => {
432
+ return User.where.id.EQ(UserRole.where.userID).AND.UserRole.roleID.EQ(self.id).MERGE(userQuery);
433
+ }),
434
+ },
435
+ };
436
+ }
437
+
438
+ class UserRole extends Model {
439
+ static fields = {
440
+ 'id': {
441
+ type: Types.UUIDV4,
442
+ defaultValue: Types.UUIDV4.Default.UUIDV4,
443
+ allowNull: false,
444
+ primaryKey: true,
445
+ },
446
+ // The new data point our boss wanted us to add
447
+ 'doorUsage': {
448
+ type: Types.STRING(16),
449
+ allowNull: false,
450
+ index: true,
451
+ },
452
+ 'userID': {
453
+ type: Types.FOREIGN_KEY('User:id', {
454
+ onDelete: 'CASCADE',
455
+ onUpdate: 'CASCADE',
456
+ }),
457
+ allowNull: false,
458
+ index: true,
459
+ },
460
+ 'roleID': {
461
+ type: Types.FOREIGN_KEY('Role:id', {
462
+ onDelete: 'CASCADE',
463
+ onUpdate: 'CASCADE',
464
+ }),
465
+ allowNull: false,
466
+ index: true,
467
+ },
468
+ // These aren't needed, but they might be
469
+ // nice to have if we already have an instance
470
+ // of a UserRole model, and want to interact
471
+ // with the user or role of that instance.
472
+ 'role': {
473
+ type: Types.Model('Role', ({ Role, self, userQuery }) => {
474
+ return Role.where.id.EQ(self.roleID).MERGE(userQuery);
475
+ })
476
+ },
477
+ 'user': {
478
+ type: Types.Model('User', ({ User, self, userQuery }) => {
479
+ return User.where.id.EQ(self.userID).MERGE(userQuery);
480
+ }),
481
+ },
482
+ };
483
+ }
484
+ ```
485
+
486
+ This is fantastic! But... we have one problem. This through-table relationship will indeed do a three-way join to collect the information requested from all tables, but what about that new `doorUsage` field we added to the `UserRole` table? Nowhere have we specified where that can be found... Mythix ORM could just inject it onto `Role` models on load... but that doesn't make sense, because the user could have multiple roles, and each role link could have a different `doorUsage` value. Also, what if the `Role` model itself had a `doorUsage` field? That would not be ideal... Instead, it would be better if we could simply access the `doorUsage` attribute on any loaded through-table relationship.
487
+
488
+ When Mythix ORM loads a model through a relationship like this, it will by default *only* load the root/target model. This default behavior is deliberate to improve the performance and efficiency of the library. In order to also fetch the related models at the same time, you must directly specify a projection that includes the related model. When you do this, Mythix ORM will *then* include the specified related models while loading data.
489
+
490
+ Because we *want* to access the `doorUsage` attribute on the through-table model (`UserRole`), in the examples below you will see we use a `.PROJECT('+UserRole:doorUsage')`. This informs Mythix ORM that we want to add (`+`) the field `UserRole:doorUsage` to the query projection, which also means that Mythix ORM will load the `UserRole` model and assign it to our role.
491
+
492
+ ```javascript
493
+ // To get the roles along with their related
494
+ // UserRole model, we PROJECT on the UserRole
495
+ // model `doorUsage` field. This will ensure
496
+ // that Mythix ORM includes the related models
497
+ // (even though those models will only be
498
+ // partially loaded, and will only contain the
499
+ // `doorUsage` field).
500
+ let roles = await thisUser.getRoles(Role.where.PROJECT('+UserRole:doorUsage'));
501
+ ```
502
+
503
+ When we load the relationship this way, every `role` in the return `roles` array will also have a `UserRoles` key, containing the related `UserRole` models from the through table relationship.
504
+
505
+ Note: Because we only projected on the `UserRole:doorUsage` field, when you access one of the related `UserRole` models, the models will be incomplete, and will only have a `doorUsage` field set on them. This is fine though, because that is the only data point we need. Do **not** try and directly store these models back to the database however, because 1) without their primary key (`id`) any attempt to save will immediately fail, and 2) with missing attributes, even if you were able to get the model to save, you might end up with corruption (missing field values) in the database.
506
+
507
+ Now, we can access the related model and find the `doorUsage` attribute we are looking for.
508
+
509
+ ```javascript
510
+ let roles = await loadedUserInstance.getRoles(Role.where.name.EQ('restricted-door-usage').PROJECT('+UserRole:doorUsage'));
511
+ let doorUsage = roles[0].UserRoles[0].doorUsage;
512
+ ```
513
+
514
+ Great! Now we can easily access this attribute and get the job done as the boss asked:
515
+
516
+ ```javascript
517
+ let user = await User.where.id.EQ(userIDFromDoorRFIDScanner).first();
518
+ let roles = await user.getRoles(Role.where.name.EQ('restricted-door-usage').PROJECT('+UserRole:doorUsage'));
519
+ let doorUsage = roles[0].UserRoles[0].doorUsage;
520
+
521
+ if (doorUsage === 'back') {
522
+ // User is only allowed to use the back door
523
+ } else if (doorUsage === 'front') {
524
+ // User is only allowed to use the front door
525
+ } else {
526
+ // User can use either door... the boss
527
+ // has conveniently ensured that this
528
+ // case applies to him...
529
+ }
530
+ ```
531
+
532
+ ## Final Notes
533
+
534
+ 1. I would like to bring it to the attention of the reader that concrete types are defined all *UPPERCASE*, whereas virtual types are defined as *CamelCase*.
535
+ 2. "But, what if I am using two, three, or more through-tables?" you ask. Well, you are in luck! Nothing described above changes. You simply define a more complex primary query using the query provider, and Mythix ORM will be smart enough to recursively walk the relationships in the query, join as many tables as it needs to to get the job done, and do the right thing.
536
+ 3. Mythix forces you to manually define all table/model fields. Yes, this is extra overhead, but it comes with the benefit of not needing to painstakingly manually define all *relationships*. This design pattern was also decided upon so that there aren't any table columns that are ambiguously "hidden" from developers. By forcing the user to always define all columns/fields manually, it simplifies seeing what fields exist on the table, removes down-stream dependencies, and prevents the user from needing to go look-up documentation to understand how things are working and why. Feel free to write your own helper methods that will automatically inject fields into your schema for you!
537
+ 4. When pulling related models, Mythix ORM will always put them in a plural {model name} key... i.e. if you noticed above, `UserRole` models that were fetched were placed into `.UserRoles`. This is true for all relationship operations. If you opt-in to loading other relationships during an operation, the related models will always be placed on the loaded model instances, under their plural name (always as an array of models).
538
+
539
+ Happy coding!
package/docs/Home.md ADDED
@@ -0,0 +1 @@
1
+ Welcome to the mythix-orm wiki!
package/lib/model.js CHANGED
@@ -1071,15 +1071,31 @@ class Model {
1071
1071
  _getFieldValue(fieldName, field) {
1072
1072
  let value = this.getDataValue(fieldName);
1073
1073
 
1074
- if (typeof field.get === 'function')
1075
- return field.get.call(this, { value, field, fieldName });
1074
+ if (typeof field.get === 'function') {
1075
+ return field.get.call(this, {
1076
+ model: this,
1077
+ set: this.setDataValue.bind(this, fieldName),
1078
+ get: this.getDataValue.bind(this, fieldName),
1079
+ value,
1080
+ field,
1081
+ fieldName,
1082
+ });
1083
+ }
1076
1084
 
1077
1085
  return value;
1078
1086
  }
1079
1087
 
1080
1088
  _setFieldValue(fieldName, field, value) {
1081
1089
  if (typeof field.set === 'function') {
1082
- field.set.call(this, { value, field, fieldName });
1090
+ field.set.call(this, {
1091
+ model: this,
1092
+ set: this.setDataValue.bind(this, fieldName),
1093
+ get: this.getDataValue.bind(this, fieldName),
1094
+ value,
1095
+ field,
1096
+ fieldName,
1097
+ });
1098
+
1083
1099
  return;
1084
1100
  }
1085
1101
 
@@ -1267,7 +1283,7 @@ class Model {
1267
1283
  async save(_options) {
1268
1284
  let options = _options || {};
1269
1285
 
1270
- if (options.force !== true && Nife.isEmpty(this.changes)) {
1286
+ if (this.isPersisted() && options.force !== true && Nife.isEmpty(this.changes)) {
1271
1287
  return false;
1272
1288
  } else if (options.force) {
1273
1289
  // Mark all fields as dirty
@@ -95,6 +95,8 @@ const AUTO_INCREMENT = defaultValueFlags(function(context) {
95
95
  return context.connection.getDefaultFieldValue('AUTO_INCREMENT', context);
96
96
  }, { literal: true, remote: true });
97
97
 
98
+ AUTO_INCREMENT._mythixIsAutoIncrement = true;
99
+
98
100
  const DATETIME_NOW = generatePermutations(function(context) {
99
101
  return context.connection.getDefaultFieldValue('DATETIME_NOW', context);
100
102
  }, { literal: true, remote: true });
@@ -112,10 +112,14 @@ const TYPE_OPERATIONS = {
112
112
 
113
113
  return result;
114
114
  },
115
- 'exists': async function({ field, type }, options, ...args) {
115
+ 'has': async function({ field, type }, options, ...args) {
116
116
  let query = await type.prepareQuery({ connection: null, self: this, field, options }, args);
117
117
  return await query.exists(options);
118
118
  },
119
+ 'pluck': async function({ field, type }, fields, options, ...args) {
120
+ let query = await type.prepareQuery({ connection: null, self: this, field, options }, args);
121
+ return await query.pluck(fields, options);
122
+ },
119
123
  };
120
124
 
121
125
  class ModelType extends RelationalTypeBase {
@@ -106,7 +106,9 @@ const TYPE_OPERATIONS = {
106
106
  // might be persisted, the through table record
107
107
  // might still be created, which might blow up
108
108
  // if a constraint fails because the records
109
- // already exist
109
+ // already exist. The opposite might also be true,
110
+ // where the target models already exist, and
111
+ // so shouldn't be created.
110
112
  return this.getConnection(options && options.connection).transaction(async (connection) => {
111
113
  let currentModels = this[field.fieldName];
112
114
  if (Nife.isEmpty(currentModels))
@@ -227,6 +229,10 @@ const TYPE_OPERATIONS = {
227
229
  let query = await type.prepareQuery({ connection: null, self: this, field, userQuery, options }, args);
228
230
  return await query.count(null, options);
229
231
  },
232
+ 'pluck': async function({ field, type }, userQuery, fields, options, ...args) {
233
+ let query = await type.prepareQuery({ connection: null, self: this, field, fields, userQuery, options }, args);
234
+ return await query.pluck(fields, options);
235
+ },
230
236
  'has': async function({ count }, userQuery, options, ...args) {
231
237
  let itemCount = await count.call(this, userQuery, options, ...args);
232
238
  return (itemCount > 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mythix-orm",
3
- "version": "1.0.4",
3
+ "version": "1.3.0",
4
4
  "description": "ORM for Mythix framework",
5
5
  "main": "lib/index.js",
6
6
  "type": "commonjs",