major bug fixes & breaking change in database code

Major bug fixes and improvements have been made. There's also a breaking change for plugins using the database system. All database related functions are now async based.
This commit is contained in:
DJj123dj
2024-11-24 23:26:14 +01:00
parent 6b36b1caf6
commit b22f7b2de7
18 changed files with 141 additions and 118 deletions
+61 -61
View File
@@ -1,7 +1,7 @@
///////////////////////////////////////
//DEFAULT DATABASE MODULE
///////////////////////////////////////
import { ODValidId, ODValidJsonType } from "../modules/base"
import { ODOptionalPromise, ODValidId, ODValidJsonType } from "../modules/base"
import { ODDatabaseManager, ODDatabase, ODFormattedJsonDatabase } from "../modules/database"
import { ODTicketJson } from "../openticket/ticket"
import { ODOptionJson } from "../openticket/option"
@@ -65,38 +65,38 @@ export interface ODFormattedJsonDatabaseIds_DefaultGlobal {
* This default class is made for the `global.json` database!
*/
export class ODFormattedJsonDatabase_DefaultGlobal extends ODFormattedJsonDatabase {
set<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultGlobal>(category:CategoryId, key:string, value:ODFormattedJsonDatabaseIds_DefaultGlobal[CategoryId]): boolean
set(category:string, key:string, value:ODValidJsonType): boolean
set<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultGlobal>(category:CategoryId, key:string, value:ODFormattedJsonDatabaseIds_DefaultGlobal[CategoryId]): ODOptionalPromise<boolean>
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean>
set(category:string, key:string, value:ODValidJsonType): boolean {
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean> {
return super.set(category,key,value)
}
get<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultGlobal>(category:CategoryId, key:string): ODFormattedJsonDatabaseIds_DefaultGlobal[CategoryId]|undefined
get(category:string, key:string): ODValidJsonType|undefined
get<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultGlobal>(category:CategoryId, key:string): ODOptionalPromise<ODFormattedJsonDatabaseIds_DefaultGlobal[CategoryId]|undefined>
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined>
get(category:string, key:string): ODValidJsonType|undefined {
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined> {
return super.get(category,key)
}
delete<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultGlobal>(category:CategoryId, key:string): boolean
delete(category:string, key:string): boolean
delete<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultGlobal>(category:CategoryId, key:string): ODOptionalPromise<boolean>
delete(category:string, key:string): ODOptionalPromise<boolean>
delete(category:string, key:string): boolean {
delete(category:string, key:string): ODOptionalPromise<boolean> {
return super.delete(category,key)
}
exists(category:keyof ODFormattedJsonDatabaseIds_DefaultGlobal, key:string): boolean
exists(category:string, key:string): boolean
exists(category:keyof ODFormattedJsonDatabaseIds_DefaultGlobal, key:string): ODOptionalPromise<boolean>
exists(category:string, key:string): ODOptionalPromise<boolean>
exists(category:string, key:string): boolean {
exists(category:string, key:string): ODOptionalPromise<boolean> {
return super.exists(category,key)
}
getCategory<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultGlobal>(category:CategoryId): {key:string, value:ODFormattedJsonDatabaseIds_DefaultGlobal[CategoryId]}[]|undefined
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined
getCategory<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultGlobal>(category:CategoryId): ODOptionalPromise<{key:string, value:ODFormattedJsonDatabaseIds_DefaultGlobal[CategoryId]}[]|undefined>
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined>
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined {
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined> {
return super.getCategory(category)
}
}
@@ -116,38 +116,38 @@ export interface ODFormattedJsonDatabaseIds_DefaultTickets {
* This default class is made for the `tickets.json` database!
*/
export class ODFormattedJsonDatabase_DefaultTickets extends ODFormattedJsonDatabase {
set<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultTickets>(category:CategoryId, key:string, value:ODFormattedJsonDatabaseIds_DefaultTickets[CategoryId]): boolean
set(category:string, key:string, value:ODValidJsonType): boolean
set<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultTickets>(category:CategoryId, key:string, value:ODFormattedJsonDatabaseIds_DefaultTickets[CategoryId]): ODOptionalPromise<boolean>
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean>
set(category:string, key:string, value:ODValidJsonType): boolean {
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean> {
return super.set(category,key,value)
}
get<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultTickets>(category:CategoryId, key:string): ODFormattedJsonDatabaseIds_DefaultTickets[CategoryId]|undefined
get(category:string, key:string): ODValidJsonType|undefined
get<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultTickets>(category:CategoryId, key:string): ODOptionalPromise<ODFormattedJsonDatabaseIds_DefaultTickets[CategoryId]|undefined>
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined>
get(category:string, key:string): ODValidJsonType|undefined {
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined> {
return super.get(category,key)
}
delete<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultTickets>(category:CategoryId, key:string): boolean
delete(category:string, key:string): boolean
delete<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultTickets>(category:CategoryId, key:string): ODOptionalPromise<boolean>
delete(category:string, key:string): ODOptionalPromise<boolean>
delete(category:string, key:string): boolean {
delete(category:string, key:string): ODOptionalPromise<boolean> {
return super.delete(category,key)
}
exists(category:keyof ODFormattedJsonDatabaseIds_DefaultTickets, key:string): boolean
exists(category:string, key:string): boolean
exists(category:keyof ODFormattedJsonDatabaseIds_DefaultTickets, key:string): ODOptionalPromise<boolean>
exists(category:string, key:string): ODOptionalPromise<boolean>
exists(category:string, key:string): boolean {
exists(category:string, key:string): ODOptionalPromise<boolean> {
return super.exists(category,key)
}
getCategory<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultTickets>(category:CategoryId): {key:string, value:ODFormattedJsonDatabaseIds_DefaultTickets[CategoryId]}[]|undefined
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined
getCategory<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultTickets>(category:CategoryId): ODOptionalPromise<{key:string, value:ODFormattedJsonDatabaseIds_DefaultTickets[CategoryId]}[]|undefined>
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined>
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined {
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined> {
return super.getCategory(category)
}
}
@@ -167,38 +167,38 @@ export interface ODFormattedJsonDatabaseIds_DefaultUsers {
* This default class is made for the `users.json` database!
*/
export class ODFormattedJsonDatabase_DefaultUsers extends ODFormattedJsonDatabase {
set<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultUsers>(category:CategoryId, key:string, value:ODFormattedJsonDatabaseIds_DefaultUsers[CategoryId]): boolean
set(category:string, key:string, value:ODValidJsonType): boolean
set<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultUsers>(category:CategoryId, key:string, value:ODFormattedJsonDatabaseIds_DefaultUsers[CategoryId]): ODOptionalPromise<boolean>
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean>
set(category:string, key:string, value:ODValidJsonType): boolean {
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean> {
return super.set(category,key,value)
}
get<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultUsers>(category:CategoryId, key:string): ODFormattedJsonDatabaseIds_DefaultUsers[CategoryId]|undefined
get(category:string, key:string): ODValidJsonType|undefined
get<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultUsers>(category:CategoryId, key:string): ODOptionalPromise<ODFormattedJsonDatabaseIds_DefaultUsers[CategoryId]|undefined>
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined>
get(category:string, key:string): ODValidJsonType|undefined {
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined> {
return super.get(category,key)
}
delete<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultUsers>(category:CategoryId, key:string): boolean
delete(category:string, key:string): boolean
delete<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultUsers>(category:CategoryId, key:string): ODOptionalPromise<boolean>
delete(category:string, key:string): ODOptionalPromise<boolean>
delete(category:string, key:string): boolean {
delete(category:string, key:string): ODOptionalPromise<boolean> {
return super.delete(category,key)
}
exists(category:keyof ODFormattedJsonDatabaseIds_DefaultUsers, key:string): boolean
exists(category:string, key:string): boolean
exists(category:keyof ODFormattedJsonDatabaseIds_DefaultUsers, key:string): ODOptionalPromise<boolean>
exists(category:string, key:string): ODOptionalPromise<boolean>
exists(category:string, key:string): boolean {
exists(category:string, key:string): ODOptionalPromise<boolean> {
return super.exists(category,key)
}
getCategory<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultUsers>(category:CategoryId): {key:string, value:ODFormattedJsonDatabaseIds_DefaultUsers[CategoryId]}[]|undefined
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined
getCategory<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultUsers>(category:CategoryId): ODOptionalPromise<{key:string, value:ODFormattedJsonDatabaseIds_DefaultUsers[CategoryId]}[]|undefined>
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined>
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined {
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined> {
return super.getCategory(category)
}
}
@@ -219,38 +219,38 @@ export interface ODFormattedJsonDatabaseIds_DefaultOptions {
* This default class is made for the `options.json` database!
*/
export class ODFormattedJsonDatabase_DefaultOptions extends ODFormattedJsonDatabase {
set<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultOptions>(category:CategoryId, key:string, value:ODFormattedJsonDatabaseIds_DefaultOptions[CategoryId]): boolean
set(category:string, key:string, value:ODValidJsonType): boolean
set<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultOptions>(category:CategoryId, key:string, value:ODFormattedJsonDatabaseIds_DefaultOptions[CategoryId]): ODOptionalPromise<boolean>
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean>
set(category:string, key:string, value:ODValidJsonType): boolean {
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean> {
return super.set(category,key,value)
}
get<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultOptions>(category:CategoryId, key:string): ODFormattedJsonDatabaseIds_DefaultOptions[CategoryId]|undefined
get(category:string, key:string): ODValidJsonType|undefined
get<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultOptions>(category:CategoryId, key:string): ODOptionalPromise<ODFormattedJsonDatabaseIds_DefaultOptions[CategoryId]|undefined>
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined>
get(category:string, key:string): ODValidJsonType|undefined {
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined> {
return super.get(category,key)
}
delete<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultOptions>(category:CategoryId, key:string): boolean
delete(category:string, key:string): boolean
delete<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultOptions>(category:CategoryId, key:string): ODOptionalPromise<boolean>
delete(category:string, key:string): ODOptionalPromise<boolean>
delete(category:string, key:string): boolean {
delete(category:string, key:string): ODOptionalPromise<boolean> {
return super.delete(category,key)
}
exists(category:keyof ODFormattedJsonDatabaseIds_DefaultOptions, key:string): boolean
exists(category:string, key:string): boolean
exists(category:keyof ODFormattedJsonDatabaseIds_DefaultOptions, key:string): ODOptionalPromise<boolean>
exists(category:string, key:string): ODOptionalPromise<boolean>
exists(category:string, key:string): boolean {
exists(category:string, key:string): ODOptionalPromise<boolean> {
return super.exists(category,key)
}
getCategory<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultOptions>(category:CategoryId): {key:string, value:ODFormattedJsonDatabaseIds_DefaultOptions[CategoryId]}[]|undefined
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined
getCategory<CategoryId extends keyof ODFormattedJsonDatabaseIds_DefaultOptions>(category:CategoryId): ODOptionalPromise<{key:string, value:ODFormattedJsonDatabaseIds_DefaultOptions[CategoryId]}[]|undefined>
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined>
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined {
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined> {
return super.getCategory(category)
}
}
+6
View File
@@ -9,6 +9,12 @@ import { ODConsoleWarningMessage, ODDebugger } from "./console"
*/
export type ODPromiseVoid = void|Promise<void>
/**## ODOptionalPromise `type`
* This is a simple type to represent a type as normal value or a promise value.
*/
export type ODOptionalPromise<T> = T|Promise<T>
/**## ODValidButtonColor `type`
* This is a collection of all the possible button colors.
*/
+2 -2
View File
@@ -391,9 +391,9 @@ export class ODDebugFileManager {
writeText(text:string){
this.#writeDebugFile(text)
}
/**Write a custom note to the debug file (starting with `[NODE]:`) */
/**Write a custom note to the debug file (starting with `[NOTE]:`) */
writeNote(text:string){
this.#writeDebugFile("[NODE]: "+text)
this.#writeDebugFile("[NOTE]: "+text)
}
}
+19 -19
View File
@@ -1,7 +1,7 @@
///////////////////////////////////////
//DATABASE MODULE
///////////////////////////////////////
import { ODId, ODManager, ODManagerData, ODSystemError, ODValidId, ODValidJsonType } from "./base"
import { ODId, ODManager, ODManagerData, ODOptionalPromise, ODSystemError, ODValidId, ODValidJsonType } from "./base"
import fs from "fs"
import nodepath from "path"
import { ODDebugger } from "./console"
@@ -87,27 +87,27 @@ export class ODDatabase extends ODManagerData {
file: string = ""
/**Add/Overwrite a specific category & key in the database. Returns `true` when overwritten. */
set(category:string, key:string, value:ODValidJsonType): boolean {
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean> {
return false
}
/**Get a specific category & key in the database */
get(category:string, key:string): ODValidJsonType|undefined {
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined> {
return undefined
}
/**Delete a specific category & key in the database */
delete(category:string, key:string): boolean {
delete(category:string, key:string): ODOptionalPromise<boolean> {
return false
}
/**Check if a specific category & key exists in the database */
exists(category:string, key:string): boolean {
exists(category:string, key:string): ODOptionalPromise<boolean> {
return false
}
/**Get a specific category in the database */
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined {
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined> {
return undefined
}
/**Get all values in the database */
getAll(): ODJsonDatabaseStructure {
getAll(): ODOptionalPromise<ODJsonDatabaseStructure> {
return []
}
}
@@ -145,7 +145,7 @@ export class ODJsonDatabase extends ODDatabase {
* const didOverwrite = database.setData("category","key","value") //value can be any of the valid types
* //You need an ODJsonDatabase class named "database" for this example to work!
*/
set(category:string, key:string, value:ODValidJsonType): boolean {
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean> {
const currentList = this.#system.getData()
const currentData = currentList.find((d) => (d.category === category) && (d.key === key))
@@ -164,7 +164,7 @@ export class ODJsonDatabase extends ODDatabase {
* const data = database.getData("category","key") //data will be the value
* //You need an ODJsonDatabase class named "database" for this example to work!
*/
get(category:string, key:string): ODValidJsonType|undefined {
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined> {
const currentList = this.#system.getData()
const tempresult = currentList.find((d) => (d.category === category) && (d.key === key))
return tempresult ? tempresult.value : undefined
@@ -174,7 +174,7 @@ export class ODJsonDatabase extends ODDatabase {
* const didExist = database.deleteData("category","key") //delete this value
* //You need an ODJsonDatabase class named "database" for this example to work!
*/
delete(category:string, key:string): boolean {
delete(category:string, key:string): ODOptionalPromise<boolean> {
const currentList = this.#system.getData()
const currentData = currentList.find((d) => (d.category === category) && (d.key === key))
if (currentData) currentList.splice(currentList.indexOf(currentData),1)
@@ -183,19 +183,19 @@ export class ODJsonDatabase extends ODDatabase {
return currentData ? true : false
}
/**Check if a value of `category` & `key` exists. Returns `false` when non-existent! */
exists(category:string, key:string): boolean {
exists(category:string, key:string): ODOptionalPromise<boolean> {
const currentList = this.#system.getData()
const tempresult = currentList.find((d) => (d.category === category) && (d.key === key))
return tempresult ? true : false
}
/**Get all values in `category`. Returns `undefined` when non-existent! */
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined {
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined> {
const currentList = this.#system.getData()
const tempresult = currentList.filter((d) => (d.category === category))
return tempresult ? tempresult.map((data) => {return {key:data.key,value:data.value}}) : undefined
}
/**Get all values in `category`. */
getAll(): ODJsonDatabaseStructure {
getAll(): ODOptionalPromise<ODJsonDatabaseStructure> {
return this.#system.getData()
}
@@ -249,7 +249,7 @@ export class ODFormattedJsonDatabase extends ODDatabase {
* const didOverwrite = database.setData("category","key","value") //value can be any of the valid types
* //You need an ODFormattedJsonDatabase class named "database" for this example to work!
*/
set(category:string, key:string, value:ODValidJsonType): boolean {
set(category:string, key:string, value:ODValidJsonType): ODOptionalPromise<boolean> {
const currentList = this.#system.getData()
const currentData = currentList.find((d) => (d.category === category) && (d.key === key))
@@ -268,7 +268,7 @@ export class ODFormattedJsonDatabase extends ODDatabase {
* const data = database.getData("category","key") //data will be the value
* //You need an ODFormattedJsonDatabase class named "database" for this example to work!
*/
get(category:string, key:string): ODValidJsonType|undefined {
get(category:string, key:string): ODOptionalPromise<ODValidJsonType|undefined> {
const currentList = this.#system.getData()
const tempresult = currentList.find((d) => (d.category === category) && (d.key === key))
return tempresult ? tempresult.value : undefined
@@ -278,7 +278,7 @@ export class ODFormattedJsonDatabase extends ODDatabase {
* const didExist = database.deleteData("category","key") //delete this value
* //You need an ODFormattedJsonDatabase class named "database" for this example to work!
*/
delete(category:string, key:string): boolean {
delete(category:string, key:string): ODOptionalPromise<boolean> {
const currentList = this.#system.getData()
const currentData = currentList.find((d) => (d.category === category) && (d.key === key))
if (currentData) currentList.splice(currentList.indexOf(currentData),1)
@@ -287,19 +287,19 @@ export class ODFormattedJsonDatabase extends ODDatabase {
return currentData ? true : false
}
/**Check if a value of `category` & `key` exists. Returns `false` when non-existent! */
exists(category:string, key:string): boolean {
exists(category:string, key:string): ODOptionalPromise<boolean> {
const currentList = this.#system.getData()
const tempresult = currentList.find((d) => (d.category === category) && (d.key === key))
return tempresult ? true : false
}
/**Get all values in `category`. Returns `undefined` when non-existent! */
getCategory(category:string): {key:string, value:ODValidJsonType}[]|undefined {
getCategory(category:string): ODOptionalPromise<{key:string, value:ODValidJsonType}[]|undefined> {
const currentList = this.#system.getData()
const tempresult = currentList.filter((d) => (d.category === category))
return tempresult ? tempresult.map((data) => {return {key:data.key,value:data.value}}) : undefined
}
/**Get all values in `category`. */
getAll(): ODJsonDatabaseStructure {
getAll(): ODOptionalPromise<ODJsonDatabaseStructure> {
return this.#system.getData()
}
+2
View File
@@ -19,7 +19,9 @@ export class ODLanguage extends ODManagerData {
file: string
/**The path to the file relative to the main directory. */
path: string
/**The raw object data of the translation. */
data: any
/**The metadata of the language if available. */
metadata: ODLanguageMetadata|null = null
constructor(id:ODValidId, file:string, customPath?:string){
+6 -4
View File
@@ -42,13 +42,14 @@ export class ODStatsManager extends ODManager<ODStatScope> {
//filter out the deletable stats
const deletableStats: ODJsonDatabaseStructure = []
this.database.getAll().forEach((data) => {
const data = await this.database.getAll()
data.forEach((data) => {
if (!validCategories.includes(data.category)) deletableStats.push(data)
})
//do additional deletion
for (const cb of this.#initListeners){
await cb(this.database.getAll(),deletableStats)
await cb(data,deletableStats)
}
//delete all deletable stats
@@ -57,9 +58,10 @@ export class ODStatsManager extends ODManager<ODStatScope> {
this.database.delete(data.category,data.key)
})
}
reset(){
async reset(){
if (!this.database) return
this.database.getAll().forEach((data) => {
const data = await this.database.getAll()
data.forEach((data) => {
if (!this.database) return
this.database.delete(data.category,data.key)
})