|
| 1 | +package com.arangodb.graphql.create; |
| 2 | + |
| 3 | +import com.arangodb.ArangoDB; |
| 4 | +import com.arangodb.ArangoDatabase; |
| 5 | +import com.arangodb.entity.CollectionType; |
| 6 | +import com.arangodb.graphql.schema.ArangoEdgeDirective; |
| 7 | +import com.arangodb.graphql.schema.ArangoVertexDirective; |
| 8 | +import com.arangodb.model.CollectionCreateOptions; |
| 9 | +import com.arangodb.model.HashIndexOptions; |
| 10 | +import graphql.schema.GraphQLArgument; |
| 11 | +import graphql.schema.GraphQLObjectType; |
| 12 | +import graphql.schema.GraphQLSchema; |
| 13 | +import graphql.schema.GraphQLType; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import java.util.*; |
| 18 | + |
| 19 | +public class DatabaseObjectCreator { |
| 20 | + |
| 21 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 22 | + private final ArangoDB arango; |
| 23 | + private final String databaseName; |
| 24 | + private final GraphQLSchema graphQLSchema; |
| 25 | + |
| 26 | + public DatabaseObjectCreator(ArangoDB arango, String databaseName, GraphQLSchema graphQLSchema) { |
| 27 | + this.arango = arango; |
| 28 | + this.databaseName = databaseName; |
| 29 | + this.graphQLSchema = graphQLSchema; |
| 30 | + } |
| 31 | + |
| 32 | + private List<TypeCollectionMapping> createTypeMappings(){ |
| 33 | + |
| 34 | + List<GraphQLType> types = graphQLSchema.getAllTypesAsList(); |
| 35 | + |
| 36 | + List<TypeCollectionMapping> mappings = new ArrayList<>(); |
| 37 | + types.forEach(type -> { |
| 38 | + if(type instanceof GraphQLObjectType){ |
| 39 | + GraphQLObjectType objectType = (GraphQLObjectType) type; |
| 40 | + ArangoVertexDirective vertexDirective = new ArangoVertexDirective(objectType); |
| 41 | + String collection = vertexDirective.getCollection(); |
| 42 | + if(collection != null){ |
| 43 | + TypeCollectionMapping mapping = new TypeCollectionMapping(collection); |
| 44 | + |
| 45 | + objectType.getFieldDefinitions().forEach(graphQLFieldDefinition -> { |
| 46 | + ArangoEdgeDirective arangoEdgeDirective = new ArangoEdgeDirective(graphQLFieldDefinition); |
| 47 | + String edgeCollectionName = arangoEdgeDirective.getCollection(); |
| 48 | + if(edgeCollectionName != null){ |
| 49 | + mapping.addEdgeCollection(edgeCollectionName); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + mappings.add(mapping); |
| 54 | + |
| 55 | + } |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + return mappings; |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + public void createDatabase(){ |
| 64 | + ArangoDatabase db = arango.db(databaseName); |
| 65 | + if(!db.exists()){ |
| 66 | + logger.info("Auto Create Database {}", databaseName); |
| 67 | + db.create(); |
| 68 | + } |
| 69 | + else{ |
| 70 | + logger.info("Database {} already exists", databaseName); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public void createCollections(){ |
| 75 | + |
| 76 | + List<TypeCollectionMapping> mappings = createTypeMappings(); |
| 77 | + mappings.forEach(mapping -> { |
| 78 | + String vertex = mapping.getVertexCollection(); |
| 79 | + createCollection(vertex, CollectionType.DOCUMENT); |
| 80 | + Set<String> edgeCollections = mapping.getEdgeCollections(); |
| 81 | + edgeCollections.forEach(edgeCollection -> { |
| 82 | + createCollection(edgeCollection, CollectionType.EDGES); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + public void createIndexes(){ |
| 89 | + |
| 90 | + ArangoDatabase database = arango.db(databaseName); |
| 91 | + GraphQLObjectType queryType = graphQLSchema.getQueryType(); |
| 92 | + queryType.getFieldDefinitions().forEach(graphQLFieldDefinition -> { |
| 93 | + ArgumentIndexMapping mapping = new ArgumentIndexMapping(graphQLFieldDefinition); |
| 94 | + |
| 95 | + String collection = mapping.getCollection(); |
| 96 | + if(collection != null){ |
| 97 | + logger.info("Auto Create Index on collection {} for fields {}", collection, mapping.getFields()); |
| 98 | + database.collection(collection).ensureHashIndex(mapping.getFields(), new HashIndexOptions()); |
| 99 | + } |
| 100 | + |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + protected CollectionCreateOptions createCollectionCreateOptions(CollectionType collectionType){ |
| 105 | + return new CollectionCreateOptions().type(collectionType); |
| 106 | + } |
| 107 | + |
| 108 | + private void createCollection(String name, CollectionType collectionType) { |
| 109 | + |
| 110 | + CollectionCreateOptions collectionCreateOptions = createCollectionCreateOptions(collectionType); |
| 111 | + ArangoDatabase database = arango.db(databaseName); |
| 112 | + |
| 113 | + if(!database.collection(name).exists()){ |
| 114 | + logger.info("Auto Create Collection {}:{}", name, collectionType); |
| 115 | + database.collection(name).create(collectionCreateOptions); |
| 116 | + } |
| 117 | + else{ |
| 118 | + logger.info("Collection already exists {}:{}", name, collectionType); |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments