Skip to content

Commit

Permalink
Updates SQL Tables to Match Column Names
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Jun 27, 2024
1 parent 3d79172 commit 4286c25
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 37 deletions.
10 changes: 5 additions & 5 deletions code/server/sql/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand All @@ -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)
);
Expand All @@ -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
Expand Down
14 changes: 4 additions & 10 deletions code/server/src/databases/postgres/PostgresResourcesDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,15 @@ export class PostgresResourcesDB implements ResourcesRepository {
}

async getResource(id: string): Promise<Resource> {
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<Resource>): Promise<void> {
const { updatedAt, ...rest } = resource;
const compatible = updatedAt ? { ...rest, updated_at: updatedAt } : rest;
async updateResource(id: string, newProps: Partial<Resource>): Promise<void> {
const results = await sql`
update resource
set ${sql(compatible)}
set ${sql(newProps)}
where id = ${id}
returning id
`;
Expand All @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions code/server/src/databases/postgres/PostgresUsersDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class PostgresUsersDB implements UsersRepository {

async getUser(id: string): Promise<User> {
const results: User[] = await sql`
select id, name, email, created_at as "createdAt"
select *
from "user"
where id = ${id}
`;
Expand All @@ -29,9 +29,6 @@ export class PostgresUsersDB implements UsersRepository {
}

async getUsers(): Promise<User[]> {
return await sql`
select id, name, email, created_at as "createdAt"
from "user"
`;
return await sql`select * from "user"`;
}
}
20 changes: 9 additions & 11 deletions code/server/src/databases/postgres/PostgresWorkspacesDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SearchParams } from '@src/utils/searchParams';
export class PostgresWorkspacesDB implements WorkspacesRepository {
async createWorkspace(name: string, isPrivate: boolean): Promise<string> {
const results = await sql`
insert into workspace (name, private)
insert into workspace (name, "isPrivate")
values (${name}, ${isPrivate})
returning id
`;
Expand All @@ -18,11 +18,11 @@ export class PostgresWorkspacesDB implements WorkspacesRepository {
}

async getWorkspaces(email?: string): Promise<WorkspaceMeta[]> {
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
Expand All @@ -34,7 +34,7 @@ export class PostgresWorkspacesDB implements WorkspacesRepository {

async getWorkspace(id: string): Promise<Workspace> {
const results: Workspace[] = await sql`
select *, private as "isPrivate", created_at as "createdAt"
select *
from workspace
where id = ${id}
`;
Expand All @@ -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
Expand All @@ -58,11 +58,9 @@ export class PostgresWorkspacesDB implements WorkspacesRepository {
}

async updateWorkspace(id: string, newProps: Partial<WorkspaceMeta>): Promise<void> {
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
`;
Expand Down Expand Up @@ -100,10 +98,10 @@ export class PostgresWorkspacesDB implements WorkspacesRepository {
async searchWorkspaces(searchParams: SearchParams): Promise<WorkspaceMeta[]> {
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}
`;
}
Expand Down
12 changes: 6 additions & 6 deletions code/server/src/services/ResourcesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export class ResourcesService {
} as DocumentResource;
}

async updateResource(id: string, resource: Partial<Resource>): Promise<void> {
async updateResource(id: string, newProps: Partial<Resource>): Promise<void> {
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<void> {
Expand Down

0 comments on commit 4286c25

Please sign in to comment.