src/app/navigation/localStorageDb.ts
Properties |
|
Methods |
|
Accessors |
constructor(name)
|
||||
|
Defined in src/app/navigation/localStorageDb.ts:22
|
||||
|
Parameters :
|
| Private _collections |
Type : string
|
|
Defined in src/app/navigation/localStorageDb.ts:11
|
| Private cacheData |
Type : any
|
Default value : []
|
|
Defined in src/app/navigation/localStorageDb.ts:12
|
| Private options |
Type : object
|
Default value : {}
|
|
Defined in src/app/navigation/localStorageDb.ts:13
|
| Private prefix |
Type : string
|
Default value : 'stbui_'
|
|
Defined in src/app/navigation/localStorageDb.ts:14
|
| Private _getItem | ||||
_getItem(key)
|
||||
|
Defined in src/app/navigation/localStorageDb.ts:184
|
||||
|
Parameters :
Returns :
any
|
| Private _guid |
_guid()
|
|
Defined in src/app/navigation/localStorageDb.ts:201
|
|
Returns :
string
|
| Private _localStorage |
_localStorage()
|
|
Defined in src/app/navigation/localStorageDb.ts:179
|
|
Returns :
any
|
| Private _removeItem | ||||
_removeItem(key)
|
||||
|
Defined in src/app/navigation/localStorageDb.ts:195
|
||||
|
Parameters :
Returns :
this
|
| Private _setItem | ||||||
_setItem(key, value)
|
||||||
|
Defined in src/app/navigation/localStorageDb.ts:189
|
||||||
|
Parameters :
Returns :
this
|
| add | |||||||||||||||
add(data, options: object)
|
|||||||||||||||
|
Defined in src/app/navigation/localStorageDb.ts:77
|
|||||||||||||||
|
Parameters :
Returns :
void
|
| createTable |
createTable()
|
|
Defined in src/app/navigation/localStorageDb.ts:154
|
|
Returns :
void
|
| delete | ||||||||
delete(options: object)
|
||||||||
|
Defined in src/app/navigation/localStorageDb.ts:112
|
||||||||
|
Parameters :
Returns :
{}
|
| deserialize | ||||
deserialize(obj)
|
||||
|
Defined in src/app/navigation/localStorageDb.ts:166
|
||||
|
Parameters :
Returns :
any
|
| dropTable |
dropTable()
|
|
Defined in src/app/navigation/localStorageDb.ts:158
|
|
Returns :
void
|
| errorCode |
errorCode(method: string, message: string, code)
|
|
Defined in src/app/navigation/localStorageDb.ts:170
|
|
Returns :
{ code: number; method: string; message: string; }
|
| find |
find()
|
|
Defined in src/app/navigation/localStorageDb.ts:44
|
|
Returns :
any
|
| getItem |
getItem()
|
|
Defined in src/app/navigation/localStorageDb.ts:138
|
|
读取数据
Returns :
any
返回数据 |
| hasCollections |
hasCollections()
|
|
Defined in src/app/navigation/localStorageDb.ts:174
|
|
Returns :
boolean
|
| Private hasIntersect | ||||||
hasIntersect(a, b)
|
||||||
|
Defined in src/app/navigation/localStorageDb.ts:227
|
||||||
|
let a = { a: 1, b: 2, c: 3 }; let b = { a: 1, b: 2 };
Parameters :
Returns :
boolean
|
| select |
select()
|
|
Defined in src/app/navigation/localStorageDb.ts:66
|
|
Returns :
any
|
| serialize | ||||
serialize(obj)
|
||||
|
Defined in src/app/navigation/localStorageDb.ts:162
|
||||
|
Parameters :
Returns :
any
|
| setItem | ||||||
setItem(item)
|
||||||
|
Defined in src/app/navigation/localStorageDb.ts:149
|
||||||
|
serialize后,写入数据
Parameters :
Returns :
void
|
| update | ||||||||||||
update(data, options: object)
|
||||||||||||
|
Defined in src/app/navigation/localStorageDb.ts:87
|
||||||||||||
|
Parameters :
Returns :
any
|
| where | ||||||||
where(options: object)
|
||||||||
|
Defined in src/app/navigation/localStorageDb.ts:39
|
||||||||
|
Parameters :
Returns :
this
|
| collections | ||||
getcollections()
|
||||
|
Defined in src/app/navigation/localStorageDb.ts:20
|
||||
setcollections(value)
|
||||
|
Defined in src/app/navigation/localStorageDb.ts:16
|
||||
|
Parameters :
Returns :
void
|
export class localStorageDb {
private _collections: string;
private cacheData: any = [];
private options: object = {};
private prefix: string = 'stbui_';
set collections(value) {
this._collections = this.prefix + value;
}
get collections() {
return this._collections;
}
constructor(name) {
this.collections = name;
// this.add({ id: 1, name: 'stbui-1', createTime: 1234 });
// this.add({ id: 2, name: 'stbui-2', createTime: 1111 });
// this.where({ id: 2 }).update({ name: 'test' });
// this.where({ id: 2, name: 'stbui-2' }).update({ name: 'test' });
// this.where({ id: 1 }).delete();
// this.where({ id: 2 }).delete();
// this.where({ id: 2, name: 'stbui-2' }).delete();
// this.where({ id: 1 }).find();
// this.where({ id: 2, name: 'stbui-2' }).find();
// this.select();
}
where(options = {}) {
this.options = Object.assign(this.options, options);
return this;
}
find() {
const _options = this.options;
const _data = this.getItem();
if (!_data.length) {
return this.errorCode('find');
}
let result = [];
_data.forEach((element, index) => {
const intersect = this.hasIntersect(element, _options);
if (intersect) {
result = element;
}
});
this.setItem(result);
return _data;
}
select() {
const _options = this.options;
const _data = this.getItem();
return _data;
}
/**
*
* @param data 添加的数据
* @param options 选项
*/
add(data, options = {}) {
if (data instanceof Array) {
this.cacheData = data;
} else {
this.cacheData.push(data);
}
this.setItem(this.cacheData);
}
update(data, options = {}) {
const _options = this.options;
const _data = this.getItem();
if (!_data.length) {
return this.errorCode('update');
}
const result = _data.map(field => {
for (let i in _options) {
if (field[i] == _options[i]) {
for (let j in data) {
field[j] = data[j];
}
}
}
return field;
});
this.setItem(result);
return result;
}
delete(options = {}) {
const _options = this.options;
const _data = this.getItem();
if (!_data.length) {
return this.errorCode('delete');
}
let result = [];
_data.forEach((element, index) => {
const intersect = this.hasIntersect(element, _options);
if (intersect) {
result = [..._data.slice(0, index), ..._data.slice(index + 1)];
}
});
this.setItem(result);
return result;
}
/**
* 读取数据
* @return {object|Array} 返回数据
*/
getItem() {
const item = this._getItem(this.collections);
let _data = this.deserialize(item);
return _data;
}
/**
* serialize后,写入数据
* @param item
*/
setItem(item = this.cacheData) {
const _data = this.serialize(item);
localStorage.setItem(this.collections, _data);
}
createTable() {
this._setItem(this.collections, []);
}
dropTable() {
this._removeItem(this.collections);
}
serialize(obj) {
return JSON.stringify(obj);
}
deserialize(obj) {
return JSON.parse(obj);
}
errorCode(method = '', message = '', code = -1) {
return { code, method, message };
}
hasCollections() {
const item = this._getItem(this.collections);
return item ? true : false;
}
private _localStorage() {
const storage = localStorage;
return storage;
}
private _getItem(key) {
const db = this._localStorage();
return db.getItem(key);
}
private _setItem(key, value) {
const db = this._localStorage();
db.setItem(key, value);
return this;
}
private _removeItem(key) {
const db = this._localStorage();
db.removeItem(key);
return this;
}
private _guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return (
s4() +
s4() +
'-' +
s4() +
'-' +
s4() +
'-' +
s4() +
'-' +
s4() +
s4() +
s4()
);
}
/**
* let a = { a: 1, b: 2, c: 3 };
* let b = { a: 1, b: 2 };
*/
private hasIntersect(a, b) {
let isCommon = true;
for (let i in a) {
for (let j in b) {
if (isCommon && b[j] == a[j]) {
isCommon = true;
continue;
} else {
isCommon = false;
}
}
}
return isCommon;
}
}