primatejs / primate

Web framework focused on flexibility and developer freedom
https://primatejs.com
MIT License
211 stars 9 forks source link

add `transact` to app #121

Open terrablue opened 6 months ago

terrablue commented 6 months ago

add the ability to create a transaction from the app object with (in the init hook)

app.set("store", "transact", async callback => {
  const { id, transaction } = await make_transaction(env);
  try {
    return await transaction([], stores => {
      const store = stores.reduce((base, [name, store]) =>
        o.extend(base, o.inflate(name, store))
      , {});
      return callback(store);
    },
    );
  } catch (error) {
    env.log.auto(error);
    TransactionRolledBack.warn(env.log, id, error.name);

    // let core handle error
    throw error;
  }
);

depends on #120