Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic collection and document handling #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions type_sense/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,77 @@ class TypeSenseMixin(models.Model):

typesense_fields = []
typesense_schema_name = None
typesense_default_sorting_field = None

def get_typesense_schema_name(self) -> str:
if self.typesense_schema_name:
return self.typesense_schema_name
return self.__class__.__name__.lower()

def _get_schema(self):
def get_typesense_fields(self):
fields = deepcopy(self.typesense_fields)

# Auto adds the pk as Id if absent
if not any(field["name"] == "id" for field in fields):
fields.append({"name": "id", "type": "string", "attribute": "pk"})

return fields

def _get_typesense_schema(self):
schema = {
"name": self.get_typesense_schema_name(),
"fields": deepcopy(self.typesense_fields),
}

if self.typesense_default_sorting_field:
schema["default_sorting_field"] = self.typesense_default_sorting_field

existing_fields = []

for field in schema["fields"]:
existing_fields.append(field["name"])
if "attribute" in field:
del field["attribute"]

return schema

def create_collection(self):
client.collections.create(self._get_schema())
def _get_typesense_field_value(self, field):
if "attribute" in field:
attribute = field["attribute"]
else:
attribute = field["name"]
return getattr(self, attribute)

def create_typesense_collection(self):
client.collections.create(self._get_typesense_schema())

def drop_collection(self):
def drop_typesense_collection(self):
client.collections[self.get_typesense_schema_name()].delete()

def retrieve_typesense_collection(self):
return client.collections[self.get_typesense_schema_name()].retrieve()

def upsert_typesense_document(self):
fields = {}

for field in self.get_typesense_fields():
fields[field["name"]] = self._get_typesense_field_value(field)

return fields

def delete_typesense_document(self):
client.collections[self.get_typesense_schema_name()].documents[
str(self.pk)
].delete()

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.upsert_typesense_document()

def delete(self, **kwargs):
self.delete_typesense_document()
super().delete(**kwargs)

@classmethod
def check(cls, **kwargs):
errors = super().check(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion type_sense/tests/test_typesense.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ def test_typesense_schema_custom_fields(self):
],
"name": "customfieldsmockmodel",
}
self.assertDictEqual(m._get_schema(), expected)
self.assertDictEqual(m._get_typesense_schema(), expected)