-
Notifications
You must be signed in to change notification settings - Fork 11
add mongo sink and some fixes for drizzle sink #127
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
33a04f7
sink-mongo: add mongodb sink
jaipaljadeja 436e6cd
sink-mongo: update queries in mongo transaction
jaipaljadeja 456cb67
sink-mongo: fix logical issues in mongo sink methods
jaipaljadeja d600561
sink-mongo: add test cases for mongo sink
jaipaljadeja 01e37cf
indexer: rename drizzle sink factory from drizzle to drizzleSink
jaipaljadeja bf9c528
indexer: fix logical issue in update method of drizzle sink
jaipaljadeja 7d040ad
examples: fix drizzle sink import
jaipaljadeja 3b81140
actions: add mongodb service
jaipaljadeja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
change/@apibara-indexer-b2b2208a-7a4e-4779-826d-c246b023891a.json
This file contains hidden or 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,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "indexer: rename drizzle sink factory and fix update method", | ||
| "packageName": "@apibara/indexer", | ||
| "email": "jadejajaipal5@gmail.com", | ||
| "dependentChangeType": "patch" | ||
| } |
7 changes: 7 additions & 0 deletions
7
change/@apibara-sink-mongo-0e59869f-bf56-417c-b432-622a2c1bb1da.json
This file contains hidden or 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,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "sink-mongo: add mongodb sink", | ||
| "packageName": "@apibara/sink-mongo", | ||
| "email": "jadejajaipal5@gmail.com", | ||
| "dependentChangeType": "patch" | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
| version: "3.8" | ||
|
|
||
| services: | ||
| postgres: | ||
| image: postgres:16 | ||
| environment: | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: postgres | ||
| ports: | ||
| - 5432:5432 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ import type { | |
| PgUpdateBase, | ||
| PgUpdateSetSource, | ||
| } from "drizzle-orm/pg-core"; | ||
| import type { Int8Range } from "./Int8Range"; | ||
| import { getDrizzleCursor } from "./utils"; | ||
|
|
||
| export class DrizzleSinkUpdate< | ||
| TTable extends PgTable, | ||
|
|
@@ -32,15 +34,36 @@ export class DrizzleSinkUpdate< | |
| return { | ||
| ...originalSet, | ||
| where: async (where: SQL | undefined) => { | ||
| await this.db | ||
| .update(this.table) | ||
| .set({ | ||
| _cursor: sql`int8range(lower(_cursor), ${Number(this.endCursor?.orderKey!)}, '[)')`, | ||
| } as PgUpdateSetSource<TTable>) | ||
| // 1. Find and store old versions of matching records | ||
| const oldRecords = await this.db | ||
| .select() | ||
| .from(this.table) | ||
| .where(sql`${where ? sql`${where} AND ` : sql``}upper_inf(_cursor)`) | ||
| .execute(); | ||
|
|
||
| return originalSet.where(where); | ||
| // 2. Insert old versions with updated upperbound cursor | ||
| if (oldRecords.length > 0) { | ||
| const oldRecordsWithNewCursor = oldRecords.map((record) => ({ | ||
| ...record, | ||
| _cursor: getDrizzleCursor([ | ||
| BigInt((record._cursor as Int8Range).range.lower!), | ||
| this.endCursor?.orderKey, | ||
| ]), | ||
| })); | ||
|
|
||
| await this.db | ||
| .insert(this.table) | ||
| .values(oldRecordsWithNewCursor) | ||
| .execute(); | ||
| } | ||
|
|
||
| // 3. Update matching records with new values and new 'lowerbound' cursor | ||
| return originalUpdate | ||
| .set({ | ||
| ...values, | ||
| _cursor: sql`int8range(${Number(this.endCursor?.orderKey!)}, NULL, '[)')`, | ||
| } as PgUpdateSetSource<TTable>) | ||
| .where(sql`${where ? sql`${where} AND ` : sql``}upper_inf(_cursor)`); | ||
|
Comment on lines
+60
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure safe conversion from BigInt to Number. |
||
| }, | ||
| } as PgUpdateBase<TTable, TQueryResult>; | ||
| } | ||
|
|
||
This file contains hidden or 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,7 @@ | ||
| # `@apibara/sink-mongo` | ||
|
|
||
| TODO | ||
|
|
||
| ## Installation | ||
|
|
||
| TODO |
This file contains hidden or 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,11 @@ | ||
| import { defineBuildConfig } from "unbuild"; | ||
|
|
||
| export default defineBuildConfig({ | ||
| entries: ["./src/index.ts"], | ||
| clean: true, | ||
| outDir: "./dist", | ||
| declaration: true, | ||
| rollup: { | ||
| emitCJS: true, | ||
| }, | ||
| }); |
This file contains hidden or 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,26 @@ | ||
| # Reference: https://medium.com/workleap/the-only-local-mongodb-replica-set-with-docker-compose-guide-youll-ever-need-2f0b74dd8384 | ||
|
|
||
| version: "3.8" | ||
|
|
||
| services: | ||
| mongo1: | ||
| image: mongo:7.0 | ||
| command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"] | ||
| ports: | ||
| - 27017:27017 | ||
| extra_hosts: | ||
| - "localhost:host-gateway" | ||
| healthcheck: | ||
| test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'localhost:27017'}]}) }" | mongosh --port 27017 --quiet | ||
| interval: 5s | ||
| timeout: 30s | ||
| start_period: 0s | ||
| start_interval: 1s | ||
| retries: 30 | ||
| volumes: | ||
| - "mongo1_data:/data/db" | ||
| - "mongo1_config:/data/configdb" | ||
|
|
||
| volumes: | ||
| mongo1_data: | ||
| mongo1_config: |
This file contains hidden or 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,26 @@ | ||
| # Reference: https://medium.com/workleap/the-only-local-mongodb-replica-set-with-docker-compose-guide-youll-ever-need-2f0b74dd8384 | ||
|
|
||
| version: "3.8" | ||
|
|
||
| services: | ||
| mongo1: | ||
| image: mongo:7.0 | ||
| command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"] | ||
| ports: | ||
| - 27017:27017 | ||
| extra_hosts: | ||
| - "host.docker.internal:host-gateway" | ||
| healthcheck: | ||
| test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'host.docker.internal:27017'}]}) }" | mongosh --port 27017 --quiet | ||
| interval: 5s | ||
| timeout: 30s | ||
| start_period: 0s | ||
| start_interval: 1s | ||
| retries: 30 | ||
| volumes: | ||
| - "mongo1_data:/data/db" | ||
| - "mongo1_config:/data/configdb" | ||
|
|
||
| volumes: | ||
| mongo1_data: | ||
| mongo1_config: |
This file contains hidden or 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,41 @@ | ||
| { | ||
| "name": "@apibara/sink-mongo", | ||
| "version": "2.0.0-beta.26", | ||
| "type": "module", | ||
| "files": [ | ||
| "dist", | ||
| "src", | ||
| "README.md" | ||
| ], | ||
| "main": "./dist/index.mjs", | ||
| "types": "./dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.mjs", | ||
| "require": "./dist/index.cjs", | ||
| "default": "./dist/index.mjs" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "build": "unbuild", | ||
| "typecheck": "tsc --noEmit", | ||
| "lint": "biome check .", | ||
| "lint:fix": "pnpm lint --write", | ||
| "test": "vitest", | ||
| "test:ci": "vitest run" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^20.14.0", | ||
| "mongodb": "^6.12.0", | ||
| "unbuild": "^2.0.0", | ||
| "vitest": "^1.6.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "mongodb": "^6.12.0" | ||
| }, | ||
| "dependencies": { | ||
| "@apibara/indexer": "workspace:*", | ||
| "@apibara/protocol": "workspace:*" | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Removing PRIMARY KEY constraint could lead to duplicated ids.
The table definition lacks a primary key, potentially allowing multiple rows with the same id. Confirm this is intentional, or reintroduce a primary key to guard against duplicate data.