Comparing version 0.0.3 to 0.0.4
@@ -97,2 +97,4 @@ module.exports = | ||
var jobCounter = 0; | ||
function createDatastore() { | ||
@@ -155,5 +157,10 @@ return { | ||
var deleteMethods = Object.keys(api.api).filter(function (x) { | ||
return api.api[x].type === 'DELETE'; | ||
}); | ||
deleteMethods = createObject(deleteMethods, deleteMethods.map(decorateDelete(api, middleware))); | ||
return _extends({ | ||
_name: api.name | ||
}, readMethods, writeMethods); | ||
}, readMethods, writeMethods, deleteMethods); | ||
}; | ||
@@ -205,3 +212,3 @@ } | ||
function bindPreWrite(hook, apiName, toDecorate, toDecorateName, middleware) { | ||
function bindPreWrite(hook, apiName, toDecorate, toDecorateName, jobId, middleware) { | ||
var _middleware2 = _toArray(middleware); | ||
@@ -217,5 +224,5 @@ | ||
return toDecorate.apply(undefined, _toConsumableArray(args)); | ||
}, apiName, toDecorateName, methodMeta); | ||
}, apiName, toDecorateName, methodMeta, jobId); | ||
} else { | ||
return x.middleware[hook].bind(null, bindPreWrite(hook, apiName, toDecorate, toDecorateName, xs), apiName, toDecorateName, methodMeta); | ||
return x.middleware[hook].bind(null, bindPreWrite(hook, apiName, toDecorate, toDecorateName, jobId, xs), apiName, toDecorateName, methodMeta, jobId); | ||
} | ||
@@ -231,3 +238,5 @@ } | ||
var preWrite = bindPreWrite('preWrite', api.name, api.api[toDecorate], toDecorate, middleware.filter(function (x) { | ||
var jobId = jobCounter++; | ||
var preWrite = bindPreWrite('preWrite', api.name, api.api[toDecorate], toDecorate, jobId, middleware.filter(function (x) { | ||
return x.middleware.preWrite; | ||
@@ -242,3 +251,3 @@ })); | ||
postWriteMiddleware.forEach(function (x) { | ||
return x.middleware.postWrite(api.name, toDecorate, methodMeta, args, result); | ||
return x.middleware.postWrite(api.name, toDecorate, methodMeta, jobId, args, result); | ||
}); | ||
@@ -252,2 +261,47 @@ return result.then(function (x) { | ||
function bindPreDelete(hook, apiName, toDecorate, toDecorateName, jobId, middleware) { | ||
var _middleware3 = _toArray(middleware); | ||
var x = _middleware3[0]; | ||
var xs = _middleware3.slice(1); | ||
var methodMeta = createMethodMeta(toDecorate); | ||
if (xs.length === 0) { | ||
return x.middleware[hook].bind(null, function (args) { | ||
return toDecorate.apply(undefined, _toConsumableArray(args)); | ||
}, apiName, toDecorateName, methodMeta, jobId); | ||
} else { | ||
return x.middleware[hook].bind(null, bindPreWrite(hook, apiName, toDecorate, toDecorateName, jobId, xs), apiName, toDecorateName, methodMeta, jobId); | ||
} | ||
} | ||
function decorateDelete(api, middleware) { | ||
return function (toDecorate) { | ||
return function () { | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
var jobId = jobCounter++; | ||
var preDelete = bindPreDelete('preDelete', api.name, api.api[toDecorate], toDecorate, jobId, middleware.filter(function (x) { | ||
return x.middleware.preDelete; | ||
})); | ||
var result = preDelete(args); | ||
var postDeleteMiddleware = middleware.filter(function (x) { | ||
return x.middleware.postDelete; | ||
}); | ||
var methodMeta = createMethodMeta(api.api[toDecorate]); | ||
postDeleteMiddleware.forEach(function (x) { | ||
return x.middleware.postDelete(api.name, toDecorate, methodMeta, jobId, args, result); | ||
}); | ||
return result.then(function (x) { | ||
return x.data; | ||
}); | ||
}; | ||
}; | ||
} | ||
function createMethodMeta(method) { | ||
@@ -281,2 +335,4 @@ return { | ||
exports.postWrite = postWrite; | ||
exports.preDelete = preDelete; | ||
exports.postDelete = postDelete; | ||
function preRead(next, apiName, toDecorateName, multipleEntities, args) { | ||
@@ -286,3 +342,3 @@ return next(args); | ||
function preWrite(next, apiName, toDecorateName, multipleEntities, args) { | ||
function preWrite(next, apiName, methodName, methodMeta, jobId, args) { | ||
return next(args); | ||
@@ -297,2 +353,10 @@ } | ||
function preDelete(next, apiName, methodName, methodMeta, jobId, args) { | ||
return next(args); | ||
} | ||
function postDelete(promise) { | ||
return promise; | ||
} | ||
/***/ }, | ||
@@ -319,2 +383,3 @@ /* 3 */ | ||
}; | ||
var temporaryIds = {}; | ||
@@ -325,3 +390,5 @@ return { | ||
preWrite: preWrite, | ||
postWrite: postWrite | ||
postWrite: postWrite, | ||
postDelete: postDelete, | ||
preDelete: preDelete | ||
}; | ||
@@ -351,5 +418,5 @@ | ||
function preWrite(next, apiName, methodName, methodMeta, args) { | ||
function preWrite(next, apiName, methodName, methodMeta, jobId, args) { | ||
var methodType = methodMeta.entity; | ||
var key = createKey(apiName, methodName, methodMeta.multipleEntities, args, methodType); | ||
var key = createKey(apiName, methodName, methodMeta.multipleEntities, args, methodType, jobId); | ||
saveToCache(key, { | ||
@@ -361,7 +428,54 @@ data: args[0] | ||
function postWrite(apiName, methodName, methodMeta, args, promise) { | ||
function postWrite(apiName, methodName, methodMeta, jobId, args, promise) { | ||
if (temporaryIds[jobId] !== undefined) { | ||
temporaryIds[jobId].promise = promise; | ||
} | ||
var config = getTypeConfig(apiName, methodMeta.entity); | ||
promise.then(replaceTemporaryWithFinalId(methodMeta, jobId)).then(invalidateCacheIfDesired(config, methodMeta.entity, args)); | ||
return promise; | ||
} | ||
function createKey(apiName, methodName, multipleEntities, args, type) { | ||
function preDelete(next, apiName, methodName, methodMeta, jobId, args) { | ||
if (methodMeta.multipleEntities) { | ||
args[0].map(deleteFromCache(methodMeta.entity)); | ||
} else { | ||
deleteFromCache(methodMeta.entity)(args[0]); | ||
} | ||
return next(args); | ||
} | ||
function deleteFromCache(type) { | ||
return function (entity) { | ||
delete storage[type][entity.id]; | ||
}; | ||
} | ||
function postDelete(promise) { | ||
return promise; | ||
} | ||
function invalidateCacheIfDesired(config, entityType, args) { | ||
if (newEntityWasCreated(args[0]) && config.invalidateOnCreate) { | ||
collectionQueries[entityType] = {}; | ||
} | ||
} | ||
function newEntityWasCreated(entity) { | ||
return entity.id === undefined; | ||
} | ||
function replaceTemporaryWithFinalId(methodMeta, jobId) { | ||
return function (realEntity) { | ||
var tempId = temporaryIds[jobId].id; | ||
var entityWithTempId = storage[methodMeta.entity][tempId]; | ||
delete storage[methodMeta.entity][tempId]; | ||
storage[methodMeta.entity][realEntity.data.id] = entityWithTempId; | ||
}; | ||
} | ||
function createKey(apiName, methodName, multipleEntities, args, type, jobId) { | ||
if (multipleEntities) { | ||
@@ -377,3 +491,3 @@ return { | ||
entityType: type, | ||
id: args[0].id | ||
id: args[0].id || createTemporaryId(jobId) | ||
}; | ||
@@ -383,2 +497,14 @@ } | ||
function createTemporaryId(jobId) { | ||
var id = generateEntityIdFromJobId(jobId); | ||
temporaryIds[jobId] = { | ||
id: id | ||
}; | ||
return id; | ||
} | ||
function generateEntityIdFromJobId(jobId) { | ||
return 'TEMPORARY_ID_' + jobId; | ||
} | ||
function getTypeConfig(apiName, typeName) { | ||
@@ -409,6 +535,8 @@ if (config[typeName]) { | ||
function getCollectionFromCache(key) { | ||
if (collectionQueries[key.value]) { | ||
if (collectionQueries[key.entityType] && collectionQueries[key.entityType][key.value]) { | ||
return { | ||
data: collectionQueries[key.value].ids.map(function (x) { | ||
return storage[key.entityType][x].data; | ||
data: collectionQueries[key.entityType][key.value].ids.map(function (x) { | ||
return getEntityDataIfExist(storage[key.entityType][x]); | ||
}).filter(function (x) { | ||
return x !== null; | ||
}) | ||
@@ -421,2 +549,10 @@ }; | ||
function getEntityDataIfExist(entity) { | ||
if (entity) { | ||
return entity.data; | ||
} else { | ||
return null; | ||
} | ||
} | ||
function getEntityFromCache(key) { | ||
@@ -432,3 +568,3 @@ if (storage[key.entityType][key.id]) { | ||
if (key.type === 'COLLECTION_KEY') { | ||
return collectionQueries[key.value].timestamp + methodConfig.ttl * 1000 >= Date.now(); | ||
return collectionQueries[key.entityType][key.value].timestamp + methodConfig.ttl * 1000 >= Date.now(); | ||
} else { | ||
@@ -448,3 +584,3 @@ return item.timestamp + methodConfig.ttl * 1000 >= Date.now(); | ||
} else { | ||
saveEntity(type, val.data); | ||
saveEntity(key, type, val.data); | ||
} | ||
@@ -465,3 +601,8 @@ } | ||
}); | ||
collectionQueries[key.value] = { | ||
if (!collectionQueries[key.entityType]) { | ||
collectionQueries[key.entityType] = {}; | ||
} | ||
collectionQueries[key.entityType][key.value] = { | ||
timestamp: Date.now(), | ||
@@ -473,4 +614,4 @@ ids: ids | ||
function saveEntity(type, val) { | ||
storage[type][val.id] = { | ||
function saveEntity(key, type, val) { | ||
storage[type][key.id] = { | ||
data: val, | ||
@@ -477,0 +618,0 @@ timestamp: Date.now() |
{ | ||
"name": "laddare", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Data fetching layaer with support for caching", | ||
@@ -5,0 +5,0 @@ "main": "dist/bundle.js", |
@@ -12,3 +12,6 @@ export default function Caching(config) { | ||
}; | ||
const temporaryIds = { | ||
}; | ||
return { | ||
@@ -18,3 +21,5 @@ preRead, | ||
preWrite, | ||
postWrite | ||
postWrite, | ||
postDelete, | ||
preDelete | ||
}; | ||
@@ -45,5 +50,10 @@ | ||
function preWrite(next, apiName, methodName, methodMeta, args) { | ||
function preWrite(next, apiName, methodName, methodMeta, jobId, args) { | ||
const methodType = methodMeta.entity; | ||
const key = createKey(apiName, methodName, methodMeta.multipleEntities, args, methodType); | ||
const key = createKey(apiName, | ||
methodName, | ||
methodMeta.multipleEntities, | ||
args, | ||
methodType, | ||
jobId); | ||
saveToCache(key, { | ||
@@ -55,7 +65,55 @@ data: args[0] | ||
function postWrite(apiName, methodName, methodMeta, args, promise) { | ||
function postWrite(apiName, methodName, methodMeta, jobId, args, promise) { | ||
if (temporaryIds[jobId] !== undefined) { | ||
temporaryIds[jobId].promise = promise; | ||
} | ||
const config = getTypeConfig(apiName, methodMeta.entity); | ||
promise.then(replaceTemporaryWithFinalId(methodMeta, jobId)) | ||
.then(invalidateCacheIfDesired(config, methodMeta.entity, args)); | ||
return promise; | ||
} | ||
function createKey(apiName, methodName, multipleEntities, args, type) { | ||
function preDelete(next, apiName, methodName, methodMeta, jobId, args) { | ||
if (methodMeta.multipleEntities) { | ||
args[0].map(deleteFromCache(methodMeta.entity)); | ||
} else { | ||
deleteFromCache(methodMeta.entity)(args[0]); | ||
} | ||
return next(args); | ||
} | ||
function deleteFromCache(type) { | ||
return entity => { | ||
delete storage[type][entity.id]; | ||
}; | ||
} | ||
function postDelete(promise) { | ||
return promise; | ||
} | ||
function invalidateCacheIfDesired(config, entityType, args) { | ||
if (newEntityWasCreated(args[0]) && config.invalidateOnCreate) { | ||
collectionQueries[entityType] = {}; | ||
} | ||
} | ||
function newEntityWasCreated(entity) { | ||
return entity.id === undefined; | ||
} | ||
function replaceTemporaryWithFinalId(methodMeta, jobId) { | ||
return realEntity => { | ||
const tempId = temporaryIds[jobId].id; | ||
const entityWithTempId = storage[methodMeta.entity][tempId]; | ||
delete storage[methodMeta.entity][tempId]; | ||
storage[methodMeta.entity][realEntity.data.id] = entityWithTempId; | ||
} | ||
} | ||
function createKey(apiName, methodName, multipleEntities, args, type, jobId) { | ||
if (multipleEntities) { | ||
@@ -71,3 +129,3 @@ return { | ||
entityType: type, | ||
id: args[0].id | ||
id: args[0].id || createTemporaryId(jobId) | ||
}; | ||
@@ -77,2 +135,14 @@ } | ||
function createTemporaryId(jobId) { | ||
const id = generateEntityIdFromJobId(jobId); | ||
temporaryIds[jobId] = { | ||
id | ||
}; | ||
return id; | ||
} | ||
function generateEntityIdFromJobId(jobId) { | ||
return 'TEMPORARY_ID_' + jobId; | ||
} | ||
function getTypeConfig(apiName, typeName) { | ||
@@ -103,6 +173,8 @@ if (config[typeName]) { | ||
function getCollectionFromCache(key) { | ||
if (collectionQueries[key.value]) { | ||
if (collectionQueries[key.entityType] && | ||
collectionQueries[key.entityType][key.value]) { | ||
return { | ||
data: collectionQueries[key.value].ids | ||
.map((x) => storage[key.entityType][x].data) | ||
data: collectionQueries[key.entityType][key.value].ids | ||
.map((x) => getEntityDataIfExist(storage[key.entityType][x])) | ||
.filter((x) => x !== null) | ||
}; | ||
@@ -114,2 +186,10 @@ } else { | ||
function getEntityDataIfExist(entity) { | ||
if (entity) { | ||
return entity.data; | ||
} else { | ||
return null; | ||
} | ||
} | ||
function getEntityFromCache(key) { | ||
@@ -125,3 +205,3 @@ if (storage[key.entityType][key.id]) { | ||
if (key.type === 'COLLECTION_KEY') { | ||
return collectionQueries[key.value].timestamp + methodConfig.ttl * 1000 >= Date.now(); | ||
return collectionQueries[key.entityType][key.value].timestamp + methodConfig.ttl * 1000 >= Date.now(); | ||
} else { | ||
@@ -141,3 +221,3 @@ return item.timestamp + methodConfig.ttl * 1000 >= Date.now(); | ||
} else { | ||
saveEntity(type, val.data); | ||
saveEntity(key, type, val.data); | ||
} | ||
@@ -156,3 +236,8 @@ } | ||
const ids = val.map((x) => x.id); | ||
collectionQueries[key.value] = { | ||
if (!collectionQueries[key.entityType]) { | ||
collectionQueries[key.entityType] = {}; | ||
} | ||
collectionQueries[key.entityType][key.value] = { | ||
timestamp: Date.now(), | ||
@@ -164,4 +249,4 @@ ids | ||
function saveEntity(type, val) { | ||
storage[type][val.id] = { | ||
function saveEntity(key, type, val) { | ||
storage[type][key.id] = { | ||
data: val, | ||
@@ -168,0 +253,0 @@ timestamp: Date.now() |
@@ -5,3 +5,3 @@ export function preRead(next, apiName, toDecorateName, multipleEntities, args) { | ||
export function preWrite(next, apiName, toDecorateName, multipleEntities, args) { | ||
export function preWrite(next, apiName, methodName, methodMeta, jobId, args) { | ||
return next(args); | ||
@@ -16,1 +16,9 @@ } | ||
} | ||
export function preDelete(next, apiName, methodName, methodMeta, jobId, args) { | ||
return next(args); | ||
} | ||
export function postDelete(promise) { | ||
return promise; | ||
} |
import * as IdentityMiddleware from './identity-middleware'; | ||
let jobCounter = 0; | ||
export function createDatastore() { | ||
@@ -58,6 +60,11 @@ return { | ||
let deleteMethods = Object.keys(api.api).filter(x => api.api[x].type === 'DELETE'); | ||
deleteMethods = createObject(deleteMethods, | ||
deleteMethods.map(decorateDelete(api, middleware))); | ||
return { | ||
_name: api.name, | ||
...readMethods, | ||
...writeMethods | ||
...writeMethods, | ||
...deleteMethods | ||
}; | ||
@@ -110,3 +117,3 @@ }; | ||
function bindPreWrite(hook, apiName, toDecorate, toDecorateName, middleware) { | ||
function bindPreWrite(hook, apiName, toDecorate, toDecorateName, jobId, middleware) { | ||
const [x, ...xs] = middleware; | ||
@@ -119,9 +126,12 @@ const methodMeta = createMethodMeta(toDecorate); | ||
toDecorateName, | ||
methodMeta); | ||
methodMeta, | ||
jobId); | ||
} else { | ||
return x.middleware[hook].bind(null, | ||
bindPreWrite(hook, apiName, toDecorate, toDecorateName, xs), | ||
apiName, | ||
toDecorateName, | ||
methodMeta); | ||
return x.middleware[hook].bind( | ||
null, | ||
bindPreWrite(hook, apiName, toDecorate, toDecorateName, jobId, xs), | ||
apiName, | ||
toDecorateName, | ||
methodMeta, | ||
jobId); | ||
} | ||
@@ -133,2 +143,4 @@ } | ||
return (...args) => { | ||
const jobId = jobCounter++; | ||
const preWrite = bindPreWrite( | ||
@@ -139,2 +151,3 @@ 'preWrite', | ||
toDecorate, | ||
jobId, | ||
middleware.filter((x) => x.middleware.preWrite)); | ||
@@ -149,2 +162,3 @@ const result = preWrite(args); | ||
methodMeta, | ||
jobId, | ||
args, | ||
@@ -157,2 +171,52 @@ result)); | ||
function bindPreDelete(hook, apiName, toDecorate, toDecorateName, jobId, middleware) { | ||
const [x, ...xs] = middleware; | ||
const methodMeta = createMethodMeta(toDecorate); | ||
if (xs.length === 0) { | ||
return x.middleware[hook].bind(null, | ||
(args) => toDecorate(...args), | ||
apiName, | ||
toDecorateName, | ||
methodMeta, | ||
jobId); | ||
} else { | ||
return x.middleware[hook].bind( | ||
null, | ||
bindPreWrite(hook, apiName, toDecorate, toDecorateName, jobId, xs), | ||
apiName, | ||
toDecorateName, | ||
methodMeta, | ||
jobId); | ||
} | ||
} | ||
function decorateDelete(api, middleware) { | ||
return (toDecorate) => { | ||
return (...args) => { | ||
const jobId = jobCounter++; | ||
const preDelete = bindPreDelete( | ||
'preDelete', | ||
api.name, | ||
api.api[toDecorate], | ||
toDecorate, | ||
jobId, | ||
middleware.filter((x) => x.middleware.preDelete)); | ||
const result = preDelete(args); | ||
const postDeleteMiddleware = middleware.filter((x) => x.middleware.postDelete); | ||
const methodMeta = createMethodMeta(api.api[toDecorate]); | ||
postDeleteMiddleware.forEach((x) => x.middleware.postDelete( | ||
api.name, | ||
toDecorate, | ||
methodMeta, | ||
jobId, | ||
args, | ||
result)); | ||
return result.then((x) => x.data); | ||
}; | ||
}; | ||
} | ||
function createMethodMeta(method) { | ||
@@ -159,0 +223,0 @@ return { |
@@ -1,2 +0,2 @@ | ||
import { get, post } from 'axios'; | ||
import { get, post, delete as remove } from 'axios'; | ||
@@ -33,2 +33,9 @@ getAllPosts.type = 'READ'; | ||
createPost.type = 'WRITE'; | ||
createPost.entity = 'JsonPlacerholderPost'; | ||
createPost.multipleEntities = false; | ||
export function createPost(entity) { | ||
return post('http://jsonplaceholder.typicode.com/posts', { ...entity }); | ||
} | ||
savePosts.type = 'WRITE'; | ||
@@ -40,1 +47,8 @@ savePosts.entity = 'JsonPlacerholderPost'; | ||
} | ||
deletePost.type = 'DELETE'; | ||
deletePost.entity = 'JsonPlacerholderPost'; | ||
deletePost.multipleEntities = true; | ||
export function deletePost(entity) { | ||
return remove('http://jsonplaceholder.typicode.com/posts', { ...entity }); | ||
} |
@@ -5,3 +5,4 @@ const defaultTTL = 300; | ||
JsonPlacerholderPost: { | ||
ttl: defaultTTL | ||
ttl: defaultTTL, | ||
invalidateOnCreate: true | ||
}, | ||
@@ -8,0 +9,0 @@ GithubRepo: { |
@@ -17,7 +17,9 @@ import { compose } from './fp'; | ||
.then(getAllPosts(datastore)) | ||
.then(savePosts(datastore)) | ||
.then(getSomePosts(datastore)) | ||
.then(getSomePosts(datastore)) | ||
.then(printAllPosts) | ||
.then(getAllPosts(datastore)) | ||
// .then(savePosts(datastore)) | ||
// .then(createPost(datastore)) | ||
// .then(getSomePosts(datastore)) | ||
// .then(getSomePosts(datastore)) | ||
// .then(printAllPosts) | ||
.then(deletePost(datastore)) | ||
.catch(getAllPosts(datastore)) | ||
.then(printAllPosts); | ||
@@ -59,2 +61,29 @@ | ||
function createPost(datastore) { | ||
return () => datastore.JsonPlaceholder.createPost( | ||
{ | ||
body: 'hej' | ||
}, | ||
'annat arg' | ||
); | ||
} | ||
function deletePost(datastore) { | ||
return () => datastore.JsonPlaceholder.deletePost([ | ||
{ | ||
id: '44' | ||
}, | ||
{ | ||
id: '45' | ||
}, | ||
{ | ||
id: '46' | ||
}, | ||
{ | ||
id: '47' | ||
} | ||
]); | ||
} | ||
function printAllPosts(posts) { | ||
@@ -61,0 +90,0 @@ console.log(posts); |
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
43860
18
1131