🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@furystack/core

Package Overview
Dependencies
Maintainers
1
Versions
229
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@furystack/core - npm Package Compare versions

Comparing version

to
14.0.2

36

esm/create-physical-store-tests.js
import { usingAsync } from '@furystack/utils';
import { Injector } from '@furystack/inject';
import { describe, it, expect } from 'vitest';
import { describe, it, expect, vi } from 'vitest';
export class TestClass {

@@ -29,3 +29,5 @@ }

await usingAsync(new Injector(), async (i) => {
const onAddListener = vi.fn();
const store = options.createStore(i);
store.addListener('onEntityAdded', onAddListener);
const entity = createMockEntity();

@@ -35,2 +37,4 @@ await store.add(entity);

expect(count).toBe(1);
expect(onAddListener).toHaveBeenCalledTimes(1);
expect(onAddListener).toHaveBeenCalledWith({ entity });
});

@@ -40,3 +44,5 @@ });

await usingAsync(new Injector(), async (i) => {
const onAddListener = vi.fn();
const store = options.createStore(i);
store.addListener('onEntityAdded', onAddListener);
const { id, ...entityWithoutId } = createMockEntity();

@@ -49,2 +55,4 @@ const { created } = await store.add(entityWithoutId);

expect(retrieved).toEqual(created[0]);
expect(onAddListener).toHaveBeenCalledTimes(1);
expect(onAddListener).toHaveBeenCalledWith({ entity: created[0] });
});

@@ -54,3 +62,5 @@ });

await usingAsync(new Injector(), async (i) => {
const onAddListener = vi.fn();
const store = options.createStore(i);
store.addListener('onEntityAdded', onAddListener);
const entity1 = createMockEntity();

@@ -61,2 +71,5 @@ const entity2 = createMockEntity();

expect(count).toBe(2);
expect(onAddListener).toHaveBeenCalledTimes(2);
expect(onAddListener).toHaveBeenCalledWith({ entity: entity1 });
expect(onAddListener).toHaveBeenCalledWith({ entity: entity2 });
});

@@ -66,3 +79,5 @@ });

await usingAsync(new Injector(), async (i) => {
const onAddListener = vi.fn();
const store = options.createStore(i);
store.addListener('onEntityAdded', onAddListener);
const entity = createMockEntity();

@@ -73,2 +88,4 @@ await store.add(entity);

expect(count).toBe(1);
expect(onAddListener).toHaveBeenCalledTimes(1);
expect(onAddListener).toHaveBeenCalledWith({ entity });
});

@@ -106,3 +123,5 @@ });

await usingAsync(new Injector(), async (i) => {
const updateListener = vi.fn();
const store = options.createStore(i);
store.addListener('onEntityUpdated', updateListener);
const entity = createMockEntity();

@@ -113,2 +132,4 @@ await store.add(entity);

expect(retrieved?.stringValue1).toEqual('modified');
expect(updateListener).toHaveBeenCalledTimes(1);
expect(updateListener).toHaveBeenCalledWith({ id: entity.id, change: { stringValue1: 'modified' } });
});

@@ -118,5 +139,8 @@ });

await usingAsync(new Injector(), async (i) => {
const updateListener = vi.fn();
const store = options.createStore(i);
store.addListener('onEntityUpdated', updateListener);
const entity = createMockEntity();
await expect(store.update(entity.id, entity)).rejects.toThrow('Entity not found');
expect(updateListener).not.toHaveBeenCalled();
});

@@ -126,3 +150,5 @@ });

await usingAsync(new Injector(), async (i) => {
const removeListener = vi.fn();
const store = options.createStore(i);
store.addListener('onEntityRemoved', removeListener);
const entity = createMockEntity();

@@ -135,2 +161,3 @@ await store.add(entity);

expect(countAferDelete).toBe(0);
expect(removeListener).toHaveBeenCalledTimes(1);
});

@@ -140,3 +167,5 @@ });

