Comparing version 1.0.2 to 1.0.3
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ const judel = require('../../index.js'); |
@@ -0,0 +0,0 @@ const Base = require('./libs/adaptor/base'); |
function AsyncStorage(RNAsyncStorage) { | ||
this.RNAsyncStorage = RNAsyncStorage; | ||
} | ||
AsyncStorage.prototype.setItem = async function (key = "", value = "") { | ||
await this.RNAsyncStorage.setItem(key, value); | ||
} | ||
this.setItem = async function (key = "", value = "") { | ||
await this.RNAsyncStorage.setItem(key, value); | ||
} | ||
AsyncStorage.prototype.getItem = async function (key = "") { | ||
return await this.RNAsyncStorage.getItem(key); | ||
} | ||
this.getItem = async function (key = "") { | ||
return await this.RNAsyncStorage.getItem(key); | ||
} | ||
AsyncStorage.prototype.removeItem = async function (key = "") { | ||
await this.RNAsyncStorage.removeItem(key); | ||
} | ||
this.removeItem = async function (key = "") { | ||
await this.RNAsyncStorage.removeItem(key); | ||
} | ||
AsyncStorage.prototype.getAllKeys = async function () { | ||
return await this.RNAsyncStorage.getAllKeys(); | ||
} | ||
this.getAllKeys = async function () { | ||
return await this.RNAsyncStorage.getAllKeys(); | ||
} | ||
AsyncStorage.prototype.multiGet = async function (keys = []) { | ||
return (await this.RNAsyncStorage.multiGet(keys)).map(x => x[1]); | ||
} | ||
this.multiGet = async function (keys = []) { | ||
return (await this.RNAsyncStorage.multiGet(keys)).map(x => x[1]); | ||
} | ||
AsyncStorage.prototype.multiRemove = async function (keys = []) { | ||
await this.RNAsyncStorage.multiRemove(keys); | ||
this.multiRemove = async function (keys = []) { | ||
await this.RNAsyncStorage.multiRemove(keys); | ||
} | ||
} | ||
module.exports = AsyncStorage; |
var storage = {}; | ||
function Adaptor() { | ||
this.setItem = async function (key = "", value = "") { | ||
storage[key] = value; | ||
} | ||
} | ||
this.getItem = async function (key = "") { | ||
return storage[key]; | ||
} | ||
Adaptor.prototype.setItem = async function (key = "", value = "") { | ||
storage[key] = value; | ||
} | ||
this.removeItem = async function (key = "") { | ||
storage[key] = null; | ||
delete storage[key]; | ||
} | ||
Adaptor.prototype.getItem = async function (key = "") { | ||
return storage[key]; | ||
} | ||
this.getAllKeys = async function () { | ||
return Object.keys(storage); | ||
} | ||
Adaptor.prototype.removeItem = async function (key = "") { | ||
storage[key] = null; | ||
delete storage[key]; | ||
} | ||
this.multiGet = async function (keys = []) { | ||
return keys.map(key => storage[key]); | ||
} | ||
Adaptor.prototype.getAllKeys = async function () { | ||
return Object.keys(storage); | ||
this.multiRemove = async function (keys = []) { | ||
keys.forEach(key => this.removeItem(key)); | ||
} | ||
} | ||
Adaptor.prototype.multiGet = async function (keys = []) { | ||
return keys.map(key => storage[key]); | ||
} | ||
Adaptor.prototype.multiRemove = async function (keys = []) { | ||
keys.forEach(key => this.removeItem(key)); | ||
} | ||
module.exports = Adaptor; |
@@ -0,0 +0,0 @@ function Base (name, { adaptor }) { |
@@ -5,10 +5,10 @@ const Repo = require('./repo'); | ||
this.adaptor = adaptor; | ||
} | ||
Factory.prototype.create = function (name) { | ||
return new Repo(name, { | ||
adaptor: this.adaptor, | ||
}); | ||
this.create = function (name) { | ||
return new Repo(name, { | ||
adaptor: this.adaptor, | ||
}); | ||
} | ||
} | ||
module.exports = Factory; |
@@ -6,78 +6,82 @@ const uuidv4 = require('uuid/v4'); | ||
Base.call(this, name, { adaptor }); | ||
} | ||
AsyncRepo.prototype.insert = async function (item = {}) { | ||
item.id = uuidv4(); | ||
await this.adaptor.setItem(this.name + item.id, JSON.stringify(item)); | ||
return item; | ||
} | ||
this.insert = async function (item = {}) { | ||
item.id = item.id || uuidv4(); | ||
await this.adaptor.setItem(this.name + item.id, JSON.stringify(item)); | ||
return item; | ||
} | ||
AsyncRepo.prototype.update = async function (item) { | ||
var old = await this.get(item.id); | ||
if (old) { | ||
Object.keys(item).forEach(key => { | ||
old[key] = item[key]; | ||
}); | ||
await this.adaptor.setItem(this.name + item.id, JSON.stringify(old)); | ||
return old; | ||
} else { | ||
return null; | ||
this.update = async function (item) { | ||
var old = await this.get(item.id); | ||
if (old) { | ||
Object.keys(item).forEach(key => { | ||
old[key] = item[key]; | ||
}); | ||
await this.adaptor.setItem(this.name + item.id, JSON.stringify(old)); | ||
return old; | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
AsyncRepo.prototype.upsert = async function (item) { | ||
if (item.id) | ||
await this.update(item); | ||
else | ||
await this.insert(item); | ||
} | ||
this.upsert = async function (item) { | ||
if (item.id) { | ||
if (await this.adaptor.getItem(this.name + item.id)) | ||
await this.update(item); | ||
else | ||
await this.insert(item); | ||
} else { | ||
await this.insert(item); | ||
} | ||
} | ||
AsyncRepo.prototype.remove = async function (id) { | ||
await this.adaptor.removeItem(this.name + id); | ||
} | ||
this.remove = async function (id) { | ||
await this.adaptor.removeItem(this.name + id); | ||
} | ||
AsyncRepo.prototype.get = async function (id) { | ||
var item = await this.adaptor.getItem(this.name + id); | ||
if (item) | ||
try { | ||
return JSON.parse(item); | ||
} catch (error) { | ||
return null; | ||
} | ||
else | ||
return null; | ||
} | ||
AsyncRepo.prototype.list = async function () { | ||
const keys = (await this.adaptor.getAllKeys()).filter( | ||
(key) => key.indexOf(this.name) === 0 | ||
); | ||
if (keys.length > 0) { | ||
var values = (await this.adaptor.multiGet(keys)); | ||
return values.map(value => { | ||
this.get = async function (id) { | ||
var item = await this.adaptor.getItem(this.name + id); | ||
if (item) | ||
try { | ||
return JSON.parse(value); | ||
return JSON.parse(item); | ||
} catch (error) { | ||
return null; | ||
} | ||
}).filter((x) => x); | ||
} else | ||
return []; | ||
} | ||
else | ||
return null; | ||
} | ||
AsyncRepo.prototype.find = async function (query) { | ||
return (await this.list()).filter(query); | ||
} | ||
this.list = async function () { | ||
const keys = (await this.adaptor.getAllKeys()).filter( | ||
(key) => key.indexOf(this.name) === 0 | ||
); | ||
if (keys.length > 0) { | ||
var values = (await this.adaptor.multiGet(keys)); | ||
return values.map(value => { | ||
try { | ||
return JSON.parse(value); | ||
} catch (error) { | ||
return null; | ||
} | ||
}).filter((x) => x); | ||
} else | ||
return []; | ||
} | ||
AsyncRepo.prototype.findOne = async function (query) { | ||
return (await this.list()).filter(query)[0]; | ||
} | ||
this.find = async function (query) { | ||
return (await this.list()).filter(query); | ||
} | ||
AsyncRepo.prototype.clear = async function () { | ||
var list = await this.list(); | ||
this.findOne = async function (query) { | ||
return (await this.list()).filter(query)[0]; | ||
} | ||
if (list.length) | ||
this.adaptor.multiRemove(list.map((x) => this.name + x.id)); | ||
this.clear = async function () { | ||
var list = await this.list(); | ||
if (list.length) | ||
this.adaptor.multiRemove(list.map((x) => this.name + x.id)); | ||
} | ||
} | ||
module.exports = AsyncRepo; |
{ | ||
"name": "judel", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "For javascript key/value storage like react-native async-storage.", | ||
@@ -22,2 +22,2 @@ "main": "index.js", | ||
} | ||
} | ||
} |
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
6719
184