All files / src/adapter memory.ts

88.46% Statements 69/78
100% Branches 24/24
80% Functions 12/15
88.46% Lines 69/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237    19x                               160x 160x       195x 195x   195x       247x   247x   247x               15x   14x         14x               23x 22x   18x 4x     14x   14x         14x               18x 17x   13x 4x     9x 15x     9x         9x                 62x 56x   52x 46x     52x 52x   52x   52x 106x 52x     106x   106x 52x 54x 6x     106x     52x         52x                 26x 25x   21x 19x     21x 4x     17x   17x 2x         15x         15x         15x                                                       144x   144x 10x     134x       120x   120x 8x     112x 8x     104x       442x               19x  
import Node from 'type/node';
import Adapter from 'interface/adapter';
import NotFoundError from 'http/error/not-found';
import FieldValue, { PrimitiveValue } from 'type/field-value';
 
interface NodeCache {
	[key: string]: Node;
}
 
interface AccountIds {
	[key: string]: string;
}
 
class MemoryAdapter implements Adapter {
	private cache: NodeCache;
	private account_ids: AccountIds;
 
	public constructor() {
		this.cache = {};
		this.account_ids = {};
	}
 
	public async fetchNode(node_key: string): Promise<Node | undefined> {
		const cache = this.getCache();
		const node = cache[node_key];
 
		return Promise.resolve(node);
	}
 
	public storeNode(node_key: string, node: Node): Promise<Node> {
		const cache = this.getCache();
 
		cache[node_key] = node;
 
		return Promise.resolve(node);
	}
 
	public async setField(
		node_key: string,
		field_key: string,
		value: FieldValue
	): Promise<Node> {
		const node = await this.fetchNodeUnsafe(node_key);
 
		const updated_node = {
			...node,
			[field_key]: value
		};
 
		return this.storeNode(node_key, updated_node);
	}
 
	public async addValueToSet(
		node_key: string,
		field_key: string,
		value: PrimitiveValue
	): Promise<Node> {
		const node = await this.fetchNodeUnsafe(node_key);
		const set_field = this.getArrayField(node, field_key);
 
		if (set_field.includes(value)) {
			return Promise.resolve(node);
		}
 
		const updated_set = [...set_field, value];
 
		const updated_node = {
			...node,
			[field_key]: updated_set
		};
 
		return this.storeNode(node_key, updated_node);
	}
 
	public async removeValueFromSet(
		node_key: string,
		field_key: string,
		value: PrimitiveValue
	): Promise<Node> {
		const node = await this.fetchNodeUnsafe(node_key);
		const set_field = this.getArrayField(node, field_key);
 
		if (!set_field.includes(value)) {
			return Promise.resolve(node);
		}
 
		const updated_set = set_field.filter((existing_value) => {
			return existing_value !== value;
		});
 
		const updated_node = {
			...node,
			[field_key]: updated_set
		};
 
		return this.storeNode(node_key, updated_node);
	}
 
	public async addValueToList(
		node_key: string,
		field_key: string,
		value: PrimitiveValue,
		position?: number
	): Promise<Node> {
		const node = await this.fetchNodeUnsafe(node_key);
		const list_field = this.getArrayField(node, field_key);
 
		if (position === undefined) {
			position = list_field.length;
		}
 
		const updated_list = [];
		const max_index = Math.max(position + 1, list_field.length);
 
		let index = 0;
 
		while (index < max_index) {
			if (index === position) {
				updated_list.push(value);
			}
 
			const current_value = list_field[index];
 
			if (current_value !== undefined) {
				updated_list.push(current_value);
			} else if (index !== position) {
				updated_list.push(null);
			}
 
			index++;
		}
 
		const updated_node = {
			...node,
			[field_key]: updated_list
		};
 
		return this.storeNode(node_key, updated_node);
	}
 
	public async removeValueFromList(
		node_key: string,
		field_key: string,
		value: PrimitiveValue,
		position?: number
	): Promise<Node> {
		const node = await this.fetchNodeUnsafe(node_key);
		const list_field = this.getArrayField(node, field_key);
 
		if (position === undefined) {
			position = list_field.lastIndexOf(value);
		}
 
		if (position === -1) {
			return Promise.resolve(node);
		}
 
		const target_value = list_field[position];
 
		if (target_value !== value) {
			throw new Error(
				`Expected to see ${value} at index ${position}, but saw ${target_value}`
			);
		}
 
		const updated_list = [
			...list_field.slice(0, position),
			...list_field.slice(position + 1)
		];
 
		const updated_node = {
			...node,
			[field_key]: updated_list
		};
 
		return this.storeNode(node_key, updated_node);
	}
 
	public fetchAccountId(
		username: string,
		password: string
	): Promise<string | undefined> {
		const serialized_credentials = `${username}:${password}`;
		const account_ids = this.getAccountIds();
		const account_id = account_ids[serialized_credentials];
 
		return Promise.resolve(account_id);
	}
 
	public storeAccountId(
		username: string,
		password: string,
		account_id: string
	): Promise<void> {
		const serialized_credentials = `${username}:${password}`;
		const account_ids = this.getAccountIds();
 
		account_ids[serialized_credentials] = account_id;
 
		return Promise.resolve();
	}
 
	private async fetchNodeUnsafe(node_key: string): Promise<Node> {
		const node = await this.fetchNode(node_key);
 
		if (node === undefined) {
			throw new NotFoundError();
		}
 
		return node;
	}
 
	private getArrayField(node: Node, field_key: string): PrimitiveValue[] {
		const field = node[field_key];
 
		if (field === undefined) {
			throw new Error(`Unable to find field for key: ${field_key}`);
		}
 
		if (!Array.isArray(field)) {
			throw new Error(`Field key ${field_key} did not point to an array`);
		}
 
		return field as PrimitiveValue[];
	}
 
	private getCache(): NodeCache {
		return this.cache;
	}
 
	private getAccountIds(): AccountIds {
		return this.account_ids;
	}
}
 
export default MemoryAdapter;