Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions apps/web/lib/folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getFolderById = Effect.fn(function* (folderId: string) {
if (!folderId) throw new Error("Folder ID is required");
const db = yield* Database;

const [folder] = yield* db.execute((db) =>
const [folder] = yield* db.use((db) =>
db
.select()
.from(folders)
Expand Down Expand Up @@ -69,7 +69,7 @@ const getSharedSpacesForVideos = Effect.fn(function* (
const db = yield* Database;

// Fetch space-level sharing
const spaceSharing = yield* db.execute((db) =>
const spaceSharing = yield* db.use((db) =>
db
.select({
videoId: spaceVideos.videoId,
Expand All @@ -90,7 +90,7 @@ const getSharedSpacesForVideos = Effect.fn(function* (
);

// Fetch organization-level sharing
const orgSharing = yield* db.execute((db) =>
const orgSharing = yield* db.use((db) =>
db
.select({
videoId: sharedVideos.videoId,
Expand Down Expand Up @@ -164,7 +164,7 @@ export const getVideosByFolderId = Effect.fn(function* (
if (!folderId) throw new Error("Folder ID is required");
const db = yield* Database;

const videoData = yield* db.execute((db) =>
const videoData = yield* db.use((db) =>
db
.select({
id: videos.id,
Expand Down Expand Up @@ -287,7 +287,7 @@ export const getChildFolders = Effect.fn(function* (
const user = yield* CurrentUser;
if (!user.activeOrganizationId) throw new Error("No active organization");

const childFolders = yield* db.execute((db) =>
const childFolders = yield* db.use((db) =>
db
.select({
id: folders.id,
Expand Down
4 changes: 2 additions & 2 deletions packages/web-backend/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const getCurrentUser = Effect.gen(function* () {
).pipe(
Option.map((session) =>
Effect.gen(function* () {
const [currentUser] = yield* db.execute((db) =>
const [currentUser] = yield* db.use((db) =>
db
.select()
.from(Db.users)
Expand Down Expand Up @@ -50,7 +50,7 @@ export const HttpAuthMiddlewareLive = Layer.effect(

if (authHeader?.length === 36) {
user = yield* database
.execute((db) =>
.use((db) =>
db
.select()
.from(Db.users)
Expand Down
15 changes: 11 additions & 4 deletions packages/web-backend/src/Database.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { db } from "@cap/database";
import { DatabaseError } from "@cap/web-domain";
import type { Query } from "drizzle-orm";
import { Effect } from "effect";

export class Database extends Effect.Service<Database>()("Database", {
effect: Effect.gen(function* () {
return {
execute: <T>(cb: (_: ReturnType<typeof db>) => Promise<T>) =>
Effect.tryPromise({
try: () => cb(db()),
use: <T>(
cb: (_: ReturnType<typeof db>) => Promise<T> & { toSQL?(): Query },
) => {
const promise = cb(db());
const sql = promise.toSQL?.().sql ?? "Transaction";

return Effect.tryPromise({
try: () => promise,
catch: (cause) => new DatabaseError({ cause }),
}),
}).pipe(Effect.withSpan("Database.execute", { attributes: { sql } }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Update the span name to match the new method name.

The span is still named "Database.execute" but the method has been renamed to use. This inconsistency could cause confusion when reviewing traces.

Apply this diff:

-				}).pipe(Effect.withSpan("Database.execute", { attributes: { sql } }));
+				}).pipe(Effect.withSpan("Database.use", { attributes: { sql } }));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
}).pipe(Effect.withSpan("Database.execute", { attributes: { sql } }));
}).pipe(Effect.withSpan("Database.use", { attributes: { sql } }));
🤖 Prompt for AI Agents
In packages/web-backend/src/Database.ts around line 18, the tracing span is
named "Database.execute" but the method was renamed to use; update the span name
to "Database.use" so traces match the current method name by changing the string
passed to Effect.withSpan from "Database.execute" to "Database.use".

},
};
}),
}) {}
8 changes: 4 additions & 4 deletions packages/web-backend/src/Folders/FoldersRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class FoldersRepo extends Effect.Service<FoldersRepo>()("FoldersRepo", {
filters?: { organizationId?: Organisation.OrganisationId },
) =>
db
.execute((db) =>
.use((db) =>
db
.select()
.from(Db.folders)
Expand All @@ -41,13 +41,13 @@ export class FoldersRepo extends Effect.Service<FoldersRepo>()("FoldersRepo", {
.pipe(Effect.map(Array.get(0)));

const delete_ = (id: Folder.FolderId) =>
db.execute((db) => db.delete(Db.folders).where(Dz.eq(Db.folders.id, id)));
db.use((db) => db.delete(Db.folders).where(Dz.eq(Db.folders.id, id)));

const create = (data: CreateFolderInput) =>
Effect.gen(function* () {
const id = Folder.FolderId.make(nanoId());

yield* db.execute((db) =>
yield* db.use((db) =>
db.insert(Db.folders).values([
{
...data,
Expand All @@ -63,7 +63,7 @@ export class FoldersRepo extends Effect.Service<FoldersRepo>()("FoldersRepo", {

const update = (id: Folder.FolderId, data: Partial<CreateFolderInput>) =>
Effect.gen(function* () {
yield* db.execute((db) =>
yield* db.use((db) =>
db
.update(Db.folders)
.set({
Expand Down
12 changes: 6 additions & 6 deletions packages/web-backend/src/Folders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Folders extends Effect.Service<Folders>()("Folders", {
spaceId: Space.SpaceIdOrOrganisationId | null;
}): Effect.Effect<void, DatabaseError, Database> =>
Effect.gen(function* () {
const children = yield* db.execute((db) =>
const children = yield* db.use((db) =>
db
.select({
id: Db.folders.id,
Expand All @@ -46,7 +46,7 @@ export class Folders extends Effect.Service<Folders>()("Folders", {
// Folders can't be both in the root and in a space
if (folder.spaceId) {
const { spaceId } = folder;
yield* db.execute((db) =>
yield* db.use((db) =>
db
.update(Db.spaceVideos)
.set({ folderId: folder.parentId })
Expand All @@ -58,15 +58,15 @@ export class Folders extends Effect.Service<Folders>()("Folders", {
),
);
} else {
yield* db.execute((db) =>
yield* db.use((db) =>
db
.update(Db.videos)
.set({ folderId: folder.parentId })
.where(Dz.eq(Db.videos.folderId, folder.id)),
);
}

yield* db.execute((db) =>
yield* db.use((db) =>
db.delete(Db.folders).where(Dz.eq(Db.folders.id, folder.id)),
);
});
Expand Down Expand Up @@ -118,7 +118,7 @@ export class Folders extends Effect.Service<Folders>()("Folders", {
*/
delete: Effect.fn("Folders.delete")(function* (id: Folder.FolderId) {
const [folder] = yield* db
.execute((db) =>
.use((db) =>
db.select().from(Db.folders).where(Dz.eq(Db.folders.id, id)),
)
.pipe(Policy.withPolicy(policy.canEdit(id)));
Expand Down Expand Up @@ -182,7 +182,7 @@ export class Folders extends Effect.Service<Folders>()("Folders", {
}
}

yield* db.execute((db) =>
yield* db.use((db) =>
db
.update(Db.folders)
.set({
Expand Down
4 changes: 2 additions & 2 deletions packages/web-backend/src/Organisations/OrganisationsRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class OrganisationsRepo extends Effect.Service<OrganisationsRepo>()(

return {
membershipForVideo: (userId: User.UserId, videoId: Video.VideoId) =>
db.execute((db) =>
db.use((db) =>
db
.select({ membershipId: Db.organizationMembers.id })
.from(Db.organizationMembers)
Expand All @@ -33,7 +33,7 @@ export class OrganisationsRepo extends Effect.Service<OrganisationsRepo>()(
),
membership: (userId: User.UserId, orgId: Organisation.OrganisationId) =>
db
.execute((db) =>
.use((db) =>
db
.select({
membershipId: Db.organizationMembers.id,
Expand Down
6 changes: 3 additions & 3 deletions packages/web-backend/src/S3Buckets/S3BucketsRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class S3BucketsRepo extends Effect.Service<S3BucketsRepo>()(
const getForVideo = Effect.fn("S3BucketsRepo.getForVideo")(
(videoId: Video.VideoId) =>
Effect.gen(function* () {
const [res] = yield* db.execute((db) =>
const [res] = yield* db.use((db) =>
db
.select({ bucket: Db.s3Buckets })
.from(Db.s3Buckets)
Expand All @@ -33,7 +33,7 @@ export class S3BucketsRepo extends Effect.Service<S3BucketsRepo>()(
const getById = Effect.fn("S3BucketsRepo.getById")(
(id: S3Bucket.S3BucketId) =>
Effect.gen(function* () {
const [res] = yield* db.execute((db) =>
const [res] = yield* db.use((db) =>
db
.select({ bucket: Db.s3Buckets })
.from(Db.s3Buckets)
Expand All @@ -51,7 +51,7 @@ export class S3BucketsRepo extends Effect.Service<S3BucketsRepo>()(
const getForUser = Effect.fn("S3BucketsRepo.getForUser")(
(userId: User.UserId) =>
Effect.gen(function* () {
const [res] = yield* db.execute((db) =>
const [res] = yield* db.use((db) =>
db
.select({ bucket: Db.s3Buckets })
.from(Db.s3Buckets)
Expand Down
6 changes: 3 additions & 3 deletions packages/web-backend/src/Spaces/SpacesRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class SpacesRepo extends Effect.Service<SpacesRepo>()("SpacesRepo", {
return {
membershipForVideo: (userId: User.UserId, videoId: Video.VideoId) =>
db
.execute((db) =>
.use((db) =>
db
.select({ membershipId: Db.spaceMembers.id })
.from(Db.spaceMembers)
Expand All @@ -34,7 +34,7 @@ export class SpacesRepo extends Effect.Service<SpacesRepo>()("SpacesRepo", {
spaceId: Space.SpaceIdOrOrganisationId,
) =>
db
.execute((db) =>
.use((db) =>
db
.select({
membershipId: Db.spaceMembers.id,
Expand All @@ -52,7 +52,7 @@ export class SpacesRepo extends Effect.Service<SpacesRepo>()("SpacesRepo", {

getById: (spaceId: Space.SpaceIdOrOrganisationId) =>
db
.execute((db) =>
.use((db) =>
db.select().from(Db.spaces).where(Dz.eq(Db.spaces.id, spaceId)),
)
.pipe(Effect.map(Array.get(0))),
Expand Down
4 changes: 2 additions & 2 deletions packages/web-backend/src/Spaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Spaces extends Effect.Service<Spaces>()("Spaces", {
spaceOrOrgId: Space.SpaceIdOrOrganisationId,
) {
const [[space], [org]] = yield* Effect.all([
db.execute((db) =>
db.use((db) =>
db
.select({
id: Db.spaces.id,
Expand All @@ -31,7 +31,7 @@ export class Spaces extends Effect.Service<Spaces>()("Spaces", {
.where(Dz.eq(Db.spaces.id, spaceOrOrgId))
.limit(1),
),
db.execute((db) =>
db.use((db) =>
db
.select({
id: Db.organizations.id,
Expand Down
6 changes: 3 additions & 3 deletions packages/web-backend/src/Videos/VideosRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class VideosRepo extends Effect.Service<VideosRepo>()("VideosRepo", {
*/
const getById = (id: Video.VideoId) =>
Effect.gen(function* () {
const [video] = yield* db.execute((db) =>
const [video] = yield* db.use((db) =>
db.select().from(Db.videos).where(Dz.eq(Db.videos.id, id)),
);

Expand All @@ -45,7 +45,7 @@ export class VideosRepo extends Effect.Service<VideosRepo>()("VideosRepo", {
});

const delete_ = (id: Video.VideoId) =>
db.execute(async (db) =>
db.use(async (db) =>
db.transaction((db) =>
Promise.all([
db.delete(Db.videos).where(Dz.eq(Db.videos.id, id)),
Expand All @@ -60,7 +60,7 @@ export class VideosRepo extends Effect.Service<VideosRepo>()("VideosRepo", {
Effect.gen(function* () {
const id = Video.VideoId.make(nanoId());

yield* db.execute((db) =>
yield* db.use((db) =>
db.transaction(async (db) => {
const promises: MySqlInsertBase<any, any, any>[] = [
db.insert(Db.videos).values([
Expand Down
2 changes: 1 addition & 1 deletion packages/web-backend/src/Videos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class Videos extends Effect.Service<Videos>()("Videos", {
videoId: Video.VideoId,
) {
const [result] = yield* db
.execute((db) =>
.use((db) =>
db
.select({
uploaded: Db.videoUploads.uploaded,
Expand Down