-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added memory database for resources tests
* Minor refactoring
- Loading branch information
1 parent
6b1db05
commit 88bbdc8
Showing
26 changed files
with
248 additions
and
85 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
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 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.
4 changes: 2 additions & 2 deletions
4
...database/memory/MemoryDocumentDatabase.ts → .../databases/documents/MemoryDocumentsDB.ts
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
2 changes: 1 addition & 1 deletion
2
...erver/src/ts/database/firestore/config.ts → ...s/databases/documents/firestore/config.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { DocumentsRepository, NoteSpaceDatabase, ResourcesRepository, WorkspacesRepository } from '@databases/types'; | ||
import { MemoryResourcesDB } from '@databases/resources/memory/MemoryResourcesDB'; | ||
import { MemoryWorkspacesDB } from '@databases/resources/memory/MemoryWorkspacesDB'; | ||
|
||
export class MemoryDB implements NoteSpaceDatabase { | ||
readonly document: DocumentsRepository; | ||
readonly resource: ResourcesRepository; | ||
readonly workspace: WorkspacesRepository; | ||
|
||
constructor(document: DocumentsRepository) { | ||
this.document = document; | ||
this.resource = new MemoryResourcesDB(); | ||
this.workspace = new MemoryWorkspacesDB(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
code/server/src/ts/databases/resources/memory/MemoryResourcesDB.ts
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,18 @@ | ||
import { ResourcesRepository } from '@databases/types'; | ||
import { ResourceType, WorkspaceResource } from '@notespace/shared/src/workspace/types/resource'; | ||
import {memoryDB} from '@databases/resources/memory/utils'; | ||
|
||
export class MemoryResourcesDB implements ResourcesRepository { | ||
createResource(wid: string, name: string, type: ResourceType, parent?: string | undefined) : Promise<string> { | ||
return Promise.resolve(memoryDB.addWorkspaceResource(wid, name)); | ||
} | ||
getResource(id: string): Promise<WorkspaceResource> { | ||
return Promise.resolve(memoryDB.getWorkspaceResource(id)); | ||
} | ||
updateResource(id: string, newProps: Partial<WorkspaceResource>): Promise<void> { | ||
return Promise.resolve(memoryDB.updateWorkspaceResource(id, newProps)); | ||
} | ||
deleteResource(id: string): Promise<void> { | ||
return Promise.resolve(memoryDB.removeWorkspaceResource(id)); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
code/server/src/ts/databases/resources/memory/MemoryWorkspacesDB.ts
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,26 @@ | ||
import { WorkspacesRepository } from '@databases/types'; | ||
import { WorkspaceMetaData } from '@notespace/shared/src/workspace/types/workspace'; | ||
import { memoryDB } from '@databases/resources/memory/utils'; | ||
import { WorkspaceResource } from '@notespace/shared/src/workspace/types/resource'; | ||
|
||
|
||
export class MemoryWorkspacesDB implements WorkspacesRepository { | ||
getWorkspaceResources(id: string):Promise<WorkspaceResource[]>{ | ||
return memoryDB.getWorkspaceResources(id); | ||
} | ||
createWorkspace(name: string): Promise<string> { | ||
return memoryDB.createWorkspace(name); | ||
} | ||
getWorkspaces(): Promise<WorkspaceMetaData[]> { | ||
return memoryDB.getWorkspaces(); | ||
} | ||
getWorkspace(id: string): Promise<WorkspaceMetaData> { | ||
return memoryDB.getWorkspace(id); | ||
} | ||
updateWorkspace(id: string, name: string): Promise<void> { | ||
return memoryDB.updateWorkspace(id, name); | ||
} | ||
deleteWorkspace(id: string): Promise<void> { | ||
return memoryDB.deleteWorkspace(id); | ||
} | ||
} |
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,94 @@ | ||
import { ResourceType, WorkspaceResource } from '@notespace/shared/src/workspace/types/resource'; | ||
|
||
type Workspace = { | ||
id: string; | ||
name: string; | ||
resources: string[]; | ||
} | ||
|
||
const workspaces = new Map<string, Workspace>(); | ||
|
||
export const createWorkspace = async (name: string): Promise<string> => { | ||
const id = Math.random().toString(36).substring(7); | ||
workspaces.set(id, { id, name, resources: [] }); | ||
return id; | ||
} | ||
|
||
export const getWorkspaces = async (): Promise<Workspace[]> => { | ||
return Array.from(workspaces.values()); | ||
} | ||
|
||
export const getWorkspace = async (id: string): Promise<Workspace> => { | ||
const workspace = workspaces.get(id); | ||
if (!workspace) throw new Error(`Workspace with id ${id} not found`); | ||
return workspace; | ||
} | ||
|
||
export const updateWorkspace = async (id: string, name: string): Promise<void> => { | ||
const workspace = workspaces.get(id); | ||
if (!workspace) throw new Error(`Workspace with id ${id} not found`); | ||
workspaces.set(id, { ...workspace, name }); | ||
} | ||
|
||
export const deleteWorkspace = async (id: string): Promise<void> => { | ||
if (!workspaces.delete(id)) throw new Error(`Workspace with id ${id} not found`); | ||
} | ||
|
||
export const getWorkspaceResources = async (id: string): Promise<WorkspaceResource[]> => { | ||
const workspace = workspaces.get(id); | ||
if (!workspace) throw new Error(`Workspace with id ${id} not found`); | ||
return workspace.resources.map(id => ({ id, | ||
name: 'Resource', | ||
workspace: id, | ||
type: ResourceType.DOCUMENT, | ||
parent: id, | ||
children: [], | ||
})); | ||
} | ||
|
||
export const getWorkspaceResource = async (resourceId: string): Promise<WorkspaceResource> => { | ||
const workspace = Array.from(workspaces.values()).find(w => w.resources.includes(resourceId)); | ||
if (!workspace) throw new Error(`Resource with id ${resourceId} not found`); | ||
return { | ||
id: resourceId, | ||
name: 'Resource', | ||
workspace: workspace.id, | ||
type: ResourceType.DOCUMENT, | ||
parent: workspace.id, | ||
children: [], | ||
}; | ||
} | ||
|
||
export const addWorkspaceResource = async (wid: string, resourceId: string): Promise<string> => { | ||
const workspace = workspaces.get(wid); | ||
if (!workspace) throw new Error(`Workspace with id ${wid} not found`); | ||
workspace.resources.push(resourceId); | ||
return Promise.resolve(resourceId); | ||
} | ||
|
||
export const updateWorkspaceResource = async (resourceId: string, newProps: Partial<WorkspaceResource>): Promise<void> => { | ||
const workspace = Array.from(workspaces.values()).find(w => w.resources.includes(resourceId)); | ||
if (!workspace) throw new Error(`Resource with id ${resourceId} not found`); | ||
const resource = workspace.resources.find(id => id === resourceId); | ||
if (!resource) throw new Error(`Resource with id ${resourceId} not found`); | ||
Object.assign(resource, newProps); | ||
} | ||
|
||
export const removeWorkspaceResource = async ( resourceId: string): Promise<void> => { | ||
const workspace = Array.from(workspaces.values()).find(w => w.resources.includes(resourceId)); | ||
if (!workspace) throw new Error(`Resource with id ${resourceId} not found`); | ||
workspace.resources = workspace.resources.filter(id => id !== resourceId); | ||
} | ||
|
||
export const memoryDB = { | ||
createWorkspace, | ||
getWorkspaces, | ||
getWorkspace, | ||
updateWorkspace, | ||
deleteWorkspace, | ||
getWorkspaceResources, | ||
getWorkspaceResource, | ||
addWorkspaceResource, | ||
updateWorkspaceResource, | ||
removeWorkspaceResource, | ||
}; |
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,15 @@ | ||
import { PSQLResourcesDB } from '@databases/resources/postgres/PSQLResourcesDB'; | ||
import { PSQLWorkspacesDB } from '@databases/resources/postgres/PSQLWorkspacesDB'; | ||
import { DocumentsRepository, NoteSpaceDatabase, ResourcesRepository, WorkspacesRepository } from '@databases/types'; | ||
|
||
export class PSQLDB implements NoteSpaceDatabase{ | ||
readonly document: DocumentsRepository; | ||
readonly resource: ResourcesRepository; | ||
readonly workspace: WorkspacesRepository; | ||
|
||
constructor(document: DocumentsRepository) { | ||
this.document = document; | ||
this.resource = new PSQLResourcesDB(); | ||
this.workspace = new PSQLWorkspacesDB(); | ||
} | ||
} |
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
File renamed without changes.
Oops, something went wrong.