-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* re-organize types * add composite storage * tests for redis storage and composition storage
- Loading branch information
Showing
31 changed files
with
611 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Stream } from 'stream'; | ||
|
||
export interface Mismatches { | ||
[dependency: string]: { | ||
host: string; | ||
component: string; | ||
}; | ||
} | ||
|
||
export type Maybe<T> = T | undefined; | ||
|
||
export type Issues<T> = Record<string, Partial<T> & { mismatches: Mismatches }>; | ||
|
||
export interface IndexStorage { | ||
getIndex(): Promise<Index>; | ||
upsertIndex(index: Index): Promise<void>; | ||
} | ||
|
||
export interface ComponentsStorage { | ||
getComponentTree(): Promise<ComponentTree>; | ||
getComponent(name: string, version: string): Promise<Maybe<ComponentGetter>>; | ||
saveComponent(component: Component, files: File[]): Promise<void>; | ||
} | ||
|
||
export type Storage = IndexStorage & ComponentsStorage; | ||
|
||
export type Index = Record< | ||
Host['id'], | ||
{ | ||
dependencies: Dependencies; | ||
components: Record<Component['name'], Required<Component>['version']>; | ||
} | ||
>; | ||
|
||
export type Dependencies = Record<string, string>; | ||
|
||
export interface Component { | ||
name: string; | ||
version?: string; | ||
} | ||
|
||
export type ComponentTree = Record< | ||
Component['name'], | ||
Record<ComponentTreeItem['version'], ComponentTreeItem['getDependencies']> | ||
>; | ||
|
||
export interface ComponentTreeItem extends Required<Component> { | ||
getDependencies: () => Promise<Dependencies>; | ||
} | ||
|
||
export interface Host { | ||
id: string; | ||
dependencies: Dependencies; | ||
} | ||
|
||
export interface File { | ||
name: string; | ||
stream: Stream; | ||
} | ||
|
||
export interface ComponentGetter extends Required<Component> { | ||
getCode: () => Promise<string>; | ||
} |
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 @@ | ||
{ | ||
"name": "@dynamico/common-types", | ||
"version": "0.0.1-alpha.6", | ||
"description": "Common types for Dynamico", | ||
"main": "./dist/index.ts", | ||
"author": "Soluto", | ||
"private": true, | ||
"scripts": { | ||
"build": "npm run clean && npm run compile", | ||
"compile": "tsc", | ||
"clean": "rm -rf ./dist" | ||
}, | ||
"license": "MIT" | ||
} | ||
|
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"rootDir": "./", | ||
"downlevelIteration": true | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
coverageDirectory: '../../coverage/CompositionStorage/' | ||
}; |
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,138 @@ | ||
import { CompositionStorage } from './'; | ||
import { IndexStorage, ComponentsStorage } from '@dynamico/common-types'; | ||
import { Index } from '@dynamico/common-types'; | ||
|
||
class MockIndexStorage implements IndexStorage { | ||
getIndex: any = jest.fn(); | ||
upsertIndex: any = jest.fn(); | ||
} | ||
|
||
class MockComponentsStorage implements ComponentsStorage { | ||
getComponentTree: any = jest.fn(); | ||
getComponent: any = jest.fn(); | ||
saveComponent: any = jest.fn(); | ||
} | ||
describe('CompositionStorage', () => { | ||
describe('getIndex', () => { | ||
it('Should proxy call to index storage', async () => { | ||
const mockIndexStorage = new MockIndexStorage(); | ||
const mockComponentsStorage = new MockComponentsStorage(); | ||
const storage = new CompositionStorage(mockIndexStorage, mockComponentsStorage); | ||
const expected = 'expected'; | ||
mockIndexStorage.getIndex.mockReturnValueOnce(Promise.resolve(expected)); | ||
|
||
const result = await storage.getIndex(); | ||
|
||
expect(result).toEqual(expected); | ||
expect(mockIndexStorage.getIndex).toBeCalled(); | ||
expect(mockIndexStorage.upsertIndex).not.toBeCalled(); | ||
expect(mockComponentsStorage.getComponent).not.toBeCalled(); | ||
expect(mockComponentsStorage.getComponentTree).not.toBeCalled(); | ||
expect(mockComponentsStorage.saveComponent).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('upsertIndex', () => { | ||
it('Should proxy call to index storage', async () => { | ||
const mockIndexStorage = new MockIndexStorage(); | ||
const mockComponentsStorage = new MockComponentsStorage(); | ||
const storage = new CompositionStorage(mockIndexStorage, mockComponentsStorage); | ||
const expected: Index = { | ||
something: { | ||
components: {}, | ||
dependencies: {} | ||
} | ||
}; | ||
const mockInputIndex: Index = { | ||
somthingElse: { | ||
components: {}, | ||
dependencies: {} | ||
} | ||
}; | ||
mockIndexStorage.upsertIndex.mockReturnValueOnce(Promise.resolve(expected)); | ||
|
||
const result = await storage.upsertIndex(mockInputIndex); | ||
|
||
expect(result).toEqual(expected); | ||
expect(mockIndexStorage.upsertIndex).toBeCalledWith(mockInputIndex); | ||
expect(mockIndexStorage.getIndex).not.toBeCalled(); | ||
expect(mockComponentsStorage.getComponent).not.toBeCalled(); | ||
expect(mockComponentsStorage.getComponentTree).not.toBeCalled(); | ||
expect(mockComponentsStorage.saveComponent).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('getComponent', () => { | ||
it('Should proxy call to components storage', async () => { | ||
const mockIndexStorage = new MockIndexStorage(); | ||
const mockComponentsStorage = new MockComponentsStorage(); | ||
const storage = new CompositionStorage(mockIndexStorage, mockComponentsStorage); | ||
const mockComponentName = 'some component'; | ||
const mockComponentVersion = 'some version'; | ||
const expectedReturnValue = { | ||
name: mockComponentName, | ||
version: mockComponentVersion, | ||
getCode: () => 'something' | ||
}; | ||
mockComponentsStorage.getComponent.mockReturnValueOnce(expectedReturnValue); | ||
|
||
const result = await storage.getComponent(mockComponentName, mockComponentVersion); | ||
|
||
expect(result).toEqual(expectedReturnValue); | ||
expect(mockComponentsStorage.getComponent).toBeCalledWith(mockComponentName, mockComponentVersion); | ||
expect(mockIndexStorage.upsertIndex).not.toBeCalled(); | ||
expect(mockIndexStorage.getIndex).not.toBeCalled(); | ||
expect(mockComponentsStorage.getComponentTree).not.toBeCalled(); | ||
expect(mockComponentsStorage.saveComponent).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('getComponentTree', () => { | ||
it('Should proxy call to components storage', async () => { | ||
const mockIndexStorage = new MockIndexStorage(); | ||
const mockComponentsStorage = new MockComponentsStorage(); | ||
const storage = new CompositionStorage(mockIndexStorage, mockComponentsStorage); | ||
const mockComponentName = 'some component'; | ||
const mockComponentVersion = 'some version'; | ||
const expectedReturnValue = { | ||
[mockComponentName]: { | ||
[mockComponentVersion]: () => 'something' | ||
} | ||
}; | ||
mockComponentsStorage.getComponentTree.mockReturnValueOnce(expectedReturnValue); | ||
|
||
const result = await storage.getComponentTree(); | ||
|
||
expect(result).toEqual(expectedReturnValue); | ||
expect(mockComponentsStorage.getComponentTree).toBeCalled(); | ||
expect(mockIndexStorage.upsertIndex).not.toBeCalled(); | ||
expect(mockIndexStorage.getIndex).not.toBeCalled(); | ||
expect(mockComponentsStorage.getComponent).not.toBeCalled(); | ||
expect(mockComponentsStorage.saveComponent).not.toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('saveComponent', () => { | ||
it('Should proxy call to components storage', async () => { | ||
const mockIndexStorage = new MockIndexStorage(); | ||
const mockComponentsStorage = new MockComponentsStorage(); | ||
const storage = new CompositionStorage(mockIndexStorage, mockComponentsStorage); | ||
const mockComponentName = 'some component'; | ||
const mockComponentVersion = 'some version'; | ||
const mockComponent = { | ||
name: mockComponentName, | ||
version: mockComponentVersion | ||
}; | ||
const files = ['some', 'files']; | ||
mockComponentsStorage.saveComponent.mockReturnValueOnce(Promise.resolve()); | ||
|
||
await storage.saveComponent(mockComponent, files as any); | ||
|
||
expect(mockComponentsStorage.saveComponent).toBeCalledWith(mockComponent, files); | ||
expect(mockComponentsStorage.getComponentTree).not.toBeCalled(); | ||
expect(mockIndexStorage.upsertIndex).not.toBeCalled(); | ||
expect(mockIndexStorage.getIndex).not.toBeCalled(); | ||
expect(mockComponentsStorage.getComponent).not.toBeCalled(); | ||
}); | ||
}); | ||
}); |
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,31 @@ | ||
import { | ||
Storage, | ||
IndexStorage, | ||
ComponentsStorage, | ||
Component, | ||
File, | ||
Index, | ||
ComponentTree, | ||
ComponentGetter, | ||
Maybe | ||
} from '@dynamico/common-types'; | ||
|
||
export class CompositionStorage implements Storage { | ||
constructor(private indexStorage: IndexStorage, private componentsStorage: ComponentsStorage) {} | ||
|
||
getIndex(): Promise<Index> { | ||
return this.indexStorage.getIndex(); | ||
} | ||
upsertIndex(index: Index): Promise<void> { | ||
return this.indexStorage.upsertIndex(index); | ||
} | ||
getComponentTree(): Promise<ComponentTree> { | ||
return this.componentsStorage.getComponentTree(); | ||
} | ||
getComponent(name: string, version: string): Promise<Maybe<ComponentGetter>> { | ||
return this.componentsStorage.getComponent(name, version); | ||
} | ||
saveComponent(component: Component, files: File[]): Promise<void> { | ||
return this.componentsStorage.saveComponent(component, files); | ||
} | ||
} |
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,34 @@ | ||
{ | ||
"name": "@dynamico/composition-storage", | ||
"version": "0.0.1-alpha.6", | ||
"description": "Dynamico composition storage provider. Allows you to use different storages for index and components", | ||
"author": "Soluto", | ||
"homepage": "https://github.com/soluto/dynamico#readme", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "npm run clean && npm run compile", | ||
"test": "jest", | ||
"coverage": "jest --coverage", | ||
"compile": "tsc", | ||
"clean": "rm -rf ./dist" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"directories": { | ||
"lib": "dist" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/soluto/dynamico.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/soluto/dynamico/issues" | ||
}, | ||
"bundledDependencies": ["@dynamico/common-types"], | ||
"devDependencies": { | ||
"@dynamico/common-types": "^0.0.1-alpha.6" | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist", | ||
"rootDir": "./lib" | ||
} | ||
} | ||
|
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
Oops, something went wrong.