-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make d1 spaces.metadata nullable and change to kysely (#284)
- spaces.metadata is now nullable - d1 tables are now typed - moved from `workers-qb` to [`kysely`](https://github.com/koskimas/kysely) with [d1 dialect](https://github.com/aidenwallis/kysely-d1) closes #280
- Loading branch information
1 parent
0f13a95
commit c8a9ce5
Showing
23 changed files
with
595 additions
and
350 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
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.