【问题标题】:How to run PostgreSQL server with Firebase Functions?如何使用 Firebase 函数运行 PostgreSQL 服务器?
【发布时间】:2021-10-31 15:13:13
【问题描述】:

我正在尝试使用 Firebase 函数运行 PostgreSQL 服务器。 $ npm run build 运行但 $ firebase serve --only functions 不起作用发送错误:

import {
>    ^^^^^^
>     
>    SyntaxError: Cannot use import statement outside a module

(实体文件出错)

我正在使用

  • 打字稿
  • Firebase 函数
  • PostgreSQL

并且这是我的代码:

  • index.ts

    import * as functions from 'firebase-functions'
    import { router } from './routers'
    import Koa from 'koa'
    import { createConnection } from 'typeorm'
    
    
    export const app = new Koa()
    const PORT = Number(process.env.PORT) || 8080
    
    app.proxy = true
    app.use(router.routes()).use(router.allowedMethods())
    
    const dbServer = async () => {
       try {
           await createConnection()
           app.listen(PORT, () => {
           console.log(`Listening to port ${PORT}`)
         })
       } catch (error) {
         console.log(error)
       }
     }
    
    dbServer()
    
    export const api = functions
       .region('asia-northeast3')
       .https.onRequest(app.callback())
     ```
    
    
  • entity/Channel.ts

    import {
        BaseEntity,
        Entity,
        PrimaryGeneratedColumn
    } from 'typeorm'
    
    @Entity('Channel')
    export class Channel extends BaseEntity {
        @PrimaryGeneratedColumn()
        id: number
    }
    

我该如何解决这个问题?

提前谢谢你!!!

【问题讨论】:

    标签: typescript postgresql firebase google-cloud-functions


    【解决方案1】:

    Google Cloud Functions 专为运行相对短暂的进程而设计。单个 Cloud Function 可以处于活动状态的最长时间为 9 分钟,之后其容器将关闭代码。这意味着 Cloud Functions 不能合理地用于运行您自己的数据库服务器或其他长期存在的进程。

    考虑使用托管数据库服务,例如 Cloud SQL。

    【讨论】:

    • 非常感谢!!!我也想知道我是否可以在云功能之上运行数据库服务器哈哈......所以我需要像云 sql 这样的单独服务来运行数据库服务器!非常感谢
    猜你喜欢
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多