@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/src/index.ts CHANGED
@@ -1,172 +1,64 @@
1
1
  // Dependencies
2
- import { Model } from 'objection';
3
- const {
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
- type CommonRelationOrTableOrForeignKeyProps = {
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
- Defines a relationship where a record in one model can own many records in
75
- another model.
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
- Defines a relationship where a record in one model can own many records in
87
- another model, via a join table
88
- */
89
- export function hasManyThroughRelation({ modelClass, from, through, to }:AdvancedRelationTypeProps) {
90
- return {
91
- relation: ManyToManyRelation,
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
- Gets the SQL table for the subject, either from the options object or the
99
- plural version of the subject model.
100
- */
101
- export function getSubjectTable({ subject, options }:SubjectProps) {
102
- return options?.subjectTable || pluralize(snakeCase(subject));
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
- Gets the SQL table for the object, either from the options object or the
107
- plural version of the object model.
108
- */
109
- export function getObjectTable({ object, options }:ObjectProps) {
110
- return options?.objectTable || pluralize(snakeCase(object));
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
- Gets the SQL foreign key for the subject, either from the options object
115
- or the snake case of the subject model.
116
- */
117
- export function getSubjectForeignKey({ subject, options }:SubjectProps) {
118
- return options?.subjectForeignKey || snakeCase(subject) + '_id';
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
- Gets the SQL foreign key for the object, either from the options object
123
- or the snake case of the object model.
124
- */
125
- export function getObjectForeignKey({ object, options }:ObjectProps) {
126
- return options?.objectForeignKey || snakeCase(object) + '_id';
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
- };
@@ -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
+ }