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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | 120x 26x 2x 8x 8x 8x 12x 16x 8x 8x 8x 8x 8x 8x 8x 10281x 10281x 10281x 10309x 14x 10295x 10295x 10295x 100x 10195x 20x 10179x 10267x 2x 2x 2x 3x 2x 2x 2x 2x 2x 1x 13x 4x 4x 11x 11x 11x 11x 2x 9x 3x 3x 10x 3x 220x 220x 220x 220x 220x 213x 213x 10132x 213x 213x 3x 3x 9x 3x 3x 14x 14x 14x 26x 14x 1x 13x 13x 25x 13x 3x 6x 6x 486x 486x 486x 3x 483x 10240x 246x 483x 486x 2x 483x 12x 12x 12x 471x 15x 15x 33x 19x 19x 19x 17x 2x 8x 1x 2x 3x 11x 11x | import CoreDBCon from '../../classes/CoreDBCon';
import {
type ConfigType,
type ICoreKernelModule,
isQInterfaceSearchAdvanced,
isQInterfaceSearchAdvancedArr,
type QInterfaceSearch,
type QInterfaceSearchAdvanced,
type QueryInterface,
type RawQuery,
type RawQueryResult,
} from '../../lib';
import CoreEntity from '../../classes/CoreEntity';
import type { EntityConfig, EUpDateProperties, IEntity } from '../../classes';
function aFilter<E extends CoreEntity>(
e: E[keyof E],
s: QInterfaceSearchAdvanced<QInterfaceSearch<E>, keyof E>,
) {
switch (s.mode) {
case 'equals':
return e === s.value;
case 'not':
return e !== s.value;
case 'like':
return (
typeof e === 'string' &&
typeof s.value === 'string' &&
e.includes(s.value)
);
case 'startsWith':
return (
typeof e === 'string' &&
typeof s.value === 'string' &&
e.startsWith(s.value)
);
case 'endsWith':
return (
typeof e === 'string' &&
typeof s.value === 'string' &&
e.endsWith(s.value)
);
case 'smallerThan':
return (
typeof e === 'number' && typeof s.value === 'number' && e < s.value
);
case 'greaterThan':
return (
typeof e === 'number' && typeof s.value === 'number' && e > s.value
);
case 'in':
return Array.isArray(s.value) && (s.value as unknown[]).includes(e);
case 'notIn':
return Array.isArray(s.value) && !(s.value as unknown[]).includes(e);
case 'between': {
Iif (!Array.isArray(s.value) || s.value.length < 2) {
return false;
}
const [min, max] = s.value as [unknown, unknown];
return e >= (min as E[keyof E]) && e <= (max as E[keyof E]);
}
case 'isNull':
return e === null || e === undefined;
case 'isNotNull':
return e !== null && e !== undefined;
default:
return false;
}
}
function eFilter<E extends CoreEntity>(row: E, search: QInterfaceSearch<E>) {
const keys: (keyof E)[] = Object.keys(search) as (keyof E)[];
let cur = true;
for (const key of keys) {
if (!cur) {
return false;
}
const r = row[key];
const s = search[key];
if (isQInterfaceSearchAdvanced(s)) {
cur = aFilter(r, s);
} else if (isQInterfaceSearchAdvancedArr(s)) {
cur = s.every((e) => aFilter(r, e));
} else {
cur = row[key] === s;
}
}
return cur;
}
/**
* In-memory implementation of {@link CoreDBCon}.
* This class stores all configuration values and entities in JavaScript Maps,
* making it suitable for testing or scenarios where persistence is not required.
*/
export default class InMemDB extends CoreDBCon<Map<string, CoreEntity[]>> {
map: Map<string, ConfigType>;
e_map: Map<string, CoreEntity[]>;
constructor(module: ICoreKernelModule<any, any, any, any, any>) {
super('0', 'main', module);
this.map = new Map<string, ConfigType>();
this.e_map = new Map<string, CoreEntity[]>();
}
async configExist(key: string): Promise<boolean> {
return this.map.has(key);
}
async connect(): Promise<boolean> {
this.setConnected();
this.setNew(true);
return true;
}
async disconnect(): Promise<boolean> {
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async runScripts(...list: RawQuery[]): Promise<any[]> {
return [];
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async queryScript<E>(query: RawQuery): Promise<RawQueryResult<E, null>> {
return { rows: [], meta: null };
}
async getConfig(key: string): Promise<ConfigType | undefined> {
return this.map.get(key);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async initEntity<E extends CoreEntity>(className: string): Promise<boolean> {
this.e_map.set(className, []);
return true;
}
async getEntityById<E extends CoreEntity>(
config: EntityConfig<E>,
e_id: string,
): Promise<E | null> {
const table = this.e_map.get(config.className);
Iif (!table) {
return null;
}
const temp = table.find((el) => el.e_id === e_id);
if (!temp) {
return null;
}
return temp as E;
}
async getEntityBulkById<E extends CoreEntity>(
config: EntityConfig<E>,
e_id: string[],
): Promise<E[]> {
const table = this.e_map.get(config.className);
Iif (!table) {
return [];
}
const temp = table.filter((el) => e_id.includes(el.e_id));
return temp as E[];
}
async createEntity<E extends CoreEntity>(
config: EntityConfig<E>,
entity: E,
): Promise<E> {
const clone = entity;
const table = this.e_map.get(config.className);
Iif (!table) {
throw this.lError('Cant create Entity');
}
table.push(clone);
return clone;
}
async deleteEntityById(className: string, e_id: string): Promise<boolean> {
const table = this.e_map.get(className);
Iif (!table) {
return false;
}
const temp = table.filter((el) => el.e_id !== e_id);
this.e_map.set(className, temp);
return true;
}
async deleteEntityBulkById(
className: string,
e_id: string[],
): Promise<boolean> {
const table = this.e_map.get(className);
Iif (!table) {
return false;
}
const temp = table.filter((el) => !e_id.includes(el.e_id));
this.e_map.set(className, temp);
return true;
}
async updateEntity<E extends CoreEntity>(
config: EntityConfig<E>,
e_id: string,
entity: EUpDateProperties<E>,
): Promise<boolean> {
const table = this.e_map.get(config.className);
Iif (!table) {
return false;
}
const target: any = table.find((el) => {
return el.e_id === e_id;
});
if (!target) {
return false;
}
const keys = Object.keys(entity) as (keyof EUpDateProperties<E>)[];
for (const key of keys) {
target[key] = entity[key];
}
return true;
}
async updateBulkEntity<E extends IEntity>(
config: EntityConfig<E>,
e_id: string[],
entity: EUpDateProperties<E>,
): Promise<boolean> {
return (
await Promise.all(
e_id.map((e) => this.updateEntity<E>(config, e, entity)),
)
).every((el) => el);
}
async getEntityList<E extends CoreEntity>(
query: QueryInterface<E>,
): Promise<E[]> {
const { config, limit, search, order, offset } = query;
const table = this.e_map.get(config.className);
if (
!table ||
limit === 0 ||
(offset !== undefined && offset > table.length)
) {
return [];
}
let out: E[];
if (search) {
out = (table as E[]).filter((row) => eFilter(row, search));
} else {
out = table as E[];
}
const odw = order ? order[0] : null;
if (odw && odw.key === 'e_id' && odw.order === 'DESC') {
out = out.reverse();
}
if (!!limit && limit > 0) {
const off = offset ?? 0;
const end = off === 0 ? limit : limit + off;
return out.slice(off, end);
}
return out;
}
async findEntity<E extends CoreEntity>(
config: EntityConfig<E>,
search: QInterfaceSearch<E>,
): Promise<E | null> {
const table = this.e_map.get(config.className);
Iif (!table) {
return null;
}
return (table as E[]).find((row) => eFilter(row, search)) ?? null;
}
async countEntity<E extends IEntity>(
config: EntityConfig<E>,
search?: QInterfaceSearch<E>,
): Promise<number> {
const table = this.e_map.get(config.className);
Iif (!table) {
return 0;
}
if (!search) {
return table.length;
}
return (
(table as E[]).reduce(
(counter, row) => counter + (eFilter(row, search) ? 1 : 0),
0,
) ?? null
);
}
getRawDBObject(): any {
return {
e_map: this.e_map,
map: this.map,
};
}
async initNewDB(): Promise<void> {
await this.setConfig('dbversion', this.dbVersion);
}
async removeConfig(key: string): Promise<void> {
this.map.delete(key);
}
async setConfig(key: string, value: string): Promise<boolean> {
this.map.set(key, {
c_value: value,
c_key: key,
});
return true;
}
}
|