Advertisement
attilan

JavaScript course

Apr 25th, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. const path = require('path');
  3. const fs = require('fs');
  4. const database = require('./index');
  5. const util = require('../common/util');
  6.  
  7. const dbPath = path.resolve(__dirname, 'education.db');
  8. const userDataPath = path.resolve(__dirname, 'initdata', 'initUser.json');
  9.  
  10. const badgeDataPath = path.resolve(__dirname, 'initdata', 'initBadge.json');
  11.  
  12. // ...
  13.  
  14. const createBadgeTable = async () => {
  15.     await database.open(dbPath);
  16.     await database.run('DROP TABLE IF EXISTS badges;');
  17.     await database.run('CREATE TABLE badges(id INTEGER PRIMARY KEY AUTOINCREMENT, name text, description text);');
  18.         await database.run(`
  19.         CREATE TABLE user_badges(
  20.             id INTEGER PRIMARY KEY AUTOINCREMENT,
  21.             user_id integer NOT_NULL,
  22.             badge_id integer NOT_NULL,
  23.             FOREIGN KEY (user_id) REFERENCES users(id)
  24.                 ON DELETE CASCADE
  25.                 ON UPDATE RESTRICT,
  26.             FOREIGN KEY (badge_id) REFERENCES badges(id)
  27.                 ON DELETE CASCADE
  28.                 ON UPDATE RESTRICT
  29.         );
  30.     `);
  31.  
  32.     const query = `INSERT INTO badges(name, description) VALUES (?, ?)`;
  33.     const lstBadge = JSON.parse(fs.readFileSync(badgeDataPath, 'utf8'));
  34.  
  35.     for (let i = 0; i < lstBadge.length; i++) {
  36.         let badge = lstBadge[i];
  37.         await database.runWithPrepareStatement(query, [badge.name, badge.description]);
  38.     }
  39.  
  40.     const badgeRows = await database.all(`SELECT * FROM badges`);
  41.     console.log(badgeRows);
  42.  
  43.     database.close();
  44.  
  45.     return badgeRows;
  46. };
  47. // createUserTable()
  48. //     .then(() => createBadgeTable())
  49. //     .catch((e) => console.log(e));
  50.  
  51.  
  52.  
  53.  
  54. const createUserTable = async () => {
  55.     await database.open(dbPath);
  56.     await database.run('DROP TABLE IF EXISTS users;');
  57.     await database.run('CREATE TABLE users(id INTEGER PRIMARY KEY AUTOINCREMENT, email text, name text, password text);');
  58.  
  59.     const query = `INSERT INTO users(email, name, password) VALUES (?, ?, ?)`;
  60.     const lstUser = JSON.parse(fs.readFileSync(userDataPath, 'utf8'));
  61.  
  62.     for (let i = 0; i < lstUser.length; i++) {
  63.         let user = lstUser[i];
  64.         user.password = await util.hashPassword(user.password);
  65.         await database.runWithPrepareStatement(query, [user.email, user.name, user.password]);
  66.     }
  67.  
  68.     const userRows = await database.all(`SELECT * FROM users`);
  69.     console.log(userRows);
  70.  
  71.     database.close();
  72.  
  73.     return userRows;
  74. };
  75.  
  76. Promise.resolve()
  77.     .then(() => createUserTable())
  78.     .then(() => createBadgeTable())
  79.     .catch((e) => console.log(e));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement