Skip to content

Commit

Permalink
feat: db client
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Oct 6, 2021
1 parent 914b403 commit 3a253d4
Show file tree
Hide file tree
Showing 26 changed files with 6,039 additions and 2,735 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ on:
- 'packages/api/**'
- '.github/workflows/api.yml'
pull_request:
branches:
- main
paths:
- 'packages/api/**'
- '.github/workflows/api.yml'
Expand Down
22 changes: 20 additions & 2 deletions .github/workflows/db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@ on:
# Nothing to do on PR yet, but having the check appear on the PR serves as a reminder
# that we don't have proper tests for db changes yet, and that merging it will deploy.
pull_request:
branches:
- main
paths:
- 'packages/db/**'
- '.github/workflows/db.yml'
jobs:
test:
runs-on: ubuntu-latest
name: Test
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- uses: bahmutov/npm-install@v1
- run: npx playwright install-deps
- run: npm test --workspace packages/db
env:
DATABASE_URL: ${{secrets.DATABASE_URL}}
DATABASE_TOKEN: ${{secrets.DATABASE_TOKEN}}
DATABASE_CONNECTION: ${{secrets.DATABASE_CONNECTION}}
DAG_CARGO_HOST: ${{secrets.DAG_CARGO_HOST}}
DAG_CARGO_DATABASE: ${{secrets.DAG_CARGO_DATABASE}}
DAG_CARGO_USER: ${{secrets.DAG_CARGO_USER}}
DAG_CARGO_PASSWORD: ${{secrets.DAG_CARGO_PASSWORD}}

deploy-staging:
name: Deploy Staging DB
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
Expand Down
5,867 changes: 3,164 additions & 2,703 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ One time set up of your cloudflare worker subdomain for dev:
wrangler secret put S3_ACCESS_KEY_ID --env $(whoami) # Get from Amazon S3 (not required for dev)
wrangler secret put S3_SECRET_ACCESS_KEY_ID --env $(whoami) # Get from Amazon S3 (not required for dev)
wrangler secret put S3_BUCKET_NAME --env $(whoami) # e.g web3.storage-staging-us-east-2 (not required for dev)
wrangler secret put DATABASE_TOKEN --env USER # Get from database postgrest
```
- `npm run publish` - Publish the worker under your env. An alias for `wrangler publish --env $(whoami)`
Expand Down
4 changes: 2 additions & 2 deletions packages/api/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ format = "service-worker"
account_id = "fffa4b4363a7e5250af8357087263b3a" # Protocol Labs CF account
zone_id = "7eee3323c1b35b6650568604c65f441e" # web3.storage zone
route = "https://api.web3.storage/*"
vars = { CLUSTER_API_URL = "https://web3.storage.ipfscluster.io/api/", ENV = "production" }
vars = { CLUSTER_API_URL = "https://web3.storage.ipfscluster.io/api/", ENV = "production", DATABASE_URL = "https://db.web3.storage" }

[env.staging]
# name = "web3-storage-staging"
account_id = "fffa4b4363a7e5250af8357087263b3a" # Protocol Labs CF account
zone_id = "7eee3323c1b35b6650568604c65f441e" # web3.storage zone
route = "https://api-staging.web3.storage/*"
vars = { CLUSTER_API_URL = "https://web3.storage.ipfscluster.io/api/", ENV = "staging" }
vars = { CLUSTER_API_URL = "https://web3.storage.ipfscluster.io/api/", ENV = "staging", DATABASE_URL = "https://db-staging.web3.storage" }

[env.alan]
workers_dev = true
Expand Down
2 changes: 2 additions & 0 deletions packages/db/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# DB

web3.storage currently uses FaunaDB by default and its setup can be seen next. However, we are migrating to use Postgres and its setup can be seen [here](./postgres/README.md).

## Getting Started

1. Sign up at https://fauna.com.
Expand Down
27 changes: 27 additions & 0 deletions packages/db/db-client-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { definitions } from './postgres/db-types'

export type UpsertUserInput = Pick<
definitions['user'],
| 'name'
| 'picture'
| 'email'
| 'issuer'
| 'github'
| 'public_address'
>

export type UserOutput = definitions['user'] & {
keys: Array<
Pick<definitions['auth_key'], 'user_id' | 'id' | 'name' | 'secret'>
>
}

export interface CreateUploadInput {
user_id: definitions['upload']['user_id']
content_cid: definitions['upload']['content_cid']
source_cid: definitions['upload']['source_cid']
auth_key_id?: definitions['upload']['auth_key_id']
type: definitions['upload']['type']
dag_size?: definitions['content']['dag_size']
name?: string
}
17 changes: 17 additions & 0 deletions packages/db/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export class DBError extends Error {
/**
* @param {{
* message: string
* details: string
* hint: string
* code: string
* }} cause
*/
constructor ({ message, details, hint, code }) {
super(`${message}, details: ${details}, hint: ${hint}, code: ${code}`)
this.name = 'DBError'
this.code = DBError.CODE
}
}

DBError.CODE = 'ERROR_DB'
29 changes: 29 additions & 0 deletions packages/db/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
import { gql } from 'graphql-request'
import { RequestDocument } from 'graphql-request/dist/types'
import { definitions } from './postgres/db-types'

export { gql }

export class DBClient {
constructor(config: { endpoint?: string; token: string })
upsertUser (user: UpsertUserInput): Promise<UserOutput>

query<T, V>(document: RequestDocument, variables: V): Promise<T>
}

export type UpsertUserInput = Pick<
definitions['user'],
| 'name'
| 'picture'
| 'email'
| 'issuer'
| 'github'
| 'public_address'
>

export type UserOutput = definitions['user'] & {
keys: Array<
Pick<definitions['auth_key'], 'user_id' | 'id' | 'name' | 'secret'>
>
}

export interface CreateUploadInput {
user_id: definitions['upload']['user_id']
content_cid: definitions['upload']['content_cid']
source_cid: definitions['upload']['source_cid']
auth_key_id?: definitions['upload']['auth_key_id']
type: definitions['upload']['type']
dag_size?: definitions['content']['dag_size']
name?: string
}
Loading

0 comments on commit 3a253d4

Please sign in to comment.