From 4e4d2672717d93be28cd8e5200b98535504fdc38 Mon Sep 17 00:00:00 2001 From: 62973268dda6dd3cbb7cce9b <62973268dda6dd3cbb7cce9b@devide> Date: Sat, 6 May 2023 09:06:01 +0000 Subject: [PATCH] Auto commit --- app/controller/index/index.js | 4 ++-- app/extend/application.js | 2 +- app/lib/db/config.js | 10 ++++++++++ app/lib/db/file.js | 12 ++++++------ app/lib/db/mysql.js | 30 +++++++++++++++++++++++++++++- app/web/page/index/index.vue | 6 ++++-- package.json | 1 + 7 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 app/lib/db/config.js diff --git a/app/controller/index/index.js b/app/controller/index/index.js index 2c0995e..e1db274 100644 --- a/app/controller/index/index.js +++ b/app/controller/index/index.js @@ -3,8 +3,8 @@ const egg = require('egg'); module.exports = class IndexController extends egg.Controller { async ssr() { - const result = this.service.article.getArtilceList(); - await this.ctx.render('index/index.js', result); + const result = await this.service.article.getArtilceList(); + await this.ctx.render('index/index.js', { list: result }); } async csr() { diff --git a/app/extend/application.js b/app/extend/application.js index 697c356..87addfc 100644 --- a/app/extend/application.js +++ b/app/extend/application.js @@ -4,7 +4,7 @@ const DBSymbol = Symbol('Application#db'); module.exports = { get db() { if (!this[DBSymbol]) { - this[DBSymbol] = Factory(); + this[DBSymbol] = Factory('mysql'); } return this[DBSymbol]; }, diff --git a/app/lib/db/config.js b/app/lib/db/config.js new file mode 100644 index 0000000..42a6c13 --- /dev/null +++ b/app/lib/db/config.js @@ -0,0 +1,10 @@ +const db = { + mysql: { + host: 'mysql.inscode.run', + user: 'root', + password: 'inscode', + database: 'blog' + } +} + +exports = module.exports = { db }; diff --git a/app/lib/db/file.js b/app/lib/db/file.js index 6bca892..5f9dbd9 100644 --- a/app/lib/db/file.js +++ b/app/lib/db/file.js @@ -11,27 +11,27 @@ module.exports = class FileDB extends Base { this.instance._.mixin(lodashid); this.create(); } - create() { + async create() { this.instance.defaults({ article: [], user: {} }).write(); } - get(collectionName) { + async get(collectionName) { return this.instance.get(collectionName); } - add(collectionName, json) { + async add(collectionName, json) { return this.get(collectionName) .push(json) .write(); } - update(collectionName, where, json) { + async update(collectionName, where, json) { return this.get(collectionName).find(where).assign(json).write(); } - delete(collectionName, field) { + async delete(collectionName, field) { return this.get(collectionName).remove(field).write(); } - getPager(collectionName, json) { + async getPager(collectionName, json) { const { where, like, diff --git a/app/lib/db/mysql.js b/app/lib/db/mysql.js index 70a8d2b..9e22b0c 100644 --- a/app/lib/db/mysql.js +++ b/app/lib/db/mysql.js @@ -1,3 +1,31 @@ 'use strict'; +const mysql = require('mysql'); + const Base = require('./base'); -module.exports = class MySQLDB extends Base {}; +const config = require('./config') + +module.exports = class MySQLDB extends Base { + constructor() { + super(); + + this.connection = mysql.createConnection({ + ...config.db.mysql + }); + + this.connection.connect(); + } + + getUserById(id) { + const sql = `select * from user where id = ${id}`; + + return new Promise((resolve, reject) => { + this.connection.query(sql, function (err, results, fields) { + err ? reject(err) : resolve(results); + }); + }); + } + + getPager(collectionName, json) { + return this.getUserById(1); + } +}; diff --git a/app/web/page/index/index.vue b/app/web/page/index/index.vue index 3e1cc10..84090fe 100644 --- a/app/web/page/index/index.vue +++ b/app/web/page/index/index.vue @@ -6,9 +6,9 @@