Skip to content

Commit 95d517c

Browse files
committed
pgTyped EntityTraversal
1 parent af266b2 commit 95d517c

File tree

8 files changed

+263
-3
lines changed

8 files changed

+263
-3
lines changed

.eslint.tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"extends": "./tsconfig.json",
33
"include": ["./src/**/*", "./src/*", "./benchmark/**/*", "./benchmark/*", "./packages/**/src"],
4-
"exclude": []
4+
"exclude": ["./src/pgtyped/src/Benchmarks/**/*.queries.ts"]
55
}

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
...require('@ackee/styleguide-backend-config/eslint'),
3-
ignorePatterns: ['dist', '.eslintrc.js'],
3+
ignorePatterns: ['dist', '.eslintrc.js', '*.queries.ts'],
44
parserOptions: {
55
project: '.eslint.tsconfig.json',
66
},

src/Packages/pgtyped/config.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"transforms": [
3+
{
4+
"mode": "sql",
5+
"include": "**/*.sql",
6+
"emitTemplate": "{{dir}}/{{name}}.queries.ts"
7+
}
8+
],
9+
"srcDir": "./src/",
10+
"failOnError": false,
11+
"camelCaseColumnNames": false,
12+
"db": {
13+
"host": "localhost",
14+
"user": "benchmark",
15+
"dbName": "benchmark",
16+
"password": "benchmark_pwd"
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/** Types generated for queries found in "src/Benchmarks/EntityTraversal/EntityTraversal.sql" */
2+
import { PreparedQuery } from '@pgtyped/runtime'
3+
4+
/** 'GetCatColorById' parameters type */
5+
export interface IGetCatColorByIdParams {
6+
catId?: number | null | void
7+
}
8+
9+
/** 'GetCatColorById' return type */
10+
export interface IGetCatColorByIdResult {
11+
hex_code: string
12+
}
13+
14+
/** 'GetCatColorById' query type */
15+
export interface IGetCatColorByIdQuery {
16+
params: IGetCatColorByIdParams
17+
result: IGetCatColorByIdResult
18+
}
19+
20+
const getCatColorByIdIR: any = {
21+
usedParamSet: { catId: true },
22+
params: [
23+
{
24+
name: 'catId',
25+
required: false,
26+
transform: { type: 'scalar' },
27+
locs: [{ a: 154, b: 159 }],
28+
},
29+
],
30+
statement:
31+
'SELECT\n hex_code\nFROM\n cat\n JOIN cat_color ON cat_color.id = cat.cat_color_id\n JOIN color_hex ON color_hex.id = cat_color.id\nWHERE\n cat.id = :catId',
32+
}
33+
34+
/**
35+
* Query generated from SQL:
36+
* ```
37+
* SELECT
38+
* hex_code
39+
* FROM
40+
* cat
41+
* JOIN cat_color ON cat_color.id = cat.cat_color_id
42+
* JOIN color_hex ON color_hex.id = cat_color.id
43+
* WHERE
44+
* cat.id = :catId
45+
* ```
46+
*/
47+
export const getCatColorById = new PreparedQuery<
48+
IGetCatColorByIdParams,
49+
IGetCatColorByIdResult
50+
>(getCatColorByIdIR)
51+
52+
/** 'CountCatsByColor' parameters type */
53+
export interface ICountCatsByColorParams {
54+
hexCode?: string | null | void
55+
}
56+
57+
/** 'CountCatsByColor' return type */
58+
export interface ICountCatsByColorResult {
59+
count: string | null
60+
}
61+
62+
/** 'CountCatsByColor' query type */
63+
export interface ICountCatsByColorQuery {
64+
params: ICountCatsByColorParams
65+
result: ICountCatsByColorResult
66+
}
67+
68+
const countCatsByColorIR: any = {
69+
usedParamSet: { hexCode: true },
70+
params: [
71+
{
72+
name: 'hexCode',
73+
required: false,
74+
transform: { type: 'scalar' },
75+
locs: [{ a: 166, b: 173 }],
76+
},
77+
],
78+
statement:
79+
'SELECT\n COUNT(*)\nFROM\n cat\n JOIN cat_color ON cat_color.id = cat.cat_color_id\n JOIN color_hex ON color_hex.id = cat_color.id\nWHERE\n color_hex.hex_code = :hexCode',
80+
}
81+
82+
/**
83+
* Query generated from SQL:
84+
* ```
85+
* SELECT
86+
* COUNT(*)
87+
* FROM
88+
* cat
89+
* JOIN cat_color ON cat_color.id = cat.cat_color_id
90+
* JOIN color_hex ON color_hex.id = cat_color.id
91+
* WHERE
92+
* color_hex.hex_code = :hexCode
93+
* ```
94+
*/
95+
export const countCatsByColor = new PreparedQuery<
96+
ICountCatsByColorParams,
97+
ICountCatsByColorResult
98+
>(countCatsByColorIR)
99+
100+
/** 'GetToysAvailableToCat' parameters type */
101+
export interface IGetToysAvailableToCatParams {
102+
catId?: number | null | void
103+
}
104+
105+
/** 'GetToysAvailableToCat' return type */
106+
export interface IGetToysAvailableToCatResult {
107+
toy_name: string
108+
}
109+
110+
/** 'GetToysAvailableToCat' query type */
111+
export interface IGetToysAvailableToCatQuery {
112+
params: IGetToysAvailableToCatParams
113+
result: IGetToysAvailableToCatResult
114+
}
115+
116+
const getToysAvailableToCatIR: any = {
117+
usedParamSet: { catId: true },
118+
params: [
119+
{
120+
name: 'catId',
121+
required: false,
122+
transform: { type: 'scalar' },
123+
locs: [{ a: 202, b: 207 }],
124+
},
125+
],
126+
statement:
127+
'SELECT\n toy.toy_name\nFROM\n toy\n JOIN toy_house ON toy_house.toy_id = toy.id\n JOIN house ON house.id = toy_house.house_id\n JOIN house_cat ON house_cat.house_id = house.id\nWHERE\n house_cat.cat_id = :catId',
128+
}
129+
130+
/**
131+
* Query generated from SQL:
132+
* ```
133+
* SELECT
134+
* toy.toy_name
135+
* FROM
136+
* toy
137+
* JOIN toy_house ON toy_house.toy_id = toy.id
138+
* JOIN house ON house.id = toy_house.house_id
139+
* JOIN house_cat ON house_cat.house_id = house.id
140+
* WHERE
141+
* house_cat.cat_id = :catId
142+
* ```
143+
*/
144+
export const getToysAvailableToCat = new PreparedQuery<
145+
IGetToysAvailableToCatParams,
146+
IGetToysAvailableToCatResult
147+
>(getToysAvailableToCatIR)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* @name getCatColorById */
2+
3+
SELECT
4+
hex_code
5+
FROM
6+
cat
7+
JOIN cat_color ON cat_color.id = cat.cat_color_id
8+
JOIN color_hex ON color_hex.id = cat_color.id
9+
WHERE
10+
cat.id = :catId;
11+
12+
/* @name countCatsByColor */
13+
14+
SELECT
15+
COUNT(*)
16+
FROM
17+
cat
18+
JOIN cat_color ON cat_color.id = cat.cat_color_id
19+
JOIN color_hex ON color_hex.id = cat_color.id
20+
WHERE
21+
color_hex.hex_code = :hexCode;
22+
23+
/* @name getToysAvailableToCat */
24+
SELECT
25+
toy.toy_name
26+
FROM
27+
toy
28+
JOIN toy_house ON toy_house.toy_id = toy.id
29+
JOIN house ON house.id = toy_house.house_id
30+
JOIN house_cat ON house_cat.house_id = house.id
31+
WHERE
32+
house_cat.cat_id = :catId;
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { EntityTraversalTest } from 'Benchmarks/EntityTraversal'
2+
import { getClient } from '../..'
3+
import {
4+
countCatsByColor,
5+
getCatColorById,
6+
getToysAvailableToCat,
7+
} from './EntityTraversal.queries'
8+
9+
const EntityTraversal: EntityTraversalTest = {
10+
getCatColor: async catId => {
11+
return getCatColorById
12+
.run({ catId }, getClient())
13+
.then(result => result[0].hex_code)
14+
},
15+
countCatsByColor: async hexCode => {
16+
return countCatsByColor
17+
.run({ hexCode }, getClient())
18+
.then(result => +result[0].count)
19+
},
20+
getToysAvailableToCat: async catId => {
21+
return getToysAvailableToCat
22+
.run({ catId }, getClient())
23+
.then(result => result.map(r => r.toy_name))
24+
},
25+
}
26+
27+
export default EntityTraversal

src/Packages/pgtyped/src/index.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Client } from 'pg'
2+
import EntityTraversal from './Benchmarks/EntityTraversal'
3+
import IORMPackage from 'BenchmarkUtils/interfaces/PackageUtils'
4+
5+
let _client: Client
6+
7+
export const getClient = () => {
8+
return _client
9+
}
10+
11+
const initialize = async () => {
12+
_client = new Client({
13+
host: 'localhost',
14+
user: 'benchmark',
15+
password: 'benchmark_pwd',
16+
database: 'benchmark',
17+
})
18+
await _client.connect()
19+
}
20+
21+
const destroy = async () => {
22+
await _client.end()
23+
}
24+
25+
const PgTypedPackage: IORMPackage = {
26+
name: 'pgtyped',
27+
initialize,
28+
destroy,
29+
implementations: {
30+
EntityTraversal,
31+
},
32+
}
33+
34+
export { PgTypedPackage }

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { KnexPackage } from './Packages/knex/src'
66
import { MikroORMPackage } from './Packages/mikroORM/src'
77
import { HtmlSerializer } from 'BenchmarkUtils/ResultSerializers/HtmlSerializer'
88
import { ObjectionPackage } from './Packages/objection/src'
9+
import { PgTypedPackage } from './Packages/pgtyped/src'
910

1011
const br = new BenchmarkRunner(
11-
[KnexPackage, MikroORMPackage, ObjectionPackage],
12+
[KnexPackage, MikroORMPackage, ObjectionPackage, PgTypedPackage],
1213
[MvpBench, EntityTraversal],
1314
[new ConsoleSerializer(), new HtmlSerializer()]
1415
)

0 commit comments

Comments
 (0)