typescriptstore
v1.0.2
Published
LocalStorage and SessionStorage for typescript
Downloads
20
Readme
tsStoreNPM
LocalStorage and SessionStorage for typescript
Installation
$ npm i typescriptstore
tsStoreNPM
requires Typescript v3.7+ to run.
How to use
Model need to inherit from TsStoreItem Object
import { TsStoreItem } from 'typescriptstore/lib/tsStoreItem';
export class UserStoreModel extends TsStoreItem {
public username: string = '';
public name: string = '';
public age: number = 0;
}
export class ProductStoreModel extends TsStoreItem {
public productType: string = '';
public productName: string = '';
}
Import relative objects
import { UserStoreModel, ProductStoreModel } from './model/testModel';
import { TsStore } from 'typescriptstore/lib/tsStore';
import { TsStoreQueryType } from 'typescriptstore/lib/queryTypes';
Init store to use
let userStore: TsStore = TsStore.getStore('userStore');
Insert or update item
let item: UserStoreModel = new UserStoreModel();
item.username = 'user' + i;
item.name = 'USER' + i;
item.age = i + 2;
item = userStore.insertOrUpdate(item)
Remove item by id
userStore.remove(item.storeItemId);
Clear all items in store
userStore.clear();
Find item by query condition base on #QueryType
let findItems: UserStoreModel[] = userStore.find('age', 2, TsStoreQueryType.Equal);
QueryType
export enum TsStoreQueryType {
Equal,
NotEqual,
GreaterThan,
GreaterThanOrEqual,
LessThan,
LessThanOrEqual
}
Get all items in store
let allItems: UserStoreModel[] = userStore.all();
Get first item in store
let fistItem: UserStoreModel = userStore.first();
Get last item in store
let lastItem: UserStoreModel = userStore.last();
Download & run demo for more clearly(Angular2+ required).
$ cd tsStoreDemo
$ npm install
$ npm start
License
MIT