@anephenix/objection-relations 0.0.14 → 0.0.18
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 +26 -0
- package/package.json +13 -4
- 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
|
@@ -61,6 +61,32 @@ The helper function will do the following:
|
|
|
61
61
|
| ----- | ---------- | ----------- |
|
|
62
62
|
| User | users | user_id |
|
|
63
63
|
|
|
64
|
+
*NOTE*
|
|
65
|
+
|
|
66
|
+
For TypeScript files to resolve correctly in Objection.js, use the model directly instead of the name as a string:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { ObjectionRelation } from '@anephenix/objection-relations';
|
|
70
|
+
import Address from './Address';
|
|
71
|
+
|
|
72
|
+
class User extends Model {
|
|
73
|
+
static get tableName() {
|
|
74
|
+
return 'users';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static get relationMappings() {
|
|
78
|
+
const or = new ObjectionRelation({
|
|
79
|
+
subject: this.name,
|
|
80
|
+
modelPath: __dirname,
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
// This changes from 'Address' to Address
|
|
84
|
+
addresses: or.hasMany(Address),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
64
90
|
## Dependencies
|
|
65
91
|
|
|
66
92
|
- Node.js
|
package/package.json
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.18",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"require": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
7
13
|
"files": [
|
|
8
14
|
"dist",
|
|
9
|
-
"
|
|
15
|
+
"README.md"
|
|
10
16
|
],
|
|
11
17
|
"engines": {
|
|
12
18
|
"node": ">=10"
|
|
@@ -19,7 +25,7 @@
|
|
|
19
25
|
"devDependencies": {
|
|
20
26
|
"@biomejs/biome": "2.2.5",
|
|
21
27
|
"@types/lodash.snakecase": "^4.1.9",
|
|
22
|
-
"@types/node": "^24.
|
|
28
|
+
"@types/node": "^24.7.0",
|
|
23
29
|
"@types/pluralize": "^0.0.33",
|
|
24
30
|
"@vitest/coverage-v8": "^3.2.4",
|
|
25
31
|
"globals": "^16.4.0",
|
|
@@ -40,6 +46,9 @@
|
|
|
40
46
|
"size": "size-limit",
|
|
41
47
|
"start": "tsdx watch --target node",
|
|
42
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",
|
|
43
52
|
"update-changelog": "node --experimental-strip-types scripts/update-changelog.ts",
|
|
44
53
|
"prepare": "husky"
|
|
45
54
|
},
|
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
|
-
export type CommonRelationOrTableOrForeignKeyProps = {
|
|
10
|
-
options?: OptionsProps;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type RelationTypeProps = {
|
|
14
|
-
modelClass: string;
|
|
15
|
-
from: string;
|
|
16
|
-
to: string;
|
|
17
|
-
};
|
package/src/helpers.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { join } from "node:path";
|
|
2
|
-
import snakeCase from "lodash.snakecase";
|
|
3
|
-
import pluralize from "pluralize";
|
|
4
|
-
import type { 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 type { 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 type {
|
|
4
|
-
CommonRelationOrTableOrForeignKeyProps,
|
|
5
|
-
RelationTypeProps,
|
|
6
|
-
} from "./global";
|
|
7
|
-
import {
|
|
8
|
-
getModelClass,
|
|
9
|
-
getObjectForeignKey,
|
|
10
|
-
getObjectTable,
|
|
11
|
-
getSubjectForeignKey,
|
|
12
|
-
getSubjectTable,
|
|
13
|
-
getViaTable,
|
|
14
|
-
} from "./helpers";
|
|
15
|
-
|
|
16
|
-
const {
|
|
17
|
-
HasOneRelation,
|
|
18
|
-
BelongsToOneRelation,
|
|
19
|
-
HasManyRelation,
|
|
20
|
-
ManyToManyRelation,
|
|
21
|
-
} = Model;
|
|
22
|
-
|
|
23
|
-
// Types
|
|
24
|
-
|
|
25
|
-
export 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
|
-
}
|