in src/main.py [0:0]
def create_indexes_for_model(self, model: 'BaseModel'):
table_name = model.Meta.table_name
collection = model.mongodb_handler().mongodb.collection(table_name)
collection.drop_indexes()
hash_key = getattr(model._hash_key_attribute(), 'attr_name', None)
range_key = getattr(model._range_key_attribute(), 'attr_name', None)
_LOG.info(f'Creating main indexes for \'{table_name}\'')
if hash_key and range_key:
collection.create_index([(hash_key, 1),
(range_key, 1)],
name='main')
elif hash_key:
collection.create_index(hash_key, name='main')
else:
_LOG.error(f'Table \'{table_name}\' has no hash_key and range_key')
indexes = model._get_schema() # GSIs & LSIs, # only PynamoDB 5.2.1+
gsi = indexes.get('global_secondary_indexes')
lsi = indexes.get('local_secondary_indexes')
if gsi:
_LOG.info(f'Creating global indexes for \'{table_name}\'')
for i in gsi:
index_name = i['index_name']
_LOG.info(f'Processing index \'{index_name}\'')
collection.create_index(
self.convert_index(i['key_schema']), name=index_name)
_LOG.info(f'Index \'{index_name}\' was created')
_LOG.info(f'Global indexes for \'{table_name}\' were created!')
if lsi:
pass # write this part if at least one LSI is used