Comparing version 4.2.0 to 4.3.0
@@ -16,2 +16,9 @@ import { Table } from "../structures/Table"; | ||
createTable<K extends keyof T>(name: K): Table; | ||
overwriteTimedData(table: keyof T, id: string, duration: number): boolean; | ||
overwriteTimedData(table: string, id: string, duration: number): boolean; | ||
deleteTimedData(table: keyof T, id: string): boolean; | ||
deleteTimedData(table: string, id: string): boolean; | ||
setTimedData(table: keyof T, id: string, duration: number): boolean; | ||
setTimedData(table: string, id: string, duration: number): boolean; | ||
hasPrimaryKey(table: string): boolean; | ||
addTable(table: Table): Table; | ||
@@ -18,0 +25,0 @@ isReady(): boolean; |
@@ -22,3 +22,3 @@ "use strict"; | ||
}; | ||
var _Database_ready; | ||
var _Database_instances, _Database_ready, _Database_timeDataTableName_get, _Database_addTimeoutTable, _Database_loadTimedData, _Database_expire; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -33,17 +33,141 @@ exports.Database = void 0; | ||
const tiny_typed_emitter_1 = require("tiny-typed-emitter"); | ||
const __1 = require(".."); | ||
const cast_1 = __importDefault(require("../functions/cast")); | ||
const sleep_1 = __importDefault(require("../functions/sleep")); | ||
class Database extends tiny_typed_emitter_1.TypedEmitter { | ||
constructor(options = DefaultDatabaseOptions_1.DefaultDatabaseOptions) { | ||
super(); | ||
_Database_instances.add(this); | ||
this.tables = new Map(); | ||
_Database_ready.set(this, false); | ||
this.options = options; | ||
__classPrivateFieldGet(this, _Database_instances, "m", _Database_addTimeoutTable).call(this); | ||
} | ||
createTable(name) { | ||
const table = new Table_1.Table((0, cast_1.default)(name), this); | ||
const table = new Table_1.Table((0, cast_1.default)(name), (0, cast_1.default)(this)); | ||
this.tables.set(table.name, table); | ||
return table; | ||
} | ||
/** | ||
* Overwrites a timed data in the table, this will disable the other tiemout. | ||
* @param table The table to perform this operation on. | ||
* @param id The id of the primary key. | ||
* @param duration Duration to wait before expiring. | ||
* @returns | ||
*/ | ||
overwriteTimedData(table, id, duration) { | ||
const timed = this.resolveTable(__classPrivateFieldGet(this, _Database_instances, "a", _Database_timeDataTableName_get)); | ||
// This is just to verify that the table exists | ||
this.resolveTable(table); | ||
if (!timed.has([ | ||
{ | ||
column: "id", | ||
equals: id, | ||
and: true | ||
}, | ||
{ | ||
column: 'refTable', | ||
equals: table | ||
} | ||
])) { | ||
return false; | ||
} | ||
const registeredAt = Date.now(); | ||
timed.update({ | ||
duration: { | ||
set: duration | ||
}, | ||
registeredAt: { | ||
set: registeredAt | ||
} | ||
}, { | ||
where: [ | ||
{ | ||
column: "id", | ||
equals: id, | ||
and: true | ||
}, | ||
{ | ||
column: 'refTable', | ||
equals: table | ||
} | ||
] | ||
}); | ||
__classPrivateFieldGet(this, _Database_instances, "m", _Database_expire).call(this, table, id, duration, registeredAt); | ||
return true; | ||
} | ||
/** | ||
* Deletes a timed data reference from the timed table, note that this will not delete the data from the actual table | ||
* @param table The table that was used to create the timed data on | ||
* @param id The primary id | ||
* @param duration The duration used | ||
* @returns | ||
*/ | ||
deleteTimedData(table, id) { | ||
const timed = this.resolveTable(__classPrivateFieldGet(this, _Database_instances, "a", _Database_timeDataTableName_get)); | ||
// This is just to verify that the table exists | ||
this.resolveTable(table); | ||
if (!timed.has([ | ||
{ | ||
column: "id", | ||
equals: id, | ||
and: true | ||
}, | ||
{ | ||
column: 'refTable', | ||
equals: table | ||
} | ||
])) { | ||
return false; | ||
} | ||
timed.delete({ | ||
where: [ | ||
{ | ||
column: 'id', | ||
equals: id, | ||
and: true | ||
}, | ||
{ | ||
column: "refTable", | ||
equals: table | ||
} | ||
] | ||
}); | ||
return true; | ||
} | ||
setTimedData(table, id, duration) { | ||
// This is just to verify that the table exists | ||
this.resolveTable(table); | ||
const timed = this.resolveTable(__classPrivateFieldGet(this, _Database_instances, "a", _Database_timeDataTableName_get)); | ||
if (timed.has([ | ||
{ | ||
column: 'id', | ||
equals: id, | ||
and: true | ||
}, | ||
{ | ||
column: 'refTable', | ||
equals: table | ||
} | ||
])) { | ||
// A timed data already exists | ||
return false; | ||
} | ||
else { | ||
const registeredAt = Date.now(); | ||
timed.insert({ | ||
id, | ||
refTable: table, | ||
registeredAt, | ||
duration | ||
}); | ||
__classPrivateFieldGet(this, _Database_instances, "m", _Database_expire).call(this, table, id, duration, registeredAt); | ||
return true; | ||
} | ||
} | ||
hasPrimaryKey(table) { | ||
return this.resolveTable(table).columns.some(c => c.data.primary); | ||
} | ||
addTable(table) { | ||
this.tables.set(table.name, table["setDatabase"](this)); | ||
this.tables.set(table.name, table["setDatabase"]((0, cast_1.default)(this))); | ||
return table; | ||
@@ -135,2 +259,3 @@ } | ||
__classPrivateFieldSet(this, _Database_ready, true, "f"); | ||
__classPrivateFieldGet(this, _Database_instances, "m", _Database_loadTimedData).call(this); | ||
this.emit("ready"); | ||
@@ -141,3 +266,106 @@ resolve(this); | ||
} | ||
_Database_ready = new WeakMap(); | ||
_Database_ready = new WeakMap(), _Database_instances = new WeakSet(), _Database_timeDataTableName_get = function _Database_timeDataTableName_get() { | ||
return "__timeouts__"; | ||
}, _Database_addTimeoutTable = function _Database_addTimeoutTable() { | ||
if (!this.options.allowTimedData) | ||
return; | ||
process.emitWarning("DatabaseOptions#allowTimedData is a experimental option and might be bugged."); | ||
this.createTable(__classPrivateFieldGet(this, _Database_instances, "a", _Database_timeDataTableName_get)) | ||
.addColumns([ | ||
new __1.Column() | ||
.setName("id") | ||
.setType('TEXT'), | ||
new __1.Column() | ||
.setName("refTable") | ||
.setType('TEXT'), | ||
new __1.Column() | ||
.setName("duration") | ||
.setType('INT'), | ||
new __1.Column() | ||
.setName("registeredAt") | ||
.setType('INT') | ||
]); | ||
}, _Database_loadTimedData = function _Database_loadTimedData() { | ||
if (!this.options.allowTimedData) | ||
return; | ||
const timed = this.resolveTable(__classPrivateFieldGet(this, _Database_instances, "a", _Database_timeDataTableName_get)).all(); | ||
for (let i = 0, len = timed.length; i < len; i++) { | ||
const d = timed[i]; | ||
__classPrivateFieldGet(this, _Database_instances, "m", _Database_expire).call(this, d.refTable, d.id, d.duration, d.registeredAt); | ||
} | ||
}, _Database_expire = function _Database_expire(table, id, duration, startedAt) { | ||
const ref = this.resolveTable(table); | ||
const timed = this.resolveTable(__classPrivateFieldGet(this, _Database_instances, "a", _Database_timeDataTableName_get)); | ||
// Double check that both exists | ||
if (!timed.has([ | ||
{ | ||
column: 'id', | ||
equals: id, | ||
and: true | ||
}, | ||
{ | ||
column: 'refTable', | ||
equals: table | ||
} | ||
]) || !ref.has(id)) { | ||
return; | ||
} | ||
// Sleep till end of timed data | ||
(0, sleep_1.default)(duration - (Date.now() - startedAt)) | ||
.then(() => { | ||
if (!timed.has([ | ||
{ | ||
column: 'id', | ||
equals: id, | ||
and: true | ||
}, | ||
{ | ||
column: 'refTable', | ||
equals: table | ||
} | ||
])) { | ||
return; | ||
} | ||
const got = timed.get([ | ||
{ | ||
column: 'id', | ||
equals: id | ||
}, | ||
{ | ||
column: 'refTable', | ||
equals: table | ||
} | ||
]); | ||
if (got.registeredAt !== startedAt) { | ||
return; | ||
} | ||
timed.delete({ | ||
where: [ | ||
{ | ||
column: 'id', | ||
equals: id, | ||
and: true | ||
}, | ||
{ | ||
column: 'refTable', | ||
equals: table | ||
} | ||
] | ||
}); | ||
// If there is no reference we assume it was yeeted | ||
if (!ref.has(id)) { | ||
return; | ||
} | ||
const received = ref.get(id); | ||
// Delete reference | ||
ref.delete({ | ||
where: { | ||
column: 'id', | ||
equals: id | ||
} | ||
}); | ||
// Emit event | ||
this.emit("expire", ref, received); | ||
}); | ||
}; | ||
__decorate([ | ||
@@ -144,0 +372,0 @@ (0, DatabaseConnected_1.default)() |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -13,4 +13,5 @@ import { RunResult } from "better-sqlite3"; | ||
ready: () => Async<void>; | ||
expire: (table: Table, data: InsertDataResolvable) => Async<void>; | ||
query: (table: Table, query: string) => Async<void>; | ||
} | ||
//# sourceMappingURL=DatabaseEvents.d.ts.map |
@@ -6,3 +6,4 @@ import { If } from "../types/If"; | ||
path: If<T, string, null>; | ||
allowTimedData: boolean; | ||
} | ||
//# sourceMappingURL=DatabaseOptions.d.ts.map |
@@ -7,4 +7,5 @@ "use strict"; | ||
sanitize: true, | ||
length: 10 | ||
length: 10, | ||
allowTimedData: false, | ||
}; | ||
//# sourceMappingURL=DefaultDatabaseOptions.js.map |
{ | ||
"name": "dbdts.db", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"description": "Easy to use wrapper for sqlite databases, mainly designed for use with dbd.ts package.", | ||
@@ -5,0 +5,0 @@ "module": "dist/index.mjs", |
@@ -0,0 +0,0 @@ # dbdts.db |
@@ -0,0 +0,0 @@ { |
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
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
152872
189
1904