/
githubmirror
/
json-server
Обзор
Документация
Войти
/
githubmirror
/
json-server
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/adapters/normalized-adapter.ts
47 строк
1 KB
Copilot
feat: auto-insert `$schema` when missing from DB file on startup (#1717)
28 фев 2026, 03:41
Не верифицирован
28 фев 2026, 03:41
037609c
Код
Авторство
О чём код?
import type { Adapter } from 'lowdb' import { randomId } from '../random-id.ts' import type { Data, Item } from '../service.ts' export const DEFAULT_SCHEMA_PATH = './node_modules/json-server/schema.json' export type RawData = Record<string, Item[] | Item | string | undefined> & { $schema?: string } export class NormalizedAdapter implements Adapter<Data> { #adapter: Adapter<RawData> constructor(adapter: Adapter<RawData>) { this.#adapter = adapter } async read(): Promise<Data | null> { const data = await this.#adapter.read() if (data === null) { return null } delete data['$schema'] for (const value of Object.values(data)) { if (Array.isArray(value)) { for (const item of value) { if (typeof item['id'] === 'number') { item['id'] = item['id'].toString() } if (item['id'] === undefined) { item['id'] = randomId() } } } } return data as Data } async write(data: Data): Promise<void> { await this.#adapter.write({ ...data, $schema: DEFAULT_SCHEMA_PATH }) } }