generated from defi-wonderland/ts-turborepo-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: application repository kisely impl
- Loading branch information
Showing
13 changed files
with
245 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/repository/src/interfaces/applicationRepository.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Address, ChainId } from "@grants-stack-indexer/shared"; | ||
|
||
import { Application, NewApplication, PartialApplication } from "../types/application.types.js"; | ||
|
||
export interface IApplicationReadRepository { | ||
/** | ||
* Retrieves a specific application by its ID, chain ID, and round ID. | ||
* @param id The ID of the application. | ||
* @param chainId The chain ID of the application. | ||
* @param roundId The round ID of the application. | ||
* @returns A promise that resolves to an Application object if found, or undefined if not found. | ||
*/ | ||
getApplicationById( | ||
id: string, | ||
chainId: ChainId, | ||
roundId: string, | ||
): Promise<Application | undefined>; | ||
|
||
/** | ||
* Retrieves a specific application by its chain ID, round ID, and project ID. | ||
* @param chainId The chain ID of the application. | ||
* @param roundId The round ID of the application. | ||
* @param projectId The project ID of the application. | ||
* @returns A promise that resolves to an Application object if found, or undefined if not found. | ||
*/ | ||
getApplicationByProjectId( | ||
chainId: ChainId, | ||
roundId: string, | ||
projectId: string, | ||
): Promise<Application | undefined>; | ||
|
||
/** | ||
* Retrieves a specific application by its chain ID, round ID, and anchor address. | ||
* @param chainId The chain ID of the application. | ||
* @param roundId The round ID of the application. | ||
* @param anchorAddress The anchor address of the application. | ||
* @returns A promise that resolves to an Application object if found, or undefined if not found. | ||
*/ | ||
getApplicationByAnchorAddress( | ||
chainId: ChainId, | ||
roundId: string, | ||
anchorAddress: Address, | ||
): Promise<Application | undefined>; | ||
|
||
/** | ||
* Retrieves all applications for a given chain ID and round ID. | ||
* @param chainId The chain ID of the applications. | ||
* @param roundId The round ID of the applications. | ||
* @returns A promise that resolves to an array of Application objects. | ||
*/ | ||
getApplicationsByRoundId(chainId: ChainId, roundId: string): Promise<Application[]>; | ||
} | ||
|
||
export interface IApplicationRepository extends IApplicationReadRepository { | ||
/** | ||
* Inserts a new application into the repository. | ||
* @param application The new application to insert. | ||
* @returns A promise that resolves when the insertion is complete. | ||
*/ | ||
insertApplication(application: NewApplication): Promise<void>; | ||
|
||
/** | ||
* Updates an existing application in the repository. | ||
* @param where An object containing the (id, chainId, and roundId) of the application to update. | ||
* @param application The partial application data to update. | ||
* @returns A promise that resolves when the update is complete. | ||
*/ | ||
updateApplication( | ||
where: { id: string; chainId: ChainId; roundId: string }, | ||
application: PartialApplication, | ||
): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./projectRepository.interface.js"; | ||
export * from "./roundRepository.interface.js"; | ||
export * from "./applicationRepository.interface.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/repository/src/repositories/kysely/application.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { Kysely } from "kysely"; | ||
|
||
import { Address, ChainId, stringify } from "@grants-stack-indexer/shared"; | ||
|
||
import { | ||
Application, | ||
Database, | ||
IApplicationRepository, | ||
NewApplication, | ||
PartialApplication, | ||
} from "../../internal.js"; | ||
|
||
export class KyselyApplicationRepository implements IApplicationRepository { | ||
constructor( | ||
private readonly db: Kysely<Database>, | ||
private readonly schemaName: string, | ||
) {} | ||
|
||
/* @inheritdoc */ | ||
async getApplicationById( | ||
id: string, | ||
chainId: ChainId, | ||
roundId: string, | ||
): Promise<Application | undefined> { | ||
return this.db | ||
.withSchema(this.schemaName) | ||
.selectFrom("applications") | ||
.where("id", "=", id) | ||
.where("chainId", "=", chainId) | ||
.where("roundId", "=", roundId) | ||
.selectAll() | ||
.executeTakeFirst(); | ||
} | ||
|
||
/* @inheritdoc */ | ||
async getApplicationByProjectId( | ||
chainId: ChainId, | ||
roundId: string, | ||
projectId: string, | ||
): Promise<Application | undefined> { | ||
return this.db | ||
.withSchema(this.schemaName) | ||
.selectFrom("applications") | ||
.where("chainId", "=", chainId) | ||
.where("roundId", "=", roundId) | ||
.where("projectId", "=", projectId) | ||
.selectAll() | ||
.executeTakeFirst(); | ||
} | ||
|
||
/* @inheritdoc */ | ||
async getApplicationByAnchorAddress( | ||
chainId: ChainId, | ||
roundId: string, | ||
anchorAddress: Address, | ||
): Promise<Application | undefined> { | ||
return this.db | ||
.withSchema(this.schemaName) | ||
.selectFrom("applications") | ||
.where("chainId", "=", chainId) | ||
.where("roundId", "=", roundId) | ||
.where("anchorAddress", "=", anchorAddress) | ||
.selectAll() | ||
.executeTakeFirst(); | ||
} | ||
|
||
/* @inheritdoc */ | ||
async getApplicationsByRoundId(chainId: ChainId, roundId: string): Promise<Application[]> { | ||
return this.db | ||
.withSchema(this.schemaName) | ||
.selectFrom("applications") | ||
.where("chainId", "=", chainId) | ||
.where("roundId", "=", roundId) | ||
.selectAll() | ||
.execute(); | ||
} | ||
|
||
/* @inheritdoc */ | ||
async insertApplication(application: NewApplication): Promise<void> { | ||
const _application = this.formatApplication(application); | ||
|
||
await this.db | ||
.withSchema(this.schemaName) | ||
.insertInto("applications") | ||
.values(_application) | ||
.execute(); | ||
} | ||
|
||
/* @inheritdoc */ | ||
async updateApplication( | ||
where: { id: string; chainId: ChainId; roundId: string }, | ||
application: PartialApplication, | ||
): Promise<void> { | ||
const _application = this.formatApplication(application); | ||
|
||
await this.db | ||
.withSchema(this.schemaName) | ||
.updateTable("applications") | ||
.set(_application) | ||
.where("id", "=", where.id) | ||
.where("chainId", "=", where.chainId) | ||
.where("roundId", "=", where.roundId) | ||
.execute(); | ||
} | ||
|
||
/** | ||
* Formats the application to ensure that the statusSnapshots are stored as a JSON string. | ||
* Also, properly handles BigInt stringification. | ||
* @param application - The application to format. | ||
* @returns The formatted application. | ||
*/ | ||
private formatApplication<T extends NewApplication | PartialApplication>(application: T): T { | ||
if (application?.statusSnapshots) { | ||
application = { | ||
...application, | ||
statusSnapshots: stringify(application.statusSnapshots), | ||
}; | ||
} | ||
return application; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./project.repository.js"; | ||
export * from "./round.repository.js"; | ||
export * from "./application.repository.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./project.types.js"; | ||
export * from "./round.types.js"; | ||
export * from "./application.types.js"; | ||
export * from "./changeset.types.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters