@anephenix/objection-relations 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -3,12 +3,12 @@
3
3
  A relations helper for Objection.js. This provides a convenient way to define
4
4
  relations in the `relationMappings` function on an Objection.js model.
5
5
 
6
- For example, say you have a table called "Persons" with this relation mapping:
6
+ For example, say you have a table called "Users" with this relation mapping:
7
7
 
8
8
  ```javascript
9
- class Person extends Model {
9
+ class User extends Model {
10
10
  static get tableName() {
11
- return 'persons';
11
+ return 'users';
12
12
  }
13
13
 
14
14
  static get relationMappings() {
@@ -17,8 +17,8 @@ class Person extends Model {
17
17
  relation: Model.HasManyRelation,
18
18
  modelClass: Address,
19
19
  join: {
20
- from: 'persons.id',
21
- to: 'addresses.person_id',
20
+ from: 'users.id',
21
+ to: 'addresses.user_id',
22
22
  },
23
23
  },
24
24
  };
@@ -29,20 +29,20 @@ class Person extends Model {
29
29
  You can use the objection-relations helper module to write the instead:
30
30
 
31
31
  ```javascript
32
- import { relation } from '@anephenix/objection-relations';
32
+ import { ObjectionRelation } from '@anephenix/objection-relations';
33
33
 
34
- class Person extends Model {
34
+ class User extends Model {
35
35
  static get tableName() {
36
- return 'persons';
36
+ return 'users';
37
37
  }
38
38
 
39
39
  static get relationMappings() {
40
+ const or = new ObjectionRelation({
41
+ subject: this.name,
42
+ modelPath: __dirname,
43
+ });
40
44
  return {
41
- addresses: relation({
42
- subject: 'Person',
43
- relType: 'hasMany',
44
- object: 'Address',
45
- }),
45
+ addresses: or.hasMany('Address'),
46
46
  };
47
47
  }
48
48
  }
@@ -53,7 +53,11 @@ The helper function will do the following:
53
53
  - Setup the relation type from Objection.js (hasOne, hasMany, hasMnayThrough, belongsTo)
54
54
  - Define the join table based on the properties of the subject and object
55
55
  models, if they follow a particular pattern (tables for models are named in
56
- plural format, and foreign keys use a singular format).
56
+ plural format, and foreign keys use a singular format). e.g.
57
+
58
+ | Model | table name | foreign key |
59
+ | ----- | ---------- | ----------- |
60
+ | User | users | user_id |
57
61
 
58
62
  ## Dependencies
59
63
 
@@ -72,7 +76,7 @@ You can setup different kinds of database table relationships like this:
72
76
  ### Belongs to
73
77
 
74
78
  ```javascript
75
- relation({ subject: 'Post', relType: 'belongsTo', object: 'User' });
79
+ or.belongsTo('Role');
76
80
  ```
77
81
 
78
82
  Is equivalent to writing:
@@ -80,10 +84,10 @@ Is equivalent to writing:
80
84
  ```javascript
81
85
  {
82
86
  relation: Model.BelongsToOneRelation,
83
- modelClass: User,
87
+ modelClass: 'Role',
84
88
  join: {
85
- from: 'posts.user_id',
86
- to: 'users.id'
89
+ from: 'users.role_id',
90
+ to: 'roles.id'
87
91
  }
88
92
  }
89
93
  ```
@@ -91,7 +95,7 @@ Is equivalent to writing:
91
95
  ### Has one
92
96
 
93
97
  ```javascript
94
- relation({ subject: 'User', relType: 'hasOne', object: 'Setting' });
98
+ or.hasOne('Setting');
95
99
  ```
96
100
 
97
101
  Is equivalent to writing:
@@ -99,7 +103,7 @@ Is equivalent to writing:
99
103
  ```javascript
100
104
  {
101
105
  relation: Model.HasOneRelation,
102
- modelClass: Setting,
106
+ modelClass: 'Setting',
103
107
  join: {
104
108
  from: 'users.id',
105
109
  to: 'settings.user_id'
@@ -110,7 +114,7 @@ Is equivalent to writing:
110
114
  ### Has many
111
115
 
112
116
  ```javascript
113
- relation({ subject: 'User', relType: 'hasMany', object: 'Address' });
117
+ or.hasMany('Address');
114
118
  ```
115
119
 
116
120
  Is equivalent to writing:
@@ -131,12 +135,7 @@ Is equivalent to writing:
131
135
  For relationships defined through a join table, you can write this:
132
136
 
133
137
  ```javascript
134
- relation({
135
- subject: 'User',
136
- relType: 'hasManyThrough',
137
- object: 'Company',
138
- via: 'Employment',
139
- });
138
+ or.hasManyThrough('Company', 'Employment');
140
139
  ```
141
140
 
142
141
  This is equivalent to:
@@ -168,12 +167,7 @@ Say a `User` model has many addresses, but the database table is called
168
167
  'account_users', you can write this code:
169
168
 
170
169
  ```javascript
171
- relation({
172
- subject: 'User',
173
- relType: 'hasMany',
174
- object: 'Address',
175
- options: { subjectTable: 'account_users' },
176
- });
170
+ or.hasMany('Address', { subjectTable: 'account_users' });
177
171
  ```
178
172
 
179
173
  Which is equivalent to writing:
@@ -195,12 +189,7 @@ The same applies for the object table. Say for example the `Address` model has
195
189
  the database table 'shipping_addresses', you could write this:
196
190
 
197
191
  ```javascript
