-
Notifications
You must be signed in to change notification settings - Fork 119
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
feat: postgres setup #508
Merged
Merged
feat: postgres setup #508
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
39000ec
feat: postgres setup
vasco-santos aae70ed
feat: db client
vasco-santos ae33eae
fix: delete key must receive user id
vasco-santos ef3a7cd
fix: get user should return _id
vasco-santos 1c49fcf
chore: add upsert pin
vasco-santos 71e0b05
chore: upload delete should add deleted_at
vasco-santos b062408
chore: apply suggestions from code review
vasco-santos bbce16d
chore: rename db env vars and types
vasco-santos 5c09eb7
chore: rename user select variables
vasco-santos d5a8dea
chore: apply suggestions from code review
vasco-santos ba0a2a9
chore: test tweaks
vasco-santos e0b6b7f
chore: address last review
vasco-santos 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -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 | ||
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. Change docs? |
||
``` | ||
|
||
- `npm run publish` - Publish the worker under your env. An alias for `wrangler publish --env $(whoami)` | ||
|
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 |
---|---|---|
@@ -0,0 +1,220 @@ | ||
import { definitions } from './postgres/pg-rest-api-types' | ||
|
||
// User | ||
export type UpsertUserInput = { | ||
id: definitions['user']['id'], | ||
name: definitions['user']['name'], | ||
picture?: definitions['user']['picture'], | ||
email: definitions['user']['email'], | ||
issuer: definitions['user']['issuer'], | ||
github?: definitions['user']['github'], | ||
publicAddress: definitions['user']['public_address'] | ||
} | ||
|
||
export type UpsertUserOutput = { | ||
issuer: string | ||
} | ||
|
||
export type User = definitions['user'] | ||
|
||
export type UserOutput = { | ||
_id: definitions['user']['id'], | ||
name: definitions['user']['name'], | ||
email: definitions['user']['email'], | ||
issuer: definitions['user']['issuer'], | ||
publicAddress: definitions['user']['public_address'] | ||
created: definitions['user']['inserted_at'], | ||
updated: definitions['user']['updated_at'] | ||
} | ||
|
||
// Auth key | ||
export interface CreateAuthKeyInput { | ||
name: definitions['auth_key']['name'] | ||
secret: definitions['auth_key']['secret'] | ||
user: definitions['auth_key']['user_id'] | ||
} | ||
|
||
export type CreateAuthKeyOutput = { | ||
_id: definitions['auth_key']['id'] | ||
} | ||
|
||
export type AuthKey = { | ||
_id: definitions['auth_key']['id'], | ||
name: definitions['auth_key']['name'], | ||
user: { | ||
_id: definitions['user']['id'], | ||
issuer: definitions['user']['issuer'] | ||
} | ||
} | ||
|
||
export type AuthKeyItem = definitions['auth_key'] & { | ||
uploads: Array< | ||
Pick<definitions['upload'], 'id'> | ||
> | ||
} | ||
|
||
export type AuthKeyItemOutput = { | ||
_id: definitions['auth_key']['id'] | ||
name: definitions['auth_key']['name'] | ||
secret: definitions['auth_key']['secret'] | ||
created: definitions['auth_key']['inserted_at'] | ||
hasUploads: boolean | ||
} | ||
|
||
// Pin | ||
export type PinItem = definitions['pin'] & { | ||
pin_location: definitions['pin_location'] | ||
} | ||
|
||
export type PinItemOutput = { | ||
_id?: definitions['pin']['id'] | ||
status: definitions['pin']['status'] | ||
created?: definitions['pin']['inserted_at'] | ||
updated: definitions['pin']['updated_at'] | ||
peerId: definitions['pin_location']['peer_id'] | ||
peerName: definitions['pin_location']['peer_name'] | ||
region: definitions['pin_location']['region'] | ||
} | ||
|
||
// Backup | ||
export type BackupOutput = { | ||
_id: definitions['backup']['id'] | ||
created: definitions['backup']['inserted_at'] | ||
url: definitions['backup']['url'] | ||
uploadId: definitions['backup']['upload_id'] | ||
} | ||
|
||
// Deal | ||
export type Deal = { | ||
dealId: definitions['deal']['deal_id'] | ||
storageProvider: definitions['deal']['provider'] | ||
status: definitions['deal']['status'] | ||
pieceCid: definitions['aggregate']['piece_cid'] | ||
dataCid: definitions['aggregate_entry']['cid_v1'] | ||
dataModelSelector: definitions['aggregate_entry']['datamodel_selector'] | ||
dealActivation: definitions['deal']['start_time'] | ||
dealExpiration: definitions['deal']['end_time'] | ||
created: definitions['deal']['entry_created'] | ||
updated: definitions['deal']['entry_last_updated'] | ||
} | ||
|
||
// Content | ||
export type ContentItem = { | ||
cid: definitions['content']['cid'] | ||
dag_size: definitions['content']['dag_size'] | ||
inserted_at?: definitions['upload']['inserted_at'] | ||
pins: Array<{ | ||
status: definitions['pin']['status'] | ||
updated_at: definitions['pin']['updated_at'] | ||
pin_location: { | ||
_id: definitions['pin_location']['id'] | ||
peer_id: definitions['pin_location']['peer_id'] | ||
peer_name: definitions['pin_location']['peer_name'] | ||
region: definitions['pin_location']['region'] | ||
} | ||
}> | ||
} | ||
|
||
export type ContentItemOutput = { | ||
created: definitions['content']['inserted_at'] | ||
cid: definitions['content']['cid'] | ||
dagSize?: definitions['content']['dag_size'] | ||
pins: Array<PinItemOutput>, | ||
deals: Array<Deal> | ||
} | ||
|
||
|
||
// Upload | ||
export interface CreateUploadInput { | ||
user: definitions['upload']['user_id'] | ||
authKey: definitions['upload']['auth_key_id'] | ||
contentCid: definitions['upload']['content_cid'] | ||
sourceCid: definitions['upload']['source_cid'] | ||
type: definitions['upload']['type'] | ||
name?: definitions['upload']['name'] | ||
dagSize?: definitions['content']['dag_size'] | ||
created?: definitions['upload']['inserted_at'] | ||
updated?: definitions['upload']['updated_at'] | ||
pins: Array<{ | ||
status: definitions['pin']['status'] | ||
location: { | ||
peerId: definitions['pin_location']['peer_id'] | ||
peerName: definitions['pin_location']['peer_name'] | ||
region: definitions['pin_location']['region'] | ||
} | ||
}>, | ||
backupUrls: Array<definitions['backup']['url']> | ||
} | ||
|
||
export type CreateUploadOutput = { | ||
_id: definitions['upload']['id'], | ||
cid: definitions['content']['cid'] | ||
} | ||
|
||
export type UploadItem = { | ||
id: definitions['upload']['id'] | ||
type: definitions['upload']['type'] | ||
name?: definitions['upload']['name'] | ||
inserted_at?: definitions['upload']['inserted_at'] | ||
updated_at?: definitions['upload']['updated_at'] | ||
content: { | ||
cid: definitions['content']['cid'] | ||
dag_size: definitions['content']['dag_size'] | ||
pins: Array<{ | ||
status: definitions['pin']['status'] | ||
updated_at: definitions['pin']['updated_at'] | ||
pin_location: { | ||
id: definitions['pin_location']['id'] | ||
peer_id: definitions['pin_location']['peer_id'] | ||
peer_name: definitions['pin_location']['peer_name'] | ||
region: definitions['pin_location']['region'] | ||
} | ||
}> | ||
} | ||
} | ||
|
||
export type UploadItemOutput = { | ||
_id: definitions['upload']['id'] | ||
type: definitions['upload']['type'] | ||
name?: definitions['upload']['name'] | ||
created: definitions['upload']['inserted_at'] | ||
updated: definitions['upload']['updated_at'] | ||
cid: definitions['content']['cid'] | ||
dagSize?: definitions['content']['dag_size'] | ||
pins: Array<PinItemOutput>, | ||
deals: Array<Deal> | ||
} | ||
|
||
export type UploadOutput = definitions['upload'] & { | ||
user: Pick<definitions['user'], 'id' | 'issuer'> | ||
key: Pick<definitions['auth_key'], 'name'> | ||
content: Pick<definitions['content'], 'dag_size'> & { | ||
pin: Pick<definitions['pin'], 'id'> & { | ||
location: Pick<definitions['pin_location'], 'peer_id' | 'peer_name' | 'region'> | ||
}[] | ||
}, | ||
deals: Deal[] | ||
} | ||
|
||
export type ListUploadsOptions = { | ||
/** | ||
* Uploads created before a given timestamp. | ||
*/ | ||
before?: string | ||
/** | ||
* Uploads created after a given timestamp. | ||
*/ | ||
after?: string | ||
/** | ||
* Max records (default: 10). | ||
*/ | ||
size?: number | ||
/** | ||
* Sort by given property. | ||
*/ | ||
sortBy?: 'Date' | 'Name' | ||
/** | ||
* Sort order. | ||
*/ | ||
sortOrder?: 'Asc' | 'Desc' | ||
} |
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,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' |
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,9 +1,45 @@ | ||
import { gql } from 'graphql-request' | ||
import { RequestDocument } from 'graphql-request/dist/types' | ||
|
||
import type { | ||
UpsertUserInput, | ||
UpsertUserOutput, | ||
UserOutput, | ||
CreateUploadInput, | ||
ListUploadsOptions, | ||
CreateUploadOutput, | ||
UploadItemOutput, | ||
ContentItemOutput, | ||
Deal, | ||
CreateAuthKeyInput, | ||
CreateAuthKeyOutput, | ||
AuthKey, | ||
AuthKeyItemOutput, | ||
PinItemOutput, | ||
BackupOutput | ||
} from './db-client-types' | ||
|
||
export { gql } | ||
|
||
export class DBClient { | ||
constructor(config: { endpoint?: string; token: string }) | ||
upsertUser (user: UpsertUserInput): Promise<UpsertUserOutput> | ||
getUser (issuer: string): Promise<UserOutput> | ||
getUsedStorage (userId: number): Promise<number> | ||
createUpload (data: CreateUploadInput): Promise<CreateUploadOutput> | ||
getUpload (cid: string, userId: number): Promise<UploadItemOutput> | ||
listUploads (userId: number, opts?: ListUploadsOptions): Promise<UploadItemOutput[]> | ||
renameUpload (cid: string, userId: number, name: string): Promise<{ name: string }> | ||
deleteUpload(cid: string, userId: number): Promise<{ _id: number }> | ||
getStatus (cid: string): Promise<ContentItemOutput> | ||
getBackups(uploadId: number): Promise<Array<BackupOutput>> | ||
upsertPin (cid: string, pin: PinItemOutput): Promise<number> | ||
getPins (cid: string): Promise<Array<PinItemOutput>> | ||
getDeals (cid: string): Promise<Deal[]> | ||
getDealsForCids (cids: string[]): Promise<Record<string, Deal[]>> | ||
createKey (key: CreateAuthKeyInput): Promise<CreateAuthKeyOutput> | ||
getKey (issuer: string, secret: string): Promise<AuthKey> | ||
listKeys (userId: number): Promise<Array<AuthKeyItemOutput>> | ||
deleteKey (id: number): Promise<void> | ||
query<T, V>(document: RequestDocument, variables: V): Promise<T> | ||
} |
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.
We should be able to run CI tests for other branch targets, like here where we target the DB migration branch