Skip to content

Commit af266b2

Browse files
committed
Objection.js EntityTraversal / CatDatabase
1 parent 0499841 commit af266b2

File tree

9 files changed

+123
-57
lines changed

9 files changed

+123
-57
lines changed

src/Packages/objection/src/Benchmarks/EntityTraversal.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1-
import { Skipped } from 'BenchmarkUtils/BenchmarkRunner'
21
import { EntityTraversalTest } from 'Benchmarks/EntityTraversal'
3-
import { Cats } from '../Databases/CatDatabase/Cats'
2+
import { Cat } from '../Databases/CatDatabase/Cat'
3+
import { Toy } from '../Databases/CatDatabase/Toy'
44

55
const EntityTraversal: EntityTraversalTest = {
66
getCatColor: async id => {
7-
return Cats.query()
7+
return Cat.query()
88
.findById(id)
99
.withGraphFetched('catColor.colorHex')
10-
.then(cat => (cat as any)?.catColor?.colorHex?.hexCode ?? '')
10+
.then(cat => {
11+
return cat?.catColor?.colorHex?.hexCode ?? ''
12+
})
1113
},
1214
countCatsByColor: async (hexColor: string) => {
13-
throw new Skipped()
15+
return Cat.query()
16+
.count()
17+
.joinRelated('catColor.colorHex')
18+
.where('hex_code', hexColor)
19+
.first()
20+
.then((result: any) => Number(result.count))
1421
},
1522
getToysAvailableToCat: async (id: number) => {
16-
throw new Skipped()
23+
return Toy.query()
24+
.joinRelated('houses')
25+
.joinRelated('houses.cats')
26+
.where('houses:cats.id', id)
27+
.then(toys => toys.map(toy => toy.toyName))
1728
},
1829
}
1930

