2021-01-03 01:21:15 -06:00
|
|
|
package dev.plex.storage;
|
2020-10-27 11:14:34 -07:00
|
|
|
|
2020-10-27 23:47:02 -05:00
|
|
|
import com.mongodb.client.MongoClient;
|
|
|
|
import com.mongodb.client.MongoClients;
|
2020-10-27 22:49:56 -05:00
|
|
|
import dev.morphia.Datastore;
|
|
|
|
import dev.morphia.Morphia;
|
2020-10-27 23:47:02 -05:00
|
|
|
import dev.morphia.mapping.MapperOptions;
|
2021-01-03 01:21:15 -06:00
|
|
|
import dev.plex.Plex;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
2020-10-27 11:14:34 -07:00
|
|
|
|
|
|
|
public class MongoConnection
|
|
|
|
{
|
|
|
|
// USE MORPHIA API FOR MONGO <3
|
|
|
|
|
2020-10-27 22:49:56 -05:00
|
|
|
private final Plex plugin = Plex.get();
|
2020-10-27 11:14:34 -07:00
|
|
|
|
|
|
|
public Datastore getDatastore()
|
|
|
|
{
|
2020-10-27 21:11:23 -07:00
|
|
|
if (!plugin.config.getString("data.central.storage").equalsIgnoreCase("mongodb"))
|
2020-10-27 11:14:34 -07:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-27 21:11:23 -07:00
|
|
|
String host = plugin.config.getString("data.central.hostname");
|
|
|
|
int port = plugin.config.getInt("data.central.port");
|
|
|
|
String username = plugin.config.getString("data.central.user");
|
|
|
|
String password = plugin.config.getString("data.central.password");
|
|
|
|
String database = plugin.config.getString("data.central.db");
|
2020-10-27 11:14:34 -07:00
|
|
|
|
2020-10-27 22:49:56 -05:00
|
|
|
String connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database;
|
2020-10-27 23:47:02 -05:00
|
|
|
MongoClient client = MongoClients.create(connectionString);
|
|
|
|
Datastore datastore = Morphia.createDatastore(client, database, MapperOptions.DEFAULT);
|
|
|
|
datastore.getMapper().map(PlexPlayer.class);
|
2020-10-27 11:14:34 -07:00
|
|
|
datastore.ensureIndexes();
|
2021-01-03 01:21:15 -06:00
|
|
|
plugin.setStorageType(StorageType.MONGODB);
|
2020-10-27 11:14:34 -07:00
|
|
|
return datastore;
|
|
|
|
}
|
|
|
|
}
|