private DatastoreImpl(final Morphia morphia, final Mapper mapper, final MongoClient mongoClient, final MongoDatabase database) { this.morphia = morphia; this.mapper = mapper; this.mongoClient = mongoClient; this.database = database.withCodecRegistry(CodecRegistries.fromRegistries( mongoClient.getMongoClientOptions().getCodecRegistry(), MongoClientSettings.getDefaultCodecRegistry())); this.db = mongoClient.getDB(database.getName()); this.defConcern = mongoClient.getWriteConcern(); this.indexHelper = new IndexHelper(mapper, database); }
@Bean MongoDatabase mongoDatabase(MongoClient mongoClient, ConnectionString mongoConnectionString, CodecRegistry codecRegistry) { return mongoClient .getDatabase(mongoConnectionString.getDatabase()) .withCodecRegistry(codecRegistry); }
/** * Configure the {@link MongoDatabase}, registering the additional codecs if configured. * @param database The database to configure * @return The configured database */ protected MongoDatabase checkAdditionalCodecs(MongoDatabase database) { return getAdditionalCodecRegistry() .map(r -> database.withCodecRegistry(CodecRegistries.fromRegistries(database.getCodecRegistry(), r))) .orElse(database); }
@NotNull @Override public MongoDatabase withCodecRegistry(@NotNull CodecRegistry codecRegistry) { return new MongoTestDatabase(db.withCodecRegistry(codecRegistry), beforeQueryException, beforeUpdateException, afterUpdateException); }
/** * Retrieves the database with the specified {@code name} on the {@code client}. * The database will be created if it does not already exist. * * @param client the MongoDB client, connected on the server * @param name the name of the database * * @return the database */ @Nonnull private MongoDatabase createDatabase(MongoClient client, String name) { final CodecRegistry registry = fromRegistries( client.getMongoClientOptions().getCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build())); return client.getDatabase(name) .withCodecRegistry(registry) .withWriteConcern(WriteConcern.MAJORITY) .withReadConcern(ReadConcern.MAJORITY); } }