Guest User

Untitled

a guest
Apr 24th, 2017
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { select, from, table, column, DbConnection, insertInto, update, val, defaultValue, SqlGenerator, PostgreQueryService, concat, not, deleteFrom } from "../src/index";
  2. import pg = require("pg");
  3.  
  4. const contacts = table({ name: "contacts", schema: "public" },
  5.     {
  6.         firstname: column<string>(),
  7.         lastname: column<string>(),
  8.         mother_id: column<number | null>(),
  9.         father_id: column<number | null>()
  10.     },
  11.     { id: column<number>() }
  12. );
  13.  
  14. const pool = new pg.Pool({
  15.     database: "postgres",
  16.     user: "postgres",
  17.     password: "FXLjrQ0"
  18. });
  19.  
  20. (async function () {
  21.  
  22.     const queryService = new PostgreQueryService(pool);
  23.     queryService.onSqlStatement.sub((queryService, args) => {
  24.         console.log(args.sql);
  25.     });
  26.     const dbCon = new DbConnection(queryService);
  27.  
  28.     await dbCon.exec(deleteFrom(contacts).where(val(true)));
  29.     await dbCon.exec(insertInto(contacts).values(
  30.         { firstname: "Hoster", lastname: "Tully", father_id: null, mother_id: null, id: 0 },
  31.         { firstname: "Catelyn", lastname: "Tully", father_id: 0, mother_id: null, id: 1 },
  32.         { firstname: "Lysa", lastname: "Tully", father_id: 0, mother_id: null, id: 2 },
  33.         { firstname: "Edmure", lastname: "Tully", father_id: 0, mother_id: null, id: 3 },
  34.  
  35.         { firstname: "Rickard", lastname: "Stark", father_id: null, mother_id: null, id: 4 },
  36.         { firstname: "Benjen", lastname: "Stark", father_id: 4, mother_id: null, id: 5 },
  37.         { firstname: "Brandon", lastname: "Stark", father_id: 4, mother_id: null, id: 6 },
  38.         { firstname: "Lyanna", lastname: "Stark", father_id: 4, mother_id: null, id: 7 },
  39.         { firstname: "Eddard", lastname: "Stark", father_id: 4, mother_id: null, id: 8 },
  40.  
  41.         { firstname: "Robb", lastname: "Stark", father_id: 8, mother_id: 1, id: 9 },
  42.         { firstname: "Sansa", lastname: "Stark", father_id: 8, mother_id: 1, id: 10 },
  43.         { firstname: "Arya", lastname: "Stark", father_id: 8, mother_id: 1, id: 11 }
  44.     ));
  45.  
  46.     const aryaId = await dbCon.firstValue(
  47.         from(contacts).where(contacts.firstname.isEqualTo("Arya")).select("id")
  48.     );
  49.  
  50.     const arya = contacts.as("arya");
  51.     const siblingsOfArya = await dbCon.exec(
  52.         from(contacts)
  53.         .leftJoin(arya).on({ id: aryaId })
  54.         .where({ father_id: arya.father_id, mother_id: arya.mother_id })
  55.         .select(contacts.firstname, contacts.lastname)
  56.     );
  57.  
  58.     await dbCon.exec(
  59.         deleteFrom(contacts).where(contacts.id.isEqualTo(
  60.             from(contacts).where({ id: aryaId }).select(contacts.father_id).asExpression().cast<number>()
  61.         ))
  62.     );
  63.  
  64.     console.log(siblingsOfArya.map(r => r.firstname + " " + r.lastname));
  65.  
  66.     await pool.end();
  67. })();
Add Comment
Please, Sign In to add comment