Guest User

Untitled

a guest
Jan 13th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. 'use strict';
  2. const connection = {
  3. client: 'pg',
  4. connection: {
  5. host : process.env.HOST,
  6. user : process.env.USER,
  7. password : process.env.PASSWORD,
  8. database : process.env.DATABASE
  9. },
  10. pool: { min: 1, max: 10 },
  11. debug: false
  12. };
  13.  
  14. const knex = require('knex')(connection);
  15. const TABLE_TODO = 'todo';
  16.  
  17. module.exports.createTodoTable = async (event, context) => {
  18. if(await knex.schema.hasTable(TABLE_TODO)){
  19. return response(403, 'Table '+ TABLE_TODO+ ' already exist');
  20. }
  21.  
  22. const table = await knex.schema.createTable(TABLE_TODO, function(table) {
  23. table.increments('id').primary();
  24. table.string('text');
  25. table.dateTime("created_at").defaultTo(knex.fn.now());
  26. table.dateTime("updated_at");
  27. table.dateTime("deleted_at");
  28. });
  29.  
  30. return response(200,table);
  31. };
  32.  
  33. module.exports.addToTodoTable = async (event, context) => {
  34. if(!await knex.schema.hasTable(TABLE_TODO)){
  35. return response(403, 'Table '+ TABLE_TODO+ ' not exist');
  36. }
  37.  
  38. const table = await knex(TABLE_TODO).insert([{
  39. text: 'Hello, This is my first note',
  40. }])
  41.  
  42. return response(200,table);
  43. };
  44.  
  45. function response(statusCode, body) {
  46. return {
  47. statusCode: statusCode,
  48. body: JSON.stringify(body),
  49. }
  50. };
Add Comment
Please, Sign In to add comment