Skip to content

Commit

Permalink
Adapt RethinkDbRepository to model classes
Browse files Browse the repository at this point in the history
  • Loading branch information
shirte committed Nov 26, 2024
1 parent c738517 commit 5758184
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions nerdd_backend/data/rethinkdb_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,30 @@ async def get_all_modules(self):
cursor = await self.r.db(RETHINKDB_DB).table("modules").run(self.connection)
return [item async for item in cursor]

async def get_module_by_id(self, id):
return await self.r.db(RETHINKDB_DB).table("modules").get(id).run(self.connection)
async def get_module_by_id(self, module_id):
return (
await self.r.db(RETHINKDB_DB)
.table("modules")
.get(module_id)
.run(self.connection)
)

async def create_module_table(self):
try:
await self.r.db(RETHINKDB_DB).table_create("modules").run(self.connection)
except ReqlOpFailedError:
pass

async def get_most_recent_version(self, module_id):
async def get_most_recent_version(self, module_name):
# TODO: incorporate versioning
return await self.r.db(RETHINKDB_DB).table("modules").get(module_id).run(self.connection)
return (
await self.r.db(RETHINKDB_DB)
.table("modules")
.get(module_name)
.run(self.connection)
)

async def upsert_module(self, module):
# TODO: incorporate versioning
# compute the primary key from name and version
# if "version" in module.keys():
# version = module["version"]
# else:
# version = "1.0.0"
# name = module["name"]
module["id"] = module["name"]

# insert the module (or update if it matches an existing name-version combo)
await (
self.r.db(RETHINKDB_DB)
Expand All @@ -90,7 +91,9 @@ async def upsert_module(self, module):
async def create_jobs_table(self):
try:
await (
self.r.db(RETHINKDB_DB).table_create("jobs", primary_key="id").run(self.connection)
self.r.db(RETHINKDB_DB)
.table_create("jobs", primary_key="id")
.run(self.connection)
)
except ReqlOpFailedError:
pass
Expand All @@ -103,11 +106,19 @@ async def upsert_job(self, job):
.run(self.connection)
)

async def get_job_by_id(self, id):
return await self.r.db(RETHINKDB_DB).table("jobs").get(id).run(self.connection)
async def get_job_by_id(self, job_id):
return (
await self.r.db(RETHINKDB_DB).table("jobs").get(job_id).run(self.connection)
)

async def delete_job_by_id(self, id):
await self.r.db(RETHINKDB_DB).table("jobs").get(id).delete().run(self.connection)
async def delete_job_by_id(self, job_id):
await (
self.r.db(RETHINKDB_DB)
.table("jobs")
.get(job_id)
.delete()
.run(self.connection)
)

#
# SOURCES
Expand All @@ -130,11 +141,22 @@ async def upsert_source(self, source):
.run(self.connection)
)

async def get_source_by_id(self, id):
return await self.r.db(RETHINKDB_DB).table("sources").get(id).run(self.connection)
async def get_source_by_id(self, source_id):
return (
await self.r.db(RETHINKDB_DB)
.table("sources")
.get(source_id)
.run(self.connection)
)

async def delete_source_by_id(self, id):
await self.r.db(RETHINKDB_DB).table("sources").get(id).delete().run(self.connection)
async def delete_source_by_id(self, source_id):
await (
self.r.db(RETHINKDB_DB)
.table("sources")
.get(source_id)
.delete()
.run(self.connection)
)

#
# RESULTS
Expand Down

0 comments on commit 5758184

Please sign in to comment.