nodejs中用于生产和开发的不同数据库

Different database for production and development in nodejs

我知道 Rails 上的 Ruby 有这个功能,并且在 railstutorial 中它特别鼓励它。但是,我在nodejs中没有发现这样的东西。如果我想在我的机器上 运行 Sqlite3 以便我可以轻松使用数据库访问,但是在 Heroku 上生产 postgres,我将如何在 Nodejs 中执行此操作?我找不到任何关于它的教程。

谢谢!

编辑: 我打算包括 Node.JS + Express。

您可以使用 jugglingdb 之类的东西来执行此操作:

JugglingDB(3) is cross-db ORM for nodejs, providing common interface to access most popular database formats. Currently supported are: mysql, sqlite3, postgres, couchdb, mongodb, redis, neo4j and js-memory-storage (yep, self-written engine for test-usage only). You can add your favorite database adapter, checkout one of the existing adapters to learn how, it's super-easy, I guarantee.

Jugglingdb also works on client-side (using WebService and Memory adapters), which allows to write rich client-side apps talking to server using JSON API.

我个人没有使用过它,但是有一个通用的 API 来访问您的所有数据库实例将使在本地使用一个和在生产中使用一个变得非常简单 - 您可以连接一些位置检测而无需也太麻烦了,它会自动 select 目标数据库,具体取决于它所在的环境。

要在生产和开发 Db 之间切换,您可以使用不同的端口 运行在本地和 Heroku 上连接您的应用程序。 作为 Heroku,默认情况下 运行 将应用程序连接到端口 80,而在本地 运行 连接您的应用程序时,您还有其他端口。

这将帮助您在 运行 时间内确定您的应用程序是 运行 在本地还是在生产中,您可以相应地切换数据库。

当然有可能,但请注意,这可能不是一个好主意:http://12factor.net/dev-prod-parity

如果您不想经历在本地设置 postgres 的麻烦,您可以在 Heroku 上使用免费的 postgres 计划并从本地计算机连接到它:

DATABASE_URL=url node server.j

一个 .env 文件可以使这更容易: