@@ -5,25 +5,25 @@ import {
55 snakeCaseMappers ,
66} from 'objection' ;
77import {
8- Schema ,
8+ RecordSchema ,
99 RecordNotFoundException ,
1010 RecordRelationship ,
1111 Record as OrbitRecord ,
12- } from '@orbit/data ' ;
12+ } from '@orbit/records ' ;
1313import { foreignKey , tableize } from 'inflected' ;
1414
1515import { tableizeJoinTable , castAttributeValue } from './utils' ;
1616
17- export class BaseModel extends Model {
17+ export abstract class BaseModel extends Model {
1818 id : string ;
1919 createdAt : string ;
2020 updatedAt : string ;
2121
2222 static get virtualAttributes ( ) {
2323 return [ 'orbitSchema' , 'orbitType' ] ;
2424 }
25- orbitSchema : Schema ;
26- orbitType : string ;
25+ abstract get orbitSchema ( ) : RecordSchema ;
26+ abstract get orbitType ( ) : string ;
2727
2828 $beforeInsert ( ) {
2929 this . createdAt = new Date ( ) . toISOString ( ) ;
@@ -57,22 +57,22 @@ export class BaseModel extends Model {
5757
5858 schema . eachAttribute ( type , ( property , attribute ) => {
5959 if ( result [ property ] != null ) {
60- ( attributes as Record < string , unknown > ) [ property ] = castAttributeValue (
60+ attributes [ property ] = castAttributeValue (
6161 result [ property ] ,
6262 attribute . type
6363 ) ;
6464 record . attributes = attributes ;
6565 }
6666 } ) ;
6767
68- schema . eachRelationship ( type , ( property , { type : kind , model : type } ) => {
68+ schema . eachRelationship ( type , ( property , { kind, type } ) => {
6969 if ( kind === 'hasOne' ) {
70- const id = result [ `${ property } Id` ] ;
70+ const id = result [ `${ property } Id` ] as string | undefined ;
7171 if ( id ) {
72- ( relationships as Record < string , unknown > ) [ property ] = {
72+ relationships [ property ] = {
7373 data : {
7474 type : type as string ,
75- id : id as string ,
75+ id : id ,
7676 } ,
7777 } ;
7878 record . relationships = relationships ;
@@ -85,7 +85,7 @@ export class BaseModel extends Model {
8585}
8686
8787export function buildModels (
88- schema : Schema
88+ schema : RecordSchema
8989) : Record < string , ModelClass < BaseModel > > {
9090 const models : Record < string , ModelClass < BaseModel > > = { } ;
9191
@@ -97,7 +97,7 @@ export function buildModels(
9797}
9898
9999export function buildModel (
100- schema : Schema ,
100+ schema : RecordSchema ,
101101 type : string ,
102102 models : Record < string , ModelClass < BaseModel > >
103103) : ModelClass < BaseModel > {
@@ -118,72 +118,66 @@ export function buildModel(
118118
119119 static get relationMappings ( ) {
120120 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+ }
129127
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+ }
135133
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 ) ;
141154
142- if ( kind === 'hasOne' ) {
143155 relationMapping = {
144- relation : Model . BelongsToOneRelation ,
156+ relation : Model . ManyToManyRelation ,
145157 modelClass : relationModel ,
146158 join : {
147- from : `${ tableName } .${ relationColumnName } ` ,
159+ from : `${ tableName } .id` ,
160+ through : {
161+ from : `${ joinTableName } .${ relationColumnName } ` ,
162+ to : `${ joinTableName } .${ inverseColumnName } ` ,
163+ } ,
148164 to : `${ relationTableName } .id` ,
149165 } ,
150166 } ;
151167 } 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+ } ;
182176 }
183-
184- relationMappings [ property ] = relationMapping ;
185177 }
186- ) ;
178+
179+ relationMappings [ property ] = relationMapping ;
180+ } ) ;
187181
188182 return relationMappings ;
189183 }
0 commit comments