You're not going to find any single rule for this. It's purely a matter of style and preference.
The best you can do is refer to a style guide—assuming you find one you like that discusses this. (Not all of them do.) Otherwise, just pick what you think looks best.
You say you prefer spaces. If you do, and nobody objects, then use them. But if you find that it causes names to be sorted in an order you don't like, then you'll need to decide if the sorting order is more important than each name's appearance. (Or find some way to have the sorting order you want despite the space or its lack.)
Some software may not even be programmed to accept a space or period (or even more than just a single letter) in a middle-initial entry field. In which case, it becomes a moot point.
For reference, although this is far from the only style guide on the subject, this is what The Chicago Manual of Style (17th ed.) says in several locations.
10.12:
Initials standing for given names are followed by a period and a space. A period is normally used even if the middle initial does not stand for a name (as in Harry S. Truman).
Roger W. Shugg
P. D. James
M. F. K. Fisher
If an entire name is abbreviated, spaces and periods can usually be omitted.
FDR (Franklin Delano Roosevelt)
MJ (Michael Jordan)
JLo (Jennifer Lopez)
but
J.Lo (the title of Lopez’s 2001 album)
14.74:
For authors who always use initials, full names should not be supplied—for example, T. S. Eliot, M. F. K. Fisher, O. Henry (pseud.), P. D. James, C. S. Lewis, J. D. Salinger, H. G. Wells. Note that space is added between initials. (Exceptions may be made for special cases like H.D.—the pen name for Hilda Doolittle.)
15.33:
The reference lists in some publications, especially journals in the natural sciences, always use initials instead of given names. When periods are used, space appears between them (Wells, H. G.); when periods are omitted, as in some journals’ styles, no comma intervenes between last name and initials, and no space appears between the initials (Wells HG). Chicago recommends using the form of the name as it appears with the source unless otherwise required.