import { strict as assert } from 'assert' import { Storage, StorageCursor, StorageSession } from '../app/storage' export class MapCursor implements StorageCursor { _map: Map _limit = 0 constructor(map: Map) { this._map = map } limit(number_: number): StorageCursor { this._limit = number_ return this } sort(parameters: Record): StorageCursor { return this } async toArray(): Promise { return Promise.resolve(Array.from(this._map.values())) } skip(offset: number): StorageCursor { return this } } export class MapStorage extends Storage { private readonly _mapPool private readonly _mapPoolId constructor(dsn: string) { super(dsn) this._mapPoolId = {} this._mapPool = {} } async find(name: string, query: Record): Promise { if (!(name in this._mapPool)) { return new MapCursor(new Map()) } const map: Map = new Map() for (const [_, element] of this._mapPool[name]) { nextParam: for (const [key, value] of Object.entries(query)) { if (key in element) { for (const symbol of ['=', '>', '<', '!']) { if (String(value).startsWith(symbol) && eval(String(element[key] + value))) { map.set(_, element) break nextParam } } if (element[key] == value) { map.set(_, element) } } } } return new MapCursor(map) } async count(name: string, query: Record): Promise { return Promise.resolve(0) } async remove(collectionName: string, uniqKeyName: string, uniqKey: string): Promise { return Promise.resolve(true) } async save(name: string, idKey: string, data: Record): Promise { if (!(name in this._mapPool)) { this._mapPool[name] = new Map() this._mapPoolId[name] = {} } const id = String(data[idKey]) this._mapPool[name].set(id, data) if (!(id in this._mapPoolId[name])) { this._mapPoolId[name][id] = Date.now() } return Promise.resolve(this._mapPoolId[name][id]) } createSession(): StorageSession { return new MapStorageSession() } } class MapStorageSession extends StorageSession { async commit(fn, options?: Record): Promise { return Promise.resolve(undefined) } async start(options?: Record): Promise { return Promise.resolve(undefined) } } describe('Storage', () => { describe('#save', () => { it('should return inner id', async () => { const storage = new MapStorage('') await storage.save('items', 'id', { id: 100, value: 'one' }) const _id2 = await storage.save('items', 'id', { id: 102, value: 'two' }) const _id3 = await storage.save('items', 'id', { id: 102, value: 'two' }) assert.equal(_id2, _id3) }) }) describe('#find', () => { it('should return cursor', async () => { const storage = new MapStorage('') await storage.save('items', 'id', { id: 1, value: 'one' }) await storage.save('items', 'id', { id: 2, value: 'two' }) await storage.save('elements', 'id', { id: 1, value: 2 }) await storage.save('elements', 'id', { id: 2, value: 11 }) await storage.save('elements', 'id', { id: 3, value: 12 }) let cursor cursor = await storage.find('items', { value: 'one' }) assert.equal((await cursor.toArray()).length, 1) cursor = await storage.find('elements', { value: '>10' }) assert.equal((await cursor.toArray()).length, 2) }) }) })