-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild-models.ts
188 lines (162 loc) · 4.97 KB
/
build-models.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
Model,
ModelClass,
RelationMapping,
snakeCaseMappers,
} from 'objection';
import {
RecordSchema,
RecordNotFoundException,
RecordRelationship,
Record as OrbitRecord,
} from '@orbit/records';
import { foreignKey, tableize } from 'inflected';
import { tableizeJoinTable, castAttributeValue } from './utils';
export abstract class BaseModel extends Model {
id: string;
createdAt: string;
updatedAt: string;
static get virtualAttributes() {
return ['orbitSchema', 'orbitType'];
}
abstract get orbitSchema(): RecordSchema;
abstract get orbitType(): string;
$beforeInsert() {
this.createdAt = new Date().toISOString();
}
$beforeUpdate() {
this.updatedAt = new Date().toISOString();
}
static get columnNameMappers() {
return snakeCaseMappers();
}
static createNotFoundError() {
const context = arguments[0];
const type = (context && context.recordType) || 'unknown type';
const id = (context && context.recordId) || 'unknown id';
const error = new RecordNotFoundException(type, id);
return error as any;
}
toOrbitRecord() {
const attributes: Record<string, any> = {};
const relationships: Record<string, RecordRelationship> = {};
const { orbitType: type, orbitSchema: schema } = this;
const result = this.toJSON() as any;
const record: OrbitRecord = {
type,
id: result.id,
};
schema.eachAttribute(type, (property, attribute) => {
if (result[property] != null) {
attributes[property] = castAttributeValue(
result[property],
attribute.type
);
record.attributes = attributes;
}
});
schema.eachRelationship(type, (property, { kind, type }) => {
if (kind === 'hasOne') {
const id = result[`${property}Id`] as string | undefined;
if (id) {
relationships[property] = {
data: {
type: type as string,
id: id,
},
};
record.relationships = relationships;
}
}
});
return record;
}
}
export function buildModels(
schema: RecordSchema
): Record<string, ModelClass<BaseModel>> {
const models: Record<string, ModelClass<BaseModel>> = {};
for (let type in schema.models) {
buildModel(schema, type, models);
}
return models;
}
export function buildModel(
schema: RecordSchema,
type: string,
models: Record<string, ModelClass<BaseModel>>
): ModelClass<BaseModel> {
if (!models[type]) {
const tableName = tableize(type);
models[type] = class extends BaseModel {
get orbitType() {
return type;
}
get orbitSchema() {
return schema;
}
static get tableName() {
return tableName;
}
static get relationMappings() {
const relationMappings: Record<string, RelationMapping<BaseModel>> = {};
schema.eachRelationship(type, (property, { kind, type, inverse }) => {
if (!inverse || !type) {
throw new Error(
`SQLSource: "type" and "inverse" are required on a relationship`
);
}
if (Array.isArray(type)) {
throw new Error(
`SQLSource: polymorphic types are not supported yet`
);
}
const relationColumnName = foreignKey(property);
const inverseColumnName = foreignKey(inverse);
const relationTableName = tableize(type);
const relationModel = buildModel(schema, type, models);
let relationMapping: RelationMapping<BaseModel>;
if (kind === 'hasOne') {
relationMapping = {
relation: Model.BelongsToOneRelation,
modelClass: relationModel,
join: {
from: `${tableName}.${relationColumnName}`,
to: `${relationTableName}.id`,
},
};
} else {
const relDef = schema.getRelationship(type, inverse);
if (relDef?.kind === 'hasMany') {
const joinTableName = tableizeJoinTable(property, inverse);
relationMapping = {
relation: Model.ManyToManyRelation,
modelClass: relationModel,
join: {
from: `${tableName}.id`,
through: {
from: `${joinTableName}.${relationColumnName}`,
to: `${joinTableName}.${inverseColumnName}`,
},
to: `${relationTableName}.id`,
},
};
} else {
relationMapping = {
relation: Model.HasManyRelation,
modelClass: relationModel,
join: {
from: `${tableName}.id`,
to: `${relationTableName}.${inverseColumnName}`,
},
};
}
}
relationMappings[property] = relationMapping;
});
return relationMappings;
}
};
}
return models[type];
}