@@ -5,25 +5,25 @@ import {
5
5
snakeCaseMappers ,
6
6
} from 'objection' ;
7
7
import {
8
- Schema ,
8
+ RecordSchema ,
9
9
RecordNotFoundException ,
10
10
RecordRelationship ,
11
11
Record as OrbitRecord ,
12
- } from '@orbit/data ' ;
12
+ } from '@orbit/records ' ;
13
13
import { foreignKey , tableize } from 'inflected' ;
14
14
15
15
import { tableizeJoinTable , castAttributeValue } from './utils' ;
16
16
17
- export class BaseModel extends Model {
17
+ export abstract class BaseModel extends Model {
18
18
id : string ;
19
19
createdAt : string ;
20
20
updatedAt : string ;
21
21
22
22
static get virtualAttributes ( ) {
23
23
return [ 'orbitSchema' , 'orbitType' ] ;
24
24
}
25
- orbitSchema : Schema ;
26
- orbitType : string ;
25
+ abstract get orbitSchema ( ) : RecordSchema ;
26
+ abstract get orbitType ( ) : string ;
27
27
28
28
$beforeInsert ( ) {
29
29
this . createdAt = new Date ( ) . toISOString ( ) ;
@@ -57,22 +57,22 @@ export class BaseModel extends Model {
57
57
58
58
schema . eachAttribute ( type , ( property , attribute ) => {
59
59
if ( result [ property ] != null ) {
60
- ( attributes as Record < string , unknown > ) [ property ] = castAttributeValue (
60
+ attributes [ property ] = castAttributeValue (
61
61
result [ property ] ,
62
62
attribute . type
63
63
) ;
64
64
record . attributes = attributes ;
65
65
}
66
66
} ) ;
67
67
68
- schema . eachRelationship ( type , ( property , { type : kind , model : type } ) => {
68
+ schema . eachRelationship ( type , ( property , { kind, type } ) => {
69
69
if ( kind === 'hasOne' ) {
70
- const id = result [ `${ property } Id` ] ;
70
+ const id = result [ `${ property } Id` ] as string | undefined ;
71
71
if ( id ) {
72
- ( relationships as Record < string , unknown > ) [ property ] = {
72
+ relationships [ property ] = {
73
73
data : {
74
74
type : type as string ,
75
- id : id as string ,
75
+ id : id ,
76
76
} ,
77
77
} ;
78
78
record . relationships = relationships ;
@@ -85,7 +85,7 @@ export class BaseModel extends Model {
85
85
}
86
86
87
87
export function buildModels (
88
- schema : Schema
88
+ schema : RecordSchema
89
89
) : Record < string , ModelClass < BaseModel > > {
90
90
const models : Record < string , ModelClass < BaseModel > > = { } ;
91
91
@@ -97,7 +97,7 @@ export function buildModels(
97
97
}
98
98
99
99
export function buildModel (
100
- schema : Schema ,
100
+ schema : RecordSchema ,
101
101
type : string ,
102
102
models : Record < string , ModelClass < BaseModel > >
103
103
) : ModelClass < BaseModel > {
@@ -118,72 +118,66 @@ export function buildModel(
118
118
119
119
static get relationMappings ( ) {
120
120
const relationMappings : Record < string , RelationMapping < BaseModel > > = { } ;
121
- schema . eachRelationship (
122
- type ,
123
- ( property , { type : kind , model : type , inverse } ) => {
124
- if ( ! inverse || ! type ) {
125
- throw new Error (
126
- `SQLSource: "type" and "inverse" are required on a relationship`
127
- ) ;
128
- }
121
+ schema . eachRelationship ( type , ( property , { kind, type, inverse } ) => {
122
+ if ( ! inverse || ! type ) {
123
+ throw new Error (
124
+ `SQLSource: "type" and "inverse" are required on a relationship`
125
+ ) ;
126
+ }
129
127
130
- if ( Array . isArray ( type ) ) {
131
- throw new Error (
132
- `SQLSource: polymorphic types are not supported yet`
133
- ) ;
134
- }
128
+ if ( Array . isArray ( type ) ) {
129
+ throw new Error (
130
+ `SQLSource: polymorphic types are not supported yet`
131
+ ) ;
132
+ }
135
133
136
- const relationColumnName = foreignKey ( property ) ;
137
- const inverseColumnName = foreignKey ( inverse ) ;
138
- const relationTableName = tableize ( type ) ;
139
- const relationModel = buildModel ( schema , type , models ) ;
140
- let relationMapping : RelationMapping < BaseModel > ;
134
+ const relationColumnName = foreignKey ( property ) ;
135
+ const inverseColumnName = foreignKey ( inverse ) ;
136
+ const relationTableName = tableize ( type ) ;
137
+ const relationModel = buildModel ( schema , type , models ) ;
138
+ let relationMapping : RelationMapping < BaseModel > ;
139
+
140
+ if ( kind === 'hasOne' ) {
141
+ relationMapping = {
142
+ relation : Model . BelongsToOneRelation ,
143
+ modelClass : relationModel ,
144
+ join : {
145
+ from : `${ tableName } .${ relationColumnName } ` ,
146
+ to : `${ relationTableName } .id` ,
147
+ } ,
148
+ } ;
149
+ } else {
150
+ const relDef = schema . getRelationship ( type , inverse ) ;
151
+
152
+ if ( relDef ?. kind === 'hasMany' ) {
153
+ const joinTableName = tableizeJoinTable ( property , inverse ) ;
141
154
142
- if ( kind === 'hasOne' ) {
143
155
relationMapping = {
144
- relation : Model . BelongsToOneRelation ,
156
+ relation : Model . ManyToManyRelation ,
145
157
modelClass : relationModel ,
146
158
join : {
147
- from : `${ tableName } .${ relationColumnName } ` ,
159
+ from : `${ tableName } .id` ,
160
+ through : {
161
+ from : `${ joinTableName } .${ relationColumnName } ` ,
162
+ to : `${ joinTableName } .${ inverseColumnName } ` ,
163
+ } ,
148
164
to : `${ relationTableName } .id` ,
149
165
} ,
150
166
} ;
151
167
} else {
152
- const { type : inverseKind } = schema . getRelationship (
153
- type ,
154
- inverse
155
- ) ;
156
-
157
- if ( inverseKind === 'hasMany' ) {
158
- const joinTableName = tableizeJoinTable ( property , inverse ) ;
159
-
160
- relationMapping = {
161
- relation : Model . ManyToManyRelation ,
162
- modelClass : relationModel ,
163
- join : {
164
- from : `${ tableName } .id` ,
165
- through : {
166
- from : `${ joinTableName } .${ relationColumnName } ` ,
167
- to : `${ joinTableName } .${ inverseColumnName } ` ,
168
- } ,
169
- to : `${ relationTableName } .id` ,
170
- } ,
171
- } ;
172
- } else {
173
- relationMapping = {
174
- relation : Model . HasManyRelation ,
175
- modelClass : relationModel ,
176
- join : {
177
- from : `${ tableName } .id` ,
178
- to : `${ relationTableName } .${ inverseColumnName } ` ,
179
- } ,
180
- } ;
181
- }
168
+ relationMapping = {
169
+ relation : Model . HasManyRelation ,
170
+ modelClass : relationModel ,
171
+ join : {
172
+ from : `${ tableName } .id` ,
173
+ to : `${ relationTableName } .${ inverseColumnName } ` ,
174
+ } ,
175
+ } ;
182
176
}
183
-
184
- relationMappings [ property ] = relationMapping ;
185
177
}
186
- ) ;
178
+
179
+ relationMappings [ property ] = relationMapping ;
180
+ } ) ;
187
181
188
182
return relationMappings ;
189
183
}
0 commit comments