await usingAsync(new Injector(), async (i) => {
const removeListener = vi.fn();
const store = options.createStore(i);
store.addListener('onEntityRemoved', removeListener);
const entity1 = createMockEntity();

@@ -151,5 +180,10 @@ const entity2 = createMockEntity();

expect(countAferDelete).toBe(1);
expect(removeListener).toHaveBeenCalledTimes(2);
expect(removeListener).toHaveBeenCalledWith({ key: entity1.id });
expect(removeListener).toHaveBeenCalledWith({ key: entity2.id });
await store.remove(entity3.id);
const countAferDeleteAll = await store.count();
expect(countAferDeleteAll).toBe(0);
expect(removeListener).toHaveBeenCalledTimes(3);
expect(removeListener).toHaveBeenCalledWith({ key: entity3.id });
});

@@ -156,0 +190,0 @@ });

import type { Constructable } from '@furystack/inject';
import type { PhysicalStore, FindOptions, PartialResult, FilterType, CreateResult } from './models/physical-store.js';
export declare class InMemoryStore<T, TPrimaryKey extends keyof T> implements PhysicalStore<T, TPrimaryKey, T> {
import { EventHub } from '@furystack/utils';
export declare class InMemoryStore<T, TPrimaryKey extends keyof T> extends EventHub<{
onEntityAdded: {
entity: T;
};
onEntityUpdated: {
id: T[TPrimaryKey];
change: Partial<T>;
};
onEntityRemoved: {
key: T[TPrimaryKey];
};
}> implements PhysicalStore<T, TPrimaryKey, T> {
/**

@@ -5,0 +17,0 @@ *

import { selectFields, isOperator, isLogicalOperator } from './models/physical-store.js';
export class InMemoryStore {
import { EventHub } from '@furystack/utils';
export class InMemoryStore extends EventHub {
/**

@@ -8,3 +9,6 @@ *

async remove(...keys) {
keys.map((key) => this.cache.delete(key));
keys.forEach((key) => {
this.cache.delete(key);
this.emit('onEntityRemoved', { key });
});
}

@@ -18,2 +22,3 @@ async add(...entries) {

this.cache.set(entry[this.primaryKey], entry);
this.emit('onEntityAdded', { entity: entry });
return entry;

@@ -172,2 +177,3 @@ });

});
this.emit('onEntityUpdated', { id, change: data });
}

@@ -186,2 +192,3 @@ dispose() {

constructor(options) {
super();
this.primaryKey = options.primaryKey;

@@ -188,0 +195,0 @@ this.model = options.model;

import type { Constructable } from '@furystack/inject';
import type { Disposable } from '@furystack/utils';
import type { EventHub } from '@furystack/utils';
export declare const NumberComparisonOperators: readonly ["$gt", "$gte", "$lt", "$lte"];

@@ -67,3 +67,14 @@ export declare const StringComparisonOperators: readonly ["$startsWith", "$endsWith", "$like", "$regex"];

*/
export interface PhysicalStore<T, TPrimaryKey extends keyof T, TWriteableData = WithOptionalId<T, TPrimaryKey>> extends Disposable {
export interface PhysicalStore<T, TPrimaryKey extends keyof T, TWriteableData = WithOptionalId<T, TPrimaryKey>> extends EventHub<{
onEntityAdded: {
entity: T;
};
onEntityUpdated: {
id: T[TPrimaryKey];
change: Partial<T>;
};
onEntityRemoved: {
key: T[TPrimaryKey];
};
}> {
/**

@@ -70,0 +81,0 @@ * The Primary key field name

2

esm/store-manager.d.ts

@@ -21,3 +21,3 @@ import type { Constructable } from '@furystack/inject';

*/
getStoreFor<T, TPrimaryKey extends keyof T, TType extends PhysicalStore<T, TPrimaryKey> = PhysicalStore<T, TPrimaryKey>>(model: Constructable<T>, primaryKey: TPrimaryKey): TType;
getStoreFor<T, TPrimaryKey extends keyof T, TType extends PhysicalStore<T, TPrimaryKey>>(model: Constructable<T>, primaryKey: TPrimaryKey): TType;
/**

@@ -24,0 +24,0 @@ * Adds a store instance to the StoreManager class

{
"name": "@furystack/core",
"version": "14.0.1",
"version": "14.0.2",
"description": "Core FuryStack package",

@@ -43,11 +43,11 @@ "type": "module",

"devDependencies": {
"@types/node": "^20.11.30",
"typescript": "^5.4.3",
"vitest": "^1.4.0"
"@types/node": "^20.12.7",
"typescript": "^5.4.5",
"vitest": "^1.5.0"
},
"dependencies": {
"@furystack/inject": "^11.0.0",
"@furystack/utils": "^6.0.1"
"@furystack/inject": "^11.0.1",
"@furystack/utils": "^7.0.0"
},
"gitHead": "1045d854bfd8c475b7035471d130d401417a2321"
}
import type { PhysicalStore } from './models/physical-store.js'
import { usingAsync } from '@furystack/utils'
import { Injector } from '@furystack/inject'
import { describe, it, expect } from 'vitest'
import { describe, it, expect, vi } from 'vitest'

@@ -48,3 +48,8 @@ export class TestClass {

await usingAsync(new Injector(), async (i) => {
const onAddListener = vi.fn()
const store = options.createStore(i)
store.addListener('onEntityAdded', onAddListener)
const entity = createMockEntity()

@@ -54,2 +59,5 @@ await store.add(entity)

expect(count).toBe(1)
expect(onAddListener).toHaveBeenCalledTimes(1)
expect(onAddListener).toHaveBeenCalledWith({ entity })
})

@@ -60,4 +68,9 @@ })

await usingAsync(new Injector(), async (i) => {
const onAddListener = vi.fn()
const store = options.createStore(i)
store.addListener('onEntityAdded', onAddListener)
const { id, ...entityWithoutId } = createMockEntity()
const { created } = await store.add(entityWithoutId)

@@ -69,2 +82,5 @@ expect(created.length).toBe(1)

expect(retrieved).toEqual(created[0])
expect(onAddListener).toHaveBeenCalledTimes(1)
expect(onAddListener).toHaveBeenCalledWith({ entity: created[0] })
})

@@ -75,3 +91,6 @@ })

await usingAsync(new Injector(), async (i) => {
const onAddListener = vi.fn()
const store = options.createStore(i)
store.addListener('onEntityAdded', onAddListener)
const entity1 = createMockEntity()

@@ -82,2 +101,6 @@ const entity2 = createMockEntity()

expect(count).toBe(2)
expect(onAddListener).toHaveBeenCalledTimes(2)
expect(onAddListener).toHaveBeenCalledWith({ entity: entity1 })
expect(onAddListener).toHaveBeenCalledWith({ entity: entity2 })
})

@@ -88,3 +111,8 @@ })

await usingAsync(new Injector(), async (i) => {
const onAddListener = vi.fn()
const store = options.createStore(i)
store.addListener('onEntityAdded', onAddListener)
const entity = createMockEntity()

@@ -95,2 +123,5 @@ await store.add(entity)

expect(count).toBe(1)
expect(onAddListener).toHaveBeenCalledTimes(1)
expect(onAddListener).toHaveBeenCalledWith({ entity })
})

@@ -132,3 +163,8 @@ })

await usingAsync(new Injector(), async (i) => {
const updateListener = vi.fn()
const store = options.createStore(i)
store.addListener('onEntityUpdated', updateListener)
const entity = createMockEntity()

@@ -139,2 +175,5 @@ await store.add(entity)

expect(retrieved?.stringValue1).toEqual('modified')
expect(updateListener).toHaveBeenCalledTimes(1)
expect(updateListener).toHaveBeenCalledWith({ id: entity.id, change: { stringValue1: 'modified' } })
})

@@ -145,5 +184,12 @@ })

await usingAsync(new Injector(), async (i) => {
const updateListener = vi.fn()
const store = options.createStore(i)
store.addListener('onEntityUpdated', updateListener)
const entity = createMockEntity()
await expect(store.update(entity.id, entity)).rejects.toThrow('Entity not found')
expect(updateListener).not.toHaveBeenCalled()
})

@@ -154,3 +200,7 @@ })

await usingAsync(new Injector(), async (i) => {
const removeListener = vi.fn()
const store = options.createStore(i)
store.addListener('onEntityRemoved', removeListener)
const entity = createMockEntity()

@@ -163,2 +213,4 @@ await store.add(entity)

expect(countAferDelete).toBe(0)
expect(removeListener).toHaveBeenCalledTimes(1)
})

@@ -169,3 +221,6 @@ })

await usingAsync(new Injector(), async (i) => {
const removeListener = vi.fn()
const store = options.createStore(i)
store.addListener('onEntityRemoved', removeListener)
const entity1 = createMockEntity()

@@ -180,5 +235,13 @@ const entity2 = createMockEntity()

expect(countAferDelete).toBe(1)
expect(removeListener).toHaveBeenCalledTimes(2)
expect(removeListener).toHaveBeenCalledWith({ key: entity1.id })
expect(removeListener).toHaveBeenCalledWith({ key: entity2.id })
await store.remove(entity3.id)
const countAferDeleteAll = await store.count()
expect(countAferDeleteAll).toBe(0)
expect(removeListener).toHaveBeenCalledTimes(3)
expect(removeListener).toHaveBeenCalledWith({ key: entity3.id })
})

@@ -185,0 +248,0 @@ })

import type { Constructable } from '@furystack/inject'
import type { PhysicalStore, FindOptions, PartialResult, FilterType, CreateResult } from './models/physical-store.js'
import { selectFields, isOperator, isLogicalOperator } from './models/physical-store.js'
import { EventHub } from '@furystack/utils'
export class InMemoryStore<T, TPrimaryKey extends keyof T> implements PhysicalStore<T, TPrimaryKey, T> {
export class InMemoryStore<T, TPrimaryKey extends keyof T>
extends EventHub<{
onEntityAdded: { entity: T }
onEntityUpdated: { id: T[TPrimaryKey]; change: Partial<T> }
onEntityRemoved: { key: T[TPrimaryKey] }
}>
implements PhysicalStore<T, TPrimaryKey, T>
{
/**

@@ -11,3 +19,6 @@ *

public async remove(...keys: Array<T[TPrimaryKey]>): Promise<void> {
keys.map((key) => this.cache.delete(key))
keys.forEach((key) => {
this.cache.delete(key)
this.emit('onEntityRemoved', { key })
})
}

@@ -22,2 +33,3 @@

this.cache.set(entry[this.primaryKey], entry)
this.emit('onEntityAdded', { entity: entry })
return entry

@@ -182,2 +194,3 @@ })

})
this.emit('onEntityUpdated', { id, change: data })
}

@@ -208,2 +221,3 @@

}) {
super()
this.primaryKey = options.primaryKey

@@ -210,0 +224,0 @@ this.model = options.model

import type { Constructable } from '@furystack/inject'
import type { Disposable } from '@furystack/utils'
import type { EventHub } from '@furystack/utils'

@@ -92,3 +92,7 @@ export const NumberComparisonOperators = ['$gt', '$gte', '$lt', '$lte'] as const

export interface PhysicalStore<T, TPrimaryKey extends keyof T, TWriteableData = WithOptionalId<T, TPrimaryKey>>
extends Disposable {
extends EventHub<{
onEntityAdded: { entity: T }
onEntityUpdated: { id: T[TPrimaryKey]; change: Partial<T> }
onEntityRemoved: { key: T[TPrimaryKey] }
}> {
/**

@@ -95,0 +99,0 @@ * The Primary key field name

@@ -36,7 +36,6 @@ import type { Constructable } from '@furystack/inject'

*/
public getStoreFor<
T,
TPrimaryKey extends keyof T,
TType extends PhysicalStore<T, TPrimaryKey> = PhysicalStore<T, TPrimaryKey>,
>(model: Constructable<T>, primaryKey: TPrimaryKey) {
public getStoreFor<T, TPrimaryKey extends keyof T, TType extends PhysicalStore<T, TPrimaryKey>>(
model: Constructable<T>,
primaryKey: TPrimaryKey,
) {
const instance = this.stores.get(model)

@@ -43,0 +42,0 @@ if (!instance) {

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