@anephenix/objection-relations 0.0.1 → 0.0.3
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 +34 -53
- package/package.json +13 -11
- package/src/global.d.ts +17 -0
- package/src/helpers.ts +59 -0
- package/src/index.ts +54 -162
- package/src/relations.ts +141 -0
- package/dist/index.d.ts +0 -80
- package/dist/index.js +0 -8
- package/dist/objection-relations.cjs.development.js +0 -218
- package/dist/objection-relations.cjs.development.js.map +0 -1
- package/dist/objection-relations.cjs.production.min.js +0 -2
- package/dist/objection-relations.cjs.production.min.js.map +0 -1
- package/dist/objection-relations.esm.js +0 -204
- package/dist/objection-relations.esm.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
# objection-relations
|
|
2
2
|
|
|
3
|
+
[](https://github.com/anephenix/objection-relations/actions/workflows/node.js.yml) [](https://codeclimate.com/github/anephenix/objection-relations/maintainability) [](https://codeclimate.com/github/anephenix/objection-relations/test_coverage)
|
|
4
|
+
|
|
3
5
|
A relations helper for Objection.js. This provides a convenient way to define
|
|
4
6
|
relations in the `relationMappings` function on an Objection.js model.
|
|
5
7
|
|
|
6
|
-
For example, say you have a table called "
|
|
8
|
+
For example, say you have a table called "Users" with this relation mapping:
|
|
7
9
|
|
|
8
10
|
```javascript
|
|
9
|
-
class
|
|
11
|
+
class User extends Model {
|
|
10
12
|
static get tableName() {
|
|
11
|
-
return '
|
|
13
|
+
return 'users';
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
static get relationMappings() {
|
|
@@ -17,8 +19,8 @@ class Person extends Model {
|
|
|
17
19
|
relation: Model.HasManyRelation,
|
|
18
20
|
modelClass: Address,
|
|
19
21
|
join: {
|
|
20
|
-
from: '
|
|
21
|
-
to: 'addresses.
|
|
22
|
+
from: 'users.id',
|
|
23
|
+
to: 'addresses.user_id',
|
|
22
24
|
},
|
|
23
25
|
},
|
|
24
26
|
};
|
|
@@ -29,20 +31,20 @@ class Person extends Model {
|
|
|
29
31
|
You can use the objection-relations helper module to write the instead:
|
|
30
32
|
|
|
31
33
|
```javascript
|
|
32
|
-
import {
|
|
34
|
+
import { ObjectionRelation } from '@anephenix/objection-relations';
|
|
33
35
|
|
|
34
|
-
class
|
|
36
|
+
class User extends Model {
|
|
35
37
|
static get tableName() {
|
|
36
|
-
return '
|
|
38
|
+
return 'users';
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
static get relationMappings() {
|
|
42
|
+
const or = new ObjectionRelation({
|
|
43
|
+
subject: this.name,
|
|
44
|
+
modelPath: __dirname,
|
|
45
|
+
});
|
|
40
46
|
return {
|
|
41
|
-
addresses:
|
|
42
|
-
subject: 'Person',
|
|
43
|
-
relType: 'hasMany',
|
|
44
|
-
object: 'Address',
|
|
45
|
-
}),
|
|
47
|
+
addresses: or.hasMany('Address'),
|
|
46
48
|
};
|
|
47
49
|
}
|
|
48
50
|
}
|
|
@@ -53,7 +55,11 @@ The helper function will do the following:
|
|
|
53
55
|
- Setup the relation type from Objection.js (hasOne, hasMany, hasMnayThrough, belongsTo)
|
|
54
56
|
- Define the join table based on the properties of the subject and object
|
|
55
57
|
models, if they follow a particular pattern (tables for models are named in
|
|
56
|
-
plural format, and foreign keys use a singular format).
|
|
58
|
+
plural format, and foreign keys use a singular format). e.g.
|
|
59
|
+
|
|
60
|
+
| Model | table name | foreign key |
|
|
61
|
+
| ----- | ---------- | ----------- |
|
|
62
|
+
| User | users | user_id |
|
|
57
63
|
|
|
58
64
|
## Dependencies
|
|
59
65
|
|
|
@@ -72,7 +78,7 @@ You can setup different kinds of database table relationships like this:
|
|
|
72
78
|
### Belongs to
|
|
73
79
|
|
|
74
80
|
```javascript
|
|
75
|
-
|
|
81
|
+
or.belongsTo('Role');
|
|
76
82
|
```
|
|
77
83
|
|
|
78
84
|
Is equivalent to writing:
|
|
@@ -80,10 +86,10 @@ Is equivalent to writing:
|
|
|
80
86
|
```javascript
|
|
81
87
|
{
|
|
82
88
|
relation: Model.BelongsToOneRelation,
|
|
83
|
-
modelClass:
|
|
89
|
+
modelClass: 'Role',
|
|
84
90
|
join: {
|
|
85
|
-
from: '
|
|
86
|
-
to: '
|
|
91
|
+
from: 'users.role_id',
|
|
92
|
+
to: 'roles.id'
|
|
87
93
|
}
|
|
88
94
|
}
|
|
89
95
|
```
|
|
@@ -91,7 +97,7 @@ Is equivalent to writing:
|
|
|
91
97
|
### Has one
|
|
92
98
|
|
|
93
99
|
```javascript
|
|
94
|
-
|
|
100
|
+
or.hasOne('Setting');
|
|
95
101
|
```
|
|
96
102
|
|
|
97
103
|
Is equivalent to writing:
|
|
@@ -99,7 +105,7 @@ Is equivalent to writing:
|
|
|
99
105
|
```javascript
|
|
100
106
|
{
|
|
101
107
|
relation: Model.HasOneRelation,
|
|
102
|
-
modelClass: Setting,
|
|
108
|
+
modelClass: 'Setting',
|
|
103
109
|
join: {
|
|
104
110
|
from: 'users.id',
|
|
105
111
|
to: 'settings.user_id'
|
|
@@ -110,7 +116,7 @@ Is equivalent to writing:
|
|
|
110
116
|
### Has many
|
|
111
117
|
|
|
112
118
|
```javascript
|
|
113
|
-
|
|
119
|
+
or.hasMany('Address');
|
|
114
120
|
```
|
|
115
121
|
|
|
116
122
|
Is equivalent to writing:
|
|
@@ -131,12 +137,7 @@ Is equivalent to writing:
|
|
|
131
137
|
For relationships defined through a join table, you can write this:
|
|
132
138
|
|
|
133
139
|
```javascript
|
|
134
|
-
|
|
135
|
-
subject: 'User',
|
|
136
|
-
relType: 'hasManyThrough',
|
|
137
|
-
object: 'Company',
|
|
138
|
-
via: 'Employment',
|
|
139
|
-
});
|
|
140
|
+
or.hasManyThrough('Company', 'Employment');
|
|
140
141
|
```
|
|
141
142
|
|
|
142
143
|
This is equivalent to:
|
|
@@ -168,12 +169,7 @@ Say a `User` model has many addresses, but the database table is called
|
|
|
168
169
|
'account_users', you can write this code:
|
|
169
170
|
|
|
170
171
|
```javascript
|
|
171
|
-
|
|
172
|
-
subject: 'User',
|
|
173
|
-
relType: 'hasMany',
|
|
174
|
-
object: 'Address',
|
|
175
|
-
options: { subjectTable: 'account_users' },
|
|
176
|
-
});
|
|
172
|
+
or.hasMany('Address', { subjectTable: 'account_users' });
|
|
177
173
|
```
|
|
178
174
|
|
|
179
175
|
Which is equivalent to writing:
|
|
@@ -195,12 +191,7 @@ The same applies for the object table. Say for example the `Address` model has
|
|
|
195
191
|
the database table 'shipping_addresses', you could write this:
|
|
196
192
|
|
|
197
193
|
```javascript
|
|
198
|
-
|
|
199
|
-
subject: 'User',
|
|
200
|
-
relType: 'hasMany',
|
|
201
|
-
object: 'Address',
|
|
202
|
-
options: { objectTable: 'shipping_addresses' },
|
|
203
|
-
});
|
|
194
|
+
or.hasMany('Address', { objectTable: 'shipping_addresses' });
|
|
204
195
|
```
|
|
205
196
|
|
|
206
197
|
Which is equivalent to writing:
|
|
@@ -222,12 +213,7 @@ If you find that the foreign key is not a singular form of the related model,
|
|
|
222
213
|
then you can pass a foreign key for the subject like this:
|
|
223
214
|
|
|
224
215
|
```javascript
|
|
225
|
-
|
|
226
|
-
subject: 'User',
|
|
227
|
-
relType: 'hasMany',
|
|
228
|
-
object: 'Address',
|
|
229
|
-
options: { subjectForeignKey: 'account_user_id' },
|
|
230
|
-
});
|
|
216
|
+
or.hasMany('Address', { subjectForeignKey: 'account_user_id' });
|
|
231
217
|
```
|
|
232
218
|
|
|
233
219
|
Which is equivalent to writing:
|
|
@@ -245,15 +231,10 @@ Which is equivalent to writing:
|
|
|
245
231
|
|
|
246
232
|
### ObjectForeignKey
|
|
247
233
|
|
|
248
|
-
You can pass a custom foreign key for the object like this:
|
|
234
|
+
You can pass a custom foreign key for the object (Like a Post model) like this:
|
|
249
235
|
|
|
250
236
|
```javascript
|
|
251
|
-
|
|
252
|
-
subject: 'Post',
|
|
253
|
-
relType: 'belongsTo',
|
|
254
|
-
object: 'User',
|
|
255
|
-
options: { objectForeignKey: 'author_id' },
|
|
256
|
-
});
|
|
237
|
+
or.belongsTo('User', { objectForeignKey: 'author_id' });
|
|
257
238
|
```
|
|
258
239
|
|
|
259
240
|
Is equivalent to writing:
|
|
@@ -277,4 +258,4 @@ npm t
|
|
|
277
258
|
|
|
278
259
|
## Licence and credits
|
|
279
260
|
|
|
280
|
-
©
|
|
261
|
+
©2025 Anephenix OÜ. Objection-relations is licenced under the MIT Licence.
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.3",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"typings": "dist/index.d.ts",
|
|
@@ -17,25 +17,27 @@
|
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@size-limit/preset-small-lib": "^7.0.8",
|
|
20
|
-
"@types/jest": "^
|
|
20
|
+
"@types/jest": "^29.5.14",
|
|
21
21
|
"@types/lodash.snakecase": "^4.1.6",
|
|
22
|
-
"@types/pluralize": "^0.0.
|
|
23
|
-
"husky": "^
|
|
22
|
+
"@types/pluralize": "^0.0.33",
|
|
23
|
+
"husky": "^9.1.7",
|
|
24
24
|
"npm-upgrade": "^3.1.0",
|
|
25
25
|
"size-limit": "^7.0.8",
|
|
26
26
|
"tsdx": "^0.14.1",
|
|
27
|
-
"tslib": "^
|
|
28
|
-
"typescript": "^
|
|
27
|
+
"tslib": "^2.8.1",
|
|
28
|
+
"typescript": "^5.7.3"
|
|
29
29
|
},
|
|
30
30
|
"author": "Paul Jensen <paul@anephenix.com>",
|
|
31
31
|
"scripts": {
|
|
32
|
-
"
|
|
32
|
+
"analyze": "size-limit --why",
|
|
33
33
|
"build": "tsdx build --target node",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
34
|
+
"lint": "tsdx lint --fix",
|
|
35
|
+
"prepare-patch-release": "npm run update-changelog && git add CHANGELOG.md && git commit -m \"Updated changelog\" && npm version patch",
|
|
36
|
+
"publish-patch-release": "npm run prepare-patch-release && git push origin main && git push --tags",
|
|
37
37
|
"size": "size-limit",
|
|
38
|
-
"
|
|
38
|
+
"start": "tsdx watch --target node",
|
|
39
|
+
"test": "tsdx test --collectCoverage",
|
|
40
|
+
"update-changelog": "node scripts/update-changelog.js"
|
|
39
41
|
},
|
|
40
42
|
"husky": {
|
|
41
43
|
"hooks": {
|
package/src/global.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type OptionsProps = {
|
|
2
|
+
subjectTable?: string;
|
|
3
|
+
objectTable?: string;
|
|
4
|
+
subjectForeignKey?: string;
|
|
5
|
+
objectForeignKey?: string;
|
|
6
|
+
modelPath?: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type CommonRelationOrTableOrForeignKeyProps = {
|
|
10
|
+
options?: OptionsProps;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type RelationTypeProps = {
|
|
14
|
+
modelClass: string;
|
|
15
|
+
from: string;
|
|
16
|
+
to: string;
|
|
17
|
+
};
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import snakeCase from 'lodash.snakecase';
|
|
2
|
+
import pluralize from 'pluralize';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { CommonRelationOrTableOrForeignKeyProps } from './global';
|
|
5
|
+
|
|
6
|
+
// Types
|
|
7
|
+
|
|
8
|
+
type SubjectProps = CommonRelationOrTableOrForeignKeyProps & {
|
|
9
|
+
subject: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type ObjectProps = CommonRelationOrTableOrForeignKeyProps & {
|
|
13
|
+
object: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type ViaProps = string | undefined;
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
Gets the SQL table for the subject, either from the options object or the
|
|
20
|
+
plural version of the subject model.
|
|
21
|
+
*/
|
|
22
|
+
export function getSubjectTable({ subject, options }: SubjectProps) {
|
|
23
|
+
return options?.subjectTable || pluralize(snakeCase(subject));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/*
|
|
27
|
+
Gets the SQL table for the object, either from the options object or the
|
|
28
|
+
plural version of the object model.
|
|
29
|
+
*/
|
|
30
|
+
export function getObjectTable({ object, options }: ObjectProps) {
|
|
31
|
+
return options?.objectTable || pluralize(snakeCase(object));
|
|
32
|
+
}
|
|
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
|
+
export function getSubjectForeignKey({ subject, options }: SubjectProps) {
|
|
39
|
+
return options?.subjectForeignKey || snakeCase(subject) + '_id';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
Gets the SQL foreign key for the object, either from the options object
|
|
44
|
+
or the snake case of the object model.
|
|
45
|
+
*/
|
|
46
|
+
export function getObjectForeignKey({ object, options }: ObjectProps) {
|
|
47
|
+
return options?.objectForeignKey || snakeCase(object) + '_id';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/*
|
|
51
|
+
Allows you to define the model path for a model
|
|
52
|
+
*/
|
|
53
|
+
export function getModelClass({ object, options }: ObjectProps) {
|
|
54
|
+
return options?.modelPath ? join(options.modelPath, object) : object;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function getViaTable(via: ViaProps) {
|
|
58
|
+
return via ? pluralize(snakeCase(via)) : null;
|
|
59
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,172 +1,64 @@
|
|
|
1
1
|
// Dependencies
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
HasOneRelation,
|
|
5
|
-
BelongsToOneRelation,
|
|
6
|
-
HasManyRelation,
|
|
7
|
-
ManyToManyRelation,
|
|
8
|
-
} = Model;
|
|
9
|
-
import snakeCase from 'lodash.snakecase';
|
|
10
|
-
import pluralize from 'pluralize';
|
|
2
|
+
import { OptionsProps } from './global';
|
|
3
|
+
import { relation } from './relations';
|
|
11
4
|
|
|
12
|
-
|
|
13
|
-
options?: {
|
|
14
|
-
subjectTable?: string;
|
|
15
|
-
objectTable?: string;
|
|
16
|
-
subjectForeignKey?: string;
|
|
17
|
-
objectForeignKey?: string;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type RelationProps = CommonRelationOrTableOrForeignKeyProps & {
|
|
22
|
-
subject: string;
|
|
23
|
-
relType: 'hasOne' | 'hasMany' | 'hasManyThrough' | 'belongsTo';
|
|
24
|
-
object: string;
|
|
25
|
-
via?: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
type RelationTypeProps = {
|
|
29
|
-
modelClass: string;
|
|
30
|
-
from: string;
|
|
31
|
-
to: string;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
type AdvancedRelationTypeProps = RelationTypeProps & {
|
|
35
|
-
through: {
|
|
36
|
-
from: string;
|
|
37
|
-
to: string;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
type SubjectProps = CommonRelationOrTableOrForeignKeyProps & {
|
|
42
|
-
subject: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
type ObjectProps = CommonRelationOrTableOrForeignKeyProps & {
|
|
46
|
-
object: string;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/*
|
|
50
|
-
Defines a relationship where a record in one model can belong to a record in
|
|
51
|
-
another model.
|
|
52
|
-
*/
|
|
53
|
-
export function belongsRelation({ modelClass, from, to }:RelationTypeProps) {
|
|
54
|
-
return {
|
|
55
|
-
relation: BelongsToOneRelation,
|
|
56
|
-
modelClass,
|
|
57
|
-
join: { from, to },
|
|
58
|
-
};
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
/*
|
|
62
|
-
Defines a relationship where a record in one model can own a record in another
|
|
63
|
-
model.
|
|
64
|
-
*/
|
|
65
|
-
export function hasOneRelation({ modelClass, from, to }:RelationTypeProps) {
|
|
66
|
-
return {
|
|
67
|
-
relation: HasOneRelation,
|
|
68
|
-
modelClass,
|
|
69
|
-
join: { from, to },
|
|
70
|
-
};
|
|
71
|
-
};
|
|
5
|
+
// Types
|
|
72
6
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
*/
|
|
77
|
-
export function hasManyRelation({ modelClass, from, to }:RelationTypeProps) {
|
|
78
|
-
return {
|
|
79
|
-
relation: HasManyRelation,
|
|
80
|
-
modelClass,
|
|
81
|
-
join: { from, to },
|
|
82
|
-
};
|
|
7
|
+
type ObjectionRelationProps = {
|
|
8
|
+
subject: string;
|
|
9
|
+
modelPath: string;
|
|
83
10
|
};
|
|
84
11
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
modelClass,
|
|
93
|
-
join: { from, through, to },
|
|
94
|
-
};
|
|
95
|
-
};
|
|
12
|
+
export class ObjectionRelation {
|
|
13
|
+
subject: string;
|
|
14
|
+
modelPath: string;
|
|
15
|
+
constructor({ subject, modelPath }: ObjectionRelationProps) {
|
|
16
|
+
this.subject = subject;
|
|
17
|
+
this.modelPath = modelPath;
|
|
18
|
+
}
|
|
96
19
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
20
|
+
belongsTo(object: string, options?: OptionsProps) {
|
|
21
|
+
if (!options) options = { modelPath: this.modelPath };
|
|
22
|
+
if (!options.modelPath) options.modelPath = this.modelPath;
|
|
23
|
+
return relation({
|
|
24
|
+
subject: this.subject,
|
|
25
|
+
relType: 'belongsTo',
|
|
26
|
+
object,
|
|
27
|
+
options,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
104
30
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
31
|
+
hasOne(object: string, options?: OptionsProps) {
|
|
32
|
+
if (!options) options = { modelPath: this.modelPath };
|
|
33
|
+
if (!options.modelPath) options.modelPath = this.modelPath;
|
|
34
|
+
return relation({
|
|
35
|
+
subject: this.subject,
|
|
36
|
+
relType: 'hasOne',
|
|
37
|
+
object,
|
|
38
|
+
options,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
112
41
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
42
|
+
hasMany(object: string, options?: OptionsProps) {
|
|
43
|
+
if (!options) options = { modelPath: this.modelPath };
|
|
44
|
+
if (!options.modelPath) options.modelPath = this.modelPath;
|
|
45
|
+
return relation({
|
|
46
|
+
subject: this.subject,
|
|
47
|
+
relType: 'hasMany',
|
|
48
|
+
object,
|
|
49
|
+
options,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
120
52
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
53
|
+
hasManyThrough(object: string, via: string, options?: OptionsProps) {
|
|
54
|
+
if (!options) options = { modelPath: this.modelPath };
|
|
55
|
+
if (!options.modelPath) options.modelPath = this.modelPath;
|
|
56
|
+
return relation({
|
|
57
|
+
subject: this.subject,
|
|
58
|
+
relType: 'hasManyThrough',
|
|
59
|
+
object,
|
|
60
|
+
via,
|
|
61
|
+
options,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
127
64
|
}
|
|
128
|
-
|
|
129
|
-
/*
|
|
130
|
-
Defines a relationship by passing the subject, the predicate, and the object,
|
|
131
|
-
along with an optional via model.
|
|
132
|
-
*/
|
|
133
|
-
export function relation({subject, relType, object, via, options}:RelationProps) {
|
|
134
|
-
const subjectTable = getSubjectTable({subject, options});
|
|
135
|
-
const objectTable = getObjectTable({object, options});
|
|
136
|
-
const subjectForeignKey = getSubjectForeignKey({subject, options});
|
|
137
|
-
const objectForeignKey = getObjectForeignKey({object, options});
|
|
138
|
-
let viaTable;
|
|
139
|
-
if (via) viaTable = pluralize(snakeCase(via));
|
|
140
|
-
switch (relType) {
|
|
141
|
-
case 'hasOne':
|
|
142
|
-
return hasOneRelation({
|
|
143
|
-
modelClass: object,
|
|
144
|
-
from: `${subjectTable}.id`,
|
|
145
|
-
to: `${objectTable}.${subjectForeignKey}`
|
|
146
|
-
});
|
|
147
|
-
case 'hasMany':
|
|
148
|
-
return hasManyRelation({
|
|
149
|
-
modelClass: object,
|
|
150
|
-
from: `${subjectTable}.id`,
|
|
151
|
-
to: `${objectTable}.${subjectForeignKey}`
|
|
152
|
-
});
|
|
153
|
-
case 'hasManyThrough':
|
|
154
|
-
return hasManyThroughRelation({
|
|
155
|
-
modelClass: object,
|
|
156
|
-
from: `${subjectTable}.id`,
|
|
157
|
-
through: {
|
|
158
|
-
from: `${viaTable}.${subjectForeignKey}`,
|
|
159
|
-
to: `${viaTable}.${objectForeignKey}`,
|
|
160
|
-
},
|
|
161
|
-
to: `${objectTable}.id`
|
|
162
|
-
});
|
|
163
|
-
case 'belongsTo':
|
|
164
|
-
return belongsRelation({
|
|
165
|
-
modelClass: object,
|
|
166
|
-
from: `${subjectTable}.${objectForeignKey}`,
|
|
167
|
-
to: `${objectTable}.id`
|
|
168
|
-
});
|
|
169
|
-
default:
|
|
170
|
-
throw new Error('No valid relationship type specified');
|
|
171
|
-
}
|
|
172
|
-
};
|
package/src/relations.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Dependencies
|
|
2
|
+
import { Model } from 'objection';
|
|
3
|
+
import {
|
|
4
|
+
RelationTypeProps,
|
|
5
|
+
CommonRelationOrTableOrForeignKeyProps,
|
|
6
|
+
} from './global';
|
|
7
|
+
import {
|
|
8
|
+
getSubjectTable,
|
|
9
|
+
getSubjectForeignKey,
|
|
10
|
+
getObjectTable,
|
|
11
|
+
getObjectForeignKey,
|
|
12
|
+
getModelClass,
|
|
13
|
+
getViaTable,
|
|
14
|
+
} from './helpers';
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
HasOneRelation,
|
|
18
|
+
BelongsToOneRelation,
|
|
19
|
+
HasManyRelation,
|
|
20
|
+
ManyToManyRelation,
|
|
21
|
+
} = Model;
|
|
22
|
+
|
|
23
|
+
// Types
|
|
24
|
+
|
|
25
|
+
type AdvancedRelationTypeProps = RelationTypeProps & {
|
|
26
|
+
through: {
|
|
27
|
+
from: string;
|
|
28
|
+
to: string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
Defines a relationship where a record in one model can belong to a record in
|
|
34
|
+
another model.
|
|
35
|
+
*/
|
|
36
|
+
export function belongsRelation({ modelClass, from, to }: RelationTypeProps) {
|
|
37
|
+
return {
|
|
38
|
+
relation: BelongsToOneRelation,
|
|
39
|
+
modelClass,
|
|
40
|
+
join: { from, to },
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/*
|
|
45
|
+
Defines a relationship where a record in one model can own a record in another
|
|
46
|
+
model.
|
|
47
|
+
*/
|
|
48
|
+
export function hasOneRelation({ modelClass, from, to }: RelationTypeProps) {
|
|
49
|
+
return {
|
|
50
|
+
relation: HasOneRelation,
|
|
51
|
+
modelClass,
|
|
52
|
+
join: { from, to },
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
Defines a relationship where a record in one model can own many records in
|
|
58
|
+
another model.
|
|
59
|
+
*/
|
|
60
|
+
export function hasManyRelation({ modelClass, from, to }: RelationTypeProps) {
|
|
61
|
+
return {
|
|
62
|
+
relation: HasManyRelation,
|
|
63
|
+
modelClass,
|
|
64
|
+
join: { from, to },
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/*
|
|
69
|
+
Defines a relationship where a record in one model can own many records in
|
|
70
|
+
another model, via a join table
|
|
71
|
+
*/
|
|
72
|
+
export function hasManyThroughRelation({
|
|
73
|
+
modelClass,
|
|
74
|
+
from,
|
|
75
|
+
through,
|
|
76
|
+
to,
|
|
77
|
+
}: AdvancedRelationTypeProps) {
|
|
78
|
+
return {
|
|
79
|
+
relation: ManyToManyRelation,
|
|
80
|
+
modelClass,
|
|
81
|
+
join: { from, through, to },
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
type RelationProps = CommonRelationOrTableOrForeignKeyProps & {
|
|
86
|
+
subject: string;
|
|
87
|
+
relType: 'hasOne' | 'hasMany' | 'hasManyThrough' | 'belongsTo';
|
|
88
|
+
object: string;
|
|
89
|
+
via?: string;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/*
|
|
93
|
+
Defines a relationship by passing the subject, the predicate, and the object,
|
|
94
|
+
along with an optional via model.
|
|
95
|
+
*/
|
|
96
|
+
export function relation({
|
|
97
|
+
subject,
|
|
98
|
+
relType,
|
|
99
|
+
object,
|
|
100
|
+
via,
|
|
101
|
+
options,
|
|
102
|
+
}: RelationProps) {
|
|
103
|
+
const subjectTable = getSubjectTable({ subject, options });
|
|
104
|
+
const objectTable = getObjectTable({ object, options });
|
|
105
|
+
const subjectForeignKey = getSubjectForeignKey({ subject, options });
|
|
106
|
+
const objectForeignKey = getObjectForeignKey({ object, options });
|
|
107
|
+
const modelClass = getModelClass({ object, options });
|
|
108
|
+
const viaTable = getViaTable(via);
|
|
109
|
+
switch (relType) {
|
|
110
|
+
case 'hasOne':
|
|
111
|
+
return hasOneRelation({
|
|
112
|
+
modelClass,
|
|
113
|
+
from: `${subjectTable}.id`,
|
|
114
|
+
to: `${objectTable}.${subjectForeignKey}`,
|
|
115
|
+
});
|
|
116
|
+
case 'hasMany':
|
|
117
|
+
return hasManyRelation({
|
|
118
|
+
modelClass,
|
|
119
|
+
from: `${subjectTable}.id`,
|
|
120
|
+
to: `${objectTable}.${subjectForeignKey}`,
|
|
121
|
+
});
|
|
122
|
+
case 'hasManyThrough':
|
|
123
|
+
return hasManyThroughRelation({
|
|
124
|
+
modelClass,
|
|
125
|
+
from: `${subjectTable}.id`,
|
|
126
|
+
through: {
|
|
127
|
+
from: `${viaTable}.${subjectForeignKey}`,
|
|
128
|
+
to: `${viaTable}.${objectForeignKey}`,
|
|
129
|
+
},
|
|
130
|
+
to: `${objectTable}.id`,
|
|
131
|
+
});
|
|
132
|
+
case 'belongsTo':
|
|
133
|
+
return belongsRelation({
|
|
134
|
+
modelClass,
|
|
135
|
+
from: `${subjectTable}.${objectForeignKey}`,
|
|
136
|
+
to: `${objectTable}.id`,
|
|
137
|
+
});
|
|
138
|
+
default:
|
|
139
|
+
throw new Error('No valid relationship type specified');
|
|
140
|
+
}
|
|
141
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
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 & {
|
|
10
|
-
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;
|
|
19
|
-
};
|
|
20
|
-
declare type AdvancedRelationTypeProps = RelationTypeProps & {
|
|
21
|
-
through: {
|
|
22
|
-
from: string;
|
|
23
|
-
to: string;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
declare type SubjectProps = CommonRelationOrTableOrForeignKeyProps & {
|
|
27
|
-
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;
|
|
46
|
-
};
|
|
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;
|
|
54
|
-
};
|
|
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: {
|
|
62
|
-
from: string;
|
|
63
|
-
to: string;
|
|
64
|
-
};
|
|
65
|
-
to: string;
|
|
66
|
-
};
|
|
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;
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
export {};
|
package/dist/index.js
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
-
|
|
7
|
-
var objection = require('objection');
|
|
8
|
-
var snakeCase = _interopDefault(require('lodash.snakecase'));
|
|
9
|
-
var pluralize = _interopDefault(require('pluralize'));
|
|
10
|
-
|
|
11
|
-
// Dependencies
|
|
12
|
-
const {
|
|
13
|
-
HasOneRelation,
|
|
14
|
-
BelongsToOneRelation,
|
|
15
|
-
HasManyRelation,
|
|
16
|
-
ManyToManyRelation
|
|
17
|
-
} = objection.Model;
|
|
18
|
-
/*
|
|
19
|
-
Defines a relationship where a record in one model can belong to a record in
|
|
20
|
-
another model.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
function belongsRelation({
|
|
24
|
-
modelClass,
|
|
25
|
-
from,
|
|
26
|
-
to
|
|
27
|
-
}) {
|
|
28
|
-
return {
|
|
29
|
-
relation: BelongsToOneRelation,
|
|
30
|
-
modelClass,
|
|
31
|
-
join: {
|
|
32
|
-
from,
|
|
33
|
-
to
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
/*
|
|
38
|
-
Defines a relationship where a record in one model can own a record in another
|
|
39
|
-
model.
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
function hasOneRelation({
|
|
43
|
-
modelClass,
|
|
44
|
-
from,
|
|
45
|
-
to
|
|
46
|
-
}) {
|
|
47
|
-
return {
|
|
48
|
-
relation: HasOneRelation,
|
|
49
|
-
modelClass,
|
|
50
|
-
join: {
|
|
51
|
-
from,
|
|
52
|
-
to
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
/*
|
|
57
|
-
Defines a relationship where a record in one model can own many records in
|
|
58
|
-
another model.
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
|
-
function hasManyRelation({
|
|
62
|
-
modelClass,
|
|
63
|
-
from,
|
|
64
|
-
to
|
|
65
|
-
}) {
|
|
66
|
-
return {
|
|
67
|
-
relation: HasManyRelation,
|
|
68
|
-
modelClass,
|
|
69
|
-
join: {
|
|
70
|
-
from,
|
|
71
|
-
to
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/*
|
|
76
|
-
Defines a relationship where a record in one model can own many records in
|
|
77
|
-
another model, via a join table
|
|
78
|
-
*/
|
|
79
|
-
|
|
80
|
-
function hasManyThroughRelation({
|
|
81
|
-
modelClass,
|
|
82
|
-
from,
|
|
83
|
-
through,
|
|
84
|
-
to
|
|
85
|
-
}) {
|
|
86
|
-
return {
|
|
87
|
-
relation: ManyToManyRelation,
|
|
88
|
-
modelClass,
|
|
89
|
-
join: {
|
|
90
|
-
from,
|
|
91
|
-
through,
|
|
92
|
-
to
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
}
|
|
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
|
-
/*
|
|
141
|
-
Defines a relationship by passing the subject, the predicate, and the object,
|
|
142
|
-
along with an optional via model.
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
function relation({
|
|
146
|
-
subject,
|
|
147
|
-
relType,
|
|
148
|
-
object,
|
|
149
|
-
via,
|
|
150
|
-
options
|
|
151
|
-
}) {
|
|
152
|
-
const subjectTable = getSubjectTable({
|
|
153
|
-
subject,
|
|
154
|
-
options
|
|
155
|
-
});
|
|
156
|
-
const objectTable = getObjectTable({
|
|
157
|
-
object,
|
|
158
|
-
options
|
|
159
|
-
});
|
|
160
|
-
const subjectForeignKey = getSubjectForeignKey({
|
|
161
|
-
subject,
|
|
162
|
-
options
|
|
163
|
-
});
|
|
164
|
-
const objectForeignKey = getObjectForeignKey({
|
|
165
|
-
object,
|
|
166
|
-
options
|
|
167
|
-
});
|
|
168
|
-
let viaTable;
|
|
169
|
-
if (via) viaTable = pluralize(snakeCase(via));
|
|
170
|
-
|
|
171
|
-
switch (relType) {
|
|
172
|
-
case 'hasOne':
|
|
173
|
-
return hasOneRelation({
|
|
174
|
-
modelClass: object,
|
|
175
|
-
from: `${subjectTable}.id`,
|
|
176
|
-
to: `${objectTable}.${subjectForeignKey}`
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
case 'hasMany':
|
|
180
|
-
return hasManyRelation({
|
|
181
|
-
modelClass: object,
|
|
182
|
-
from: `${subjectTable}.id`,
|
|
183
|
-
to: `${objectTable}.${subjectForeignKey}`
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
case 'hasManyThrough':
|
|
187
|
-
return hasManyThroughRelation({
|
|
188
|
-
modelClass: object,
|
|
189
|
-
from: `${subjectTable}.id`,
|
|
190
|
-
through: {
|
|
191
|
-
from: `${viaTable}.${subjectForeignKey}`,
|
|
192
|
-
to: `${viaTable}.${objectForeignKey}`
|
|
193
|
-
},
|
|
194
|
-
to: `${objectTable}.id`
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
case 'belongsTo':
|
|
198
|
-
return belongsRelation({
|
|
199
|
-
modelClass: object,
|
|
200
|
-
from: `${subjectTable}.${objectForeignKey}`,
|
|
201
|
-
to: `${objectTable}.id`
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
default:
|
|
205
|
-
throw new Error('No valid relationship type specified');
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
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;
|
|
218
|
-
//# sourceMappingURL=objection-relations.cjs.development.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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,uBAAu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BF;AAgCA;;;;;;;;;;;;"}
|
|
@@ -1,2 +0,0 @@
|
|
|
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")}};
|
|
2
|
-
//# sourceMappingURL=objection-relations.cjs.production.min.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"objection-relations.cjs.production.min.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":"gOAEA,MAAMA,eACLA,EADKC,qBAELA,EAFKC,gBAGLA,EAHKC,mBAILA,GACGC,iBA6CYC,GAAgBC,WAAEA,EAAFC,KAAcA,EAAdC,GAAoBA,UAC5C,CACNC,SAAUR,EACVK,WAAAA,EACAI,KAAM,CAAEH,KAAAA,EAAMC,GAAAA,aAQAG,GAAeL,WAAEA,EAAFC,KAAcA,EAAdC,GAAoBA,UAC3C,CACNC,SAAUT,EACVM,WAAAA,EACAI,KAAM,CAAEH,KAAAA,EAAMC,GAAAA,aAQAI,GAAgBN,WAAEA,EAAFC,KAAcA,EAAdC,GAAoBA,UAC5C,CACNC,SAAUP,EACJI,WAAAA,EACNI,KAAM,CAAEH,KAAAA,EAAMC,GAAAA,aAQAK,GAAuBP,WAAEA,EAAFC,KAAcA,EAAdO,QAAoBA,EAApBN,GAA6BA,UAC5D,CACNC,SAAUN,EACJG,WAAAA,EACNI,KAAM,CAAEH,KAAAA,EAAMO,QAAAA,EAASN,GAAAA,aAQTO,GAAgBC,QAAEA,EAAFC,QAAWA,iBAChCA,SAAAA,EAASC,eAAgBC,EAAUC,EAAUJ,aAOxCK,GAAeC,OAAEA,EAAFL,QAAUA,iBAC9BA,SAAAA,EAASM,cAAeJ,EAAUC,EAAUE,aAOvCE,GAAqBR,QAAEA,EAAFC,QAAWA,iBACrCA,SAAAA,EAASQ,oBAAqBL,EAAUJ,GAAW,eAO9CU,GAAoBJ,OAAEA,EAAFL,QAAUA,iBACnCA,SAAAA,EAASU,mBAAoBP,EAAUE,GAAU,+PAOnCN,QAACA,EAADY,QAAUA,EAAVN,OAAmBA,EAAnBO,IAA2BA,EAA3BZ,QAAgCA,UAClDC,EAAeH,EAAgB,CAACC,QAAAA,EAASC,QAAAA,IACzCM,EAAcF,EAAe,CAACC,OAAAA,EAAQL,QAAAA,IACtCQ,EAAoBD,EAAqB,CAACR,QAAAA,EAASC,QAAAA,IACnDU,EAAmBD,EAAoB,CAACJ,OAAAA,EAAQL,QAAAA,QAClDa,SACAD,IAAKC,EAAWX,EAAUC,EAAUS,KAChCD,OACF,gBACGjB,EAAe,CACTL,WAAYgB,EACxBf,KAASW,QACTV,MAAOe,KAAeE,UAEnB,iBACGb,EAAgB,CACtBN,WAAYgB,EACZf,KAASW,QACTV,MAAOe,KAAeE,UAEnB,wBACGZ,EAAuB,CAC7BP,WAAYgB,EACZf,KAASW,QACTJ,QAAS,CACRP,QAASuB,KAAYL,IACrBjB,MAAOsB,KAAYH,KAEpBnB,GAAOe,cAEJ,mBACGlB,EAAgB,CACtBC,WAAYgB,EACZf,QAASW,KAAgBS,IACzBnB,GAAOe,wBAGF,IAAIQ,MAAM"}
|
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import { Model } from 'objection';
|
|
2
|
-
import snakeCase from 'lodash.snakecase';
|
|
3
|
-
import pluralize from 'pluralize';
|
|
4
|
-
|
|
5
|
-
// Dependencies
|
|
6
|
-
const {
|
|
7
|
-
HasOneRelation,
|
|
8
|
-
BelongsToOneRelation,
|
|
9
|
-
HasManyRelation,
|
|
10
|
-
ManyToManyRelation
|
|
11
|
-
} = Model;
|
|
12
|
-
/*
|
|
13
|
-
Defines a relationship where a record in one model can belong to a record in
|
|
14
|
-
another model.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
function belongsRelation({
|
|
18
|
-
modelClass,
|
|
19
|
-
from,
|
|
20
|
-
to
|
|
21
|
-
}) {
|
|
22
|
-
return {
|
|
23
|
-
relation: BelongsToOneRelation,
|
|
24
|
-
modelClass,
|
|
25
|
-
join: {
|
|
26
|
-
from,
|
|
27
|
-
to
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
/*
|
|
32
|
-
Defines a relationship where a record in one model can own a record in another
|
|
33
|
-
model.
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
function hasOneRelation({
|
|
37
|
-
modelClass,
|
|
38
|
-
from,
|
|
39
|
-
to
|
|
40
|
-
}) {
|
|
41
|
-
return {
|
|
42
|
-
relation: HasOneRelation,
|
|
43
|
-
modelClass,
|
|
44
|
-
join: {
|
|
45
|
-
from,
|
|
46
|
-
to
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
/*
|
|
51
|
-
Defines a relationship where a record in one model can own many records in
|
|
52
|
-
another model.
|
|
53
|
-
*/
|
|
54
|
-
|
|
55
|
-
function hasManyRelation({
|
|
56
|
-
modelClass,
|
|
57
|
-
from,
|
|
58
|
-
to
|
|
59
|
-
}) {
|
|
60
|
-
return {
|
|
61
|
-
relation: HasManyRelation,
|
|
62
|
-
modelClass,
|
|
63
|
-
join: {
|
|
64
|
-
from,
|
|
65
|
-
to
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
/*
|
|
70
|
-
Defines a relationship where a record in one model can own many records in
|
|
71
|
-
another model, via a join table
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
function hasManyThroughRelation({
|
|
75
|
-
modelClass,
|
|
76
|
-
from,
|
|
77
|
-
through,
|
|
78
|
-
to
|
|
79
|
-
}) {
|
|
80
|
-
return {
|
|
81
|
-
relation: ManyToManyRelation,
|
|
82
|
-
modelClass,
|
|
83
|
-
join: {
|
|
84
|
-
from,
|
|
85
|
-
through,
|
|
86
|
-
to
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
/*
|
|
91
|
-
Gets the SQL table for the subject, either from the options object or the
|
|
92
|
-
plural version of the subject model.
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
function getSubjectTable({
|
|
96
|
-
subject,
|
|
97
|
-
options
|
|
98
|
-
}) {
|
|
99
|
-
return (options == null ? void 0 : options.subjectTable) || pluralize(snakeCase(subject));
|
|
100
|
-
}
|
|
101
|
-
/*
|
|
102
|
-
Gets the SQL table for the object, either from the options object or the
|
|
103
|
-
plural version of the object model.
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
function getObjectTable({
|
|
107
|
-
object,
|
|
108
|
-
options
|
|
109
|
-
}) {
|
|
110
|
-
return (options == null ? void 0 : options.objectTable) || pluralize(snakeCase(object));
|
|
111
|
-
}
|
|
112
|
-
/*
|
|
113
|
-
Gets the SQL foreign key for the subject, either from the options object
|
|
114
|
-
or the snake case of the subject model.
|
|
115
|
-
*/
|
|
116
|
-
|
|
117
|
-
function getSubjectForeignKey({
|
|
118
|
-
subject,
|
|
119
|
-
options
|
|
120
|
-
}) {
|
|
121
|
-
return (options == null ? void 0 : options.subjectForeignKey) || snakeCase(subject) + '_id';
|
|
122
|
-
}
|
|
123
|
-
/*
|
|
124
|
-
Gets the SQL foreign key for the object, either from the options object
|
|
125
|
-
or the snake case of the object model.
|
|
126
|
-
*/
|
|
127
|
-
|
|
128
|
-
function getObjectForeignKey({
|
|
129
|
-
object,
|
|
130
|
-
options
|
|
131
|
-
}) {
|
|
132
|
-
return (options == null ? void 0 : options.objectForeignKey) || snakeCase(object) + '_id';
|
|
133
|
-
}
|
|
134
|
-
/*
|
|
135
|
-
Defines a relationship by passing the subject, the predicate, and the object,
|
|
136
|
-
along with an optional via model.
|
|
137
|
-
*/
|
|
138
|
-
|
|
139
|
-
function relation({
|
|
140
|
-
subject,
|
|
141
|
-
relType,
|
|
142
|
-
object,
|
|
143
|
-
via,
|
|
144
|
-
options
|
|
145
|
-
}) {
|
|
146
|
-
const subjectTable = getSubjectTable({
|
|
147
|
-
subject,
|
|
148
|
-
options
|
|
149
|
-
});
|
|
150
|
-
const objectTable = getObjectTable({
|
|
151
|
-
object,
|
|
152
|
-
options
|
|
153
|
-
});
|
|
154
|
-
const subjectForeignKey = getSubjectForeignKey({
|
|
155
|
-
subject,
|
|
156
|
-
options
|
|
157
|
-
});
|
|
158
|
-
const objectForeignKey = getObjectForeignKey({
|
|
159
|
-
object,
|
|
160
|
-
options
|
|
161
|
-
});
|
|
162
|
-
let viaTable;
|
|
163
|
-
if (via) viaTable = pluralize(snakeCase(via));
|
|
164
|
-
|
|
165
|
-
switch (relType) {
|
|
166
|
-
case 'hasOne':
|
|
167
|
-
return hasOneRelation({
|
|
168
|
-
modelClass: object,
|
|
169
|
-
from: `${subjectTable}.id`,
|
|
170
|
-
to: `${objectTable}.${subjectForeignKey}`
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
case 'hasMany':
|
|
174
|
-
return hasManyRelation({
|
|
175
|
-
modelClass: object,
|
|
176
|
-
from: `${subjectTable}.id`,
|
|
177
|
-
to: `${objectTable}.${subjectForeignKey}`
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
case 'hasManyThrough':
|
|
181
|
-
return hasManyThroughRelation({
|
|
182
|
-
modelClass: object,
|
|
183
|
-
from: `${subjectTable}.id`,
|
|
184
|
-
through: {
|
|
185
|
-
from: `${viaTable}.${subjectForeignKey}`,
|
|
186
|
-
to: `${viaTable}.${objectForeignKey}`
|
|
187
|
-
},
|
|
188
|
-
to: `${objectTable}.id`
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
case 'belongsTo':
|
|
192
|
-
return belongsRelation({
|
|
193
|
-
modelClass: object,
|
|
194
|
-
from: `${subjectTable}.${objectForeignKey}`,
|
|
195
|
-
to: `${objectTable}.id`
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
default:
|
|
199
|
-
throw new Error('No valid relationship type specified');
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export { belongsRelation, getObjectForeignKey, getObjectTable, getSubjectForeignKey, getSubjectTable, hasManyRelation, hasManyThroughRelation, hasOneRelation, relation };
|
|
204
|
-
//# sourceMappingURL=objection-relations.esm.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"objection-relations.esm.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,KALJ;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;;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