· 9 years ago · Jan 22, 2017, 09:22 AM
1// Copyright 2015-2016, Google, Inc.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14'use strict';
15
16const extend = require('lodash').assign;
17const mysql = require('mysql');
18const config = require('../config');
19
20function getConnection () {
21 const options = {
22 user: config.get('root'),
23 password: config.get('brexit2016scotland2017'),
24 database: 'www'
25 };
26
27 if (config.get('INSTANCE_CONNECTION_NAME') && config.get('NODE_ENV') === 'production') {
28 options.socketPath = `/cloudsql/${config.get('INSTANCE_CONNECTION_NAME')}`;
29 }
30
31 return mysql.createConnection(options);
32}
33
34/*function list (limit, token, cb) {
35 token = token ? parseInt(token, 10) : 0;
36 const connection = getConnection();
37 connection.query(
38 'SELECT * FROM `www` LIMIT ? OFFSET ?', [limit, token],
39 (err, results) => {
40 if (err) {
41 cb(err);
42 return;
43 }
44 const hasMore = results.length === limit ? token + results.length : false;
45 cb(null, results, hasMore);
46 }
47 );
48 connection.end();
49}*/
50
51function create (data, cb) {
52 const connection = getConnection();
53 connection.query('INSERT INTO `CLOTHES` SET ?', data, (err, res) => {
54 if (err) {
55 cb(err);
56 return;
57 }
58 read(res.insertId, cb);
59 });
60 connection.end();
61}
62
63/*
64function read (id, cb) {
65 const connection = getConnection();
66 connection.query(
67 'SELECT * FROM `books` WHERE `id` = ?', id, (err, results) => {
68 if (err) {
69 cb(err);
70 return;
71 }
72 if (!results.length) {
73 cb({
74 code: 404,
75 message: 'Not found'
76 });
77 return;
78 }
79 cb(null, results[0]);
80 });
81 connection.end();
82}
83
84function update (id, data, cb) {
85 const connection = getConnection();
86 connection.query(
87 'UPDATE `books` SET ? WHERE `id` = ?', [data, id], (err) => {
88 if (err) {
89 cb(err);
90 return;
91 }
92 read(id, cb);
93 });
94 connection.end();
95}
96
97function _delete (id, cb) {
98 const connection = getConnection();
99 connection.query('DELETE FROM `books` WHERE `id` = ?', id, cb);
100 connection.end();
101}*/
102/*
103module.exports = {
104 createSchema: createSchema,
105 list: list,
106 create: create,
107 read: read,
108 update: update,
109 delete: _delete
110};
111
112if (module === require.main) {
113 const prompt = require('prompt');
114 prompt.start();
115
116 console.log(
117 `Running this script directly will allow you to initialize your mysql database.
118 This script will not modify any existing tables.`);
119
120 prompt.get(['user', 'password'], (err, result) => {
121 if (err) {
122 return;
123 }
124 createSchema(result);
125 });
126}
127
128function createSchema (config) {
129 const connection = mysql.createConnection(extend({
130 multipleStatements: true
131 }, config));
132
133 connection.query(
134 `CREATE DATABASE IF NOT EXISTS \`bookshelf\`
135 DEFAULT CHARACTER SET = 'utf8'
136 DEFAULT COLLATE 'utf8_general_ci';
137 USE \`bookshelf\`;
138 CREATE TABLE IF NOT EXISTS \`bookshelf\`.\`books\` (
139 \`id\` INT UNSIGNED NOT NULL AUTO_INCREMENT,
140 \`title\` VARCHAR(255) NULL,
141 \`author\` VARCHAR(255) NULL,
142 \`publishedDate\` VARCHAR(255) NULL,
143 \`imageUrl\` VARCHAR(255) NULL,
144 \`description\` TEXT NULL,
145 \`createdBy\` VARCHAR(255) NULL,
146 \`createdById\` VARCHAR(255) NULL,
147 PRIMARY KEY (\`id\`));`,
148 (err) => {
149 if (err) {
150 throw err;
151 }
152 console.log('Successfully created schema');
153 connection.end();
154 }
155 );
156}*/