diff --git a/code/server/sql/create_tables.sql b/code/server/sql/create_tables.sql index 4335139..35ae1e4 100644 --- a/code/server/sql/create_tables.sql +++ b/code/server/sql/create_tables.sql @@ -7,8 +7,8 @@ begin; create table if not exists workspace ( id char(16) primary key default encode(gen_random_bytes(8), 'hex'), name text not null, - private boolean not null default false, - created_at timestamp not null default now(), + "isPrivate" boolean not null default false, + "createdAt" timestamp not null default now(), members text[] not null default '{}'::text[] -- references "user"(email) ); @@ -17,8 +17,8 @@ begin; workspace char(16) not null references workspace(id) on delete cascade, name text not null, type resource_type not null, - created_at timestamp not null default now(), - updated_at timestamp not null default now(), + "createdAt" timestamp not null default now(), + "updatedAt" timestamp not null default now(), parent char(16) default null references resource(id) on delete cascade, children char(16)[] not null default '{}'::char(16)[] -- references resource(id) ); @@ -27,7 +27,7 @@ begin; id char(28) primary key, name text not null, email text not null unique, - created_at timestamp not null default now() + "createdAt" timestamp not null default now() ); -- Triggers diff --git a/code/server/src/databases/postgres/PostgresResourcesDB.ts b/code/server/src/databases/postgres/PostgresResourcesDB.ts index 6bad312..c796a54 100644 --- a/code/server/src/databases/postgres/PostgresResourcesDB.ts +++ b/code/server/src/databases/postgres/PostgresResourcesDB.ts @@ -21,21 +21,15 @@ export class PostgresResourcesDB implements ResourcesRepository { } async getResource(id: string): Promise { - const results: Resource[] = await sql` - select *, updated_at as "updatedAt", created_at as "createdAt" - from resource - where id = ${id} - `; + const results: Resource[] = await sql`select *from resource where id = ${id}`; if (isEmpty(results)) throw new NotFoundError('Resource not found'); return results[0]; } - async updateResource(id: string, resource: Partial): Promise { - const { updatedAt, ...rest } = resource; - const compatible = updatedAt ? { ...rest, updated_at: updatedAt } : rest; + async updateResource(id: string, newProps: Partial): Promise { const results = await sql` update resource - set ${sql(compatible)} + set ${sql(newProps)} where id = ${id} returning id `; @@ -55,7 +49,7 @@ export class PostgresResourcesDB implements ResourcesRepository { const results = await sql` select row_to_json(t) as resource from ( - select *, updated_at as "updatedAt", created_at as "createdAt" + select * from resource where type = 'D' and workspace in ( select id from workspace where ${email} = any(members) diff --git a/code/server/src/databases/postgres/PostgresUsersDB.ts b/code/server/src/databases/postgres/PostgresUsersDB.ts index 2cab122..1984ace 100644 --- a/code/server/src/databases/postgres/PostgresUsersDB.ts +++ b/code/server/src/databases/postgres/PostgresUsersDB.ts @@ -11,7 +11,7 @@ export class PostgresUsersDB implements UsersRepository { async getUser(id: string): Promise { const results: User[] = await sql` - select id, name, email, created_at as "createdAt" + select * from "user" where id = ${id} `; @@ -29,9 +29,6 @@ export class PostgresUsersDB implements UsersRepository { } async getUsers(): Promise { - return await sql` - select id, name, email, created_at as "createdAt" - from "user" - `; + return await sql`select * from "user"`; } } diff --git a/code/server/src/databases/postgres/PostgresWorkspacesDB.ts b/code/server/src/databases/postgres/PostgresWorkspacesDB.ts index 8bbe7da..6bed7a0 100644 --- a/code/server/src/databases/postgres/PostgresWorkspacesDB.ts +++ b/code/server/src/databases/postgres/PostgresWorkspacesDB.ts @@ -9,7 +9,7 @@ import { SearchParams } from '@src/utils/searchParams'; export class PostgresWorkspacesDB implements WorkspacesRepository { async createWorkspace(name: string, isPrivate: boolean): Promise { const results = await sql` - insert into workspace (name, private) + insert into workspace (name, "isPrivate") values (${name}, ${isPrivate}) returning id `; @@ -18,11 +18,11 @@ export class PostgresWorkspacesDB implements WorkspacesRepository { } async getWorkspaces(email?: string): Promise { - const condition = email ? sql`${email} = any(members)` : sql`private = false`; + const condition = email ? sql`${email} = any(members)` : sql`"isPrivate" = false`; const results = await sql` select row_to_json(t) as workspace from ( - select id, name, private as "isPrivate", created_at as "createdAt", count(members) as members + select *, count(members) as members from workspace where ${condition} group by id @@ -34,7 +34,7 @@ export class PostgresWorkspacesDB implements WorkspacesRepository { async getWorkspace(id: string): Promise { const results: Workspace[] = await sql` - select *, private as "isPrivate", created_at as "createdAt" + select * from workspace where id = ${id} `; @@ -47,7 +47,7 @@ export class PostgresWorkspacesDB implements WorkspacesRepository { await sql` select row_to_json(t) as resources from ( - select *, created_at as "createdAt", updated_at as "updatedAt" + select * from resource where workspace = ${wid} group by id @@ -58,11 +58,9 @@ export class PostgresWorkspacesDB implements WorkspacesRepository { } async updateWorkspace(id: string, newProps: Partial): Promise { - const { isPrivate, ...rest } = newProps; - const compatible = isPrivate ? { private: isPrivate, ...rest } : rest; const results = await sql` update workspace - set ${sql(compatible)} + set ${sql(newProps)} where id = ${id} returning id `; @@ -100,10 +98,10 @@ export class PostgresWorkspacesDB implements WorkspacesRepository { async searchWorkspaces(searchParams: SearchParams): Promise { const { query, skip, limit } = searchParams; return sql` - select id, name, created_at, array_length(members, 1), private + select *, array_length(members, 1) as members from workspace - where private = false and name ilike ${'%' + query + '%'} - order by created_at desc + where "isPrivate" = false and name ilike ${'%' + query + '%'} + order by "createdAt" desc offset ${skip} limit ${limit} `; } diff --git a/code/server/src/services/ResourcesService.ts b/code/server/src/services/ResourcesService.ts index 48d5c81..a1b2986 100644 --- a/code/server/src/services/ResourcesService.ts +++ b/code/server/src/services/ResourcesService.ts @@ -30,13 +30,13 @@ export class ResourcesService { } as DocumentResource; } - async updateResource(id: string, resource: Partial): Promise { + async updateResource(id: string, newProps: Partial): Promise { validateId(id); - if (resource.id) throw new Error('Cannot update resource id'); - if (resource.type) throw new Error('Cannot update resource type'); - if (resource.workspace) throw new Error('Cannot update resource workspace'); - if (resource.createdAt) throw new Error('Cannot update resource creation date'); - await this.databases.resources.updateResource(id, resource); + if (newProps.id) throw new Error('Cannot update resource id'); + if (newProps.type) throw new Error('Cannot update resource type'); + if (newProps.workspace) throw new Error('Cannot update resource workspace'); + if (newProps.createdAt) throw new Error('Cannot update resource creation date'); + await this.databases.resources.updateResource(id, newProps); } async deleteResource(id: string): Promise {