@@ -69,6 +69,80 @@ defmodule ExTypesense.Collection do
6969 OpenApiTypesense.Collections . get_collections ( conn , opts )
7070 end
7171
72+ @ doc """
73+ Creates collection with timestamped name and points to an alias.
74+
75+ > #### Use case {: .warning}
76+ >
77+ > When using this function, it will append
78+ > a timestamp in name and adds an alias based on schema name.
79+ > E.g. if table name is "bricks", then collection name is "bricks-1738558695"
80+ > and alias name is "bricks". The reason for this addition can be useful
81+ > when encountering like [full re-indexing](https://typesense.org/docs/guide/syncing-data-into-typesense.html#full-re-indexing)
82+
83+ > One common use-case for aliases is to reindex your data in the
84+ > background on a new collection and then switch your application
85+ > to it without any changes to your code. [Source](https://typesense.org/docs/latest/api/collection-alias.html#use-case)
86+ """
87+ @ doc since: "1.1.0"
88+ @ spec create_collection_with_alias ( map ( ) | module ( ) ) ::
89+ { :ok , OpenApiTypesense.CollectionResponse . t ( ) }
90+ | { :error , OpenApiTypesense.ApiResponse . t ( ) }
91+ def create_collection_with_alias ( schema ) do
92+ create_collection_with_alias ( schema , [ ] )
93+ end
94+
95+ @ doc """
96+ Same as [create_collection_with_alias/1](`create_collection_with_alias/1`) but passes another connection or opts.
97+
98+ ```elixir
99+ ExTypesense.create_collection_with_alias(%{api_key: xyz, host: ...}, schema)
100+
101+ ExTypesense.create_collection_with_alias(OpenApiTypesense.Connection.new(), MyModule.Context)
102+
103+ ExTypesense.create_collection_with_alias(schema, src_name: "companies")
104+ ```
105+ """
106+ @ doc since: "1.1.0"
107+ @ spec create_collection_with_alias ( map ( ) | Connection . t ( ) , map ( ) | module ( ) | keyword ( ) ) ::
108+ { :ok , OpenApiTypesense.CollectionResponse . t ( ) }
109+ | { :error , OpenApiTypesense.ApiResponse . t ( ) }
110+ def create_collection_with_alias ( schema , opts ) when is_list ( opts ) do
111+ Connection . new ( ) |> create_collection_with_alias ( schema , opts )
112+ end
113+
114+ def create_collection_with_alias ( conn , schema ) do
115+ create_collection_with_alias ( conn , schema , [ ] )
116+ end
117+
118+ @ doc """
119+ Same as [create_collection_with_alias/2](`create_collection_with_alias/2`) but explicitly passes all arguments.
120+
121+ ```elixir
122+ ExTypesense.create_collection_with_alias(%{api_key: xyz, host: ...}, schema, opts)
123+
124+ ExTypesense.create_collection_with_alias(OpenApiTypesense.Connection.new(), MyModule.Context.Schema, opts)
125+ ```
126+ """
127+ @ doc since: "1.1.0"
128+ @ spec create_collection_with_alias ( map ( ) | Connection . t ( ) , map ( ) | module ( ) , keyword ( ) ) ::
129+ { :ok , OpenApiTypesense.CollectionResponse . t ( ) }
130+ | { :error , OpenApiTypesense.ApiResponse . t ( ) }
131+ def create_collection_with_alias ( conn , module , opts ) when is_atom ( module ) do
132+ schema = module . get_field_types ( )
133+ create_collection_with_alias ( conn , schema , opts )
134+ end
135+
136+ def create_collection_with_alias ( conn , schema , opts ) do
137+ coll_name = Map . get ( schema , "name" ) || Map . get ( schema , :name )
138+ coll_name_ts = "#{ coll_name } -#{ DateTime . utc_now ( ) |> DateTime . to_unix ( ) } "
139+ updated_schema = Map . put ( schema , :name , coll_name_ts )
140+ coll = create_collection ( conn , updated_schema , opts )
141+ upsert_collection_alias ( conn , coll_name , coll_name_ts )
142+
143+ coll
144+ end
145+
72146 @ doc """
73147 Create collection from a map, or module name. Collection name
74148 is matched on table name if using Ecto schema by default.
@@ -306,6 +380,7 @@ defmodule ExTypesense.Collection do
306380 Get the collection name by alias.
307381 """
308382 @ doc since: "1.0.0"
383+ @ deprecated "Use Collection.get_collection_alias/1 instead"
309384 @ spec get_collection_name ( String . t ( ) ) ::
310385 { :ok , OpenApiTypesense.CollectionAlias . t ( ) } | { :error , OpenApiTypesense.ApiResponse . t ( ) }
311386 def get_collection_name ( alias_name ) do
@@ -322,6 +397,7 @@ defmodule ExTypesense.Collection do
322397 ```
323398 """
324399 @ doc since: "1.0.0"
400+ @ deprecated "Use Collection.get_collection_alias/2 instead"
325401 @ spec get_collection_name ( map ( ) | Connection . t ( ) | String . t ( ) , String . t ( ) | keyword ( ) ) ::
326402 { :ok , OpenApiTypesense.CollectionAlias . t ( ) } | { :error , OpenApiTypesense.ApiResponse . t ( ) }
327403 def get_collection_name ( alias_name , opts ) when is_list ( opts ) do
@@ -342,6 +418,7 @@ defmodule ExTypesense.Collection do
342418 ```
343419 """
344420 @ doc since: "1.0.0"
421+ @ deprecated "Use Collection.get_collection_alias/3 instead"
345422 @ spec get_collection_name ( map ( ) | Connection . t ( ) , String . t ( ) , keyword ( ) ) ::
346423 { :ok , OpenApiTypesense.CollectionAlias . t ( ) } | { :error , OpenApiTypesense.ApiResponse . t ( ) }
347424 def get_collection_name ( conn , alias_name , opts ) when is_binary ( alias_name ) do
@@ -402,7 +479,11 @@ defmodule ExTypesense.Collection do
402479 ```
403480 """
404481 @ doc since: "1.0.0"
405- @ spec update_collection_fields ( map ( ) | Connection . t ( ) , String . t ( ) | module ( ) , map ( ) | keyword ( ) ) ::
482+ @ spec update_collection_fields (
483+ map ( ) | Connection . t ( ) | String . t ( ) | module ( ) ,
484+ String . t ( ) | module ( ) | map ( ) ,
485+ map ( ) | keyword ( )
486+ ) ::
406487 { :ok , OpenApiTypesense.CollectionUpdateSchema . t ( ) }
407488 | { :error , OpenApiTypesense.ApiResponse . t ( ) }
408489 def update_collection_fields ( name , fields , opts ) when is_list ( opts ) do
@@ -547,10 +628,10 @@ defmodule ExTypesense.Collection do
547628 end
548629
549630 @ doc """
550- Get a specific collection alias.
631+ Get a specific collection by alias.
551632 """
552633 @ doc since: "1.0.0"
553- @ spec get_collection_alias ( String . t ( ) ) ::
634+ @ spec get_collection_alias ( String . t ( ) | module ( ) ) ::
554635 { :ok , OpenApiTypesense.CollectionAlias . t ( ) } | { :error , OpenApiTypesense.ApiResponse . t ( ) }
555636 def get_collection_alias ( alias_name ) do
556637 get_collection_alias ( alias_name , [ ] )
@@ -568,7 +649,10 @@ defmodule ExTypesense.Collection do
568649 ```
569650 """
570651 @ doc since: "1.0.0"
571- @ spec get_collection_alias ( map ( ) | Connection . t ( ) | String . t ( ) , String . t ( ) | keyword ( ) ) ::
652+ @ spec get_collection_alias (
653+ map ( ) | Connection . t ( ) | String . t ( ) | module ( ) ,
654+ String . t ( ) | module ( ) | keyword ( )
655+ ) ::
572656 { :ok , OpenApiTypesense.CollectionAlias . t ( ) } | { :error , OpenApiTypesense.ApiResponse . t ( ) }
573657 def get_collection_alias ( alias_name , opts ) when is_list ( opts ) do
574658 Connection . new ( ) |> get_collection_alias ( alias_name , opts )
@@ -588,8 +672,13 @@ defmodule ExTypesense.Collection do
588672 ```
589673 """
590674 @ doc since: "1.0.0"
591- @ spec get_collection_alias ( map ( ) | Connection . t ( ) , String . t ( ) , keyword ( ) ) ::
675+ @ spec get_collection_alias ( map ( ) | Connection . t ( ) , String . t ( ) | module ( ) , keyword ( ) ) ::
592676 { :ok , OpenApiTypesense.CollectionAlias . t ( ) } | { :error , OpenApiTypesense.ApiResponse . t ( ) }
677+ def get_collection_alias ( conn , module , opts ) when is_atom ( module ) do
678+ alias_name = module . __schema__ ( :source )
679+ get_collection_alias ( conn , alias_name , opts )
680+ end
681+
593682 def get_collection_alias ( conn , alias_name , opts ) do
594683 OpenApiTypesense.Collections . get_alias ( conn , alias_name , opts )
595684 end
@@ -623,14 +712,18 @@ defmodule ExTypesense.Collection do
623712 ```
624713 """
625714 @ doc since: "1.0.0"
626- @ spec upsert_collection_alias ( map ( ) | Connection . t ( ) , String . t ( ) , String . t ( ) | module ( ) ) ::
715+ @ spec upsert_collection_alias (
716+ map ( ) | Connection . t ( ) | String . t ( ) ,
717+ String . t ( ) | module ( ) ,
718+ String . t ( ) | module ( ) | keyword ( )
719+ ) ::
627720 { :ok , OpenApiTypesense.CollectionAlias . t ( ) } | { :error , OpenApiTypesense.ApiResponse . t ( ) }
628721 def upsert_collection_alias ( alias_name , coll_name , opts ) when is_list ( opts ) do
629722 Connection . new ( ) |> upsert_collection_alias ( alias_name , coll_name , opts )
630723 end
631724
632- def upsert_collection_alias ( conn , alias_name , collection_name ) do
633- upsert_collection_alias ( conn , alias_name , collection_name , [ ] )
725+ def upsert_collection_alias ( conn , alias_name , coll_name ) do
726+ upsert_collection_alias ( conn , alias_name , coll_name , [ ] )
634727 end
635728
636729 @ doc """
0 commit comments