-
Notifications
You must be signed in to change notification settings - Fork 23
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
fix: make d1 spaces.metadata nullable and change to kysely #284
Merged
Merged
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions
44
packages/access-api/migrations/0003_space.metadata_can_be_null.sql
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,44 @@ | ||
-- Migration number: 0003 2022-12-12T18:58:30.339Z | ||
ALTER TABLE spaces | ||
RENAME TO _spaces_old; | ||
|
||
CREATE TABLE | ||
spaces ( | ||
did TEXT NOT NULL PRIMARY KEY, | ||
product TEXT NOT NULL, | ||
email TEXT NOT NULL, | ||
agent TEXT NOT NULL, | ||
inserted_at TEXT NOT NULL DEFAULT (strftime ('%Y-%m-%dT%H:%M:%fZ', 'now')), | ||
updated_at TEXT NOT NULL DEFAULT (strftime ('%Y-%m-%dT%H:%M:%fZ', 'now')), | ||
metadata JSON DEFAULT EMPTY, | ||
invocation TEXT NOT NULL DEFAULT EMPTY, | ||
delegation TEXT DEFAULT NULL, | ||
UNIQUE (did) | ||
); | ||
|
||
INSERT INTO | ||
spaces ( | ||
did, | ||
product, | ||
email, | ||
agent, | ||
inserted_at, | ||
updated_at, | ||
metadata, | ||
invocation, | ||
delegation | ||
) | ||
SELECT | ||
did, | ||
product, | ||
email, | ||
agent, | ||
inserted_at, | ||
updated_at, | ||
metadata, | ||
invocation, | ||
delegation | ||
FROM | ||
_spaces_old; | ||
|
||
DROP TABLE "_spaces_old"; |
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 was deleted.
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// @ts-ignore | ||
// eslint-disable-next-line no-unused-vars | ||
import * as Ucanto from '@ucanto/interface' | ||
import { delegationToString } from '@web3-storage/access/encoding' | ||
import { Kysely } from 'kysely' | ||
import { D1Dialect } from 'kysely-d1' | ||
import { D1Error, SpacePlugin } from '../utils/d1.js' | ||
|
||
const spacePlugin = new SpacePlugin() | ||
|
||
/** | ||
* Spaces | ||
*/ | ||
export class Spaces { | ||
/** | ||
* | ||
* @param {D1Database} d1 | ||
*/ | ||
constructor(d1) { | ||
this.d1 = /** @type {Kysely<import('../bindings').D1Schema>} */ ( | ||
new Kysely({ dialect: new D1Dialect({ database: d1 }) }) | ||
) | ||
} | ||
|
||
/** | ||
* @param {import('@web3-storage/capabilities/types').VoucherRedeem} capability | ||
* @param {Ucanto.Invocation<import('@web3-storage/capabilities/types').VoucherRedeem>} invocation | ||
* @param {Ucanto.Delegation<[import('@web3-storage/access/types').Top]> | undefined} delegation | ||
*/ | ||
async create(capability, invocation, delegation) { | ||
try { | ||
const metadata = | ||
/** @type {import('@web3-storage/access/types').SpaceTableMetadata | undefined} */ ( | ||
/** @type {unknown} */ (invocation.facts[0]) | ||
) | ||
const result = await this.d1 | ||
.withPlugin(spacePlugin) | ||
.insertInto('spaces') | ||
.values({ | ||
agent: invocation.issuer.did(), | ||
did: capability.nb.space, | ||
email: capability.nb.identity.replace('mailto:', ''), | ||
invocation: delegationToString(invocation), | ||
product: capability.nb.product, | ||
metadata, | ||
delegation: !delegation ? undefined : delegationToString(delegation), | ||
}) | ||
.returning('spaces.did') | ||
.execute() | ||
return { data: result } | ||
} catch (error) { | ||
return { | ||
error: new D1Error( | ||
/** @type {import('../bindings').D1ErrorRaw} */ (error) | ||
), | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get space by DID | ||
* | ||
* @param {Ucanto.URI<"did:">} did | ||
*/ | ||
async get(did) { | ||
const space = await this.d1 | ||
.withPlugin(spacePlugin) | ||
.selectFrom('spaces') | ||
.selectAll() | ||
.where('spaces.did', '=', did) | ||
.executeTakeFirst() | ||
|
||
if (space) { | ||
return space | ||
} | ||
} | ||
|
||
/** | ||
* @param {Ucanto.URI<"mailto:">} email | ||
*/ | ||
async getByEmail(email) { | ||
const spaces = await this.d1 | ||
.withPlugin(spacePlugin) | ||
.selectFrom('spaces') | ||
.selectAll() | ||
.where('spaces.email', '=', email.replace('mailto:', '')) | ||
.execute() | ||
|
||
if (spaces.length === 0) { | ||
return | ||
} | ||
|
||
return spaces | ||
} | ||
} |
File renamed without changes.
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
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.
Why return error and not throw?
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.
cause i need to use the error on the provider side and i dont want to try catch the full provider logic. im working on normalising this all thing in the w3space/account refactor