198
- relation({
199
- subject: 'User',
200
- relType: 'hasMany',
201
- object: 'Address',
202
- options: { objectTable: 'shipping_addresses' },
203
- });
192
+ or.hasMany('Address', { objectTable: 'shipping_addresses' });
204
193
  ```
205
194
 
206
195
  Which is equivalent to writing:
@@ -222,12 +211,7 @@ If you find that the foreign key is not a singular form of the related model,
222
211
  then you can pass a foreign key for the subject like this:
223
212
 
224
213
  ```javascript
225
- relation({
226
- subject: 'User',
227
- relType: 'hasMany',
228
- object: 'Address',
229
- options: { subjectForeignKey: 'account_user_id' },
230
- });
214
+ or.hasMany('Address', { subjectForeignKey: 'account_user_id' });
231
215
  ```
232
216
 
233
217
  Which is equivalent to writing:
@@ -245,15 +229,10 @@ Which is equivalent to writing:
245
229
 
246
230
  ### ObjectForeignKey
247
231
 
248
- You can pass a custom foreign key for the object like this:
232
+ You can pass a custom foreign key for the object (Like a Post model) like this:
249
233
 
250
234
  ```javascript
251
- relation({
252
- subject: 'Post',
253
- relType: 'belongsTo',
254
- object: 'User',
255
- options: { objectForeignKey: 'author_id' },
256
- });
235
+ or.belongsTo('User', { objectForeignKey: 'author_id' });
257
236
  ```
258
237
 
259
238
  Is equivalent to writing:
@@ -0,0 +1,15 @@
1
+ import { CommonRelationOrTableOrForeignKeyProps } from './global';
2
+ declare type SubjectProps = CommonRelationOrTableOrForeignKeyProps & {
3
+ subject: string;
4
+ };
5
+ declare type ObjectProps = CommonRelationOrTableOrForeignKeyProps & {
6
+ object: string;
7
+ };
8
+ declare type ViaProps = string | undefined;
9
+ export declare function getSubjectTable({ subject, options }: SubjectProps): string;
10
+ export declare function getObjectTable({ object, options }: ObjectProps): string;
11
+ export declare function getSubjectForeignKey({ subject, options }: SubjectProps): string;
12
+ export declare function getObjectForeignKey({ object, options }: ObjectProps): string;
13
+ export declare function getModelClass({ object, options }: ObjectProps): string;
14
+ export declare function getViaTable(via: ViaProps): string | null;
15
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,80 +1,43 @@
1
- declare type CommonRelationOrTableOrForeignKeyProps = {
2
- options?: {
3
- subjectTable?: string;
4
- objectTable?: string;
5
- subjectForeignKey?: string;
6
- objectForeignKey?: string;
7
- };
8
- };
9
- declare type RelationProps = CommonRelationOrTableOrForeignKeyProps & {
1
+ import { OptionsProps } from './global';
2
+ declare type ObjectionRelationProps = {
10
3
  subject: string;
11
- relType: 'hasOne' | 'hasMany' | 'hasManyThrough' | 'belongsTo';
12
- object: string;
13
- via?: string;
14
- };
15
- declare type RelationTypeProps = {
16
- modelClass: string;
17
- from: string;
18
- to: string;
4
+ modelPath: string;
19
5
  };
20
- declare type AdvancedRelationTypeProps = RelationTypeProps & {
21
- through: {
22
- from: string;
23
- to: string;
24
- };
25
- };
26
- declare type SubjectProps = CommonRelationOrTableOrForeignKeyProps & {
6
+ export declare class ObjectionRelation {
27
7
  subject: string;
28
- };
29
- declare type ObjectProps = CommonRelationOrTableOrForeignKeyProps & {
30
- object: string;
31
- };
32
- export declare function belongsRelation({ modelClass, from, to }: RelationTypeProps): {
33
- relation: import("objection").RelationType;
34
- modelClass: string;
35
- join: {
36
- from: string;
37
- to: string;
38
- };
39
- };
40
- export declare function hasOneRelation({ modelClass, from, to }: RelationTypeProps): {
41
- relation: import("objection").RelationType;
42
- modelClass: string;
43
- join: {
44
- from: string;
45
- to: string;
8
+ modelPath: string;
9
+ constructor({ subject, modelPath }: ObjectionRelationProps);
10
+ belongsTo(object: string, options?: OptionsProps): {
11
+ relation: import("objection").RelationType;
12
+ modelClass: string;
13
+ join: {
14
+ from: string;
15
+ to: string;
16
+ };
46
17
  };
47
- };
48
- export declare function hasManyRelation({ modelClass, from, to }: RelationTypeProps): {
49
- relation: import("objection").RelationType;
50
- modelClass: string;
51
- join: {
52
- from: string;
53
- to: string;
18
+ hasOne(object: string, options?: OptionsProps): {
19
+ relation: import("objection").RelationType;
20
+ modelClass: string;
21
+ join: {
22
+ from: string;
23
+ to: string;
24
+ };
54
25
  };
55
- };
56
- export declare function hasManyThroughRelation({ modelClass, from, through, to }: AdvancedRelationTypeProps): {
57
- relation: import("objection").RelationType;
58
- modelClass: string;
59
- join: {
60
- from: string;
61
- through: {
26
+ hasMany(object: string, options?: OptionsProps): {
27
+ relation: import("objection").RelationType;
28
+ modelClass: string;
29
+ join: {
62
30
  from: string;
63
31
  to: string;
64
32
  };
65
- to: string;
66
33
  };
67
- };
68
- export declare function getSubjectTable({ subject, options }: SubjectProps): string;
69
- export declare function getObjectTable({ object, options }: ObjectProps): string;
70
- export declare function getSubjectForeignKey({ subject, options }: SubjectProps): string;
71
- export declare function getObjectForeignKey({ object, options }: ObjectProps): string;
72
- export declare function relation({ subject, relType, object, via, options }: RelationProps): {
73
- relation: import("objection").RelationType;
74
- modelClass: string;
75
- join: {
76
- from: string;
77
- to: string;
34
+ hasManyThrough(object: string, via: string, options?: OptionsProps): {
35
+ relation: import("objection").RelationType;
36
+ modelClass: string;
37
+ join: {
38
+ from: string;
39
+ to: string;
40
+ };
78
41
  };
79
- };
42
+ }
80
43
  export {};
@@ -7,6 +7,65 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
7
7
  var objection = require('objection');
8
8
  var snakeCase = _interopDefault(require('lodash.snakecase'));
9
9
  var pluralize = _interopDefault(require('pluralize'));
10
+ var path = require('path');
11
+
12
+ /*
13
+ Gets the SQL table for the subject, either from the options object or the
14
+ plural version of the subject model.
15
+ */
16
+
17
+ function getSubjectTable({
18
+ subject,
19
+ options
20
+ }) {
21
+ return (options == null ? void 0 : options.subjectTable) || pluralize(snakeCase(subject));
22
+ }
23
+ /*
24
+ Gets the SQL table for the object, either from the options object or the
25
+ plural version of the object model.
26
+ */
27
+
28
+ function getObjectTable({
29
+ object,
30
+ options
31
+ }) {
32
+ return (options == null ? void 0 : options.objectTable) || pluralize(snakeCase(object));
33
+ }
34
+ /*
35
+ Gets the SQL foreign key for the subject, either from the options object
36
+ or the snake case of the subject model.
37
+ */
38
+
39
+ function getSubjectForeignKey({
40
+ subject,
41
+ options
42
+ }) {
43
+ return (options == null ? void 0 : options.subjectForeignKey) || snakeCase(subject) + '_id';
44
+ }
45
+ /*
46
+ Gets the SQL foreign key for the object, either from the options object
47
+ or the snake case of the object model.
48
+ */
49
+
50
+ function getObjectForeignKey({
51
+ object,
52
+ options
53
+ }) {
54
+ return (options == null ? void 0 : options.objectForeignKey) || snakeCase(object) + '_id';
55
+ }
56
+ /*
57
+ Allows you to define the model path for a model
58
+ */
59
+
60
+ function getModelClass({
61
+ object,
62
+ options
63
+ }) {
64
+ return options != null && options.modelPath ? path.join(options.modelPath, object) : object;
65
+ }
66
+ function getViaTable(via) {
67
+ return via ? pluralize(snakeCase(via)) : null;
68
+ }
10
69
 
11
70
  // Dependencies
12
71
  const {
@@ -93,50 +152,6 @@ function hasManyThroughRelation({
93
152
  }
94
153
  };
95
154
  }
96
- /*
97
- Gets the SQL table for the subject, either from the options object or the
98
- plural version of the subject model.
99
- */
100
-
101
- function getSubjectTable({
102
- subject,
103
- options
104
- }) {
105
- return (options == null ? void 0 : options.subjectTable) || pluralize(snakeCase(subject));
106
- }
107
- /*
108
- Gets the SQL table for the object, either from the options object or the
109
- plural version of the object model.
110
- */
111
-
112
- function getObjectTable({
113
- object,
114
- options
115
- }) {
116
- return (options == null ? void 0 : options.objectTable) || pluralize(snakeCase(object));
117
- }
118
- /*
119
- Gets the SQL foreign key for the subject, either from the options object
120
- or the snake case of the subject model.
121
- */
122
-
123
- function getSubjectForeignKey({
124
- subject,
125
- options
126
- }) {
127
- return (options == null ? void 0 : options.subjectForeignKey) || snakeCase(subject) + '_id';
128
- }
129
- /*
130
- Gets the SQL foreign key for the object, either from the options object
131
- or the snake case of the object model.
132
- */
133
-
134
- function getObjectForeignKey({
135
- object,
136
- options
137
- }) {
138
- return (options == null ? void 0 : options.objectForeignKey) || snakeCase(object) + '_id';
139
- }
140
155
  /*
141
156
  Defines a relationship by passing the subject, the predicate, and the object,
142
157
  along with an optional via model.
@@ -165,27 +180,30 @@ function relation({
165
180
  object,
166
181
  options
167
182
  });
168
- let viaTable;
169
- if (via) viaTable = pluralize(snakeCase(via));
183
+ const modelClass = getModelClass({
184
+ object,
185
+ options
186
+ });
187
+ const viaTable = getViaTable(via);
170
188
 
171
189
  switch (relType) {
172
190
  case 'hasOne':
173
191
  return hasOneRelation({
174
- modelClass: object,
192
+ modelClass,
175
193
  from: `${subjectTable}.id`,
176
194
  to: `${objectTable}.${subjectForeignKey}`
177
195
  });
178
196
 
179
197
  case 'hasMany':
180
198
  return hasManyRelation({
181
- modelClass: object,
199
+ modelClass,
182
200
  from: `${subjectTable}.id`,
183
201
  to: `${objectTable}.${subjectForeignKey}`
184
202
  });
185
203
 
186
204
  case 'hasManyThrough':
187
205
  return hasManyThroughRelation({
188
- modelClass: object,
206
+ modelClass,
189
207
  from: `${subjectTable}.id`,
190
208
  through: {
191
209
  from: `${viaTable}.${subjectForeignKey}`,
@@ -196,7 +214,7 @@ function relation({
196
214
 
197
215
  case 'belongsTo':
198
216
  return belongsRelation({
199
- modelClass: object,
217
+ modelClass,
200
218
  from: `${subjectTable}.${objectForeignKey}`,
201
219
  to: `${objectTable}.id`
202
220
  });
@@ -206,13 +224,69 @@ function relation({
206
224
  }
207
225
  }
208
226
 
209
- exports.belongsRelation = belongsRelation;
210
- exports.getObjectForeignKey = getObjectForeignKey;
211
- exports.getObjectTable = getObjectTable;
212
- exports.getSubjectForeignKey = getSubjectForeignKey;
213
- exports.getSubjectTable = getSubjectTable;
214
- exports.hasManyRelation = hasManyRelation;
215
- exports.hasManyThroughRelation = hasManyThroughRelation;
216
- exports.hasOneRelation = hasOneRelation;
217
- exports.relation = relation;
227
+ class ObjectionRelation {
228
+ constructor({
229
+ subject,
230
+ modelPath
231
+ }) {
232
+ this.subject = subject;
233
+ this.modelPath = modelPath;
234
+ }
235
+
236
+ belongsTo(object, options) {
237
+ if (!options) options = {
238
+ modelPath: this.modelPath
239
+ };
240
+ if (!options.modelPath) options.modelPath = this.modelPath;
241
+ return relation({
242
+ subject: this.subject,
243
+ relType: 'belongsTo',
244
+ object,
245
+ options
246
+ });
247
+ }
248
+
249
+ hasOne(object, options) {
250
+ if (!options) options = {
251
+ modelPath: this.modelPath
252
+ };
253
+ if (!options.modelPath) options.modelPath = this.modelPath;
254
+ return relation({
255
+ subject: this.subject,
256
+ relType: 'hasOne',
257
+ object,
258
+ options
259
+ });
260
+ }
261
+
262
+ hasMany(object, options) {
263
+ if (!options) options = {
264
+ modelPath: this.modelPath
265
+ };
266
+ if (!options.modelPath) options.modelPath = this.modelPath;
267
+ return relation({
268
+ subject: this.subject,
269
+ relType: 'hasMany',
270
+ object,
271
+ options
272
+ });
273
+ }
274
+
275
+ hasManyThrough(object, via, options) {
276
+ if (!options) options = {
277
+ modelPath: this.modelPath
278
+ };
279
+ if (!options.modelPath) options.modelPath = this.modelPath;
280
+ return relation({
281
+ subject: this.subject,
282
+ relType: 'hasManyThrough',
283
+ object,
284
+ via,
285
+ options
286
+ });
287
+ }
288
+
289
+ }
290
+
291
+ exports.ObjectionRelation = ObjectionRelation;
218
292
  //# sourceMappingURL=objection-relations.cjs.development.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"objection-relations.cjs.development.js","sources":["../src/index.ts"],"sourcesContent":["// Dependencies\nimport { Model } from 'objection';\nconst {\n\tHasOneRelation,\n\tBelongsToOneRelation,\n\tHasManyRelation,\n\tManyToManyRelation,\n} = Model;\nimport snakeCase from 'lodash.snakecase';\nimport pluralize from 'pluralize';\n\ntype CommonRelationOrTableOrForeignKeyProps = {\n options?: {\n subjectTable?: string;\n objectTable?: string;\n subjectForeignKey?: string;\n objectForeignKey?: string;\n }\n}\n\ntype RelationProps = CommonRelationOrTableOrForeignKeyProps & {\n subject: string;\n relType: 'hasOne' | 'hasMany' | 'hasManyThrough' | 'belongsTo';\n object: string;\n via?: string;\n}\n\ntype RelationTypeProps = {\n modelClass: string;\n from: string;\n to: string;\n};\n\ntype AdvancedRelationTypeProps = RelationTypeProps & {\n through: {\n from: string;\n to: string;\n };\n}\n\ntype SubjectProps = CommonRelationOrTableOrForeignKeyProps & {\n subject: string;\n}\n\ntype ObjectProps = CommonRelationOrTableOrForeignKeyProps & {\n object: string;\n}\n\n/*\n Defines a relationship where a record in one model can belong to a record in\n another model.\n*/\nexport function belongsRelation({ modelClass, from, to }:RelationTypeProps) {\n\treturn {\n\t\trelation: BelongsToOneRelation,\n\t\tmodelClass,\n\t\tjoin: { from, to },\n\t};\n};\n\n/*\n\tDefines a relationship where a record in one model can own a record in another\n\tmodel.\n*/\nexport function hasOneRelation({ modelClass, from, to }:RelationTypeProps) {\n\treturn {\n\t\trelation: HasOneRelation,\n\t\tmodelClass,\n\t\tjoin: { from, to },\n\t};\n};\n\n/*\n\tDefines a relationship where a record in one model can own many records in\n\tanother model.\n*/\nexport function hasManyRelation({ modelClass, from, to }:RelationTypeProps) {\n\treturn {\n\t\trelation: HasManyRelation,\n modelClass,\n\t\tjoin: { from, to },\n\t};\n};\n\n/*\n\tDefines a relationship where a record in one model can own many records in\n\tanother model, via a join table\n*/\nexport function hasManyThroughRelation({ modelClass, from, through, to }:AdvancedRelationTypeProps) {\n\treturn {\n\t\trelation: ManyToManyRelation,\n modelClass,\n\t\tjoin: { from, through, to },\n\t};\n};\n\n/*\n Gets the SQL table for the subject, either from the options object or the\n plural version of the subject model.\n*/\nexport function getSubjectTable({ subject, options }:SubjectProps) {\n return options?.subjectTable || pluralize(snakeCase(subject));\n}\n\n/*\n Gets the SQL table for the object, either from the options object or the\n plural version of the object model.\n*/\nexport function getObjectTable({ object, options }:ObjectProps) { \n return options?.objectTable || pluralize(snakeCase(object));\n}\n\n/*\n Gets the SQL foreign key for the subject, either from the options object \n or the snake case of the subject model.\n*/\nexport function getSubjectForeignKey({ subject, options }:SubjectProps) {\n return options?.subjectForeignKey || snakeCase(subject) + '_id';\n}\n\n/*\n Gets the SQL foreign key for the object, either from the options object \n or the snake case of the object model.\n*/\nexport function getObjectForeignKey({ object, options }:ObjectProps) {\n return options?.objectForeignKey || snakeCase(object) + '_id';\n}\n\n/*\n\tDefines a relationship by passing the subject, the predicate, and the object,\n\talong with an optional via model.\n*/\nexport function relation({subject, relType, object, via, options}:RelationProps) {\n\tconst subjectTable = getSubjectTable({subject, options});\n\tconst objectTable = getObjectTable({object, options});\n\tconst subjectForeignKey = getSubjectForeignKey({subject, options});\n\tconst objectForeignKey = getObjectForeignKey({object, options});\n\tlet viaTable;\n\tif (via) viaTable = pluralize(snakeCase(via));\n\tswitch (relType) {\n\t\tcase 'hasOne':\n\t\t\treturn hasOneRelation({\n modelClass: object,\n\t\t\t\tfrom: `${subjectTable}.id`,\n\t\t\t\tto: `${objectTable}.${subjectForeignKey}`\n });\n\t\tcase 'hasMany':\n\t\t\treturn hasManyRelation({\n\t\t\t\tmodelClass: object,\n\t\t\t\tfrom: `${subjectTable}.id`,\n\t\t\t\tto: `${objectTable}.${subjectForeignKey}`\n\t\t\t});\n\t\tcase 'hasManyThrough':\n\t\t\treturn hasManyThroughRelation({\n\t\t\t\tmodelClass: object,\n\t\t\t\tfrom: `${subjectTable}.id`,\n\t\t\t\tthrough: {\n\t\t\t\t\tfrom: `${viaTable}.${subjectForeignKey}`,\n\t\t\t\t\tto: `${viaTable}.${objectForeignKey}`,\n\t\t\t\t},\n\t\t\t\tto: `${objectTable}.id`\n });\n\t\tcase 'belongsTo':\n\t\t\treturn belongsRelation({\n\t\t\t\tmodelClass: object,\n\t\t\t\tfrom: `${subjectTable}.${objectForeignKey}`,\n\t\t\t\tto: `${objectTable}.id`\n });\n\t\tdefault:\n\t\t\tthrow new Error('No valid relationship type specified');\n\t}\n};"],"names":["HasOneRelation","BelongsToOneRelation","HasManyRelation","ManyToManyRelation","Model","belongsRelation","modelClass","from","to","relation","join","hasOneRelation","hasManyRelation","hasManyThroughRelation","through","getSubjectTable","subject","options","subjectTable","pluralize","snakeCase","getObjectTable","object","objectTable","getSubjectForeignKey","subjectForeignKey","getObjectForeignKey","objectForeignKey","relType","via","viaTable","Error"],"mappings":";;;;;;;;;;AAAA;AAEA,MAAM;AACLA,EAAAA,cADK;AAELC,EAAAA,oBAFK;AAGLC,EAAAA,eAHK;AAILC,EAAAA;AAJK,IAKFC,eALJ;AA8CA;;;;;SAIgBC,gBAAgB;AAAEC,EAAAA,UAAF;AAAcC,EAAAA,IAAd;AAAoBC,EAAAA;AAApB;AAC/B,SAAO;AACNC,IAAAA,QAAQ,EAAER,oBADJ;AAENK,IAAAA,UAFM;AAGNI,IAAAA,IAAI,EAAE;AAAEH,MAAAA,IAAF;AAAQC,MAAAA;AAAR;AAHA,GAAP;AAKA;AAED;;;;;SAIgBG,eAAe;AAAEL,EAAAA,UAAF;AAAcC,EAAAA,IAAd;AAAoBC,EAAAA;AAApB;AAC9B,SAAO;AACNC,IAAAA,QAAQ,EAAET,cADJ;AAENM,IAAAA,UAFM;AAGNI,IAAAA,IAAI,EAAE;AAAEH,MAAAA,IAAF;AAAQC,MAAAA;AAAR;AAHA,GAAP;AAKA;AAED;;;;;SAIgBI,gBAAgB;AAAEN,EAAAA,UAAF;AAAcC,EAAAA,IAAd;AAAoBC,EAAAA;AAApB;AAC/B,SAAO;AACNC,IAAAA,QAAQ,EAAEP,eADJ;AAEAI,IAAAA,UAFA;AAGNI,IAAAA,IAAI,EAAE;AAAEH,MAAAA,IAAF;AAAQC,MAAAA;AAAR;AAHA,GAAP;AAKA;AAED;;;;;SAIgBK,uBAAuB;AAAEP,EAAAA,UAAF;AAAcC,EAAAA,IAAd;AAAoBO,EAAAA,OAApB;AAA6BN,EAAAA;AAA7B;AACtC,SAAO;AAC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ourcesContent":["import snakeCase from 'lodash.snakecase';\nimport pluralize from 'pluralize';\nimport { join } from 'path';\nimport { CommonRelationOrTableOrForeignKeyProps } from './global';\n\n// Types\n\ntype SubjectProps = CommonRelationOrTableOrForeignKeyProps & {\n subject: string;\n};\n\ntype ObjectProps = CommonRelationOrTableOrForeignKeyProps & {\n object: string;\n};\n\ntype ViaProps = string | undefined;\n\n/*\n Gets the SQL table for the subject, either from the options object or the\n plural version of the subject model.\n*/\nexport function getSubjectTable({ subject, options }: SubjectProps) {\n return options?.subjectTable || pluralize(snakeCase(subject));\n}\n\n/*\n Gets the SQL table for the object, either from the options object or the\n plural version of the object model.\n*/\nexport function getObjectTable({ object, options }: ObjectProps) {\n return options?.objectTable || pluralize(snakeCase(object));\n}\n\n/*\n Gets the SQL foreign key for the subject, either from the options object \n or the snake case of the subject model.\n*/\nexport function getSubjectForeignKey({ subject, options }: SubjectProps) {\n return options?.subjectForeignKey || snakeCase(subject) + '_id';\n}\n\n/*\n Gets the SQL foreign key for the object, either from the options object \n or the snake case of the object model.\n*/\nexport function getObjectForeignKey({ object, options }: ObjectProps) {\n return options?.objectForeignKey || snakeCase(object) + '_id';\n}\n\n/*\n\tAllows you to define the model path for a model\n*/\nexport function getModelClass({ object, options }: ObjectProps) {\n return options?.modelPath ? join(options.modelPath, object) : object;\n}\n\nexport function getViaTable(via: ViaProps) {\n return via ? pluralize(snakeCase(via)) : null;\n}\n","// Dependencies\nimport { Model } from 'objection';\nimport {\n RelationTypeProps,\n CommonRelationOrTableOrForeignKeyProps,\n} from './global';\nimport {\n getSubjectTable,\n getSubjectForeignKey,\n getObjectTable,\n getObjectForeignKey,\n getModelClass,\n getViaTable,\n} from './helpers';\n\nconst {\n HasOneRelation,\n BelongsToOneRelation,\n HasManyRelation,\n ManyToManyRelation,\n} = Model;\n\n// Types\n\ntype AdvancedRelationTypeProps = RelationTypeProps & {\n through: {\n from: string;\n to: string;\n };\n};\n\n/*\n Defines a relationship where a record in one model can belong to a record in\n another model.\n*/\nexport function belongsRelation({ modelClass, from, to }: RelationTypeProps) {\n return {\n relation: BelongsToOneRelation,\n modelClass,\n join: { from, to },\n };\n}\n\n/*\n\tDefines a relationship where a record in one model can own a record in another\n\tmodel.\n*/\nexport function hasOneRelation({ modelClass, from, to }: RelationTypeProps) {\n return {\n relation: HasOneRelation,\n modelClass,\n join: { from, to },\n };\n}\n\n/*\n\tDefines a relationship where a record in one model can own many records in\n\tanother model.\n*/\nexport function hasManyRelation({ modelClass, from, to }: RelationTypeProps) {\n return {\n relation: HasManyRelation,\n modelClass,\n join: { from, to },\n };\n}\n\n/*\n\tDefines a relationship where a record in one model can own many records in\n\tanother model, via a join table\n*/\nexport function hasManyThroughRelation({\n modelClass,\n from,\n through,\n to,\n}: AdvancedRelationTypeProps) {\n return {\n relation: ManyToManyRelation,\n modelClass,\n join: { from, through, to },\n };\n}\n\ntype RelationProps = CommonRelationOrTableOrForeignKeyProps & {\n subject: string;\n relType: 'hasOne' | 'hasMany' | 'hasManyThrough' | 'belongsTo';\n object: string;\n via?: string;\n};\n\n/*\n\tDefines a relationship by passing the subject, the predicate, and the object,\n\talong with an optional via model.\n*/\nexport function relation({\n subject,\n relType,\n object,\n via,\n options,\n}: RelationProps) {\n const subjectTable = getSubjectTable({ subject, options });\n const objectTable = getObjectTable({ object, options });\n const subjectForeignKey = getSubjectForeignKey({ subject, options });\n const objectForeignKey = getObjectForeignKey({ object, options });\n const modelClass = getModelClass({ object, options });\n const viaTable = getViaTable(via);\n switch (relType) {\n case 'hasOne':\n return hasOneRelation({\n modelClass,\n from: `${subjectTable}.id`,\n to: `${objectTable}.${subjectForeignKey}`,\n });\n case 'hasMany':\n return hasManyRelation({\n modelClass,\n from: `${subjectTable}.id`,\n to: `${objectTable}.${subjectForeignKey}`,\n });\n case 'hasManyThrough':\n return hasManyThroughRelation({\n modelClass,\n from: `${subjectTable}.id`,\n through: {\n from: `${viaTable}.${subjectForeignKey}`,\n to: `${viaTable}.${objectForeignKey}`,\n },\n to: `${objectTable}.id`,\n });\n case 'belongsTo':\n return belongsRelation({\n modelClass,\n from: `${subjectTable}.${objectForeignKey}`,\n to: `${objectTable}.id`,\n });\n default:\n throw new Error('No valid relationship type specified');\n }\n}\n","// Dependencies\nimport { OptionsProps } from './global';\nimport { relation } from './relations';\n\n// Types\n\ntype ObjectionRelationProps = {\n subject: string;\n modelPath: string;\n};\n\nexport class ObjectionRelation {\n subject: string;\n modelPath: string;\n constructor({ subject, modelPath }: ObjectionRelationProps) {\n this.subject = subject;\n this.modelPath = modelPath;\n }\n\n belongsTo(object: string, options?: OptionsProps) {\n if (!options) options = { modelPath: this.modelPath };\n if (!options.modelPath) options.modelPath = this.modelPath;\n return relation({\n subject: this.subject,\n relType: 'belongsTo',\n object,\n options,\n });\n }\n\n hasOne(object: string, options?: OptionsProps) {\n if (!options) options = { modelPath: this.modelPath };\n if (!options.modelPath) options.modelPath = this.modelPath;\n return relation({\n subject: this.subject,\n relType: 'hasOne',\n object,\n options,\n });\n }\n\n hasMany(object: string, options?: OptionsProps) {\n if (!options) options = { modelPath: this.modelPath };\n if (!options.modelPath) options.modelPath = this.modelPath;\n return relation({\n subject: this.subject,\n relType: 'hasMany',\n object,\n options,\n });\n }\n\n hasManyThrough(object: string, via: string, options?: OptionsProps) {\n if (!options) options = { modelPath: this.modelPath };\n if (!options.modelPath) options.modelPath = this.modelPath;\n return relation({\n subject: this.subject,\n relType: 'hasManyThrough',\n object,\n via,\n options,\n });\n }\n}\n"],"names":["getSubjectTable","subject","options","subjectTable","pluralize","snakeCase","getObjectTable","object","objectTable","getSubjectForeignKey","subjectForeignKey","getObjectForeignKey","objectForeignKey","getModelClass","modelPath","join","getViaTable","via","HasOneRelation","BelongsToOneRelation","HasManyRelation","ManyToManyRelation","Model","belongsRelation","modelClass","from","to","relation","hasOneRelation","hasManyRelation","hasManyThroughRelation","through","relType","viaTable","Error","ObjectionRelation","constructor","belongsTo","hasOne","hasMany","hasManyThrough"],"mappings":";;;;;;;;;;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E,SAFK;AAGdzB,MAAAA,MAHc;AAIdL,MAAAA;AAJc,KAAD,CAAf;AAMD;;AAEDsC,EAAAA,cAAc,CAACjC,MAAD,EAAiBU,GAAjB,EAA8Bf,OAA9B;AACZ,QAAI,CAACA,OAAL,EAAcA,OAAO,GAAG;AAAEY,MAAAA,SAAS,EAAE,KAAKA;AAAlB,KAAV;AACd,QAAI,CAACZ,OAAO,CAACY,SAAb,EAAwBZ,OAAO,CAACY,SAAR,GAAoB,KAAKA,SAAzB;AACxB,WAAOa,QAAQ,CAAC;AACd1B,MAAAA,OAAO,EAAE,KAAKA,OADA;AAEd+B,MAAAA,OAAO,EAAE,gBAFK;AAGdzB,MAAAA,MAHc;AAIdU,MAAAA,GAJc;AAKdf,MAAAA;AALc,KAAD,CAAf;AAOD;;;;;;"}
@@ -1,2 +1,2 @@
1
- "use strict";function o(o){return o&&"object"==typeof o&&"default"in o?o.default:o}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("objection"),t=o(require("lodash.snakecase")),n=o(require("pluralize"));const{HasOneRelation:r,BelongsToOneRelation:s,HasManyRelation:i,ManyToManyRelation:l}=e.Model;function a({modelClass:o,from:e,to:t}){return{relation:s,modelClass:o,join:{from:e,to:t}}}function u({modelClass:o,from:e,to:t}){return{relation:r,modelClass:o,join:{from:e,to:t}}}function c({modelClass:o,from:e,to:t}){return{relation:i,modelClass:o,join:{from:e,to:t}}}function d({modelClass:o,from:e,through:t,to:n}){return{relation:l,modelClass:o,join:{from:e,through:t,to:n}}}function f({subject:o,options:e}){return(null==e?void 0:e.subjectTable)||n(t(o))}function b({object:o,options:e}){return(null==e?void 0:e.objectTable)||n(t(o))}function p({subject:o,options:e}){return(null==e?void 0:e.subjectForeignKey)||t(o)+"_id"}function j({object:o,options:e}){return(null==e?void 0:e.objectForeignKey)||t(o)+"_id"}exports.belongsRelation=a,exports.getObjectForeignKey=j,exports.getObjectTable=b,exports.getSubjectForeignKey=p,exports.getSubjectTable=f,exports.hasManyRelation=c,exports.hasManyThroughRelation=d,exports.hasOneRelation=u,exports.relation=function({subject:o,relType:e,object:r,via:s,options:i}){const l=f({subject:o,options:i}),m=b({object:r,options:i}),h=p({subject:o,options:i}),g=j({object:r,options:i});let y;switch(s&&(y=n(t(s))),e){case"hasOne":return u({modelClass:r,from:l+".id",to:`${m}.${h}`});case"hasMany":return c({modelClass:r,from:l+".id",to:`${m}.${h}`});case"hasManyThrough":return d({modelClass:r,from:l+".id",through:{from:`${y}.${h}`,to:`${y}.${g}`},to:m+".id"});case"belongsTo":return a({modelClass:r,from:`${l}.${g}`,to:m+".id"});default:throw new Error("No valid relationship type specified")}};
1
+ "use strict";function o(o){return o&&"object"==typeof o&&"default"in o?o.default:o}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("objection"),e=o(require("lodash.snakecase")),n=o(require("pluralize")),s=require("path");const{HasOneRelation:l,BelongsToOneRelation:r,HasManyRelation:a,ManyToManyRelation:i}=t.Model;function u({subject:o,relType:t,object:u,via:h,options:c}){const d=function({subject:o,options:t}){return(null==t?void 0:t.subjectTable)||n(e(o))}({subject:o,options:c}),m=function({object:o,options:t}){return(null==t?void 0:t.objectTable)||n(e(o))}({object:u,options:c}),b=function({subject:o,options:t}){return(null==t?void 0:t.subjectForeignKey)||e(o)+"_id"}({subject:o,options:c}),j=function({object:o,options:t}){return(null==t?void 0:t.objectForeignKey)||e(o)+"_id"}({object:u,options:c}),f=function({object:o,options:t}){return null!=t&&t.modelPath?s.join(t.modelPath,o):o}({object:u,options:c}),p=function(o){return o?n(e(o)):null}(h);switch(t){case"hasOne":return function({modelClass:o,from:t,to:e}){return{relation:l,modelClass:o,join:{from:t,to:e}}}({modelClass:f,from:d+".id",to:`${m}.${b}`});case"hasMany":return function({modelClass:o,from:t,to:e}){return{relation:a,modelClass:o,join:{from:t,to:e}}}({modelClass:f,from:d+".id",to:`${m}.${b}`});case"hasManyThrough":return function({modelClass:o,from:t,through:e,to:n}){return{relation:i,modelClass:o,join:{from:t,through:e,to:n}}}({modelClass:f,from:d+".id",through:{from:`${p}.${b}`,to:`${p}.${j}`},to:m+".id"});case"belongsTo":return function({modelClass:o,from:t,to:e}){return{relation:r,modelClass:o,join:{from:t,to:e}}}({modelClass:f,from:`${d}.${j}`,to:m+".id"});default:throw new Error("No valid relationship type specified")}}exports.ObjectionRelation=class{constructor({subject:o,modelPath:t}){this.subject=o,this.modelPath=t}belongsTo(o,t){return t||(t={modelPath:this.modelPath}),t.modelPath||(t.modelPath=this.modelPath),u({subject:this.subject,relType:"belongsTo",object:o,options:t})}hasOne(o,t){return t||(t={modelPath:this.modelPath}),t.modelPath||(t.modelPath=this.modelPath),u({subject:this.subject,relType:"hasOne",object:o,options:t})}hasMany(o,t){return t||(t={modelPath:this.modelPath}),t.modelPath||(t.modelPath=this.modelPath),u({subject:this.subject,relType:"hasMany",object:o,options:t})}hasManyThrough(o,t,e){return e||(e={modelPath:this.modelPath}),e.modelPath||(e.modelPath=this.modelPath),u({subject:this.subject,relType:"hasManyThrough",object:o,via:t,options:e})}};
2
2
  //# sourceMappingURL=objection-relations.cjs.production.min.js.map