簡體   English   中英

Node.js - controller 和服務中具有相同名稱的函數,最佳實踐?

[英]Node.js - Functions with same name in controller and service, best practice?

我已經使用 Node.js 和 Fastify 幾個星期了,我仍在努力掌握一些最佳實踐。 我來自 PHP/Laravel 背景。

我已經將我的項目分解為路由、控制器和服務(通常是 Laravel 中的模型)。

I've discovered that using the same function name in the controller and service is causing an issue as the code appears to be executing the function in the service file as opposed to the controller.

如果不使用不同的 function 名稱,通常如何處理此問題?

路由器 - /router/transactions.js

function routes(fastify, options, done) {

    fastify.register(require('@fastify/formbody'))
    fastify.register(require('../controllers/transactions'))

    fastify.post('/get-initial-transaction', (req, rep) => {
        getInitialTransaction(req.body, rep)
    })

    done()
}

module.exports = routes

Controller - /controllers/transactions.js

controller = (fastify, options, done) => {

    const service = fastify.register(require('../services/transactions'))

    getInitialTransaction = (req, rep) => {  
        rep.send(getInitialTransaction(req.uuid));
    }

    done()
}

module.exports = controller

服務 - /services/transactions.js

const service = (fastify, options, done) => {

    getInitialTransaction = (uuid) => {
        fastify.mysql.query(
            `SELECT t.amount, t.currency, t.reference, m.name
            FROM transactions t
            JOIN merchants m 
            ON m.id = t.merchant_id
            WHERE t.uuid = :uuid
            AND t.status_desc = "INITIAL"`, { uuid: uuid },
            function onResult(err, result) {
                if (!err) {
                    return (result[0])
                }
                else {
                    return (err)
                }
            }
        )
    }

    done()
}

module.exports = service

這就是我設法解決我的問題的方法。 再說一次,我是 Node.js 和 Fastify 的新手,所以希望能收到有關我使用的設計模式的反饋。

文件結構:

  • 配置
    • 環境.js
    • mysql.js
  • 控制器
    • 交易.js
  • 路線
    • 交易.js
  • 服務
    • 交易.js
  • 服務器.js

路線/transactions.js

function routes(fastify, options, done) {

    fastify.register(require('@fastify/formbody'))
    fastify.register(require('../controllers/transactions'))

    fastify.get('/get-transactions', (req, rep) => {
        getTransactions(req, rep)
    })

    fastify.post('/get-initial-transaction', (req, rep) => {
        getInitialTransaction(req.body, rep)
    })

    fastify.post('/create-transaction', (req, rep) => {
        createTransaction(req, rep)
    })

    done()
}

module.exports = routes

控制器/transactions.js

controller = (fastify, options, done) => {

    const transaction = require('../services/transactions')
    const service = new transaction(fastify)

    getTransactions = async (req, rep) => {
        rep.send(await service.getTransactions())
    }

    getInitialTransaction = async (req, rep) => {
        rep.send(await service.getInitialTransaction(req.uuid));
        await service.updateTransactionStatusDesc(req.uuid, 'OPEN');
    }

    done()
}

module.exports = controller

服務/transactions.js

class transaction {

    constructor(fastify) {
        this.fastify = fastify
    }

    getTransactions = async () => {

        const connection = await this.fastify.mysql.getConnection()
        const [rows, fields] = await connection.query(
            `SELECT * FROM transactions`,
        )
        connection.release()
        return rows
    }

    getInitialTransaction = async (uuid) => {

        const connection = await this.fastify.mysql.getConnection()
        const [rows, fields] = await connection.query(
            `SELECT t.amount, t.currency, t.reference, m.name
            FROM transactions t
            JOIN merchants m 
            ON m.id = t.merchant_id
            WHERE t.uuid = :uuid
            AND t.status_desc = "INITIAL"`, { uuid: uuid }
        )
        connection.release()
        return rows[0]
    }

    updateTransactionStatusDesc = async (uuid, statusDesc) => {

        const connection = await this.fastify.mysql.getConnection()
        const [rows, fields] = await connection.query(
            `UPDATE transactions SET status_desc = :statusDesc WHERE uuid = :uuid`, { uuid: uuid, statusDesc: statusDesc },
        )
        connection.release()
        console.log(rows)
        // return rows[0]
    }
}

module.exports = transaction

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM