I am creating a project which should be able to use different databases for the persistence of data. I have implemented a DAO pattern, but I have a problem when working with the ID in the struct, since for example MongoDB uses BSON whereas SQL databases use uint.
What solution do you recommend to implement the ID field in the struct.
What I can think of is to create an ID object that contains fields for BSON and uint IDs with GetBSON, GetUint methods and a GetID method that encodes either BSON or uint to MD5 (or some other encoding) I would use the GetBSON and GetUint methods to work with the Databases and the GetID method to work on the application as well as to send it through the REST API.
Cases like this your best bet is to use a universal identifier (UID) There are many ways to do this, though my personal favorite is just UUID v4. You can read the full UUID spec here https://tools.ietf.org/html/rfc4122.
Just configure your SQL tables and code to ensure uniqueness with this identifier and you can easily do lookups between databases using whichever access pattern you wish to implement.
e.g.
SQL
ID(Primary Key) | UID (unique)
0 | xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
MONGO
{
_id: ObjectID(),
uid: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
now just lookup on uid only and you can be database agnostic at the higher level.