sql-create.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const pgp = require('pg-promise')();
  2. // 连接到数据库
  3. const db = pgp({
  4. user: 'web3',
  5. password: '666',
  6. host: 'web2023.fmode.cn',
  7. port: 25432,
  8. database: 'dev'
  9. });
  10. // 执行SELECT语句并返回结果的函数
  11. async function main() {
  12. try {
  13. // 查询:数据库版本信息
  14. let sql = `
  15. CREATE TABLE "Member" (
  16. "objectId" text PRIMARY KEY,
  17. "name" text,
  18. "mobile" text,
  19. "registerDate" timestamp with time zone,
  20. "invite" text REFERENCES "Member" ("objectId"),
  21. "invitePath" jsonb,
  22. "inviteDate" timestamp with time zone
  23. );
  24. CREATE TABLE "InviteEvent" (
  25. "objectId" text PRIMARY KEY,
  26. "member" text REFERENCES "Member" ("objectId"),
  27. "type" text,
  28. "click" text,
  29. "share" text,
  30. "count" double precision DEFAULT 1
  31. );
  32. INSERT INTO "Member" ("objectId", "name", "mobile", "registerDate", "invite", "invitePath", "inviteDate")
  33. VALUES
  34. ('1', '张三', '13812345678', '2024-06-16 12:00:00', NULL, '[]', NULL),
  35. ('2', '李四', '13987654321', '2024-06-16 12:30:00', '1', '["1"]', '2024-06-16 13:00:00'),
  36. ('3', '王五', '13611112222', '2024-06-16 13:00:00', '2', '["1", "2"]', '2024-06-16 13:30:00'),
  37. ('4', '赵六', '13533334444', '2024-06-16 14:00:00', '2', '["1", "2"]', '2024-06-16 14:30:00'),
  38. ('5', '刘七', '13755556666', '2024-06-16 15:00:00', '4', '["1", "2", "4"]', '2024-06-16 15:30:00');
  39. INSERT INTO "InviteEvent" ("objectId", "member", "type", "click", "share", "count")
  40. VALUES
  41. ('101', '1', '邀请注册', '10', '5', 1),
  42. ('102', '2', '邀请注册', '8', '3', 1),
  43. ('103', '3', '邀请注册', '6', '2', 1),
  44. ('104', '4', '邀请注册', '5', '1', 1),
  45. ('105', '5', '邀请注册', '4', '1', 1);
  46. `
  47. const data = await db.any(sql);
  48. console.log(data)
  49. return data;
  50. } catch (error) {
  51. console.error('Error executing SELECT query:', error);
  52. return null;
  53. }
  54. }
  55. // 调用函数并处理结果
  56. main()