Your task is to implement a simple data store with JSON-like structure, e.g.:
{
foo: {
bar: {
baz: 42
},
hello: 'world'
}
}
The data store interface is specified in src/dataStore.ts
and it consists of 3 asynchronous methods:
-
read(path: string)
- should return aPromise
with data located atpath
. Path is a concatenated list of keys separated with.
, for examplefoo.bar.baz
references data located at keybaz
of objectbar
nested in objectfoo
. Duplicated.
should not matter. If there's no value atpath
the method should returnPromise
withundefined
. -
write(path: string, value: StoredValue)
- should writevalue
at givenpath
. Value could be a primitive value (string
,number
,boolean
), anobject
(with primitive values or other objects) or anundefined
value. Ifpath
doesn't exist yet in the store it should be created. Writing at root path (empty string or.
) should not be allowed. -
observe(path: string, callback: ObserverCallback)
- should register a callback that will be called every time the value atpath
or any sub-paths is modified. It should be also called immediately after registering with the current value. For example if observer is registered forfoo.bar
path and laterfoo.bar.baz
value is written the callback should be called.
For example, given the data structure shown above we should expect the following results:
read('foo.bar.baz') // 42
read('foo.bar') // { baz: 42 }
write('foo.bar', 'test'); read('foo') // { bar: 'test', hello: 'world' }
There are three classes to be implemented:
InMemoryStore
- should keep the data in-memory.FileStore
- should read and write the data to file.TypedStore
-read
should return values with types depending on provided schema
There are ordered test files in tests/
folder, we strongly suggest that you fix them one by one:
01.read.test
- testsread
method ofInMemoryStore
02.write.test
- testswrite
method ofInMemoryStore
03.observe.test
- testsobserve
method ofInMemoryStore
04.file.test
- tests reading and saving to file byFileStore
05.typed.test
- tests checking if types returned byTypedStore
are correct