diff --git a/PG/knex.js b/PG/knex.js index 0dc2648..b1a2053 100644 --- a/PG/knex.js +++ b/PG/knex.js @@ -7,6 +7,13 @@ const knex = require('knex')({ database: 'sandbox', port: 54321, }, + pool: { + min: 2, + max: 10, + idleTimeoutMillis: 30000, + createTimeoutMillis: 3000, + acquireTimeoutMillis: 30000, + }, // connection:`postgres://postgres:123456@yhm.ink:54321/sandbox` }) @@ -14,13 +21,17 @@ async function createTableIfNotExist({ tableName, columns }) { const exists = await knex.schema.hasTable(tableName) if (!exists) { await knex.schema.createTable(tableName, (table) => { - table.increments('id').primary() + tableName === 'codes' + ? table.increments('id').primary() + : table.string('id').primary() table.timestamp('created_at').defaultTo(knex.fn.now()) table.timestamp('updated_at').defaultTo(knex.fn.now()) columns.forEach((column) => { - const { name, type, references } = column + const { name, type, references, onDelete } = column const tableExist = table[type](name) - references && tableExist.references(references).onDelete('CASCADE') + references && onDelete + ? tableExist.references(references).onDelete('CASCADE') + : tableExist.references(references) }) }) console.log(`table ${tableName} created`) @@ -40,16 +51,32 @@ const tables = [ tableName: 'types', columns: [ { name: 'name', type: 'string' }, - { name: 'parent_id', type: 'integer', references: 'types.id' }, + { + name: 'parent_id', + type: 'string', + references: 'types.id', + onDelete: false, + }, ], }, + { + tableName: 'templates', + columns: [{ name: 'name', type: 'string' }], + }, { tableName: 'codes', columns: [ { name: 'type_id', - type: 'integer', + type: 'string', references: 'types.id', + onDelete: false, + }, + { + name: 'template_id', + type: 'string', + references: 'templates.id', + onDelete: false, }, { name: 'title', @@ -70,7 +97,6 @@ const tables = [ ], }, ] - createTables(tables) module.exports = { knex, diff --git a/PG/model/types.js b/PG/model/types.js index 7b00708..faba2e8 100644 --- a/PG/model/types.js +++ b/PG/model/types.js @@ -6,4 +6,16 @@ class Types extends Model { } } +// async function preTypes() { +// const types = await Types.query() +// if (types.length === 0) { +// await Types.query().insert([ +// { name: 'Cesium' }, +// { name: 'Vue' }, +// { name: 'HTML' }, +// ]) +// } +// } + +// preTypes() module.exports = Types diff --git a/app.js b/app.js index 0e84b93..37bbe11 100644 --- a/app.js +++ b/app.js @@ -7,6 +7,7 @@ var logger = require('morgan') var indexRouter = require('./routes/index') var usersRouter = require('./routes/users') var codesRouter = require('./routes/Codes') +var typesRouter = require('./routes/Types') var app = express() @@ -23,6 +24,7 @@ app.use(express.static(path.join(__dirname, 'public'))) app.use('/', indexRouter) app.use('/users', usersRouter) app.use('/codes', codesRouter) +app.use('/types', typesRouter) // catch 404 and forward to error handler app.use(function (req, res, next) { diff --git a/routes/Codes/controller/index.js b/routes/Codes/controller/index.js new file mode 100644 index 0000000..06a31b9 --- /dev/null +++ b/routes/Codes/controller/index.js @@ -0,0 +1,50 @@ +const { + getCodeListByTypeId, + getCodeById, + addCode: addCodeService, + updateCode: updateCodeService, +} = require('../service') +const { sendResponse } = require('../../../utils') + +const getCodeList = async (req, res) => { + const { typeId } = req.params + const gCodeList = await getCodeListByTypeId(typeId) + + sendResponse({ res, status: 200, message: '', data: gCodeList }) +} + +const getCode = async (req, res) => { + const { id } = req.params + const gCode = await getCodeById(id) + + sendResponse({ res, status: 200, message: '', data: gCode }) +} + +const addCode = async (req, res) => { + const { typeId, code, name, description } = req.body + const aCode = await addCodeService(req.body) + + sendResponse({ res, status: 200, message: '', data: aCode }) +} + +const updateCode = async (req, res) => { + const { id } = req.params + if (!id) { + sendResponse({ res, status: 400, message: 'id is required', data: null }) + return + } + const getCode = await getCodeById(id) + if (getCode) { + const uCode = await updateCodeService(id, req.body) + sendResponse({ res, status: 200, message: '', data: uCode }) + } else { + sendResponse({ res, status: 400, message: 'id not found', data: null }) + } +} + +module.exports = { + getCodeList, + getCode, + addCode, + updateCode, +} diff --git a/routes/Codes/index.js b/routes/Codes/index.js index 3865202..5940c2c 100644 --- a/routes/Codes/index.js +++ b/routes/Codes/index.js @@ -1,15 +1,11 @@ -const { getCodeListByTypeId } = require('./service') -const { sendResponse } = require('../../utils') +const { getCodeList, getCode, addCode } = require('./controller') const express = require('express') const router = express.Router() -router.get('/list/:typeId', async (req, res) => { - const { typeId } = req.params - - const gCodeList = await getCodeListByTypeId(typeId) - - sendResponse({ res, status: 200, message: '', data: gCodeList }) -}) +router.get('/list/:typeId', getCodeList) +router.get('/:id', getCode) +router.post('/', addCode) +router.put('/', addCode) module.exports = router diff --git a/routes/Codes/service/index.js b/routes/Codes/service/index.js index 8838823..6e064bc 100644 --- a/routes/Codes/service/index.js +++ b/routes/Codes/service/index.js @@ -6,6 +6,22 @@ async function getCodeListByTypeId(type_id) { : await Codes.query() } +async function getCodeById(id) { + return await Codes.query().findById(id) +} + +async function addCode(code) { + return await Codes.query().insert(code) +} + +async function updateCode(code) { + const { id } = code + return await Codes.query().patchAndFetchById(id, code) +} + module.exports = { getCodeListByTypeId, + getCodeById, + addCode, + updateCode, } diff --git a/routes/Types/controller/index.js b/routes/Types/controller/index.js new file mode 100644 index 0000000..f4547b5 --- /dev/null +++ b/routes/Types/controller/index.js @@ -0,0 +1,17 @@ +const { sendResponse, arrayToTree } = require('../../../utils/index') +const { getTypes } = require('../service') +async function getTypeTree(req, res) { + const types = await getTypes() + const treeTypes = arrayToTree(types) + + sendResponse({ + res, + status: 200, + message: '', + data: treeTypes, + }) +} + +module.exports = { + getTypeTree, +} diff --git a/routes/Types/index.js b/routes/Types/index.js new file mode 100644 index 0000000..f9fda70 --- /dev/null +++ b/routes/Types/index.js @@ -0,0 +1,8 @@ +const { getTypeTree } = require('./controller') + +const express = require('express') +const router = express.Router() + +router.get('/tree', getTypeTree) + +module.exports = router diff --git a/routes/Types/service/index.js b/routes/Types/service/index.js new file mode 100644 index 0000000..eaddee2 --- /dev/null +++ b/routes/Types/service/index.js @@ -0,0 +1,9 @@ +const Types = require('../../../PG/model/types') + +async function getTypes() { + return await Types.query() +} + +module.exports = { + getTypes, +} diff --git a/utils/index.js b/utils/index.js index 5c7a1cf..33ce965 100644 --- a/utils/index.js +++ b/utils/index.js @@ -8,6 +8,33 @@ function sendResponse({ res, status, message, data }) { res.status(status).send(response) } +function arrayToTree(items) { + const map = new Map() + + // 初始化 Map + items.forEach((item) => { + map.set(item.id, { ...item, children: null }) + }) + + const result = [] + items.forEach((item) => { + if (item.parent_id === null) { + result.push(map.get(item.id)) + } else { + const parent = map.get(item.parent_id) + if (parent) { + parent.children = parent.children + ? [...parent.children, map.get(item.id)] + : [map.get(item.id)] + // parent.children.push(map.get(item.id)) + } + } + }) + + return result +} + module.exports = { sendResponse, + arrayToTree, }