@edjopato/datastore
Advanced tools
export * from './key-value'; | ||
export * from './raw-object'; | ||
export * from './cache'; | ||
export * from './transform'; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./key-value")); | ||
__export(require("./raw-object")); | ||
__exportStar(require("./key-value"), exports); | ||
__exportStar(require("./raw-object"), exports); | ||
__exportStar(require("./cache"), exports); | ||
__exportStar(require("./transform"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,11 +0,13 @@ | ||
import { KeyValueStorage } from './type'; | ||
export declare class KeyValueInMemoryFile<T> implements KeyValueStorage<T> { | ||
import { ExtendedStore } from './type'; | ||
export declare class KeyValueInMemoryFile<T> implements ExtendedStore<T> { | ||
private readonly _filepath; | ||
private _inMemoryStorage; | ||
readonly ttlSupport = false; | ||
private readonly _inMemoryStorage; | ||
constructor(_filepath: string); | ||
entries(): Record<string, T | undefined>; | ||
keys(): readonly string[]; | ||
get(key: string): T | undefined; | ||
set(key: string, value: T): Promise<void>; | ||
delete(key: string): void; | ||
delete(key: string): Promise<boolean>; | ||
clear(): void; | ||
private _createFileContent; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.KeyValueInMemoryFile = void 0; | ||
const fs_1 = require("fs"); | ||
@@ -8,30 +9,48 @@ const writeJsonFile = require("write-json-file"); | ||
this._filepath = _filepath; | ||
this._inMemoryStorage = {}; | ||
this.ttlSupport = false; | ||
this._inMemoryStorage = new Map(); | ||
if (fs_1.existsSync(this._filepath)) { | ||
const raw = fs_1.readFileSync(this._filepath, 'utf8'); | ||
const json = JSON.parse(raw); | ||
this._inMemoryStorage = json; | ||
const keys = Object.keys(json); | ||
for (const key of keys) { | ||
this._inMemoryStorage.set(key, json[key]); | ||
} | ||
} | ||
} | ||
entries() { | ||
return this._inMemoryStorage; | ||
} | ||
keys() { | ||
return Object.keys(this._inMemoryStorage); | ||
return [...this._inMemoryStorage.keys()]; | ||
} | ||
get(key) { | ||
return this._inMemoryStorage[key]; | ||
return this._inMemoryStorage.get(key); | ||
} | ||
async set(key, value) { | ||
this._inMemoryStorage[key] = value; | ||
await writeJsonFile(this._filepath, this._inMemoryStorage, { sortKeys: true }); | ||
this._inMemoryStorage.set(key, value); | ||
await writeJsonFile(this._filepath, this._createFileContent(), { sortKeys: true }); | ||
} | ||
delete(key) { | ||
delete this._inMemoryStorage[key]; | ||
if (JSON.stringify(this._inMemoryStorage) === '{}' && fs_1.existsSync(this._filepath)) { | ||
async delete(key) { | ||
const result = this._inMemoryStorage.delete(key); | ||
if (this._inMemoryStorage.size > 0) { | ||
await writeJsonFile(this._filepath, this._createFileContent(), { sortKeys: true }); | ||
} | ||
else if (fs_1.existsSync(this._filepath)) { | ||
fs_1.unlinkSync(this._filepath); | ||
} | ||
return result; | ||
} | ||
clear() { | ||
this._inMemoryStorage.clear(); | ||
if (fs_1.existsSync(this._filepath)) { | ||
fs_1.unlinkSync(this._filepath); | ||
} | ||
} | ||
_createFileContent() { | ||
const json = {}; | ||
for (const key of this._inMemoryStorage.keys()) { | ||
json[key] = this._inMemoryStorage.get(key); | ||
} | ||
return json; | ||
} | ||
} | ||
exports.KeyValueInMemoryFile = KeyValueInMemoryFile; | ||
//# sourceMappingURL=in-memory-file.js.map |
@@ -1,11 +0,12 @@ | ||
import { KeyValueStorage } from './type'; | ||
export declare class KeyValueInMemoryFiles<T> implements KeyValueStorage<T> { | ||
import { ExtendedStore } from './type'; | ||
export declare class KeyValueInMemoryFiles<T> implements ExtendedStore<T> { | ||
private readonly _directory; | ||
private _inMemoryStorage; | ||
readonly ttlSupport = false; | ||
private readonly _inMemoryStorage; | ||
constructor(_directory: string); | ||
entries(): Record<string, T | undefined>; | ||
keys(): readonly string[]; | ||
get(key: string): T | undefined; | ||
set(key: string, value: T): Promise<void>; | ||
delete(key: string): void; | ||
delete(key: string): boolean; | ||
clear(): void; | ||
private _pathOfKey; | ||
@@ -12,0 +13,0 @@ private _listFromFS; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.KeyValueInMemoryFiles = void 0; | ||
const fs_1 = require("fs"); | ||
@@ -8,28 +9,32 @@ const writeJsonFile = require("write-json-file"); | ||
this._directory = _directory; | ||
this._inMemoryStorage = {}; | ||
this.ttlSupport = false; | ||
this._inMemoryStorage = new Map(); | ||
fs_1.mkdirSync(_directory, { recursive: true }); | ||
const entries = this._listFromFS(); | ||
for (const e of entries) { | ||
this._inMemoryStorage[e] = this._getFromFS(e); | ||
for (const entry of entries) { | ||
this._inMemoryStorage.set(entry, this._getFromFS(entry)); | ||
} | ||
} | ||
entries() { | ||
return this._inMemoryStorage; | ||
} | ||
keys() { | ||
return Object.keys(this._inMemoryStorage); | ||
return [...this._inMemoryStorage.keys()]; | ||
} | ||
get(key) { | ||
return this._inMemoryStorage[key]; | ||
return this._inMemoryStorage.get(key); | ||
} | ||
async set(key, value) { | ||
this._inMemoryStorage[key] = value; | ||
this._inMemoryStorage.set(key, value); | ||
await writeJsonFile(this._pathOfKey(key), value, { sortKeys: true }); | ||
} | ||
delete(key) { | ||
delete this._inMemoryStorage[key]; | ||
const result = this._inMemoryStorage.delete(key); | ||
if (fs_1.existsSync(this._pathOfKey(key))) { | ||
fs_1.unlinkSync(this._pathOfKey(key)); | ||
} | ||
return result; | ||
} | ||
clear() { | ||
for (const key of this.keys()) { | ||
this.delete(key); | ||
} | ||
} | ||
_pathOfKey(key) { | ||
@@ -36,0 +41,0 @@ return `${this._directory}/${key}.json`; |
@@ -1,9 +0,10 @@ | ||
import { KeyValueStorage } from './type'; | ||
export declare class KeyValueInMemory<T> implements KeyValueStorage<T> { | ||
private _inMemoryStorage; | ||
entries(): Record<string, T | undefined>; | ||
import { ExtendedStore } from './type'; | ||
export declare class KeyValueInMemory<T> implements ExtendedStore<T> { | ||
readonly ttlSupport = false; | ||
private readonly _inMemoryStorage; | ||
keys(): readonly string[]; | ||
get(key: string): T | undefined; | ||
set(key: string, value: T): void; | ||
delete(key: string): void; | ||
delete(key: string): boolean; | ||
clear(): void; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.KeyValueInMemory = void 0; | ||
class KeyValueInMemory { | ||
constructor() { | ||
this._inMemoryStorage = {}; | ||
this.ttlSupport = false; | ||
this._inMemoryStorage = new Map(); | ||
} | ||
entries() { | ||
return this._inMemoryStorage; | ||
} | ||
keys() { | ||
return Object.keys(this._inMemoryStorage); | ||
return [...this._inMemoryStorage.keys()]; | ||
} | ||
get(key) { | ||
return this._inMemoryStorage[key]; | ||
return this._inMemoryStorage.get(key); | ||
} | ||
set(key, value) { | ||
this._inMemoryStorage[key] = value; | ||
this._inMemoryStorage.set(key, value); | ||
} | ||
delete(key) { | ||
delete this._inMemoryStorage[key]; | ||
return this._inMemoryStorage.delete(key); | ||
} | ||
clear() { | ||
this._inMemoryStorage.clear(); | ||
} | ||
} | ||
exports.KeyValueInMemory = KeyValueInMemory; | ||
//# sourceMappingURL=in-memory.js.map |
@@ -1,4 +0,6 @@ | ||
export * from './type'; | ||
export * from './ttl-in-memory-file'; | ||
export * from './ttl-in-memory-files'; | ||
export * from './ttl-in-memory'; | ||
export * from './in-memory-file'; | ||
export * from './in-memory-files'; | ||
export * from './in-memory'; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
// ./type is explicitly not exported from index. | ||
// Check comment there | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./in-memory-file")); | ||
__export(require("./in-memory-files")); | ||
__export(require("./in-memory")); | ||
__exportStar(require("./ttl-in-memory-file"), exports); | ||
__exportStar(require("./ttl-in-memory-files"), exports); | ||
__exportStar(require("./ttl-in-memory"), exports); | ||
__exportStar(require("./in-memory-file"), exports); | ||
__exportStar(require("./in-memory-files"), exports); | ||
__exportStar(require("./in-memory"), exports); | ||
//# sourceMappingURL=index.js.map |
import { MaybePromise } from '../types'; | ||
export interface KeyValueStorage<T> { | ||
delete(key: string): MaybePromise<void>; | ||
entries(): MaybePromise<Record<string, T | undefined>>; | ||
get(key: string): MaybePromise<T | undefined>; | ||
keys(): MaybePromise<readonly string[]>; | ||
set(key: string, value: T): MaybePromise<void>; | ||
/** | ||
* Keyv compatible Store. See https://github.com/lukechilds/keyv | ||
*/ | ||
export interface Store<T> { | ||
readonly ttlSupport: boolean; | ||
/** | ||
* Get the current value of a key. Is undefined when the value is currently not set. | ||
*/ | ||
readonly get: (key: string) => MaybePromise<T | undefined>; | ||
/** | ||
* Set a key to a specific value. | ||
* @param key key to be set | ||
* @param value value to set to the key | ||
* @param ttl time to live of the object in milliseconds from now (when supported by the implementation) | ||
*/ | ||
readonly set: (key: string, value: T, ttl?: number) => MaybePromise<void>; | ||
/** | ||
* Delete a key from the store. Returns true when the key existed, false if the element does not exist. | ||
*/ | ||
readonly delete: (key: string) => MaybePromise<boolean>; | ||
/** | ||
* Remove all entries | ||
*/ | ||
readonly clear: () => MaybePromise<void>; | ||
} | ||
export interface ExtendedStore<T> extends Store<T> { | ||
/** | ||
* Return all currently set keys | ||
*/ | ||
readonly keys: () => MaybePromise<readonly string[]>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RawObjectInMemoryFile = void 0; | ||
const fs_1 = require("fs"); | ||
@@ -4,0 +5,0 @@ const writeJsonFile = require("write-json-file"); |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./in-memory-file")); | ||
__exportStar(require("./type"), exports); | ||
__exportStar(require("./in-memory-file"), exports); | ||
//# sourceMappingURL=index.js.map |
import { MaybePromise } from '../types'; | ||
export interface RawObjectStorage<T> { | ||
delete(): MaybePromise<void>; | ||
get(): MaybePromise<T | undefined>; | ||
set(value: T): MaybePromise<void>; | ||
readonly delete: () => MaybePromise<void>; | ||
readonly get: () => MaybePromise<T | undefined>; | ||
readonly set: (value: T) => MaybePromise<void>; | ||
} |
{ | ||
"name": "@edjopato/datastore", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Handles different ways to store data within NodeJS", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -19,3 +19,3 @@ "scripts": { | ||
"prepack": "npm run build", | ||
"test": "tsc && xo" | ||
"test": "tsc --sourceMap && xo" | ||
}, | ||
@@ -39,24 +39,13 @@ "main": "dist", | ||
"@sindresorhus/tsconfig": "^0.7.0", | ||
"@types/node": "^13.1.4", | ||
"@typescript-eslint/eslint-plugin": "^2.14.0", | ||
"@typescript-eslint/parser": "^2.14.0", | ||
"@types/node": "^14.0.5", | ||
"del-cli": "^3.0.0", | ||
"eslint-config-xo-typescript": "^0.24.1", | ||
"typescript": "^3.7.4", | ||
"xo": "^0.25.3" | ||
"xo": "^0.30.0" | ||
}, | ||
"xo": { | ||
"semi": false, | ||
"extends": "xo-typescript", | ||
"extensions": [ | ||
"ts" | ||
], | ||
"semicolon": false, | ||
"rules": { | ||
"@typescript-eslint/no-dynamic-delete": "off", | ||
"@typescript-eslint/semi": [ | ||
"error", | ||
"never" | ||
] | ||
"@typescript-eslint/class-literal-property-style": "off" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
43899
176.65%5
-37.5%51
54.55%676
199.12%6
50%1
Infinity%