|
@@ -1,15 +1,58 @@ |
|
|
import { Plugin } from '../../core/src/plugin' |
|
|
|
|
|
|
|
|
import { Plugin, StoragePlugin } from '../../core/src/plugin' |
|
|
import Storage from './Storage' |
|
|
import Storage from './Storage' |
|
|
|
|
|
|
|
|
type PluginConfig = { |
|
|
type PluginConfig = { |
|
|
baseUrl: string, |
|
|
baseUrl: string, |
|
|
|
|
|
idAttribute?: string, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const RemoteStoragePlugin: Plugin<PluginConfig> = config => ({ |
|
|
|
|
|
currentUserId, |
|
|
|
|
|
}) => { |
|
|
|
|
|
new Storage(currentUserId, config.baseUrl, 'notes') |
|
|
|
|
|
new Storage(currentUserId, config.baseUrl, 'folders') |
|
|
|
|
|
|
|
|
const AVAILABLE_COLLECTIONS = ['notes', 'folders'] as const |
|
|
|
|
|
|
|
|
|
|
|
const RemoteStoragePlugin: Plugin<PluginConfig> = ({ |
|
|
|
|
|
baseUrl, |
|
|
|
|
|
idAttribute = 'id', |
|
|
|
|
|
}) => class RemoteStorage implements StoragePlugin { |
|
|
|
|
|
private readonly storageMap: Record<string, Storage> |
|
|
|
|
|
|
|
|
|
|
|
public static readonly type: 'storage' |
|
|
|
|
|
|
|
|
|
|
|
constructor({ currentUserId, }) { |
|
|
|
|
|
this.storageMap = AVAILABLE_COLLECTIONS.reduce( |
|
|
|
|
|
(theStorages, collectionId) => ({ |
|
|
|
|
|
...theStorages, |
|
|
|
|
|
[collectionId]: new Storage(currentUserId, baseUrl, collectionId) |
|
|
|
|
|
}), |
|
|
|
|
|
{} |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async save(collectionId, item) { |
|
|
|
|
|
const { [collectionId]: storage = null } = this.storageMap |
|
|
|
|
|
if (storage === null) { |
|
|
|
|
|
throw new Error(`Invalid collection "${collectionId}"`) |
|
|
|
|
|
} |
|
|
|
|
|
return storage.saveItem(item) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async load(collectionId, itemId) { |
|
|
|
|
|
const { [collectionId]: storage = null } = this.storageMap |
|
|
|
|
|
if (storage === null) { |
|
|
|
|
|
throw new Error(`Invalid collection "${collectionId}"`) |
|
|
|
|
|
} |
|
|
|
|
|
const items = await storage.queryItems() |
|
|
|
|
|
if (!itemId) { |
|
|
|
|
|
return items |
|
|
|
|
|
} |
|
|
|
|
|
return items.filter(i => i[idAttribute] === itemId) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async remove(collectionId, item) { |
|
|
|
|
|
const { [collectionId]: storage = null } = this.storageMap |
|
|
|
|
|
if (storage === null) { |
|
|
|
|
|
throw new Error(`Invalid collection "${collectionId}"`) |
|
|
|
|
|
} |
|
|
|
|
|
return storage.deleteItem(item) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export default RemoteStoragePlugin |
|
|
export default RemoteStoragePlugin |