Neo4j.Rb Documentation Release 8.0.18
Total Page:16
File Type:pdf, Size:1020Kb
Neo4j.rb Documentation Release 8.0.18 Chris Grigg, Brian Underwood Sep 18, 2017 Contents 1 Introduction 3 1.1 Terminology...............................................3 1.1.1 Neo4j..............................................3 1.1.2 Neo4j.rb.............................................4 1.2 Code Examples..............................................4 1.2.1 ActiveNode...........................................4 1.3 Setup...................................................4 2 Setup 5 2.1 Ruby on Rails..............................................5 2.1.1 Generating a new app......................................5 2.1.2 Adding the gem to an existing project.............................6 2.1.3 Rails configuration.......................................6 2.1.4 Configuring Faraday......................................7 2.2 Any Ruby Project............................................8 2.2.1 Connection...........................................8 2.3 Heroku..................................................9 3 Upgrade Guide 11 3.1 What has changed............................................ 11 3.2 The neo4j-core gem......................................... 11 3.2.1 The new API.......................................... 12 3.3 The neo4j gem............................................. 13 3.3.1 Sessions............................................. 13 3.3.2 server_db............................................ 13 3.3.3 Outside of Rails......................................... 14 3.3.4 Indexes and Constraints..................................... 14 3.3.5 Migrations............................................ 15 3.3.6 neo_id id_properties...................................... 15 3.3.7 Exceptions........................................... 15 4 Rake Tasks 17 5 ActiveNode 19 5.1 Properties................................................. 19 5.1.1 Labels.............................................. 20 5.1.2 Indexes............................................. 20 i 5.1.3 Constraints........................................... 20 5.1.4 Labels.............................................. 20 5.1.5 Serialization........................................... 21 5.1.6 Enums.............................................. 21 5.2 Scopes.................................................. 23 5.3 Wrapping................................................. 24 5.4 Callbacks................................................. 24 5.5 created_at, updated_at.......................................... 25 5.6 Validation................................................. 25 5.7 id property (primary key)........................................ 25 5.8 Associations............................................... 25 5.8.1 Dependent Associations.................................... 26 5.8.2 Creating Unique Relationships................................. 26 5.8.3 Eager Loading......................................... 27 6 ActiveRel 29 6.1 When to Use?.............................................. 29 6.2 Setup................................................... 29 6.3 Relationship Creation.......................................... 30 6.3.1 From an ActiveRel Model................................... 30 6.3.2 From a has_many or has_one association........................... 30 6.3.3 Creating Unique Relationships................................. 31 6.4 Query and Loading existing relationships................................ 31 6.4.1 :each_rel, :each_with_rel, or :pluck methods.......................... 31 6.5 Accessing related nodes......................................... 31 6.6 Advanced Usage............................................. 32 6.6.1 Separation of Relationship Logic................................ 32 6.7 Additional methods........................................... 33 6.8 Regarding: from and to......................................... 33 7 Properties 35 7.1 Undeclared Properties.......................................... 36 7.1.1 Types and Conversion...................................... 36 7.1.2 Custom Converters....................................... 37 8 Unique IDs 39 8.1 Defining your own ID.......................................... 39 8.2 Using internal Neo4j IDs as id_property................................. 40 8.3 A note regarding constraints....................................... 40 8.4 Adding IDs to Existing Data....................................... 40 8.5 Working with Legacy Schemas..................................... 40 9 Querying 43 9.1 Simple Query Methods.......................................... 43 9.1.1 .find ............................................. 43 9.1.2 .find_by ........................................... 43 9.2 Scope Method Chaining......................................... 43 9.2.1 Querying the scope....................................... 44 9.2.2 Chaining associations...................................... 44 9.2.3 Branching............................................ 44 9.2.4 Associations and Unpersisted Nodes.............................. 45 9.2.5 Parameters........................................... 45 9.2.6 Variable-length relationships.................................. 46 9.3 The Query API.............................................. 47 9.4 #proxy_as................................................ 48 ii 9.5 match_to and first_rel_to ................................... 48 9.6 Finding in Batches............................................ 48 9.7 Orm_Adapter............................................... 48 9.8 Find or Create By............................................... 49 10 QueryClauseMethods 51 10.1 Neo4j::Core::Query........................................... 51 10.1.1 #match............................................. 51 10.1.2 #optional_match........................................ 54 10.1.3 #using.............................................. 54 10.1.4 #where............................................. 55 10.1.5 #where_not........................................... 59 10.1.6 #match_nodes.......................................... 61 10.1.7 #unwind............................................. 62 10.1.8 #return.............................................. 63 10.1.9 #order.............................................. 64 10.1.10 #limit.............................................. 66 10.1.11 #skip.............................................. 67 10.1.12 #with.............................................. 68 10.1.13 #create.............................................. 69 10.1.14 #create_unique......................................... 71 10.1.15 #merge............................................. 72 10.1.16 #delete.............................................. 73 10.1.17 #set_props............................................ 74 10.1.18 #set............................................... 75 10.1.19 #on_create_set......................................... 76 10.1.20 #on_match_set......................................... 77 10.1.21 #remove............................................. 78 10.1.22 #start.............................................. 79 10.1.23 clause combinations....................................... 80 11 Configuration 85 11.1 In Rails.................................................. 85 11.2 Other Ruby apps............................................. 85 11.2.1 Variables............................................ 85 12 Migrations 87 12.1 Generators................................................ 87 12.2 Transactions............................................... 88 12.3 Tasks................................................... 88 12.3.1 neo4j:migrate:all........................................ 88 12.3.2 neo4j:migrate.......................................... 88 12.3.3 neo4j:migrate:up........................................ 88 12.3.4 neo4j:migrate:down....................................... 88 12.3.5 neo4j:migrate:status....................................... 89 12.3.6 neo4j:rollback.......................................... 89 12.4 Integrate Neo4j.rb with ActiveRecord migrations............................ 89 12.5 Migration Helpers............................................ 89 12.5.1 #execute............................................. 89 12.5.2 #query.............................................. 89 12.5.3 #remove_property........................................ 89 12.5.4 #rename_property........................................ 90 12.5.5 #drop_nodes.......................................... 90 12.5.6 #add_label............................................ 90 iii 12.5.7 #add_labels........................................... 90 12.5.8 #remove_label......................................... 90 12.5.9 #remove_labels......................................... 90 12.5.10 #rename_label.......................................... 90 12.5.11 #add_constraint......................................... 91 12.5.12 #drop_constraint........................................ 91 12.5.13 #add_index........................................... 91 12.5.14 #drop_index........................................... 91 12.5.15 #say............................................... 91 12.5.16 #say_with_time......................................... 92 12.5.17 #populate_id_property..................................... 92 12.5.18 #relabel_relation........................................ 92 12.5.19 #change_relations_style.................................... 93 13 Testing 95 13.1 How to clear the database........................................ 95 13.1.1 Cypher............................................. 95 13.1.2 Delete data files........................................