@bitblit/ratchet-aws
Version:
Common tools for use with AWS browser and node
77 lines • 2.65 kB
JavaScript
import { Logger } from '@bitblit/ratchet-common/logger/logger';
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
export class S3SimpleDao {
cache;
prefix;
constructor(cache, prefix) {
this.cache = cache;
this.prefix = prefix;
if (!cache) {
throw new Error('cache object may not be null');
}
if (!cache.getDefaultBucket()) {
throw new Error('Supplied cache must have default bucket set');
}
}
buildPathPrefix(path) {
let rval = '';
if (this.prefix) {
rval += this.prefix;
}
if (path) {
rval += path;
}
return rval;
}
buildFullPath(id, path) {
let rval = this.buildPathPrefix(path);
if (rval.length > 0) {
rval += '/';
}
rval += id + '.json';
return rval;
}
async exists(id, path) {
const fullPath = this.buildFullPath(id, path);
Logger.debug('Check file existence : %s', fullPath);
return this.cache.fileExists(fullPath);
}
async fetch(id, path) {
const fullPath = this.buildFullPath(id, path);
Logger.debug('Fetching : %s', fullPath);
const rval = (await this.cache.fetchCacheFileAsObject(fullPath));
rval.id = id;
rval.path = path;
return rval;
}
async store(item, path) {
item.id = item.id || StringRatchet.createType4Guid();
item.lastModifiedEpochMS = new Date().getTime();
const fullPath = this.buildFullPath(item.id, path);
Logger.debug('Storing : %s', fullPath);
const _stored = await this.cache.writeObjectToCacheFile(fullPath, item);
const read = await this.fetch(item.id, path);
return read;
}
async listItems(path) {
const fullPath = this.buildPathPrefix(path);
Logger.debug('Listing : %s', fullPath);
const rval = await this.cache.directChildrenOfPrefix(fullPath);
return rval;
}
async fetchItemsInPath(path) {
const fullPath = this.buildPathPrefix(path);
Logger.debug('Full fetch of : %s', fullPath);
const items = await this.listItems(path);
const promises = items.map((s) => this.fetch(s, path));
const rval = await Promise.all(promises);
return rval;
}
async delete(id, path) {
const fullPath = this.buildFullPath(id, path);
Logger.debug('Deleting : %s', fullPath);
const del = await this.cache.removeCacheFile(fullPath);
return del != null;
}
}
//# sourceMappingURL=s3-simple-dao.js.map