src/Packages/objection/src/Databases/CatDatabase/Cats.ts renamed to src/Packages/objection/src/Databases/CatDatabase/Cat.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
import { Model } from 'objection'
3+
import { CatColor } from './CatColor'
4+
5+
export class Cat extends Model {
6+
id!: number
7+
catColorId!: number
8+
catName!: string
9+
dateOfBirth!: Date
10+
catColor?: CatColor
311

4-
export class Cats extends Model {
512
static get tableName() {
6-
return 'cats'
13+
return 'cat'
714
}
815

916
static get idColumn() {
@@ -24,28 +31,28 @@ export class Cats extends Model {
2431
}
2532

2633
static get relationMappings() {
27-
const { CatColors } = require('./CatColors')
34+
const { CatColor } = require('./CatColor')
2835
const { House } = require('./House')
2936

3037
return {
3138
catColor: {
3239
relation: Model.BelongsToOneRelation,
33-
modelClass: CatColors,
40+
modelClass: CatColor,
3441
join: {
35-
from: 'cats.cat_color_id',
36-
to: 'cat_colors.id',
42+
from: 'cat.catColorId',
43+
to: 'catColor.id',
3744
},
3845
},
3946
houses: {
4047
relation: Model.ManyToManyRelation,
4148
modelClass: House,
4249
join: {
43-
from: 'cats.id',
50+
from: 'cat.id',
4451
through: {
45-
from: 'house_cats.cat_id',
46-
to: 'house_cats.house_id',
52+
from: 'houseCat.catId',
53+
to: 'houseCat.houseId',
4754
},
48-
to: 'houses.id',
55+
to: 'house.id',
4956
},
5057
},
5158
}

src/Packages/objection/src/Databases/CatDatabase/CatColors.ts renamed to src/Packages/objection/src/Databases/CatDatabase/CatColor.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
import { Model } from 'objection'
3+
import { ColorHex } from './ColorHex'
4+
import { Cat } from './Cat'
5+
6+
export class CatColor extends Model {
7+
id: number
8+
name: string
9+
colorHex?: ColorHex
10+
cats?: Cat[]
311

4-
export class CatColors extends Model {
512
static get tableName() {
6-
return 'cat_colors'
13+
return 'catColor'
714
}
815

916
static get idColumn() {
@@ -24,23 +31,23 @@ export class CatColors extends Model {
2431

2532
static get relationMappings() {
2633
const { ColorHex } = require('./ColorHex')
27-
const { Cats } = require('./Cats')
34+
const { Cat } = require('./Cat')
2835

2936
return {
3037
cats: {
3138
relation: Model.HasManyRelation,
32-
modelClass: Cats,
39+
modelClass: Cat,
3340
join: {
34-
from: 'cat_colors.id',
35-
to: 'cats.cat_color_id',
41+
from: 'catColor.id',
42+
to: 'cat.catColorId',
3643
},
3744
},
3845
colorHex: {
3946
relation: Model.HasOneRelation,
4047
modelClass: ColorHex,
4148
join: {
42-
from: 'cat_colors.id',
43-
to: 'color_hex.id',
49+
from: 'catColor.id',
50+
to: 'colorHex.id',
4451
},
4552
},
4653
}

src/Packages/objection/src/Databases/CatDatabase/ColorHex.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import { Model } from 'objection'
33

44
export class ColorHex extends Model {
5+
id!: number
6+
hexCode!: string
7+
58
static get tableName() {
6-
return 'color_hex'
9+
return 'colorHex'
710
}
811

912
static get idColumn() {
@@ -30,8 +33,8 @@ export class ColorHex extends Model {
3033
relation: Model.HasOneRelation,
3134
modelClass: CatColor,
3235
join: {
33-
from: 'color_hex.id',
34-
to: 'cat_colors.id',
36+
from: 'colorHex.id',
37+
to: 'catColor.id',
3538
},
3639
},
3740
}

src/Packages/objection/src/Databases/CatDatabase/House.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
import { Model } from 'objection'
33

44
export class House extends Model {
5+
id: number
6+
houseAddress: string
7+
hasDog: boolean
8+
59
static get tableName() {
6-
return 'houses'
10+
return 'house'
711
}
812

913
static get idColumn() {
@@ -23,28 +27,28 @@ export class House extends Model {
2327
}
2428

2529
static get relationMappings() {
26-
const { CatsModel } = require('./Cats')
27-
const { ToysHouse } = require('./ToysHouse')
30+
const { Cat } = require('./Cat')
31+
const { ToyHouse } = require('./ToyHouse')
2832

2933
return {
3034
cats: {
3135
relation: Model.ManyToManyRelation,
32-
modelClass: CatsModel,
36+
modelClass: Cat,
3337
join: {
34-
from: 'houses.id',
38+
from: 'house.id',
3539
through: {
36-
from: 'houseCats.houseId',
37-
to: 'houseCats.catId',
40+
from: 'houseCat.houseId',
41+
to: 'houseCat.catId',
3842
},
39-
to: 'cats.id',
43+
to: 'cat.id',
4044
},
4145
},
4246
toysHouse: {
4347
relation: Model.HasManyRelation,
44-
modelClass: ToysHouse,
48+
modelClass: ToyHouse,
4549
join: {
46-
from: 'houses.id',
47-
to: 'toysHouse.house_id',
50+
from: 'house.id',
51+
to: 'toyHouse.houseId',
4852
},
4953
},
5054
}

src/Packages/objection/src/Databases/CatDatabase/Toys.ts renamed to src/Packages/objection/src/Databases/CatDatabase/Toy.ts

+32-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
import { Model } from 'objection'
3+
import { ToysProducer } from './ToysProducer'
4+
import { House } from './House'
5+
6+
export class Toy extends Model {
7+
id!: number
8+
currency!: string
9+
toyName!: string
10+
price!: number
11+
naugthy!: string
12+
barcode!: string
13+
toysProducerId!: number
14+
toysProducer?: ToysProducer
15+
houses?: House[]
316

4-
export class Toys extends Model {
517
static get tableName() {
6-
return 'toys'
18+
return 'toy'
719
}
820

921
static get idColumn() {
@@ -27,26 +39,39 @@ export class Toys extends Model {
2739
}
2840

2941
static get relationMappings() {
30-
const { ToysHouse } = require('./ToysHouse')
42+
const { ToyHouse } = require('./ToyHouse')
3143
const { ToysProducer } = require('./ToysProducer')
44+
const { House } = require('./House')
3245

3346
return {
3447
toysHouse: {
3548
relation: Model.HasManyRelation,
36-
modelClass: ToysHouse,
49+
modelClass: ToyHouse,
3750
join: {
38-
from: 'toys.id',
39-
to: 'toysHouse.toys_id',
51+
from: 'toy.id',
52+
to: 'toyHouse.toyId',
4053
},
4154
},
4255
toysProducer: {
4356
relation: Model.BelongsToOneRelation,
4457
modelClass: ToysProducer,
4558
join: {
46-
from: 'toys.producer_id',
59+
from: 'toy.producerId',
4760
to: 'toysProducer.id',
4861
},
4962
},
63+
houses: {
64+
relation: Model.ManyToManyRelation,
65+
modelClass: House,
66+
join: {
67+
from: 'toy.id',
68+
through: {
69+
from: 'toyHouse.toyId',
70+
to: 'toyHouse.houseId',
71+
},
72+
to: 'house.id',
73+
},
74+
},
5075
}
5176
}
5277
}

src/Packages/objection/src/Databases/CatDatabase/ToysHouse.ts renamed to src/Packages/objection/src/Databases/CatDatabase/ToyHouse.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
import { Model } from 'objection'
33

4-
export class ToysHouse extends Model {
4+
export class ToyHouse extends Model {
5+
toyId: number
6+
houseId: number
7+
amount: number
8+
59
static get tableName() {
6-
return 'toysHouse'
10+
return 'toyHouse'
711
}
812

913
static get idColumn() {
10-
return ['toys_id', 'house_id']
14+
return ['toy_id', 'house_id']
1115
}
1216

1317
static get jsonSchema() {
@@ -16,31 +20,31 @@ export class ToysHouse extends Model {
1620
required: ['name'],
1721

1822
properties: {
19-
toys_id: { type: 'integer' },
20-
house_id: { type: 'integer' },
23+
toyId: { type: 'integer' },
24+
houseId: { type: 'integer' },
2125
amount: { type: 'integer' },
2226
},
2327
}
2428
}
2529

2630
static get relationMappings() {
27-
const { Toys } = require('./Toys')
31+
const { Toy } = require('./Toy')
2832
const { House } = require('./House')
2933

3034
return {
3135
toys: {
3236
relation: Model.BelongsToOneRelation,
33-
modelClass: Toys,
37+
modelClass: Toy,
3438
join: {
35-
from: 'toysHouse.toys_id',
36-
to: 'toys.id',
39+
from: 'toyHouse.toyId',
40+
to: 'toy.id',
3741
},
3842
},
3943
house: {
4044
relation: Model.BelongsToOneRelation,
4145
modelClass: House,
4246
join: {
43-
from: 'toysHouse.house_id',
47+
from: 'toyHouse.houseId',
4448
to: 'house.id',
4549
},
4650
},

src/Packages/objection/src/Databases/CatDatabase/ToysProducer.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import { Model } from 'objection'
33

44
export class ToysProducer extends Model {
5+
id: number
6+
stockInfo: Record<string, any>
7+
hqLocation: Record<string, any>
8+
59
static get tableName() {
610
return 'toysProducer'
711
}
@@ -31,7 +35,7 @@ export class ToysProducer extends Model {
3135
modelClass: Toys,
3236
join: {
3337
from: 'toysProducer.id',
34-
to: 'toys.producer_id',
38+
to: 'toy.producerId',
3539
},
3640
},
3741
}

src/Packages/objection/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Model } from 'objection'
1+
import { Model, knexSnakeCaseMappers } from 'objection'
22
import knex from 'knex'
33
import IORMPackage from 'BenchmarkUtils/interfaces/PackageUtils'
44
import EntityTraversal from './Benchmarks/EntityTraversal'
@@ -12,6 +12,7 @@ const initialize = async () => {
1212
password: 'benchmark_pwd',
1313
database: 'benchmark',
1414
},
15+
...knexSnakeCaseMappers(),
1516
})
1617

1718
Model.knex(_knex)

0 commit comments

Comments
 (0)