@anephenix/objection-relations 0.0.13 → 0.0.17
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 +1 -1
- package/package.json +72 -65
- package/src/global.d.ts +0 -17
- package/src/helpers.ts +0 -59
- package/src/index.ts +0 -64
- package/src/relations.ts +0 -141
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# objection-relations
|
|
2
2
|
|
|
3
|
-
[](https://badge.fury.io/js/%40anephenix%2Fobjection-relations) [](https://github.com/anephenix/objection-relations/actions/workflows/node.js.yml) [](https://badge.fury.io/js/%40anephenix%2Fobjection-relations) [](https://github.com/anephenix/objection-relations/actions/workflows/node.js.yml) [](https://socket.dev/npm/package/@anephenix/objection-relations)
|
|
4
4
|
|
|
5
5
|
A relations helper for Objection.js. This provides a convenient way to define
|
|
6
6
|
relations in the `relationMappings` function on an Objection.js model.
|
package/package.json
CHANGED
|
@@ -1,67 +1,74 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
2
|
+
"version": "0.0.17",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"require": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=10"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"lodash.snakecase": "^4.1.1",
|
|
22
|
+
"objection": "^3.1.5",
|
|
23
|
+
"pluralize": "^8.0.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@biomejs/biome": "2.2.5",
|
|
27
|
+
"@types/lodash.snakecase": "^4.1.9",
|
|
28
|
+
"@types/node": "^24.7.0",
|
|
29
|
+
"@types/pluralize": "^0.0.33",
|
|
30
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
31
|
+
"globals": "^16.4.0",
|
|
32
|
+
"husky": "^9.1.7",
|
|
33
|
+
"size-limit": "^11.2.0",
|
|
34
|
+
"size-limit-preset-node-lib": "^0.4.0",
|
|
35
|
+
"typescript": "^5.9.3",
|
|
36
|
+
"vitest": "^3.2.4"
|
|
37
|
+
},
|
|
38
|
+
"author": "Paul Jensen <paul@anephenix.com>",
|
|
39
|
+
"scripts": {
|
|
40
|
+
"analyze": "size-limit --why",
|
|
41
|
+
"build": "tsc --project tsconfig.json",
|
|
42
|
+
"cover": "NODE_ENV=test vitest --coverage --run",
|
|
43
|
+
"lint": "npx @biomejs/biome check --write src test scripts",
|
|
44
|
+
"prepare-patch-release": "npm run update-changelog && git add CHANGELOG.md && git commit -m \"Updated changelog\" && npm version patch",
|
|
45
|
+
"publish-patch-release": "npm run prepare-patch-release && git push origin main && git push --tags",
|
|
46
|
+
"size": "size-limit",
|
|
47
|
+
"start": "tsdx watch --target node",
|
|
48
|
+
"test": "NODE_ENV=test vitest --run",
|
|
49
|
+
"test:build:commonjs": "node e2e/test-commonjs.cjs",
|
|
50
|
+
"test:build:esm": "node e2e/test-esm.js",
|
|
51
|
+
"test:e2e": "npm run build && npm run test:build:commonjs && npm run test:build:esm",
|
|
52
|
+
"update-changelog": "node --experimental-strip-types scripts/update-changelog.ts",
|
|
53
|
+
"prepare": "husky"
|
|
54
|
+
},
|
|
55
|
+
"husky": {
|
|
56
|
+
"hooks": {
|
|
57
|
+
"pre-commit": "npm run lint && npm test"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"name": "@anephenix/objection-relations",
|
|
61
|
+
"module": "dist/objection-relations.esm.js",
|
|
62
|
+
"prettier": {
|
|
63
|
+
"printWidth": 80,
|
|
64
|
+
"semi": true,
|
|
65
|
+
"singleQuote": true,
|
|
66
|
+
"trailingComma": "es5"
|
|
67
|
+
},
|
|
68
|
+
"size-limit": [
|
|
69
|
+
{
|
|
70
|
+
"path": "dist/*.js",
|
|
71
|
+
"limit": "10 KB"
|
|
72
|
+
}
|
|
73
|
+
]
|
|
67
74
|
}
|
package/src/global.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
// Dependencies
|
|
2
|
-
import { OptionsProps } from './global';
|
|
3
|
-
import { relation } from './relations';
|
|
4
|
-
|
|
5
|
-
// Types
|
|
6
|
-
|
|
7
|
-
type ObjectionRelationProps = {
|
|
8
|
-
subject: string;
|
|
9
|
-
modelPath: string;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export class ObjectionRelation {
|
|
13
|
-
subject: string;
|
|
14
|
-
modelPath: string;
|
|
15
|
-
constructor({ subject, modelPath }: ObjectionRelationProps) {
|
|
16
|
-
this.subject = subject;
|
|
17
|
-
this.modelPath = modelPath;
|
|
18
|
-
}
|
|
19
|
-
|
|
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
|
-
}
|
|
30
|
-
|
|
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
|
-
}
|
|
41
|
-
|
|
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
|
-
}
|
|
52
|
-
|
|
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
|
-
}
|
|
64
|
-
}
|
package/src/relations.ts
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
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
|
-
}
|