Comparing version 3.4.1 to 3.5.0
@@ -9,2 +9,3 @@ import { TypedEmitter } from "tiny-typed-emitter"; | ||
declare class Database extends TypedEmitter<DatabaseEvents> { | ||
#private; | ||
/** | ||
@@ -37,2 +38,5 @@ * The sqlite database. | ||
private _validateOptions; | ||
deleteTimedData(table: string, key: string): boolean; | ||
setTimedData(table: string, key: string, duration: number): this; | ||
hasTimeoutsTable(): boolean; | ||
/** | ||
@@ -157,2 +161,3 @@ * Resolves a string or object into a table. | ||
get<K = undefined>(table: TableResolvable, options: SqliteQueryOptions): K extends undefined ? DataResolvable : K; | ||
addTimeoutsTable(): this; | ||
/** | ||
@@ -159,0 +164,0 @@ * Connects the database to its corresponding file. |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var _Database_instances, _Database_timeouts, _Database_loadTimeouts, _Database_createTimeout, _Database_maxAllowedTimeout_get, _Database_sleep; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -12,2 +27,3 @@ const tiny_typed_emitter_1 = require("tiny-typed-emitter"); | ||
const DatabaseError_1 = require("./DatabaseError"); | ||
const __1 = require(".."); | ||
/** | ||
@@ -23,3 +39,9 @@ * Database wrapper class for sqlite. | ||
super(); | ||
_Database_instances.add(this); | ||
_Database_timeouts.set(this, new Map() | ||
/** | ||
* Date at which the database started. | ||
*/ | ||
); | ||
/** | ||
* The assigned tables for this database. | ||
@@ -38,2 +60,44 @@ */ | ||
} | ||
deleteTimedData(table, key) { | ||
if (!this.hasTimeoutsTable()) | ||
throw new DatabaseError_1.DatabaseError(`There is not timeouts table instantiated.`); | ||
const existing = __classPrivateFieldGet(this, _Database_timeouts, "f").get(`${table}_${key}`); | ||
if (existing) { | ||
if (existing.timeout) | ||
clearTimeout(existing.timeout); | ||
if (existing.sleep) | ||
clearTimeout(existing.sleep); | ||
__classPrivateFieldGet(this, _Database_timeouts, "f").delete(`${table}_${key}`); | ||
this.delete(`__timeouts`, { | ||
where: [ | ||
{ | ||
column: 'key', | ||
equals: key | ||
}, | ||
{ | ||
column: '_table', | ||
equals: table | ||
} | ||
] | ||
}); | ||
return true; | ||
} | ||
else | ||
return false; | ||
} | ||
setTimedData(table, key, duration) { | ||
if (!this.hasTimeoutsTable()) | ||
throw new DatabaseError_1.DatabaseError(`There is not timeouts table instantiated.`); | ||
if (duration < 1) | ||
throw new DatabaseError_1.DatabaseError(`The provided duration is under 1ms.`); | ||
const tbl = this.resolveTable(table); | ||
if (!tbl.columns.find(c => c.primary)) | ||
throw new DatabaseError_1.DatabaseError(`Table '${table}' has no primary key.`); | ||
const left = Date.now() + duration; | ||
__classPrivateFieldGet(this, _Database_instances, "m", _Database_createTimeout).call(this, table, key, left); | ||
return this; | ||
} | ||
hasTimeoutsTable() { | ||
return this.tables.has('__timeouts'); | ||
} | ||
/** | ||
@@ -207,2 +271,18 @@ * Resolves a string or object into a table. | ||
} | ||
addTimeoutsTable() { | ||
this.createTable("__timeouts") | ||
.addColumns([ | ||
new __1.Column() | ||
.setName('endsAt') | ||
.setType('INTEGER'), | ||
new __1.Column() | ||
.setName('key') | ||
.setType('TEXT') | ||
.setPrimary(true), | ||
new __1.Column() | ||
.setName('_table') | ||
.setType('TEXT') | ||
]); | ||
return this; | ||
} | ||
/** | ||
@@ -218,2 +298,5 @@ * Connects the database to its corresponding file. | ||
} | ||
if (this.hasTimeoutsTable()) { | ||
__classPrivateFieldGet(this, _Database_instances, "m", _Database_loadTimeouts).call(this); | ||
} | ||
this.readyAt = new Date(); | ||
@@ -235,3 +318,69 @@ this.emit("ready"); | ||
} | ||
_Database_timeouts = new WeakMap(), _Database_instances = new WeakSet(), _Database_loadTimeouts = function _Database_loadTimeouts() { | ||
const timeouts = this.all('__timeouts'); | ||
for (const timeout of timeouts) { | ||
__classPrivateFieldGet(this, _Database_instances, "m", _Database_createTimeout).call(this, timeout._table, timeout.key, timeout.endsAt); | ||
} | ||
}, _Database_createTimeout = function _Database_createTimeout(table, key, endsAt) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const total = endsAt - Date.now(); | ||
const id = `${table}_${key}`; | ||
const existing = __classPrivateFieldGet(this, _Database_timeouts, "f").get(id); | ||
if (existing) { | ||
if (existing.timeout) | ||
clearTimeout(existing.timeout); | ||
if (existing.sleep) | ||
clearTimeout(existing.sleep); | ||
__classPrivateFieldGet(this, _Database_timeouts, "f").delete(id); | ||
} | ||
this.set('__timeouts', { | ||
key, | ||
endsAt, | ||
_table: table | ||
}); | ||
if (total > __classPrivateFieldGet(this, _Database_instances, "a", _Database_maxAllowedTimeout_get)) { | ||
const sleep = setTimeout(() => { | ||
__classPrivateFieldGet(this, _Database_instances, "m", _Database_createTimeout).call(this, table, key, endsAt); | ||
}, __classPrivateFieldGet(this, _Database_instances, "a", _Database_maxAllowedTimeout_get)); | ||
__classPrivateFieldGet(this, _Database_timeouts, "f").set(id, { sleep }); | ||
return; | ||
} | ||
else { | ||
const timeout = setTimeout(() => { | ||
const data = this.get(table, key); | ||
this.delete(table, key); | ||
if (data) { | ||
this.emit('expire', table, data); | ||
} | ||
this.delete('__timeouts', { | ||
where: [ | ||
{ | ||
column: '_table', | ||
equals: table | ||
}, | ||
{ | ||
column: 'key', | ||
equals: key | ||
} | ||
] | ||
}); | ||
__classPrivateFieldGet(this, _Database_timeouts, "f").delete(id); | ||
}, total); | ||
__classPrivateFieldGet(this, _Database_timeouts, "f").set(id, { timeout }); | ||
return; | ||
} | ||
}); | ||
}, _Database_maxAllowedTimeout_get = function _Database_maxAllowedTimeout_get() { | ||
return 2073600000; | ||
}, _Database_sleep = function _Database_sleep(duration) { | ||
return new Promise((resolve, reject) => { | ||
if (duration >= __classPrivateFieldGet(this, _Database_instances, "a", _Database_maxAllowedTimeout_get)) { | ||
return __classPrivateFieldGet(this, _Database_instances, "m", _Database_sleep).call(this, __classPrivateFieldGet(this, _Database_instances, "a", _Database_maxAllowedTimeout_get)).then(resolve); | ||
} | ||
else { | ||
return setTimeout(resolve, __classPrivateFieldGet(this, _Database_instances, "a", _Database_maxAllowedTimeout_get)); | ||
} | ||
}); | ||
}; | ||
exports.default = Database; | ||
//# sourceMappingURL=Database.js.map |
@@ -32,3 +32,3 @@ import { Column } from "../structures/Column"; | ||
ready: () => void; | ||
expire: (data: DataResolvable) => void; | ||
expire: (table: string, data: DataResolvable) => void; | ||
/** | ||
@@ -35,0 +35,0 @@ * Fired whenever the database is told to set data. |
{ | ||
"name": "dbdts.db", | ||
"version": "3.4.1", | ||
"version": "3.5.0", | ||
"description": "Easy to use wrapper for sqlite databases, mainly designed for use with dbd.ts package.", | ||
@@ -5,0 +5,0 @@ "exports": { |
@@ -128,2 +128,26 @@ # dbdts.db | ||
}) | ||
``` | ||
``` | ||
## Setting expiring time to data | ||
That's right! With the release of v3.5.0, we've finally added the ability to add a expiring duration to data in the database. <br> | ||
Here's a small example: | ||
```js | ||
... // Assuming you got a working database setup of dbdts.db and the variable is defined as db. | ||
db.on('expire', (table, data) => { | ||
console.log(`Data`, data, 'expired in table', table) | ||
}) | ||
// Set some data as usual to db. | ||
db.set("sometable", { | ||
id: 'someid', | ||
data: {}, | ||
value: 10 | ||
}, { | ||
where: { column: "id", equals: "someid" } | ||
}) | ||
// The above data will be deleted from the database after 25000ms. | ||
db.setTimedData('sometable', 'someid', 25000) | ||
``` | ||
And that's it! Make sure to adapt the example to what you got in your project and remember the id is attached to the table's primary key. |
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
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
133044
2091
152