Strapi database.js/多个数据库配置 [英] strapi database.js / multiple database configs

查看:61
本文介绍了Strapi database.js/多个数据库配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 如果我在 database.js 中运行 sqlite,Strapi 可以在本地工作,但如果我在运行 postgres 则不行/我在网上发现我可以指定 npm run develop 使用 sqlite,而生产应该使用 postgres.

对于 REF - 我在这里找到了这个答案:

我想象发生的事情是因为本地的 Strapi 是使用 sqlite 构建的,而 heroku 要求我使用 postgres 作为数据库,数据库不一样所以数据没有被正确读取?

(我可能错了....)

在这种情况下:您如何将您的本地开发人员(推荐的快速启动设置)转移到您的生产站点?

  • 你 npm 运行构建吗?然后在您的普通前端站点上托管 Strapi?

  • 有没有办法将数据从一个数据库移动到另一个数据库?

很抱歉在这里问了很多小问题 -我可能错过了一个将它们联系在一起的概念.

在此先感谢您的帮助,沃利

解决方案

我发现了一个非常有用的 Youtube 视频,它实际上解释了步骤 1) 的这个过程

如何运行不同的数据库取决于它是被开发者还是生产者使用.

简短的答案是在 database.js 文件中编写 if 语句以查看您使用的是开发者还是生产者:

一旦我实现了这个,我会写一个完整的答案:)

视频链接:https://www.youtube.com/watch?v=xNE0TrI5OKk


第 2 部分:您能否将您在 Strapi 中所做的工作迁移到生产数据库,例如 Heroku ......?

可悲的是,简单的答案令人沮丧(并且令人沮丧)-不!

这是来自 Strapis 网站:

Strapi 是否处理内容的部署或迁移?Strapi 目前不提供任何工具来在不同环境(即从开发到生产)之间迁移或部署您的数据更改.除了内容管理器设置之外,要了解有关此选项的更多信息,请参阅以下 CLI 文档.

在这里找到:https://strapi.io/documentation/v3.x/getting-started/troubleshooting.html#frequently-asked-questions


我还在 Strapi Slack 页面上与一位非常有帮助的代表交谈,以找出我做错的 WTF(结果是分配.....).

无论如何:

  • 在本地创建您的内容类型
  • 将它们推送到您的在线 Strapi (Heroku)
  • 在线添加所有内容,而不是本地.....

一线希望?

我还没有这样做,但显然您可以使用以下方法手动传输数据并将其从 sqlite 转换为 postgres:

您必须使用一些本地数据库客户端(DBeaver 支持 SQLite)进行数据转储,然后将该数据导入 PG 服务器(您也可以在那里使用 DBeaver)Strapi 目前没有任何工具或建议用于环境之间的内容迁移.

好吧,我希望这能帮助像我一样遇到这个问题的其他人......

沃利:)

  1. Strapi works locally if im running sqlite in the database.js but not if I'm running postgres / I've found online that I can specify that npm run develop uses sqlite, and production should use postgres.

For REF - I found this answer here: https://github.com/strapi/strapi/discussions/6832

Can anyone show me how to set this up as I am really finding it hard to read the docus for this issue.

Currently in the file structure:

config/database.js

I have these two set ups (for local and heroku) - I comment out the postgres set up for heroku to work locally

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'sqlite',
        filename: env('DATABASE_FILENAME', '.tmp/data.db'),
      },
      options: {
        useNullAsDefault: true,
      },
    },
  },
});


module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'postgres',
        host: env('DATABASE_HOST', '127.0.0.1'),
        port: env.int('DATABASE_PORT', 27017),
        database: env('DATABASE_NAME', 'strapi'),
        username: env('DATABASE_USERNAME', ''),
        password: env('DATABASE_PASSWORD', ''),
      },
      options: {
        ssl: false,
      },
    },
  },
});


  1. When I pushed my strapi project to Heroku the correct structure is there i.e. names of articles (SHOWN IN THE BELOW IMAGE) - but none of the content is there: i.e. posts, images etc

What I imagine is happening is because the local strapi has been built using sqlite and heroku required me to use postgres as a database, the databases are not the same so the data is not being read correctly?

(I could be wrong about that....)

In this case: how do you move your local dev (recommended quick start set up) to your production site?

  • Do you npm run build? and then host strapi on your normal front end site?

  • is there a way to move the data from one database to another?

Sorry for asking many small questions here - I might have missed a concept which ties it all together.

Thanks in advance for any help, Wally

解决方案

I found a very useful Youtube video that actually explains this process for step 1)

How to run a different database depending if it is being used by dev or production.

Short answer is in the database.js file you write an if statement to see if your using dev or production:

Once I have implemented this I will write a full answer :)

VIDEO LINK: https://www.youtube.com/watch?v=xNE0TrI5OKk


PART 2: Can you migrate the work you have done in strapi to a production database, for example Heroku....?

Simple answer is sadly (and hella frustrating) - NO!

This is from Strapis site:

Does Strapi handle deploying or migrating of content? Strapi does not currently provide any tools for migrating or deploying your data changes between different environments (ie. from development to production). With the exception being the Content-Manager settings, to read more about this option please see the following CLI documentation.

FOUND HERE: https://strapi.io/documentation/v3.x/getting-started/troubleshooting.html#frequently-asked-questions


I've also been speaking with a really helpful rep on the Strapi Slack page to figure out WTF I've been doing wrong (turns out allot.....).

Anyway:

  • Create your content types in your local strapi
  • Push them to your online strapi (Heroku)
  • Add all content online NOT locally.....

SMALL RAY OF HOPE?

I have not done this yet but apparently you can manually transfer the data and convert it from sqlite to postgres by using:

You will have to do a data dump using some local DB client (DBeaver supports SQLite) then import that data onto the PG server (you can also use DBeaver there) Strapi doesn't have any tooling or suggestions for content migration between environments currently.

Well I hope this helps anyone else who comes across this issue like me....

Wally :)

这篇关于Strapi database.js/多个数据库配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