%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 04. 7月 2023 12:31
%%%-------------------------------------------------------------------
-module(mnesia_id_generator).
-author("aresei").
-include("iot.hrl").

%% API
-export([next_id/1, create_table/0]).

create_table() ->
    %% id生成器
    mnesia:create_table(id_generator, [
        {attributes, record_info(fields, id_generator)},
        {record_name, id_generator},
        {disc_copies, [node()]},
        {type, ordered_set}
    ]).

next_id(Tab) when is_atom(Tab) ->
    mnesia:dirty_update_counter(id_generator, Tab, 1).