@furystack/core
Advanced tools
Comparing version 5.0.1 to 5.0.2
@@ -5,7 +5,7 @@ /// <reference types="node" /> | ||
import { Logger } from '@furystack/logging'; | ||
import { DefaultFilter, PhysicalStore } from './Models/PhysicalStore'; | ||
import { PhysicalStore, SearchOptions } from './Models/PhysicalStore'; | ||
/** | ||
* Store implementation that stores info in a simple JSON file | ||
*/ | ||
export declare class FileStore<T> implements PhysicalStore<T, DefaultFilter<T>> { | ||
export declare class FileStore<T> implements PhysicalStore<T> { | ||
private readonly options; | ||
@@ -21,3 +21,3 @@ private readonly watcher?; | ||
add(data: T): Promise<T>; | ||
filter: (filter: DefaultFilter<T>) => Promise<T[]>; | ||
search<TFields extends Array<keyof T>>(filter: SearchOptions<T, TFields>): Promise<T[]>; | ||
count(): Promise<number>; | ||
@@ -24,0 +24,0 @@ private fileLock; |
@@ -20,12 +20,2 @@ "use strict"; | ||
}; | ||
this.filter = async (filter) => { | ||
return [...this.cache.values()].filter(item => { | ||
for (const key in filter.filter) { | ||
if (filter.filter[key] !== item[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
}; | ||
this.fileLock = new semaphore_async_await_1.default(1); | ||
@@ -64,2 +54,13 @@ this.readFile = fs_1.readFile; | ||
} | ||
async search(filter) { | ||
// ToDo: Top, skip, order (from InMemoryStore) | ||
return [...this.cache.values()].filter(item => { | ||
for (const key in filter.filter) { | ||
if (filter.filter[key] !== item[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
} | ||
async count() { | ||
@@ -66,0 +67,0 @@ return await this.fileLock.execute(async () => { |
@@ -6,2 +6,3 @@ "use strict"; | ||
tslib_1.__exportStar(require("./ServerManager"), exports); | ||
tslib_1.__exportStar(require("./Models/PhysicalStore"), exports); | ||
tslib_1.__exportStar(require("./Models/User"), exports); | ||
@@ -8,0 +9,0 @@ tslib_1.__exportStar(require("./FileStore"), exports); |
import { Constructable } from '@furystack/inject'; | ||
import { DefaultFilter, PhysicalStore } from './Models/PhysicalStore'; | ||
import { PhysicalStore, SearchOptions, PartialResult } from './Models/PhysicalStore'; | ||
/** | ||
* Store implementation that stores data in an in-memory cache | ||
*/ | ||
export declare class InMemoryStore<T> implements PhysicalStore<T, Partial<T>> { | ||
export declare class InMemoryStore<T> implements PhysicalStore<T> { | ||
remove(key: T[this['primaryKey']]): Promise<void>; | ||
@@ -11,3 +11,3 @@ add(data: T): Promise<T>; | ||
get: (key: T[this["primaryKey"]]) => Promise<T | undefined>; | ||
filter: (filter: DefaultFilter<T>) => Promise<T[]>; | ||
search<TFields extends Array<keyof T>>(filter: SearchOptions<T, TFields>): Promise<PartialResult<T, TFields[number]>[]>; | ||
count(): Promise<number>; | ||
@@ -14,0 +14,0 @@ update(id: T[this['primaryKey']], data: T): Promise<void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const PhysicalStore_1 = require("./Models/PhysicalStore"); | ||
/** | ||
@@ -14,12 +15,2 @@ * Store implementation that stores data in an in-memory cache | ||
this.get = async (key) => this.cache.get(key); | ||
this.filter = async (filter) => { | ||
return [...this.cache.values()].filter(item => { | ||
for (const key in filter.filter) { | ||
if (filter.filter[key] !== item[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
}; | ||
this.primaryKey = options.primaryKey; | ||
@@ -38,2 +29,24 @@ this.model = options.model; | ||
} | ||
async search(filter) { | ||
let value = [...this.cache.values()].filter(item => { | ||
for (const key in filter.filter) { | ||
if (filter.filter[key] !== item[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
if (filter.order) { | ||
// ToDo | ||
} | ||
if (filter.top || filter.skip) { | ||
value = value.slice(filter.skip, (filter.skip || 0) + (filter.top || 0)); | ||
} | ||
if (filter.select !== undefined) { | ||
value = value.map(item => { | ||
return PhysicalStore_1.selectFields(item, ...filter.select); | ||
}); | ||
} | ||
return value; | ||
} | ||
async count() { | ||
@@ -40,0 +53,0 @@ return this.cache.size; |
@@ -6,3 +6,3 @@ import { Constructable } from '@furystack/inject'; | ||
*/ | ||
export interface DefaultFilter<T> { | ||
export interface SearchOptions<T, TSelect extends Array<keyof T>> { | ||
/** | ||
@@ -25,3 +25,3 @@ * Limits the hits | ||
*/ | ||
select?: Array<keyof T>; | ||
select?: TSelect; | ||
/** | ||
@@ -32,6 +32,10 @@ * The fields should match this filter | ||
} | ||
export declare type PartialResult<T, TFields extends keyof T> = { | ||
[K in TFields]: T[K]; | ||
}; | ||
export declare const selectFields: <T, TField extends (keyof T)[]>(entry: T, ...fields: TField) => PartialResult<T, TField[number]>; | ||
/** | ||
* Interface that defines a physical store implementation | ||
*/ | ||
export interface PhysicalStore<T, TFilter = DefaultFilter<T>> extends Disposable { | ||
export interface PhysicalStore<T> extends Disposable { | ||
/** | ||
@@ -64,3 +68,3 @@ * The Primary key field name | ||
*/ | ||
filter(filter: TFilter): Promise<T[]>; | ||
search<TSelect extends Array<keyof T>>(filter: SearchOptions<T, TSelect>): Promise<Array<PartialResult<T, TSelect[number]>>>; | ||
/** | ||
@@ -67,0 +71,0 @@ * Returns a promise that will be resolved with an entry with the defined primary key or undefined |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.selectFields = (entry, ...fields) => { | ||
const returnValue = {}; | ||
Object.keys(entry).map(key => { | ||
const field = key; | ||
if (fields.includes(field)) { | ||
returnValue[field] = entry[field]; | ||
} | ||
}); | ||
return returnValue; | ||
}; | ||
//# sourceMappingURL=PhysicalStore.js.map |
import { Constructable, Injector } from '@furystack/inject'; | ||
import { Disposable } from '@sensenet/client-utils'; | ||
import { DefaultFilter, PhysicalStore } from './Models/PhysicalStore'; | ||
import { PhysicalStore } from './Models/PhysicalStore'; | ||
/** | ||
@@ -19,3 +19,3 @@ * Manager class for store instances | ||
*/ | ||
getStoreFor<T>(model: Constructable<T>): PhysicalStore<T, DefaultFilter<T>>; | ||
getStoreFor<T>(model: Constructable<T>): PhysicalStore<T>; | ||
/** | ||
@@ -26,3 +26,3 @@ * Adds a store instance to the StoreManager class | ||
*/ | ||
addStore<T>(store: PhysicalStore<T, DefaultFilter<T>>): this; | ||
addStore<T>(store: PhysicalStore<T>): this; | ||
private logger; | ||
@@ -29,0 +29,0 @@ constructor(injector: Injector); |
{ | ||
"name": "@furystack/core", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "Core FuryStack package", | ||
@@ -59,3 +59,3 @@ "main": "dist/index.js", | ||
"typings": "./dist/index.d.ts", | ||
"gitHead": "b06043e4b53a8389175682728d9cfe6987f976bc" | ||
"gitHead": "b9b26c447d042bd74ad48b70e21e1bf79e6cae34" | ||
} |
@@ -5,3 +5,3 @@ import { FSWatcher, readFile as nodeReadFile, watch, writeFile as nodeWriteFile } from 'fs' | ||
import Semaphore from 'semaphore-async-await' | ||
import { DefaultFilter, PhysicalStore } from './Models/PhysicalStore' | ||
import { PhysicalStore, SearchOptions } from './Models/PhysicalStore' | ||
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
export class FileStore<T> implements PhysicalStore<T, DefaultFilter<T>> { | ||
export class FileStore<T> implements PhysicalStore<T> { | ||
private readonly watcher?: FSWatcher | ||
@@ -41,3 +41,4 @@ | ||
public filter = async (filter: DefaultFilter<T>) => { | ||
public async search<TFields extends Array<keyof T>>(filter: SearchOptions<T, TFields>) { | ||
// ToDo: Top, skip, order (from InMemoryStore) | ||
return [...this.cache.values()].filter(item => { | ||
@@ -44,0 +45,0 @@ for (const key in filter.filter) { |
import { Constructable } from '@furystack/inject' | ||
import { DefaultFilter, PhysicalStore } from './Models/PhysicalStore' | ||
import { PhysicalStore, SearchOptions, selectFields, PartialResult } from './Models/PhysicalStore' | ||
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
export class InMemoryStore<T> implements PhysicalStore<T, Partial<T>> { | ||
export class InMemoryStore<T> implements PhysicalStore<T> { | ||
public async remove(key: T[this['primaryKey']]): Promise<void> { | ||
@@ -24,4 +24,4 @@ this.cache.delete(key) | ||
public filter = async (filter: DefaultFilter<T>) => { | ||
return [...this.cache.values()].filter(item => { | ||
public async search<TFields extends Array<keyof T>>(filter: SearchOptions<T, TFields>) { | ||
let value: Array<PartialResult<T, TFields[number]>> = [...this.cache.values()].filter(item => { | ||
for (const key in filter.filter) { | ||
@@ -34,2 +34,18 @@ if ((filter.filter as any)[key] !== (item as any)[key]) { | ||
}) | ||
if (filter.order) { | ||
// ToDo | ||
} | ||
if (filter.top || filter.skip) { | ||
value = value.slice(filter.skip, (filter.skip || 0) + (filter.top || 0)) | ||
} | ||
if (filter.select !== undefined) { | ||
value = value.map(item => { | ||
return selectFields(item, ...(filter.select as Array<keyof T>)) | ||
}) | ||
} | ||
return value | ||
} | ||
@@ -36,0 +52,0 @@ |
@@ -7,3 +7,3 @@ import { Constructable } from '@furystack/inject' | ||
*/ | ||
export interface DefaultFilter<T> { | ||
export interface SearchOptions<T, TSelect extends Array<keyof T>> { | ||
/** | ||
@@ -27,3 +27,3 @@ * Limits the hits | ||
*/ | ||
select?: Array<keyof T> | ||
select?: TSelect | ||
@@ -36,6 +36,19 @@ /** | ||
export type PartialResult<T, TFields extends keyof T> = { [K in TFields]: T[K] } | ||
export const selectFields = <T, TField extends Array<keyof T>>(entry: T, ...fields: TField) => { | ||
const returnValue: PartialResult<T, TField[number]> = {} as any | ||
Object.keys(entry).map(key => { | ||
const field: TField[number] = key as TField[number] | ||
if (fields.includes(field)) { | ||
returnValue[field] = entry[field] | ||
} | ||
}) | ||
return returnValue | ||
} | ||
/** | ||
* Interface that defines a physical store implementation | ||
*/ | ||
export interface PhysicalStore<T, TFilter = DefaultFilter<T>> extends Disposable { | ||
export interface PhysicalStore<T> extends Disposable { | ||
/** | ||
@@ -73,3 +86,5 @@ * The Primary key field name | ||
*/ | ||
filter(filter: TFilter): Promise<T[]> | ||
search<TSelect extends Array<keyof T>>( | ||
filter: SearchOptions<T, TSelect>, | ||
): Promise<Array<PartialResult<T, TSelect[number]>>> | ||
@@ -76,0 +91,0 @@ /** |
import { Constructable, Injectable, Injector } from '@furystack/inject' | ||
import { ScopedLogger } from '@furystack/logging' | ||
import { Disposable } from '@sensenet/client-utils' | ||
import { DefaultFilter, PhysicalStore } from './Models/PhysicalStore' | ||
import { PhysicalStore } from './Models/PhysicalStore' | ||
@@ -49,3 +49,3 @@ /** | ||
*/ | ||
public addStore<T>(store: PhysicalStore<T, DefaultFilter<T>>) { | ||
public addStore<T>(store: PhysicalStore<T>) { | ||
this.stores.set(store.model, store) | ||
@@ -52,0 +52,0 @@ return this |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75121
1157