-
Notifications
You must be signed in to change notification settings - Fork 49
/
company.js
94 lines (87 loc) · 2.36 KB
/
company.js
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
import { Model } from 'objection';
import path from 'path';
export default class Company extends Model {
static tableName = 'companies'
static jsonSchema = {
type: 'object',
required: ['name'],
properties: {
id: { type: 'integer' },
name: { type: 'string' },
ceo: { type: ['integer', 'null'] },
size: { type: ['string', 'null'], enum: ['small', 'medium', 'large', null] },
jsonObject: {
type: ['object', 'null'],
properties: {
stringField: { type: 'string' },
numberField: { type: 'number' },
objectField: {
type: 'object',
properties: {
object: { type: 'string' }
}
},
'first.founder': { type: ['string', 'null'] }
}
},
jsonArray: { type: ['array', 'null'] },
jsonbObject: { type: ['object', 'null'] },
jsonbArray: { type: ['array', 'null'] }
}
}
static modifiers = {
orderByName: builder => {
builder.orderBy('name');
},
google: builder => {
builder.where('companies.name', 'Google')
.select(['companies.name'])
.groupBy(['companies.name']);
},
googleWithEager: builder => {
builder.where('companies.name', 'Google')
.select(['companies.name'])
.groupBy(['companies.name', 'companies.id']);
},
apple: (builder, hasCeo) => {
builder.where('name', 'Apple');
},
large: (builder, hasCeo) => {
builder.where('size', 'large');
if (hasCeo) { builder.whereNot('ceo', null); }
},
withRelation: builder => {
builder.withGraphFetched('employees');
}
}
// This object defines the relations to other models.
static relationMappings = {
ceos: {
relation: Model.BelongsToOneRelation,
modelClass: path.join(__dirname, '/people'),
join: {
from: 'companies.ceo',
to: 'people.id'
}
},
employees: {
relation: Model.HasManyRelation,
modelClass: path.join(__dirname, '/employee'),
join: {
from: 'companies.id',
to: 'employees.companyId'
}
},
clients: {
relation: Model.HasManyRelation,
modelClass: path.join(__dirname, '/client'),
join: {
from: 'companies.id',
to: 'clients.companyId'
}
}
}
$beforeInsert () {
if (this.id) { this.id = 99; }
}
}