mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
fix(db): store addons.enabled as the integer flag on Postgres too
addons.enabled was the one flag MikroORM's boolean type declared as a Postgres boolean, while SQLite and every other flag hold 0/1, so an addon migration's usual SET enabled = 1 would fail there and a Kysely read would answer true instead of 1. The column now uses IntegerFlagType: the entity still reads a boolean, both engines store 0/1, and the regenerated Postgres baseline declares it bigint default 0. The entity generator renders the type for the columns in BOOLEAN_COLUMNS.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { EntityRepositoryType, type Opt, defineEntity, p } from '@mikro-orm/core';
|
||||
import { AddonsRepository } from '../repositories/Addons.repository';
|
||||
import { IntegerFlagType } from '../types';
|
||||
|
||||
export interface AddonConfig {
|
||||
[key: string]: unknown;
|
||||
@@ -12,7 +13,7 @@ export class Addons {
|
||||
description?: string | null;
|
||||
type: string & Opt = 'global';
|
||||
icon?: string | null = 'Puzzle';
|
||||
enabled?: boolean | null = false;
|
||||
enabled?: boolean | null;
|
||||
config?: AddonConfig | null;
|
||||
sort_order?: number | null = 0;
|
||||
}
|
||||
@@ -26,7 +27,7 @@ export const AddonsSchema = defineEntity({
|
||||
description: p.text().nullable(),
|
||||
type: p.text().default('global'),
|
||||
icon: p.text().nullable(),
|
||||
enabled: p.boolean().nullable(),
|
||||
enabled: p.type(IntegerFlagType).nullable().default(false),
|
||||
config: p.json<AddonConfig>().nullable().default('{}'),
|
||||
sort_order: p.integer().nullable(),
|
||||
},
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
export { DB_TIMESTAMP_RE, DbTimestampType, dbNow, parseDbTimestamp, utcSuffix } from './db-timestamp.type';
|
||||
export type { DbTimestamp } from './db-timestamp.type';
|
||||
export { IntegerFlagType } from './integer-flag.type';
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Type, type EntityProperty, type Platform } from '@mikro-orm/core';
|
||||
|
||||
type FlagValue = boolean | null | undefined;
|
||||
type FlagColumn = number | null | undefined;
|
||||
|
||||
/**
|
||||
* A flag the entity reads as a boolean and the database stores as SQLite
|
||||
* stores every flag: the integer 0 or 1, in an INTEGER column (a `bigint` on
|
||||
* Postgres, `schema-adjust.ts`).
|
||||
*
|
||||
* MikroORM's own `BooleanType` declares a `boolean` column on Postgres and
|
||||
* writes `true`/`false`, so that one column would hold a different type on
|
||||
* each engine: a migration's `SET enabled = 1` would fail there, and a Kysely
|
||||
* read would answer `true` where SQLite answers 1. This type keeps the storage
|
||||
* model of every other flag on both engines while the entity's field stays a
|
||||
* boolean. A query value goes through it as well, so `find({ enabled: true })`
|
||||
* compares with 1.
|
||||
*/
|
||||
export class IntegerFlagType extends Type<FlagValue, FlagColumn> {
|
||||
override convertToDatabaseValue(value: FlagValue): FlagColumn {
|
||||
if (value === null || value === undefined) return value;
|
||||
return value ? 1 : 0;
|
||||
}
|
||||
|
||||
/** 0/1 from either driver (Postgres's `bigint` reads as a number); a boolean from a value set in JS passes through. */
|
||||
override convertToJSValue(value: FlagColumn | boolean): FlagValue {
|
||||
if (value === null || value === undefined) return value;
|
||||
return Boolean(Number(value));
|
||||
}
|
||||
|
||||
override getColumnType(prop: EntityProperty, platform: Platform): string {
|
||||
return platform.getIntegerTypeDeclarationSQL(prop);
|
||||
}
|
||||
|
||||
override compareAsType(): string {
|
||||
return 'boolean';
|
||||
}
|
||||
|
||||
override get runtimeType(): string {
|
||||
return 'boolean';
|
||||
}
|
||||
|
||||
override ensureComparable(): boolean {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user