-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
37 lines (29 loc) · 961 Bytes
/
index.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
import {Model, RelationMappingsThunk} from "objection";
const Example = async () => {
const customer = await Customer.query().findById("123").throwIfNotFound();
/**
* The program relation on customer is an optional relation. I would expect this to be returned from $relatedQuery
* as optional, but the typing is just Program
*/
const program = await customer.$relatedQuery("program");
console.log(program.id);
}
class Program extends Model {
static tableName = "user";
id!: string;
}
class Customer extends Model {
static tableName = "test";
programId?: string;
program?: Program;
static relationMappings: RelationMappingsThunk = () => ({
user: {
relation: Model.BelongsToOneRelation,
modelClass: Program,
join: {
from: `${Customer.tableName}.programId`,
to: `${Program.tableName}.id`,
},
},
});
}