Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add delete bulk import users api #197

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ static int update(Start start, String QUERY, PreparedStatementValueSetter setter
}
}

static <T> T update(Start start, String QUERY, PreparedStatementValueSetter setter, ResultSetValueExtractor<T> mapper)
throws SQLException, StorageQueryException {
try (Connection con = ConnectionPool.getConnection(start)) {
try (PreparedStatement pst = con.prepareStatement(QUERY)) {
setter.setValues(pst);
try (ResultSet result = pst.executeQuery()) {
return mapper.extract(result);
}
}
}
}

static int update(Connection con, String QUERY, PreparedStatementValueSetter setter)
throws SQLException, StorageQueryException {
try (PreparedStatement pst = con.prepareStatement(QUERY)) {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -3064,7 +3064,7 @@ public void addBulkImportUsers(AppIdentifier appIdentifier, List<BulkImportUser>
}

@Override
public List<BulkImportUser> getBulkImportUsers(AppIdentifier appIdentifier, @Nonnull Integer limit, @Nullable BulkImportUserStatus status,
public List<BulkImportUser> getBulkImportUsers(AppIdentifier appIdentifier, @Nonnull Integer limit, @Nullable BULK_IMPORT_USER_STATUS status,
@Nullable String bulkImportUserId, @Nullable Long createdAt) throws StorageQueryException {
try {
return BulkImportQueries.getBulkImportUsers(this, appIdentifier, limit, status, bulkImportUserId, createdAt);
Expand All @@ -3074,7 +3074,7 @@ public List<BulkImportUser> getBulkImportUsers(AppIdentifier appIdentifier, @Non
}

@Override
public void updateBulkImportUserStatus_Transaction(AppIdentifier appIdentifier, TransactionConnection con, @Nonnull String[] bulkImportUserIds, @Nonnull BulkImportUserStatus status)
public void updateBulkImportUserStatus_Transaction(AppIdentifier appIdentifier, TransactionConnection con, @Nonnull String[] bulkImportUserIds, @Nonnull BULK_IMPORT_USER_STATUS status)
throws StorageQueryException {
Connection sqlCon = (Connection) con.getConnection();
try {
Expand All @@ -3083,4 +3083,13 @@ public void updateBulkImportUserStatus_Transaction(AppIdentifier appIdentifier,
throw new StorageQueryException(e);
}
}

@Override
public List<String> deleteBulkImportUsers(AppIdentifier appIdentifier, @Nonnull String[] bulkImportUserIds) throws StorageQueryException {
try {
return BulkImportQueries.deleteBulkImportUsers(this, appIdentifier, bulkImportUserIds);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import javax.annotation.Nullable;

import io.supertokens.pluginInterface.RowMapper;
import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BulkImportUserStatus;
import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BULK_IMPORT_USER_STATUS;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
Expand Down Expand Up @@ -87,12 +87,12 @@ public static void insertBulkImportUsers(Start start, AppIdentifier appIdentifie
for (BulkImportUser user : users) {
pst.setString(parameterIndex++, user.id);
pst.setString(parameterIndex++, appIdentifier.getAppId());
pst.setString(parameterIndex++, user.toString());
pst.setString(parameterIndex++, user.toRawDataForDbStorage());
}
});
}

public static void updateBulkImportUserStatus_Transaction(Start start, Connection con, AppIdentifier appIdentifier, @Nonnull String[] bulkImportUserIds, @Nonnull BulkImportUserStatus status)
public static void updateBulkImportUserStatus_Transaction(Start start, Connection con, AppIdentifier appIdentifier, @Nonnull String[] bulkImportUserIds, @Nonnull BULK_IMPORT_USER_STATUS status)
throws SQLException, StorageQueryException {
if (bulkImportUserIds.length == 0) {
return;
Expand Down Expand Up @@ -125,7 +125,7 @@ public static void updateBulkImportUserStatus_Transaction(Start start, Connectio
});
}

public static List<BulkImportUser> getBulkImportUsers(Start start, AppIdentifier appIdentifier, @Nonnull Integer limit, @Nullable BulkImportUserStatus status,
public static List<BulkImportUser> getBulkImportUsers(Start start, AppIdentifier appIdentifier, @Nonnull Integer limit, @Nullable BULK_IMPORT_USER_STATUS status,
@Nullable String bulkImportUserId, @Nullable Long createdAt)
throws SQLException, StorageQueryException {

Expand Down Expand Up @@ -168,6 +168,43 @@ public static List<BulkImportUser> getBulkImportUsers(Start start, AppIdentifier
});
}

public static List<String> deleteBulkImportUsers(Start start, AppIdentifier appIdentifier, @Nonnull String[] bulkImportUserIds) throws SQLException, StorageQueryException {
if (bulkImportUserIds.length == 0) {
return new ArrayList<>();
}

String baseQuery = "DELETE FROM " + Config.getConfig(start).getBulkImportUsersTable();
StringBuilder queryBuilder = new StringBuilder(baseQuery);

List<Object> parameters = new ArrayList<>();

queryBuilder.append(" WHERE app_id = ?");
parameters.add(appIdentifier.getAppId());

queryBuilder.append(" AND id IN (");
for (int i = 0; i < bulkImportUserIds.length; i++) {
if (i != 0) {
queryBuilder.append(", ");
}
queryBuilder.append("?");
parameters.add(bulkImportUserIds[i]);
}
queryBuilder.append(") RETURNING id");

String query = queryBuilder.toString();

return update(start, query, pst -> {
for (int i = 0; i < parameters.size(); i++) {
pst.setObject(i + 1, parameters.get(i));
}
}, result -> {
List<String> deletedIds = new ArrayList<>();
while (result.next()) {
deletedIds.add(result.getString("id"));
}
return deletedIds;
});
}
private static class BulkImportUserRowMapper implements RowMapper<BulkImportUser, ResultSet> {
private static final BulkImportUserRowMapper INSTANCE = new BulkImportUserRowMapper();

Expand All @@ -180,8 +217,8 @@ private static BulkImportUserRowMapper getInstance() {

@Override
public BulkImportUser map(ResultSet result) throws Exception {
return BulkImportUser.fromDBJson(result.getString("id"), result.getString("raw_data"),
BulkImportUserStatus.valueOf(result.getString("status")),
return BulkImportUser.fromRawDataFromDbStorage(result.getString("id"), result.getString("raw_data"),
BULK_IMPORT_USER_STATUS.valueOf(result.getString("status")),
result.getLong("created_at"), result.getLong("updated_at"));
}
}
Expand Down
Loading