pallas-db
Version:
All in the name
123 lines (122 loc) • 3.34 kB
TypeScript
import { Model } from 'sequelize';
type PrimitiveValue = string | number | boolean | null | undefined;
type ComplexObject = {
[key: string]: PrimitiveValue | ComplexObject | Array<PrimitiveValue | ComplexObject>;
};
type AnyValue = PrimitiveValue | ComplexObject | Array<PrimitiveValue | ComplexObject>;
interface PallasDBOptions {
/**
* Database dialect type
* @required
*/
dialect: 'postgres' | 'sqlite' | 'mysql' | 'memory' | 'json';
/**
* Database login credentials
*/
login?: LoginBody;
/**
* Path to store the .sqlite file (sqlite) or .json file (json)
*/
filePath?: string;
/**
* List of table names to register
* @example
```
tables: ["user", "rank", "login"]
```
* You can only use tables registered here!
*/
tables: string[];
/**
* Enable/Disable verbose logging mode
*/
enableVerbose?: boolean;
}
interface LoginBody {
/**
* Database host (e.g. 127.0.0.1)
* @requires
*/
host: string;
/**
* Database port (default: 5432 for postgres, 3306 for mysql)
* @default 5432
*/
port?: number;
/**
* Database username
* @requires
*/
username: string;
/**
* Database password
* @requires
*/
password: string;
/**
* Database name
* @requires
*/
database: string;
}
export interface DataModel extends Model {
id: string;
value: AnyValue;
}
declare class PallasDB {
/**
* Validates that a value respects the AnyValue schema (PrimitiveValue, ComplexObject or Array).
* Rejects undefined or null values.
*/
private validateValue;
private validateKey;
/**
* Repairs the database: removes or corrects abnormal rows.
* Logs the corrections made.
*/
repair(): Promise<void>;
private models;
private options;
private sequelize;
private currentTable;
constructor(options: PallasDBOptions, initialModels?: {
[key: string]: typeof Model & {
new (): DataModel;
};
}, initialTable?: string);
private secureTableName;
private getColumnsName;
private getMemoryTable;
private getJSONStorage;
table(tableName: string): PallasDB;
private verboseLog;
private initModels;
forceSync(): Promise<void>;
private ensureModelsInitialized;
private getBaseKey;
private getNestedKeys;
private setNestedValue;
private getNestedValue;
private deleteNestedKey;
get(key: string, defaultValue?: any): Promise<any>;
set(key: string, value: any): Promise<void>;
pull(key: string, element: any): Promise<void>;
add(key: string, amount: number): Promise<void>;
sub(key: string, amount: number): Promise<void>;
delete(key: string): Promise<void>;
cache(key: string, value: AnyValue, time: number): Promise<void>;
push(key: string, element: any): Promise<void>;
deleteAll(): Promise<void>;
has(key: string): Promise<boolean>;
all(): Promise<Array<{
id: string;
value: any;
}>>;
/**
* Memory and JSON dialect specific method to get usage statistics
*/
getMemoryStats(): {
[tableName: string]: number;
} | null;
}
export { PallasDB, PallasDBOptions, AnyValue, ComplexObject, PrimitiveValue, LoginBody };