@anephenix/objection-relations 0.0.19 → 0.0.21

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.
@@ -0,0 +1,16 @@
1
+ import type { CommonRelationOrTableOrForeignKeyProps, ModelType } from "./global.js";
2
+ type SubjectProps = CommonRelationOrTableOrForeignKeyProps & {
3
+ subject: string;
4
+ };
5
+ type ObjectProps = CommonRelationOrTableOrForeignKeyProps & {
6
+ object: ModelType;
7
+ };
8
+ type ViaProps = string | undefined;
9
+ export declare function isModel(object: ModelType): boolean;
10
+ export declare function getSubjectTable({ subject, options }: SubjectProps): string;
11
+ export declare function getObjectTable({ object, options }: ObjectProps): any;
12
+ export declare function getSubjectForeignKey({ subject, options }: SubjectProps): string;
13
+ export declare function getObjectForeignKey({ object, options }: ObjectProps): string;
14
+ export declare function getModelClass({ object, options }: ObjectProps): any;
15
+ export declare function getViaTable(via: ViaProps): string | null;
16
+ export {};
@@ -0,0 +1,61 @@
1
+ import { join } from "node:path";
2
+ import snakeCase from "lodash.snakecase";
3
+ import pluralize from "pluralize";
4
+ /*
5
+ We use this function to detect if the object is
6
+ an Objection.js model or a string.
7
+ */
8
+ export function isModel(object) {
9
+ return (typeof object === "function" &&
10
+ object?.tableName !== undefined &&
11
+ object?.name !== undefined);
12
+ }
13
+ /*
14
+ Gets the SQL table for the subject, either from the options object or the
15
+ plural version of the subject model.
16
+ */
17
+ export function getSubjectTable({ subject, options }) {
18
+ return options?.subjectTable || pluralize(snakeCase(subject));
19
+ }
20
+ /*
21
+ Gets the SQL table for the object, either from the options object or the
22
+ plural version of the object model.
23
+ */
24
+ export function getObjectTable({ object, options }) {
25
+ // If the object is already a Model class, return the table name
26
+ if (isModel(object))
27
+ return object.tableName;
28
+ return options?.objectTable || pluralize(snakeCase(object));
29
+ }
30
+ /*
31
+ Gets the SQL foreign key for the subject, either from the options object
32
+ or the snake case of the subject model.
33
+ */
34
+ export function getSubjectForeignKey({ subject, options }) {
35
+ return options?.subjectForeignKey || `${snakeCase(subject)}_id`;
36
+ }
37
+ /*
38
+ Gets the SQL foreign key for the object, either from the options object
39
+ or the snake case of the object model.
40
+ */
41
+ export function getObjectForeignKey({ object, options }) {
42
+ if (isModel(object)) {
43
+ const modelName = object.name;
44
+ return options?.objectForeignKey || `${snakeCase(modelName)}_id`;
45
+ }
46
+ return options?.objectForeignKey || `${snakeCase(object)}_id`;
47
+ }
48
+ /*
49
+ Allows you to define the model path for a model
50
+ */
51
+ export function getModelClass({ object, options }) {
52
+ // If the object is already a Model class, return it
53
+ if (isModel(object))
54
+ return object;
55
+ return options?.modelPath
56
+ ? join(options.modelPath, object)
57
+ : object;
58
+ }
59
+ export function getViaTable(via) {
60
+ return via ? pluralize(snakeCase(via)) : null;
61
+ }
@@ -0,0 +1,43 @@
1
+ import type { ModelType, OptionsProps } from "./global.js";
2
+ type ObjectionRelationProps = {
3
+ subject: string;
4
+ modelPath: string;
5
+ };
6
+ export declare class ObjectionRelation {
7
+ subject: string;
8
+ modelPath: string;
9
+ constructor({ subject, modelPath }: ObjectionRelationProps);
10
+ belongsTo(object: ModelType, options?: OptionsProps): {
11
+ relation: import("objection").RelationType;
12
+ modelClass: any;
13
+ join: {
14
+ from: string;
15
+ to: string;
16
+ };
17
+ };
18
+ hasOne(object: ModelType, options?: OptionsProps): {
19
+ relation: import("objection").RelationType;
20
+ modelClass: any;
21
+ join: {
22
+ from: string;
23
+ to: string;
24
+ };
25
+ };
26
+ hasMany(object: ModelType, options?: OptionsProps): {
27
+ relation: import("objection").RelationType;
28
+ modelClass: any;
29
+ join: {
30
+ from: string;
31
+ to: string;
32
+ };
33
+ };
34
+ hasManyThrough(object: ModelType, via: string, options?: OptionsProps): {
35
+ relation: import("objection").RelationType;
36
+ modelClass: any;
37
+ join: {
38
+ from: string;
39
+ to: string;
40
+ };
41
+ };
42
+ }
43
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,58 @@
1
+ import { relation } from "./relations.js";
2
+ export class ObjectionRelation {
3
+ subject;
4
+ modelPath;
5
+ constructor({ subject, modelPath }) {
6
+ this.subject = subject;
7
+ this.modelPath = modelPath;
8
+ }
9
+ belongsTo(object, options) {
10
+ if (!options)
11
+ options = { modelPath: this.modelPath };
12
+ if (!options.modelPath)
13
+ options.modelPath = this.modelPath;
14
+ return relation({
15
+ subject: this.subject,
16
+ relType: "belongsTo",
17
+ object,
18
+ options,
19
+ });
20
+ }
21
+ hasOne(object, options) {
22
+ if (!options)
23
+ options = { modelPath: this.modelPath };
24
+ if (!options.modelPath)
25
+ options.modelPath = this.modelPath;
26
+ return relation({
27
+ subject: this.subject,
28
+ relType: "hasOne",
29
+ object,
30
+ options,
31
+ });
32
+ }
33
+ hasMany(object, options) {
34
+ if (!options)
35
+ options = { modelPath: this.modelPath };
36
+ if (!options.modelPath)
37
+ options.modelPath = this.modelPath;
38
+ return relation({
39
+ subject: this.subject,
40
+ relType: "hasMany",
41
+ object,
42
+ options,
43
+ });
44
+ }
45
+ hasManyThrough(object, via, options) {
46
+ if (!options)
47
+ options = { modelPath: this.modelPath };
48
+ if (!options.modelPath)
49
+ options.modelPath = this.modelPath;
50
+ return relation({
51
+ subject: this.subject,
52
+ relType: "hasManyThrough",
53
+ object,
54
+ via,
55
+ options,
56
+ });
57
+ }
58
+ }
@@ -0,0 +1,58 @@
1
+ import type { CommonRelationOrTableOrForeignKeyProps, ModelType, RelationTypeProps } from "./global.js";
2
+ export 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: any;
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: any;
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: any;
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: any;
35
+ join: {
36
+ from: string;
37
+ through: {
38
+ from: string;
39
+ to: string;
40
+ };
41
+ to: string;
42
+ };
43
+ };
44
+ type RelationProps = CommonRelationOrTableOrForeignKeyProps & {
45
+ subject: string;
46
+ relType: "hasOne" | "hasMany" | "hasManyThrough" | "belongsTo";
47
+ object: ModelType;
48
+ via?: string;
49
+ };
50
+ export declare function relation({ subject, relType, object, via, options, }: RelationProps): {
51
+ relation: import("objection").RelationType;
52
+ modelClass: any;
53
+ join: {
54
+ from: string;
55
+ to: string;
56
+ };
57
+ };
58
+ export {};
@@ -0,0 +1,92 @@
1
+ // Dependencies
2
+ import { Model } from "objection";
3
+ import { getModelClass, getObjectForeignKey, getObjectTable, getSubjectForeignKey, getSubjectTable, getViaTable, } from "./helpers.js";
4
+ const { HasOneRelation, BelongsToOneRelation, HasManyRelation, ManyToManyRelation, } = Model;
5
+ /*
6
+ Defines a relationship where a record in one model can belong to a record in
7
+ another model.
8
+ */
9
+ export function belongsRelation({ modelClass, from, to }) {
10
+ return {
11
+ relation: BelongsToOneRelation,
12
+ modelClass,
13
+ join: { from, to },
14
+ };
15
+ }
16
+ /*
17
+ Defines a relationship where a record in one model can own a record in another
18
+ model.
19
+ */
20
+ export function hasOneRelation({ modelClass, from, to }) {
21
+ return {
22
+ relation: HasOneRelation,
23
+ modelClass,
24
+ join: { from, to },
25
+ };
26
+ }
27
+ /*
28
+ Defines a relationship where a record in one model can own many records in
29
+ another model.
30
+ */
31
+ export function hasManyRelation({ modelClass, from, to }) {
32
+ return {
33
+ relation: HasManyRelation,
34
+ modelClass,
35
+ join: { from, to },
36
+ };
37
+ }
38
+ /*
39
+ Defines a relationship where a record in one model can own many records in
40
+ another model, via a join table
41
+ */
42
+ export function hasManyThroughRelation({ modelClass, from, through, to, }) {
43
+ return {
44
+ relation: ManyToManyRelation,
45
+ modelClass,
46
+ join: { from, through, to },
47
+ };
48
+ }
49
+ /*
50
+ Defines a relationship by passing the subject, the predicate, and the object,
51
+ along with an optional via model.
52
+ */
53
+ export function relation({ subject, relType, object, via, options, }) {
54
+ const subjectTable = getSubjectTable({ subject, options });
55
+ const objectTable = getObjectTable({ object, options });
56
+ const subjectForeignKey = getSubjectForeignKey({ subject, options });
57
+ const objectForeignKey = getObjectForeignKey({ object, options });
58
+ const modelClass = getModelClass({ object, options });
59
+ const viaTable = getViaTable(via);
60
+ switch (relType) {
61
+ case "hasOne":
62
+ return hasOneRelation({
63
+ modelClass,
64
+ from: `${subjectTable}.id`,
65
+ to: `${objectTable}.${subjectForeignKey}`,
66
+ });
67
+ case "hasMany":
68
+ return hasManyRelation({
69
+ modelClass,
70
+ from: `${subjectTable}.id`,
71
+ to: `${objectTable}.${subjectForeignKey}`,
72
+ });
73
+ case "hasManyThrough":
74
+ return hasManyThroughRelation({
75
+ modelClass,
76
+ from: `${subjectTable}.id`,
77
+ through: {
78
+ from: `${viaTable}.${subjectForeignKey}`,
79
+ to: `${viaTable}.${objectForeignKey}`,
80
+ },
81
+ to: `${objectTable}.id`,
82
+ });
83
+ case "belongsTo":
84
+ return belongsRelation({
85
+ modelClass,
86
+ from: `${subjectTable}.${objectForeignKey}`,
87
+ to: `${objectTable}.id`,
88
+ });
89
+ default:
90
+ throw new Error("No valid relationship type specified");
91
+ }
92
+ }
package/package.json CHANGED
@@ -1,14 +1,19 @@
1
1
  {
2
2
  "name": "@anephenix/objection-relations",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
+ "main": "./dist/index.js",
6
7
  "exports": {
7
8
  ".": {
9
+ "types": "./dist/index.d.ts",
8
10
  "import": "./dist/index.js",
9
- "require": "./dist/index.js"
11
+ "default": "./dist/index.js"
10
12
  }
11
13
  },
14
+ "engines": {
15
+ "node": ">=16"
16
+ },
12
17
  "types": "./dist/index.d.ts",
13
18
  "files": [
14
19
  "dist",