db.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import pg from 'pg'
  2. const { Pool } = pg
  3. export const pool = new Pool({
  4. connectionString: process.env.DATABASE_URL || 'postgresql://xinghen:xinghen@localhost:5432/xinghen'
  5. })
  6. export const mapUser = (r: any) => ({
  7. id: r.id,
  8. phone: r.phone,
  9. role: r.role || 'USER',
  10. organization: r.organization || '',
  11. wechat: r.wechat || '',
  12. contactName: r.contact_name || '',
  13. bio: r.bio || '',
  14. createdAt: r.created_at,
  15. updatedAt: r.updated_at
  16. })
  17. export const mapAsset = (r: any) => ({
  18. id: r.id,
  19. originalName: r.original_name,
  20. size: Number(r.size),
  21. mimeType: r.mime_type,
  22. url: `/uploads/${r.stored_name}`,
  23. createdAt: r.created_at
  24. })
  25. export const mapInstructor = (r: any) => ({
  26. id: r.id,
  27. name: r.name,
  28. organization: r.organization || '',
  29. introduction: r.introduction || '',
  30. imageUrl: r.image_stored_name ? `/uploads/${r.image_stored_name}` : null
  31. })
  32. export const mapDeliverable = (r: any) => ({
  33. id: r.id,
  34. originalName: r.original_name,
  35. size: Number(r.size),
  36. mimeType: r.mime_type,
  37. url: `/uploads/${r.stored_name}`,
  38. createdAt: r.created_at
  39. })
  40. export const mapCourse = (
  41. r: any,
  42. assets: any[] = [],
  43. instructors: any[] = [],
  44. deliverables: any[] = [],
  45. creatorUser: any = null
  46. ) => ({
  47. id: r.id,
  48. userId: r.user_id,
  49. name: r.name,
  50. category: r.category || '',
  51. audience: r.audience || '',
  52. description: r.description || '',
  53. status: r.status,
  54. productionNotes: r.production_notes || '',
  55. createdAt: r.created_at,
  56. submittedAt: r.submitted_at || null,
  57. completedAt: r.completed_at || null,
  58. assets: assets.map(mapAsset),
  59. instructors: instructors.map(mapInstructor),
  60. deliverables: deliverables.map(mapDeliverable),
  61. creator: creatorUser ? mapUser(creatorUser) : (r.user_phone ? {
  62. id: r.user_id,
  63. phone: r.user_phone,
  64. role: r.user_role || 'USER',
  65. organization: r.user_organization || '',
  66. wechat: r.user_wechat || '',
  67. contactName: r.user_contact_name || '',
  68. bio: r.user_bio || '',
  69. createdAt: r.user_created_at
  70. } : undefined)
  71. })