Enhance database management with cleanup and reset scripts

- Added a new database cleanup script to remove all data while preserving the schema, facilitating a clean state before seeding.
- Updated package.json to include new commands for database cleaning and resetting.
- Modified schema definitions to use an array format for index and constraint definitions, improving clarity and consistency.
- Updated product seeding logic to utilize role-based assignments directly from mock data, enhancing flexibility in product-role relationships.
This commit is contained in:
Bastian Masanek
2025-11-02 10:35:07 +01:00
parent ff9960edef
commit 268d91f548
5 changed files with 121 additions and 39 deletions

View File

@@ -149,11 +149,11 @@ export const products = pgTable(
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
},
(table) => ({
navProductIdIdx: index('products_nav_product_id_idx').on(table.navProductId),
activeIdx: index('products_active_idx').on(table.active),
categoryIdx: index('products_category_idx').on(table.category),
})
(table) => [
index('products_nav_product_id_idx').on(table.navProductId),
index('products_active_idx').on(table.active),
index('products_category_idx').on(table.category),
]
)
/**
@@ -204,15 +204,12 @@ export const userRoles = pgTable(
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
},
(table) => ({
(table) => [
// Unique constraint: User can only have one entry per role
userIdRoleIdUnique: index('user_roles_user_id_role_id_unique').on(
table.userId,
table.roleId
),
userIdIdx: index('user_roles_user_id_idx').on(table.userId),
statusIdx: index('user_roles_status_idx').on(table.status),
})
index('user_roles_user_id_role_id_unique').on(table.userId, table.roleId),
index('user_roles_user_id_idx').on(table.userId),
index('user_roles_status_idx').on(table.status),
]
)
/**
@@ -233,14 +230,11 @@ export const productRoleVisibility = pgTable(
.references(() => roles.id, { onDelete: 'cascade' }),
createdAt: timestamp('created_at').defaultNow().notNull(),
},
(table) => ({
(table) => [
// Unique constraint: Product-Role pair can only exist once
productIdRoleIdUnique: index('product_role_visibility_product_id_role_id_unique').on(
table.productId,
table.roleId
),
productIdIdx: index('product_role_visibility_product_id_idx').on(table.productId),
})
index('product_role_visibility_product_id_role_id_unique').on(table.productId, table.roleId),
index('product_role_visibility_product_id_idx').on(table.productId),
]
)
/**
@@ -296,11 +290,11 @@ export const orders = pgTable(
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
},
(table) => ({
orderNumberIdx: index('orders_order_number_idx').on(table.orderNumber),
userIdIdx: index('orders_user_id_idx').on(table.userId),
statusIdx: index('orders_status_idx').on(table.status),
})
(table) => [
index('orders_order_number_idx').on(table.orderNumber),
index('orders_user_id_idx').on(table.userId),
index('orders_status_idx').on(table.status),
]
)
/**