Skip to content

Commit

Permalink
fix: patch kysely types for dynamic selections 🩹 (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiAbdou authored Apr 7, 2024
1 parent 5108a92 commit b54a9f7
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 24 deletions.
12 changes: 5 additions & 7 deletions apps/admin-dashboard/app/routes/_dashboard.schools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ export async function loader({ request }: LoaderFunctionArgs) {
}

async function listSchools({ limit, page, search }: ListSearchParams) {
let query = db.selectFrom('schools');

if (search) {
query = query
.where(sql`similarity(name, ${search}) > 0.15`)
.where(sql`word_similarity(name, ${search}) > 0.15`);
}
const query = db.selectFrom('schools').$if(!!search, (qb) => {
return qb
.where(sql<boolean>`similarity(name, ${search}) > 0.15`)
.where(sql<boolean>`word_similarity(name, ${search}) > 0.15`);
});

const [rows, countResult] = await Promise.all([
query
Expand Down
2 changes: 1 addition & 1 deletion apps/member-profile/app/routes/countries.search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function searchCountries(search: string) {
eb('countries.demonym', 'ilike', `%${search}%`),
eb('countries.name', 'ilike', `%${search}%`),
eb('countries.flagEmoji', '=', search),
sql`similarity(countries.name, ${search}) > 0.5`,
sql<boolean>`similarity(countries.name, ${search}) > 0.5`,
]);
})
.orderBy(sql`similarity(countries.name, ${search})`, 'desc');
Expand Down
4 changes: 2 additions & 2 deletions apps/member-profile/app/routes/schools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ async function listSchools(search: string) {

if (search) {
query = query
.where(sql`similarity(name, ${search}) > 0.15`)
.where(sql`word_similarity(name, ${search}) > 0.15`)
.where(sql<boolean>`similarity(name, ${search}) > 0.15`)
.where(sql<boolean>`word_similarity(name, ${search}) > 0.15`)
.orderBy(sql`similarity(name, ${search})`, 'desc')
.orderBy(sql`word_similarity(name, ${search})`, 'desc');
} else {
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"googleapis": "^126.0.1",
"ioredis": "^5.3.2",
"jsonwebtoken": "^9.0.2",
"kysely": "^0.26.3",
"nanoid": "^3.0.0",
"nodemailer": "^6.9.13",
"postmark": "^3.0.15",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SelectExpression } from 'kysely';
import { type SelectExpression } from 'kysely';

import type { DB } from '@oyster/db';
import { type DB } from '@oyster/db';

import { db } from '@/infrastructure/database';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/event/queries/list-events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SelectExpression } from 'kysely';
import { type SelectExpression } from 'kysely';

import { type DB } from '@oyster/db';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "@oyster/tsconfig/base.json",
"include": ["src", "../db/dist/db.d.ts"],
"include": ["src", "../db/kysely-patch.d.ts", "../db/dist/db.d.ts"],
"compilerOptions": {
"baseUrl": ".",
"jsx": "react-jsx",
Expand Down
14 changes: 14 additions & 0 deletions packages/db/kysely-patch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This file is needed in order to fix a Typescript error when implementing
// a dynamic selection of columns ("This is likely not portable. A type
// annotation is necessary."). Though it's likely something that Typescript
// needs to fix, it can be fixed by exporting the necessary dynamic types from
// the "kysely" package. Since Kysely doesn't export them by default, we need to
// manually export them in this declaration file.

import type {} from '../../node_modules/kysely/dist/esm/dynamic/dynamic-reference-builder';
import type {} from '../../node_modules/kysely/dist/esm/util/type-utils';

declare module 'kysely' {
export * from '../../node_modules/kysely/dist/esm/dynamic/dynamic-reference-builder';
export * from '../../node_modules/kysely/dist/esm/util/type-utils';
}
3 changes: 2 additions & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "module",
"exports": {
".": "./src/index.ts",
"./dist/db": "./dist/db.d.ts",
"./test/*": "./src/test/*",
"./test/constants": "./src/test/constants.ts"
},
Expand All @@ -19,7 +20,7 @@
"types": "kysely-codegen --dialect=postgres --camel-case --out-file=./dist/db.d.ts"
},
"dependencies": {
"kysely": "^0.26.3",
"kysely": "^0.27.3",
"pg": "^8.8.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/db/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "@oyster/tsconfig/base.json",
"include": ["src", "./dist/db.d.ts"],
"include": ["src", "kysely-patch.d.ts", "./dist/db.d.ts"],
"compilerOptions": {
"jsx": "react-jsx",
"module": "ESNext",
Expand Down
39 changes: 32 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6336,10 +6336,10 @@ kysely-codegen@^0.10.1:
micromatch "^4.0.5"
minimist "^1.2.8"

kysely@^0.26.3:
version "0.26.3"
resolved "https://registry.yarnpkg.com/kysely/-/kysely-0.26.3.tgz#45fdd0153d8c9418b0ea9a6f05ed46b95ed27678"
integrity sha512-yWSgGi9bY13b/W06DD2OCDDHQmq1kwTGYlQ4wpZkMOJqMGCstVCFIvxCCVG4KfY1/3G0MhDAcZsip/Lw8/vJWw==
kysely@^0.27.3:
version "0.27.3"
resolved "https://registry.yarnpkg.com/kysely/-/kysely-0.27.3.tgz#6cc6c757040500b43c4ac596cdbb12be400ee276"
integrity sha512-lG03Ru+XyOJFsjH3OMY6R/9U38IjDPfnOfDgO3ynhbDr+Dz8fak+X6L62vqu3iybQnj+lG84OttBuU9KY3L9kA==

lazy-cache@^1.0.3:
version "1.0.4"
Expand Down Expand Up @@ -9445,7 +9445,7 @@ string-hash@^1.1.1:
resolved "https://registry.yarnpkg.com/string-hash/-/string-hash-1.1.3.tgz#e8aafc0ac1855b4666929ed7dd1275df5d6c811b"
integrity sha512-kJUvRUFK49aub+a7T1nNE66EJbZBMnBgoC1UbCZ5n6bsZKBRga4KgBRTMn/pFkeCZSYtNeSyMxPDM0AXWELk2A==

"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand All @@ -9463,6 +9463,15 @@ string-width@^1.0.1:
is-fullwidth-code-point "^1.0.0"
strip-ansi "^3.0.0"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^5.0.1, string-width@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794"
Expand Down Expand Up @@ -9531,7 +9540,7 @@ stringify-entities@^4.0.0:
character-entities-html4 "^2.0.0"
character-entities-legacy "^3.0.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand All @@ -9552,6 +9561,13 @@ strip-ansi@^5.0.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1, strip-ansi@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
Expand Down Expand Up @@ -10684,7 +10700,16 @@ word-wrap@^1.1.0:
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"
integrity sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand Down

0 comments on commit b54a9f7

Please sign in to comment.