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:
@@ -47,7 +47,16 @@ const standardRoles = [
|
||||
/**
|
||||
* Sample annual pass products for experimenta
|
||||
*/
|
||||
const mockProducts = [
|
||||
const mockProducts: Array<{
|
||||
navProductId: string
|
||||
name: string
|
||||
description: string
|
||||
price: string
|
||||
stockQuantity: number
|
||||
category: string
|
||||
active: boolean
|
||||
roles: Array<'private' | 'educator' | 'company'>
|
||||
}> = [
|
||||
{
|
||||
navProductId: 'MSPACE-JK-2025',
|
||||
name: 'Makerspace Jahreskarte',
|
||||
@@ -57,6 +66,7 @@ const mockProducts = [
|
||||
stockQuantity: 100,
|
||||
category: 'makerspace-annual-pass',
|
||||
active: true,
|
||||
roles: ['private', 'educator', 'company'],
|
||||
},
|
||||
{
|
||||
navProductId: 'EXPERIMENTA-JK-2025',
|
||||
@@ -67,6 +77,7 @@ const mockProducts = [
|
||||
stockQuantity: 200,
|
||||
category: 'annual-pass',
|
||||
active: true,
|
||||
roles: ['private', 'educator', 'company'],
|
||||
},
|
||||
{
|
||||
navProductId: 'PAEDAGOGEN-JK-2025',
|
||||
@@ -77,6 +88,7 @@ const mockProducts = [
|
||||
stockQuantity: 50,
|
||||
category: 'educator-annual-pass',
|
||||
active: true,
|
||||
roles: ['educator'],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -151,24 +163,22 @@ async function seed() {
|
||||
console.log(` - ${product.name} (${product.navProductId}) - €${product.price}`)
|
||||
})
|
||||
|
||||
// 3. Assign Roles to Products (Category-based mapping)
|
||||
console.log(`\n🔗 Assigning roles to products based on category...`)
|
||||
|
||||
// Category to role mapping
|
||||
const categoryRoleMapping: Record<string, string[]> = {
|
||||
'makerspace-annual-pass': ['private', 'educator'],
|
||||
'annual-pass': ['private'],
|
||||
'educator-annual-pass': ['educator'],
|
||||
}
|
||||
// 3. Assign Roles to Products (using roles array from mock data)
|
||||
console.log(`\n🔗 Assigning roles to products...`)
|
||||
|
||||
let assignmentCount = 0
|
||||
for (const product of insertedProducts) {
|
||||
const roleCodes = categoryRoleMapping[product.category] || []
|
||||
for (let i = 0; i < insertedProducts.length; i++) {
|
||||
const product = insertedProducts[i]
|
||||
const productData = mockProducts[i]
|
||||
const roleCodes = productData.roles || []
|
||||
|
||||
for (const roleCode of roleCodes) {
|
||||
// Find role by code
|
||||
const role = insertedRoles.find((r) => r.code === roleCode)
|
||||
if (!role) continue
|
||||
if (!role) {
|
||||
console.warn(` ⚠️ Role '${roleCode}' not found for product ${product.name}`)
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if assignment already exists
|
||||
const existing = await db.query.productRoleVisibility.findFirst({
|
||||
|
||||
Reference in New Issue
Block a user