简体   繁体   English

为不同的环境配置带有rails的Neo4j

[英]Configuring Neo4j with rails for different environments

I have configured Neo4j in development environment using neo4j core gem. 我使用neo4j核心gem在开发环境中配置了Neo4j。

To create a new connection, i have used the below code in application.rb 要创建新连接,我在application.rb中使用了以下代码

Application.rb application.rb中

neo4j_adaptor = Neo4j::Core::CypherSession::Adaptors::HTTP.new('http://localhost:7474')
neo4j_session = Neo4j::Core::CypherSession.new(neo4j_adaptor)

How to configure the neo4j so that development environment and test environment uses different database. 如何配置neo4j使开发环境和测试环境使用不同的数据库。

eg: for development sample_development and for running rspec sample_test. 例如:用于开发sample_development和用于运行rspec sample_test。

I don't know how much experience you have with Rails. 我不知道你对Rails有多少经验。 But in rails you mainly have 3 environments be default: 但是在rails中你主要有3个环境是默认的:

1- development 1-发展

2- test 2-测试

3- production 3-生产

You can have different configs for different environments as shown in this SO question: 您可以针对不同的环境使用不同的配置,如此SO问题所示:

Best way to create custom config options for my Rails app? 为我的Rails应用程序创建自定义配置选项的最佳方法?

Last thing, I would not recommend to use the Neo4j adapter directly unless you are really experienced and need direct access to it because of business requirements. 最后,我不建议直接使用Neo4j适配器,除非您真的有经验并且由于业务需求需要直接访问它。

I'd recommend to use Neo4jrb wrapper around the core adapter as shown here: 我建议在核心适配器周围使用Neo4jrb包装器,如下所示:

https://neo4jrb.readthedocs.io/en/9.2.x/Setup.html#generating-a-new-app https://neo4jrb.readthedocs.io/en/9.2.x/Setup.html#generating-a-new-app


UPDATE UPDATE

Create a file called neo4j.yml inside config directory in your project with following code: 使用以下代码在项目的config目录中创建一个名为neo4j.yml的文件:

development:
  neo4j_api: http://localhost:7474

test:
  neo4j_api: http://something_else:7474

production:
  neo4j_api: http://maybe_something_else:7474

then create an initializer in your project lets call it neo4j.rb so its path should be: config/initializers/neo4j.rb . 然后在你的项目中创建一个initializer ,让我们称之为neo4j.rb因此它的路径应该是: config/initializers/neo4j.rb

Inside this initializer put the following code: 在这个初始化程序中放入以下代码:

NEO4J_CONFIG = YAML.load_file(Rails.root.join('config/neo4j.yml'))[Rails.env]

Then you will have the NEO4J_CONFIG accessible at any part of your rails application like: 然后,您可以在rails应用程序的任何部分访问NEO4J_CONFIG ,例如:

NEO4J_CONFIG['neo4j_api']

and your code should look like: 并且您的代码应如下所示:

neo4j_adaptor = Neo4j::Core::CypherSession::Adaptors::HTTP.new(NEO4J_CONFIG['neo4j_api'])
neo4j_session = Neo4j::Core::CypherSession.new(neo4j_adaptor)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM