Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Database in Multi-tenant #64

Open
Kokleng-Dev opened this issue Oct 24, 2024 · 4 comments
Open

Switch Database in Multi-tenant #64

Kokleng-Dev opened this issue Oct 24, 2024 · 4 comments

Comments

@Kokleng-Dev
Copy link

How to switch database ? and is it support sub query?

@Kokleng-Dev Kokleng-Dev changed the title Switch Data in Multi-tenant Switch Database in Multi-tenant Oct 24, 2024
@kiddyuchina
Copy link
Member

Hi, You can add multiple database connections through addConnection, and you can specify a connection to use in the model:

// Query builder
sutando.addConnection({
  client: 'mysql2',
  connection: {
    ...
  },
});
const mysqlConnection = sutando.connection();

sutando.addConnection({
  client: 'pg',
  connection: {
    ...
  },
}, 'another_connection');
const postgresConnection = sutando.connection('another_connection');

// Model
// default connection
class User extends Model {
}

// another_connection connection
class PgUser extends Model {
  connection = 'another_connection';
}

The query builder supports sub-query:

const users = await db.table('users')
  .where('name', '=', 'John')
  .where((query) => {
    query.where('votes', '>', 100).orWhere('title', '=', 'Admin');
  })
  .get();

@Kokleng-Dev
Copy link
Author

Kokleng-Dev commented Oct 27, 2024

Thank you so much !!!.

how about this in laravel, can sutando do? and sutando can use with bun (javascript run time) or not? is it fast ?

DB::table('users')
      ->joinSub(
          DB::table('books')->select(DB::raw("COUNT(*) as total"), 'user_id')->groupBy('user_id'), 
          'a',
          function($query){
              $query->on('users.id','a.user_id');
          }
      )
      ->select('users.name as user','a.total as total_book')
      ->get();

@kiddyuchina
Copy link
Member

kiddyuchina commented Oct 27, 2024

it seems that you don't need to use a subquery, you can achieve the same result with:

db.table('users').select('users.name as user', db.raw('COUNT(books.id) as total_book'))
      .join('posts', 'users.id', '=', 'books.user_id')
      .groupBy('users.id')
      .get();

// or you can use model relationship:
User.query().select('name').withCount('books').get()

as for converting this Laravel query into sutando, you'll need to use raw to write the native SQL statement, as sutando has not yet implemented the joinSub method.

sutando can run on Bun, although I don't know the actual running speed, but when executing sutando test cases, bun is much faster than node. if you encounter any issues with Bun, feel free to post them.

@Kokleng-Dev
Copy link
Author

Kokleng-Dev commented Oct 27, 2024

Thank you for your response!

Would you consider adding joinSub, leftJoinSub, and similar methods? I know I can write my query without sub-joins, but this is just an example. Could you explain how to handle scenarios where sub-joins are necessary? I feel that this is a common requirement when developing any application, and I’m concerned that Sutando may not handle it well.

A few more questions: I see Sutando is based on Knex.js, which now supports TypeScript. Would you consider adding TypeScript support to Sutando as well?

Is it Avoiding N+1 ?

Are you using Sutando in a real production application, and do you know if anyone else is using it in production? Also, is Sutando stable enough for production, or is it more of a personal project for now? I’m curious if you plan to continue developing it actively.

I just wanted to say how much I love the syntax of Sutando for querying the database! Even though my friends and team have recommended other libraries like Prisma, Drizzle, and Lucid, I keep coming back to Sutando because of its clean documentation and Laravel-like syntax – it’s a pleasure to work with.

I really appreciate that it includes features like transactions, soft deletes, and migrations. I haven’t seen seeding yet – maybe that’s coming? 😊 Thank you for your work on this; it’s really made a positive impact on my development experience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants