useDB() composableThe useDB() composable is provided to access the database and perform CRUD operations. It's auto-imported across the project, so you can use it in any file.
export default defineEventHandler(async (event) => {
const userRecord = await useDB()
.select()
.from(tables.users)
.where(eq(tables.users.email, email));
return userRecord;
});
Supersaas organizes database actions into separate files for different entities:
UserActions.js: User CRUD operationsAuthActions.js: Auth CRUD operationsAdminActions.js: Admin CRUD operationsSubscriptionActions.js: Subscription CRUD operationsImageActions.js: Image CRUD operationsPostsActions.js: Post CRUD operationsEach action file contains a class with methods for specific database operations. For example, in UserActions.js
class UserActions {
async findUserByEmail(email) {
try {
const [existingUser] = await useDB()
.select()
.from(tables.users)
.where(eq(tables.users.email, email));
return existingUser || null;
} catch (error) {
console.error(error);
return null;
}
}
// ... other methods
}
export const userActions = new UserActions();