233 lines
5.5 KiB
TypeScript
233 lines
5.5 KiB
TypeScript
import GenericDBApi from './base.api.ts';
|
|
import db from '../models/index.ts';
|
|
import type {
|
|
DbFindByOptions,
|
|
EntityRecord,
|
|
GlobalUiControlDefaultsData,
|
|
GlobalUiControlDefaultsFieldMapping,
|
|
GlobalUiControlDefaultsModel,
|
|
GlobalUiControlDefaultsRecord,
|
|
GlobalUiControlSettingsJson,
|
|
ServiceOptions,
|
|
UpdateOptions,
|
|
} from '../../types/index.ts';
|
|
|
|
function isMissingTableError(error: unknown): boolean {
|
|
if (!error || typeof error !== 'object' || !('original' in error)) {
|
|
return false;
|
|
}
|
|
|
|
const original = error.original;
|
|
if (!original || typeof original !== 'object' || !('code' in original)) {
|
|
return false;
|
|
}
|
|
|
|
return original.code === '42P01';
|
|
}
|
|
|
|
const DEFAULT_SETTINGS: GlobalUiControlSettingsJson = {
|
|
fullscreen: {
|
|
enabled: true,
|
|
hidden: false,
|
|
xPercent: 92.75,
|
|
yPercent: 6,
|
|
anchor: 'center',
|
|
buttonSizePercent: 2.6,
|
|
iconSizePercent: 1.35,
|
|
defaultIconUrl: '',
|
|
activeIconUrl: '',
|
|
defaultBackgroundColor: '#2563EB',
|
|
activeBackgroundColor: '#2563EB',
|
|
hoverBackgroundColor: '#1D4ED8',
|
|
color: '#FFFFFF',
|
|
defaultBorderColor: '#2563EB',
|
|
activeBorderColor: '#2563EB',
|
|
borderRadiusPercent: 0.42,
|
|
opacity: 1,
|
|
boxShadow: '',
|
|
zIndex: 900,
|
|
order: 2,
|
|
},
|
|
sound: {
|
|
enabled: true,
|
|
hidden: false,
|
|
xPercent: 96,
|
|
yPercent: 6,
|
|
anchor: 'center',
|
|
buttonSizePercent: 2.6,
|
|
iconSizePercent: 1.35,
|
|
defaultIconUrl: '',
|
|
activeIconUrl: '',
|
|
defaultBackgroundColor: '#2563EB',
|
|
activeBackgroundColor: '#2563EB',
|
|
hoverBackgroundColor: '#1D4ED8',
|
|
color: '#FFFFFF',
|
|
defaultBorderColor: '#2563EB',
|
|
activeBorderColor: '#2563EB',
|
|
borderRadiusPercent: 0.42,
|
|
opacity: 1,
|
|
boxShadow: '',
|
|
zIndex: 900,
|
|
order: 3,
|
|
},
|
|
offline: {
|
|
enabled: true,
|
|
hidden: false,
|
|
xPercent: 89.5,
|
|
yPercent: 6,
|
|
anchor: 'center',
|
|
buttonSizePercent: 2.6,
|
|
iconSizePercent: 1.35,
|
|
defaultIconUrl: '',
|
|
activeIconUrl: '',
|
|
defaultBackgroundColor: '#2563EB',
|
|
activeBackgroundColor: '#059669',
|
|
hoverBackgroundColor: '#1D4ED8',
|
|
color: '#FFFFFF',
|
|
defaultBorderColor: '#2563EB',
|
|
activeBorderColor: '#059669',
|
|
borderRadiusPercent: 0.42,
|
|
opacity: 1,
|
|
boxShadow: '',
|
|
zIndex: 900,
|
|
order: 1,
|
|
},
|
|
};
|
|
|
|
class Global_ui_control_defaultsDBApi extends GenericDBApi {
|
|
static initializationPromise: Promise<void> | null = null;
|
|
|
|
static override get MODEL(): GlobalUiControlDefaultsModel {
|
|
return db.global_ui_control_defaults;
|
|
}
|
|
|
|
static override get TABLE_NAME(): string {
|
|
return 'global_ui_control_defaults';
|
|
}
|
|
|
|
static override get SEARCHABLE_FIELDS(): string[] {
|
|
return [];
|
|
}
|
|
|
|
static override get RANGE_FIELDS(): string[] {
|
|
return [];
|
|
}
|
|
|
|
static override get ENUM_FIELDS(): string[] {
|
|
return [];
|
|
}
|
|
|
|
static get DEFAULT_SETTINGS(): GlobalUiControlSettingsJson {
|
|
return DEFAULT_SETTINGS;
|
|
}
|
|
|
|
static override getFieldMapping(
|
|
data: GlobalUiControlDefaultsData,
|
|
): GlobalUiControlDefaultsFieldMapping {
|
|
return {
|
|
id: data.id || undefined,
|
|
settings_json: data.settings_json || data.settings || DEFAULT_SETTINGS,
|
|
};
|
|
}
|
|
|
|
static async ensureInitialized(): Promise<void> {
|
|
if (!this.initializationPromise) {
|
|
this.initializationPromise = (async () => {
|
|
let count = 0;
|
|
|
|
try {
|
|
count = await this.MODEL.count();
|
|
} catch (error) {
|
|
if (!isMissingTableError(error)) {
|
|
throw error;
|
|
}
|
|
|
|
await this.MODEL.sync();
|
|
count = await this.MODEL.count();
|
|
}
|
|
|
|
if (count > 0) return;
|
|
|
|
const now = new Date();
|
|
await this.MODEL.create({
|
|
settings_json: DEFAULT_SETTINGS,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
})().catch((error) => {
|
|
this.initializationPromise = null;
|
|
throw error;
|
|
});
|
|
}
|
|
|
|
await this.initializationPromise;
|
|
}
|
|
|
|
static async findOne(
|
|
options: ServiceOptions = {},
|
|
): Promise<GlobalUiControlDefaultsRecord | null> {
|
|
await this.ensureInitialized();
|
|
|
|
const record = await this.MODEL.findOne({
|
|
transaction: options.transaction,
|
|
});
|
|
|
|
if (!record) return null;
|
|
return record.get({ plain: true });
|
|
}
|
|
|
|
static override async update({
|
|
id,
|
|
data,
|
|
currentUser,
|
|
transaction,
|
|
runtimeContext,
|
|
}: UpdateOptions<GlobalUiControlDefaultsData>): Promise<EntityRecord> {
|
|
await this.ensureInitialized();
|
|
|
|
const updateOptions: UpdateOptions<GlobalUiControlDefaultsData> = {
|
|
id,
|
|
data,
|
|
currentUser: currentUser ?? null,
|
|
};
|
|
|
|
if (transaction) {
|
|
updateOptions.transaction = transaction;
|
|
}
|
|
|
|
if (runtimeContext) {
|
|
updateOptions.runtimeContext = runtimeContext;
|
|
}
|
|
|
|
return super.update(updateOptions);
|
|
}
|
|
|
|
static override async findBy(
|
|
where: { id: string },
|
|
options?: ServiceOptions,
|
|
): Promise<EntityRecord | null>;
|
|
static override async findBy(
|
|
options: DbFindByOptions,
|
|
): Promise<EntityRecord | null>;
|
|
static override async findBy(
|
|
whereOrOptions: { id: string } | DbFindByOptions,
|
|
options: ServiceOptions = {},
|
|
): Promise<EntityRecord | null> {
|
|
await this.ensureInitialized();
|
|
|
|
if ('where' in whereOrOptions) {
|
|
return super.findBy(whereOrOptions);
|
|
}
|
|
|
|
const findOptions: DbFindByOptions = { where: whereOrOptions };
|
|
|
|
if (options.transaction) {
|
|
findOptions.transaction = options.transaction;
|
|
}
|
|
|
|
return super.findBy(findOptions);
|
|
}
|
|
}
|
|
|
|
export default Global_ui_control_defaultsDBApi;
|