Comments (4)
Yes. See this recipe https://kysely.dev/docs/recipes/schemas
from kysely.
Thanks, but unfortunately it only works with a schema but not with a database.schema
from kysely.
Oh, that's true. I'm not sure if we can make that work without complicating everything too much. But let's take a look. For now it's impossible to do this without using raw SQL.
// PersonTable is the table interface.
.selectFrom(sql<PersonTable>`foo.bar.baz.person`.as('person'))
or something like:
function table<T>(...t: string[]) {
return sql<T>`${sql.id(...t)}`
}
...
.selectFrom(table<PersonTable>("foo", "bar", "baz", "spam").as('person'))
from kysely.
Just want to add this here:
With some slight modifications, the solution above covers all of my needs for cross database joins.
I have a bunch of databases that I do not have much control over and it's structured such that there are a few situations in which I need to join two tables across databases. I also have kysely-codegen writing all of my database schemas out to separate TypeScript files. I have some custom codegen alongside that which will create a TypeScript type that includes all of the databases from my server like so in file called schemas/index.ts:
import { DB as DB1 } from "./db1";
import { DB as DB2 } from "./db2";
export type Server = {
DB1: DB1;
DB2: DB2;
};
where ./db1 is something like
export interface DB {
person: {
id: Generated<string>
first_name: string
last_name: string | null
created_at: Generated<Date>
age: number
}
}
and ./db2 is something like
export interface DB {
pet: {
id: Generated<string>
name: string
owner_id: string
species: 'cat' | 'dog'
is_favorite: boolean
}
}
DB1 and DB2 would be the name of my databases.
All of my databases share the same schema name, so I can just do something like the following:
import { Server } from "./schemas";
export function table<
DB extends keyof Server,
Table extends string & keyof Server[DB]
>(database: DB, table: Table) {
return sql<Server[DB][Table]>`${sql.id(database, "dbo", table)}`;
}
With that, you can use it as such for a join:
const result = await db1.selectFrom("person")
.innerJoin(table("DB2", "pet").as("pet"), "person.id", "pet.owner_id")
.selectAll()
.execute();
You can also use the json helpers as such:
const result = await db1.selectFrom("person")
.selectAll("person")
.select((eb) =>
jsonArrayFrom(
eb
.selectFrom(table("DB2", "pet").as("pet"))
.select(['pet.id', 'pet.name'])
.whereRef("pet.owner_id", "=", "person.id")
).as("pets")
)
.execute();
Both of these keep the type safety without requiring explicit generics and allowing you to join across databases.
Apologies if any of this syntax is slightly off, I more or less took the example from kysely's relations recipe, modified it with the tables helper function and to fit the schemas in my databases. Then I subbed the example table's names and schemas back in, but I think this covers the gist of it.
Edit: here's a kysely playground link which might be a bit more digestible.
from kysely.
Related Issues (20)
- Are there any plans to support mikro-orm? HOT 2
- Support for Postgres ?| operator (do any of these array strings exist as top-level keys) HOT 1
- Invalid object name upon inserting value into temp table in mssql HOT 3
- Type Error for BLOB columns when using D1 Databases HOT 1
- [Question] best way to create reusable type-aware filters HOT 2
- Calling Migrator.migrateToLast() exits process HOT 2
- Add an AlterTypeBuilder
- where with sql complains about Property 'isSelectQueryBuilder' is missing HOT 1
- Improvement of lateral support HOT 2
- Migrations can be modified without notice HOT 3
- Incorrect type when using left join and json agg (postgres) HOT 3
- Also publish to JSR?
- Large offsets are converted to scientific notation HOT 1
- Documentation for UpdateResult HOT 1
- OnConflict update all Fields HOT 6
- Inserting large number of rows leads to exec_bind_message error in postgres HOT 1
- Question about a generic getById HOT 2
- May anyone help me with the short version of selectAll type? HOT 1
- RuntimeDriver is not exported HOT 4
- `SELECT` in SQL template tag has parens around it, making it invalid in some contexts in SQLite HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kysely.