sutandojs / sutando

Sutando is a modern Node.js ORM, like Laravel Eloquent.
https://sutando.org
MIT License
186 stars 15 forks source link

Switch Database in Multi-tenant #64

Open Kokleng-Dev opened 3 weeks ago

Kokleng-Dev commented 3 weeks ago

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

kiddyuchina commented 3 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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!