fix: make LevelDocument._id and _documentType required

These properties are always present after document creation.
Update PluginDevice constructor to require id parameter.
This commit is contained in:
Koushik Dutta
2026-04-02 12:49:18 -07:00
parent 0d4a0d2c2c
commit cfa9e2a200
2 changed files with 8 additions and 8 deletions

View File

@@ -2,8 +2,8 @@ import { LevelDocument } from "./level";
import { ScryptedNativeId, SystemDeviceState } from "@scrypted/types";
export class ScryptedDocument implements LevelDocument {
_id?: string;
_documentType?: string;
_id: string;
_documentType: string;
}
export class Settings extends ScryptedDocument {
@@ -31,7 +31,7 @@ export class ScryptedAlert extends ScryptedDocument {
}
export class PluginDevice extends ScryptedDocument {
constructor(id?: string) {
constructor(id: string) {
super();
this._id = id;
}

View File

@@ -1,8 +1,8 @@
import { GetOptions, Level, OpenOptions, PutOptions } from 'level';
export interface LevelDocument {
_id?: any;
_documentType?: string;
_id: any;
_documentType: string;
}
export interface LevelDocumentConstructor<T extends LevelDocument> {
@@ -20,7 +20,7 @@ export class WrappedLevel extends Level<string, string | number> {
override async open(): Promise<void>;
override async open(options?: OpenOptions): Promise<void> {
await super.open(options);
await super.open(options!);
try {
this.curId = parseInt(await this.get('_id') as string);
}
@@ -35,7 +35,7 @@ export class WrappedLevel extends Level<string, string | number> {
try {
const _documentType = documentConstructor.name;
const key = `${_documentType}/${_id}`;
const json = await this.get(key, options)
const json = await this.get(key, options!)
return createLevelDocument(documentConstructor, json);
}
catch (e) {
@@ -83,7 +83,7 @@ export class WrappedLevel extends Level<string, string | number> {
value._documentType = _documentType;
const key = `${_documentType}/${value._id}`;
await this.put(key, JSON.stringify(value), options);
await this.put(key, JSON.stringify(value), options!);
return value;
};