mirror of
https://github.com/mokuappio/serverless-invoices.git
synced 2025-10-30 01:11:07 -04:00
Abstract backend functionality to be able to easily switch out backend implementation.
This commit is contained in:
65
src/services/adapters/local.adapter.js
Normal file
65
src/services/adapters/local.adapter.js
Normal file
@ -0,0 +1,65 @@
|
||||
import storage from 'localforage';
|
||||
import { removeVuexORMFlags } from '@/utils/helpers';
|
||||
|
||||
class LocalAdapter {
|
||||
async get(uri) {
|
||||
const parts = uri.split('/');
|
||||
|
||||
if (parts.length === 1) {
|
||||
return storage.getItem(parts[0]) || [];
|
||||
}
|
||||
|
||||
if (parts.length === 2) {
|
||||
return (await storage.getItem(parts[0]))
|
||||
.find(it => it.id === parts[1]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async post(uri, data) {
|
||||
const items = await this.get(uri);
|
||||
|
||||
removeVuexORMFlags(data);
|
||||
items.push(data);
|
||||
|
||||
return storage.setItem(uri, items);
|
||||
}
|
||||
|
||||
async patch(uri, data) {
|
||||
const parts = uri.split('/');
|
||||
|
||||
if (parts.length === 2) {
|
||||
const items = await this.get(parts[0]);
|
||||
|
||||
const index = items.findIndex(it => it.id === parts[1]);
|
||||
removeVuexORMFlags(data);
|
||||
items[index] = data;
|
||||
|
||||
return storage.setItem(parts[0], items);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async put(uri, data) {
|
||||
return storage.setItem(uri, data);
|
||||
}
|
||||
|
||||
async delete(uri) {
|
||||
const parts = uri.split('/');
|
||||
|
||||
if (parts.length === 2) {
|
||||
const items = await this.get(parts[0]);
|
||||
|
||||
const index = items.findIndex(it => it.id === parts[1]);
|
||||
items.splice(index, 1);
|
||||
|
||||
return storage.setItem(parts[0], items);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export default new LocalAdapter();
|
||||
Reference in New Issue
Block a user