@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 +31 -52
- package/dist/helpers.d.ts +15 -0
- package/dist/index.d.ts +32 -69
- package/dist/objection-relations.cjs.development.js +133 -59
- package/dist/objection-relations.cjs.development.js.map +1 -1
- package/dist/objection-relations.cjs.production.min.js +1 -1
- package/dist/objection-relations.cjs.production.min.js.map +1 -1
- package/dist/objection-relations.esm.js +133 -51
- package/dist/objection-relations.esm.js.map +1 -1
- package/dist/relations.d.ts +58 -0
- package/package.json +3 -3
- 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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"objection-relations.cjs.production.min.js","sources":["../src/index.ts"],"sourcesContent":["// Dependencies\nimport { Model } from 'objection';\
|
|
1
|
+
{"version":3,"file":"objection-relations.cjs.production.min.js","sources":["../src/relations.ts","../src/helpers.ts","../src/index.ts"],"sourcesContent":["// 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","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 { 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":["HasOneRelation","BelongsToOneRelation","HasManyRelation","ManyToManyRelation","Model","relation","subject","relType","object","via","options","subjectTable","pluralize","snakeCase","getSubjectTable","objectTable","getObjectTable","subjectForeignKey","getSubjectForeignKey","objectForeignKey","getObjectForeignKey","modelClass","modelPath","join","getModelClass","viaTable","getViaTable","from","to","hasOneRelation","hasManyRelation","through","hasManyThroughRelation","belongsRelation","Error","constructor","belongsTo","this","hasOne","hasMany","hasManyThrough"],"mappings":"kPAeA,MAAMA,eACJA,EADIC,qBAEJA,EAFIC,gBAGJA,EAHIC,mBAIJA,GACEC,QA2EJ,SAAgBC,GAASC,QACvBA,EADuBC,QAEvBA,EAFuBC,OAGvBA,EAHuBC,IAIvBA,EAJuBC,QAKvBA,UAEMC,YCjFwBL,QAAEA,EAAFI,QAAWA,iBAClCA,SAAAA,EAASC,eAAgBC,EAAUC,EAAUP,IDgF/BQ,CAAgB,CAAER,QAAAA,EAASI,QAAAA,IAC1CK,YC1EuBP,OAAEA,EAAFE,QAAUA,iBAChCA,SAAAA,EAASK,cAAeH,EAAUC,EAAUL,IDyE/BQ,CAAe,CAAER,OAAAA,EAAQE,QAAAA,IACvCO,YCnE6BX,QAAEA,EAAFI,QAAWA,iBACvCA,SAAAA,EAASO,oBAAqBJ,EAAUP,GAAW,MDkEhCY,CAAqB,CAAEZ,QAAAA,EAASI,QAAAA,IACpDS,YC5D4BX,OAAEA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AAImB,MAAM,yEE5HpBC,aAAY7B,QAAEA,EAAFgB,UAAWA,SAChBhB,QAAUA,OACVgB,UAAYA,EAGnBc,UAAU5B,EAAgBE,UACnBA,IAASA,EAAU,CAAEY,UAAWe,KAAKf,YACrCZ,EAAQY,YAAWZ,EAAQY,UAAYe,KAAKf,WAC1CjB,EAAS,CACdC,QAAS+B,KAAK/B,QACdC,QAAS,YACTC,OAAAA,EACAE,QAAAA,IAIJ4B,OAAO9B,EAAgBE,UAChBA,IAASA,EAAU,CAAEY,UAAWe,KAAKf,YACrCZ,EAAQY,YAAWZ,EAAQY,UAAYe,KAAKf,WAC1CjB,EAAS,CACdC,QAAS+B,KAAK/B,QACdC,QAAS,SACTC,OAAAA,EACAE,QAAAA,IAIJ6B,QAAQ/B,EAAgBE,UACjBA,IAASA,EAAU,CAAEY,UAAWe,KAAKf,YACrCZ,EAAQY,YAAWZ,EAAQY,UAAYe,KAAKf,WAC1CjB,EAAS,CACdC,QAAS+B,KAAK/B,QACdC,QAAS,UACTC,OAAAA,EACAE,QAAAA,IAIJ8B,eAAehC,EAAgBC,EAAaC,UACrCA,IAASA,EAAU,CAAEY,UAAWe,KAAKf,YACrCZ,EAAQY,YAAWZ,EAAQY,UAAYe,KAAKf,WAC1CjB,EAAS,CACdC,QAAS+B,KAAK/B,QACdC,QAAS,iBACTC,OAAAA,EACAC,IAAAA,EACAC,QAAAA"}
|
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
import { Model } from 'objection';
|
|
2
2
|
import snakeCase from 'lodash.snakecase';
|
|
3
3
|
import pluralize from 'pluralize';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
Gets the SQL table for the subject, either from the options object or the
|
|
8
|
+
plural version of the subject model.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
function getSubjectTable({
|
|
12
|
+
subject,
|
|
13
|
+
options
|
|
14
|
+
}) {
|
|
15
|
+
return (options == null ? void 0 : options.subjectTable) || pluralize(snakeCase(subject));
|
|
16
|
+
}
|
|
17
|
+
/*
|
|
18
|
+
Gets the SQL table for the object, either from the options object or the
|
|
19
|
+
plural version of the object model.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
function getObjectTable({
|
|
23
|
+
object,
|
|
24
|
+
options
|
|
25
|
+
}) {
|
|
26
|
+
return (options == null ? void 0 : options.objectTable) || pluralize(snakeCase(object));
|
|
27
|
+
}
|
|
28
|
+
/*
|
|
29
|
+
Gets the SQL foreign key for the subject, either from the options object
|
|
30
|
+
or the snake case of the subject model.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
function getSubjectForeignKey({
|
|
34
|
+
subject,
|
|
35
|
+
options
|
|
36
|
+
}) {
|
|
37
|
+
return (options == null ? void 0 : options.subjectForeignKey) || snakeCase(subject) + '_id';
|
|
38
|
+
}
|
|
39
|
+
/*
|
|
40
|
+
Gets the SQL foreign key for the object, either from the options object
|
|
41
|
+
or the snake case of the object model.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
function getObjectForeignKey({
|
|
45
|
+
object,
|
|
46
|
+
options
|
|
47
|
+
}) {
|
|
48
|
+
return (options == null ? void 0 : options.objectForeignKey) || snakeCase(object) + '_id';
|
|
49
|
+
}
|
|
50
|
+
/*
|
|
51
|
+
Allows you to define the model path for a model
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
function getModelClass({
|
|
55
|
+
object,
|
|
56
|
+
options
|
|
57
|
+
}) {
|
|
58
|
+
return options != null && options.modelPath ? join(options.modelPath, object) : object;
|
|
59
|
+
}
|
|
60
|
+
function getViaTable(via) {
|
|
61
|
+
return via ? pluralize(snakeCase(via)) : null;
|
|
62
|
+
}
|
|
4
63
|
|
|
5
64
|
// Dependencies
|
|
6
65
|
const {
|
|
@@ -87,50 +146,6 @@ function hasManyThroughRelation({
|
|
|
87
146
|
}
|
|
88
147
|
};
|
|
89
148
|
}
|
|
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
149
|
/*
|
|
135
150
|
Defines a relationship by passing the subject, the predicate, and the object,
|
|
136
151
|
along with an optional via model.
|
|
@@ -159,27 +174,30 @@ function relation({
|
|
|
159
174
|
object,
|
|
160
175
|
options
|
|
161
176
|
});
|
|
162
|
-
|
|
163
|
-
|
|
177
|
+
const modelClass = getModelClass({
|
|
178
|
+
object,
|
|
179
|
+
options
|
|
180
|
+
});
|
|
181
|
+
const viaTable = getViaTable(via);
|
|
164
182
|
|
|
165
183
|
switch (relType) {
|
|
166
184
|
case 'hasOne':
|
|
167
185
|
return hasOneRelation({
|
|
168
|
-
modelClass
|
|
186
|
+
modelClass,
|
|
169
187
|
from: `${subjectTable}.id`,
|
|
170
188
|
to: `${objectTable}.${subjectForeignKey}`
|
|
171
189
|
});
|
|
172
190
|
|
|
173
191
|
case 'hasMany':
|
|
174
192
|
return hasManyRelation({
|
|
175
|
-
modelClass
|
|
193
|
+
modelClass,
|
|
176
194
|
from: `${subjectTable}.id`,
|
|
177
195
|
to: `${objectTable}.${subjectForeignKey}`
|
|
178
196
|
});
|
|
179
197
|
|
|
180
198
|
case 'hasManyThrough':
|
|
181
199
|
return hasManyThroughRelation({
|
|
182
|
-
modelClass
|
|
200
|
+
modelClass,
|
|
183
201
|
from: `${subjectTable}.id`,
|
|
184
202
|
through: {
|
|
185
203
|
from: `${viaTable}.${subjectForeignKey}`,
|
|
@@ -190,7 +208,7 @@ function relation({
|
|
|
190
208
|
|
|
191
209
|
case 'belongsTo':
|
|
192
210
|
return belongsRelation({
|
|
193
|
-
modelClass
|
|
211
|
+
modelClass,
|
|
194
212
|
from: `${subjectTable}.${objectForeignKey}`,
|
|
195
213
|
to: `${objectTable}.id`
|
|
196
214
|
});
|
|
@@ -200,5 +218,69 @@ function relation({
|
|
|
200
218
|
}
|
|
201
219
|
}
|
|
202
220
|
|
|
203
|
-
|
|
221
|
+
class ObjectionRelation {
|
|
222
|
+
constructor({
|
|
223
|
+
subject,
|
|
224
|
+
modelPath
|
|
225
|
+
}) {
|
|
226
|
+
this.subject = subject;
|
|
227
|
+
this.modelPath = modelPath;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
belongsTo(object, options) {
|
|
231
|
+
if (!options) options = {
|
|
232
|
+
modelPath: this.modelPath
|
|
233
|
+
};
|
|
234
|
+
if (!options.modelPath) options.modelPath = this.modelPath;
|
|
235
|
+
return relation({
|
|
236
|
+
subject: this.subject,
|
|
237
|
+
relType: 'belongsTo',
|
|
238
|
+
object,
|
|
239
|
+
options
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
hasOne(object, options) {
|
|
244
|
+
if (!options) options = {
|
|
245
|
+
modelPath: this.modelPath
|
|
246
|
+
};
|
|
247
|
+
if (!options.modelPath) options.modelPath = this.modelPath;
|
|
248
|
+
return relation({
|
|
249
|
+
subject: this.subject,
|
|
250
|
+
relType: 'hasOne',
|
|
251
|
+
object,
|
|
252
|
+
options
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
hasMany(object, options) {
|
|
257
|
+
if (!options) options = {
|
|
258
|
+
modelPath: this.modelPath
|
|
259
|
+
};
|
|
260
|
+
if (!options.modelPath) options.modelPath = this.modelPath;
|
|
261
|
+
return relation({
|
|
262
|
+
subject: this.subject,
|
|
263
|
+
relType: 'hasMany',
|
|
264
|
+
object,
|
|
265
|
+
options
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
hasManyThrough(object, via, options) {
|
|
270
|
+
if (!options) options = {
|
|
271
|
+
modelPath: this.modelPath
|
|
272
|
+
};
|
|
273
|
+
if (!options.modelPath) options.modelPath = this.modelPath;
|
|
274
|
+
return relation({
|
|
275
|
+
subject: this.subject,
|
|
276
|
+
relType: 'hasManyThrough',
|
|
277
|
+
object,
|
|
278
|
+
via,
|
|
279
|
+
options
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export { ObjectionRelation };
|
|
204
286
|
//# sourceMappingURL=objection-relations.esm.js.map
|
|
@@ -1 +1 @@
|
|
|
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;;;;;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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objection-relations.esm.js","sources":["../src/helpers.ts","../src/relations.ts","../src/index.ts"],"sourcesContent":["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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AO,EAAE,KAAKA,OADA;AAEd+B,MAAAA,OAAO,EAAE,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;;;;;;"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { RelationTypeProps, CommonRelationOrTableOrForeignKeyProps } from './global';
|
|
2
|
+
declare type AdvancedRelationTypeProps = RelationTypeProps & {
|
|
3
|
+
through: {
|
|
4
|
+
from: string;
|
|
5
|
+
to: string;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export declare function belongsRelation({ modelClass, from, to }: RelationTypeProps): {
|
|
9
|
+
relation: import("objection").RelationType;
|
|
10
|
+
modelClass: string;
|
|
11
|
+
join: {
|
|
12
|
+
from: string;
|
|
13
|
+
to: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export declare function hasOneRelation({ modelClass, from, to }: RelationTypeProps): {
|
|
17
|
+
relation: import("objection").RelationType;
|
|
18
|
+
modelClass: string;
|
|
19
|
+
join: {
|
|
20
|
+
from: string;
|
|
21
|
+
to: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export declare function hasManyRelation({ modelClass, from, to }: RelationTypeProps): {
|
|
25
|
+
relation: import("objection").RelationType;
|
|
26
|
+
modelClass: string;
|
|
27
|
+
join: {
|
|
28
|
+
from: string;
|
|
29
|
+
to: string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
export declare function hasManyThroughRelation({ modelClass, from, through, to, }: AdvancedRelationTypeProps): {
|
|
33
|
+
relation: import("objection").RelationType;
|
|
34
|
+
modelClass: string;
|
|
35
|
+
join: {
|
|
36
|
+
from: string;
|
|
37
|
+
through: {
|
|
38
|
+
from: string;
|
|
39
|
+
to: string;
|
|
40
|
+
};
|
|
41
|
+
to: string;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
declare type RelationProps = CommonRelationOrTableOrForeignKeyProps & {
|
|
45
|
+
subject: string;
|
|
46
|
+
relType: 'hasOne' | 'hasMany' | 'hasManyThrough' | 'belongsTo';
|
|
47
|
+
object: string;
|
|
48
|
+
via?: string;
|
|
49
|
+
};
|
|
50
|
+
export declare function relation({ subject, relType, object, via, options, }: RelationProps): {
|
|
51
|
+
relation: import("objection").RelationType;
|
|
52
|
+
modelClass: string;
|
|
53
|
+
join: {
|
|
54
|
+
from: string;
|
|
55
|
+
to: string;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.2",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"typings": "dist/index.d.ts",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"scripts": {
|
|
32
32
|
"start": "tsdx watch --target node",
|
|
33
33
|
"build": "tsdx build --target node",
|
|
34
|
-
"test": "tsdx test",
|
|
35
|
-
"lint": "tsdx lint",
|
|
34
|
+
"test": "tsdx test --collectCoverage",
|
|
35
|
+
"lint": "tsdx lint --fix",
|
|
36
36
|
"prepare": "tsdx build --target node",
|
|
37
37
|
"size": "size-limit",
|
|
38
38
|
"analyze": "size-limit --why"
|
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
|
+
}
|