Is it possible to pass dynamic callback arguments in JavaScript functions?
I am trying to combine three different functions, aiming to streamline the process and minimize code repetition. You can see an example below:
const initDB = (schemas: any[]) =>
Realm.open({ path: 'CircleYY.realm', schema: schemas })
.then((realm: Realm) => ({ realm }))
.catch((error: Error) => ({ error }));
This function initializes a database and returns either a database instance or an error.
I also have specific database write functions like the one shown here:
// Delete a message
const deleteOrder = (orderID: string, realm: Realm) => {
realm.write(() => {
const order = realm.objects('Orders').filtered(`primaryKey = ${id}`);
realm.delete(order);
});
};
In addition, there are three more functions as follows:
makeDBTransaction(deleteOrder(id));
and
makeDBTransaction(writeCommentInOrder(orderId, comment))
and
const makeDBTransaction = async (callback: Function) => {
const { error, realm } = (await initDB([
OrderSchema,
ProductSchema,
])) as InitRealm;
if (error) return { error };
callback(realm);
return realm.close();
};
I am looking for a way to pass the realm
into the callback with more than two arguments. How can I accomplish this task?