Hi everyone. We are using Blitz to build a multite...
# orm-help
s
Hi everyone. We are using Blitz to build a multitenant application. One of our options (for reasons) is doing multitenancy manually, by having something like
Copy code
model Tenant {
  id      Int      @id @default(autoincrement())
  name    String

  widgets  Widget[]
}

model Widget {
  tenant    Tenant @relation(fields: [tenantId], references: [id])
  tenantId  Int
  id        Int     @default(autoincrement())
  @@id([tenantId, id])
  //...
}
The syntax seems to require the
widgets  Widget[]
field in the
Tenant
model, and the question is: what would happen if the number of total records in the
Widget
table is large? Is the array a proactively loaded construct, i.e. would it load the entirety of the DB at all times? Or is the array just syntactic sugar over something that is smarter than that? Also, is there a way to not require that array field? The schema is likely to be rather large, and the
Tenant
model is going to be just lines and lines of those array fields... Disclaimer: I know there are multitenancy plugins, I'm just comparing the alternatives.
1
d
(a) you only get the widget property if you include widgets in your query, you can qualitfy the include — see this example (https://github.com/prisma/prisma/discussions/11172)
Copy code
const user = await prisma.user.findFirst({
        where: {
          email,
        },
        include: {
          wishlist: {
            include: {
              product: {
                include: {
                  images: true,
                  pricing: {
                    take: 1,
                    orderBy: {
                      createdAt: "desc",
                    },
                  },
                  productDiscount: {
                    where: {
                      isValid: true,
                    },
                  },
                },
              },
            },
          },
s
So the field is not in the object otherwise?
d
yes - otherwise, every query would be an unending nested mess.
if you created a DB of github users , projects and issues, every time you queried a single user, you’d get half your database.
also even if you do include dependent tables, you can use “take” to limit the return count. (equivalent to SQL “SELECT …LIMIT 10").
s
Ah. I see. Thank you.