-
Notifications
You must be signed in to change notification settings - Fork 3
2. Get Started
Sammwy edited this page Mar 16, 2023
·
5 revisions
import com.sammwy.milkshake.Entity;
import com.sammwy.classserializer.annotations.Prop;
public class User extends Entity {
@Prop
public String name;
@Prop
public int age;
}
or
import com.sammwy.milkshake.Entity;
import com.sammwy.classserializer.annotations.Serializable;
@Serializable
public class User extends Entity {
public String name;
public int age;
}
Provider provider = Milkshake.connect("mongodb://localhost/database_name");
Repository<User> users = Milkshake.addRepository(User.class, provider, "MongoCollection");
User user = new User();
user.name = "Sammwy";
user.age = 20;
user.save();
// Find by Key
User user = users.findOne(new FindFilter("name", "Sammwy"));
// Find by Mongo ID
User user = users.findByID("<mongo_id>");
// Find many entries
List<User> users = users.findMany(new FindFilter("age", 20));
// Find all entries
List<User> users = users.findMany(new FindFilter());
// Edit using entity
User user = users.findOne(new FindFilter("name", "Sammwy"));
user.name = "cookie";
user.save();
// Edit using operations
users.updateOne(
new FindFilter("name", "Sammwy"),
new Operation().set("name", "cookie")
);
// Delete using entity
User user = users.findOne(new FindFilter("name", "sammwy"));
user.delete();
// Delete using find filter
users.deleteOne(new FindFilter("name", "sammwy"));
If we have an entity obtained from a repository we can refresh its values using the refresh method.
User user = ...
user.refresh();