From 0b3764267c208bdf814be8b52cd2c57977d26b39 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Tue, 16 Apr 2024 14:56:39 +0800 Subject: [PATCH] Small fixes Tidy up some parts of the codebase. --- packages/core/src/backend/data-source.ts | 18 ++++++---- packages/core/test/utils.ts | 3 +- packages/data-sources/file-jsonl/src/index.ts | 34 +++++++++---------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/packages/core/src/backend/data-source.ts b/packages/core/src/backend/data-source.ts index 0c39aea..0284484 100644 --- a/packages/core/src/backend/data-source.ts +++ b/packages/core/src/backend/data-source.ts @@ -7,16 +7,20 @@ type TotalCount = number; type DeleteResult = unknown; -export interface DataSource { +export interface DataSource< + ItemData extends object = object, + Query extends object = object, + ID extends string = string +> { initialize(): Promise; getTotalCount?(query?: Query): Promise; - getMultiple(query?: Query): Promise; - getById(id: ID): Promise; - getSingle?(query?: Query): Promise; - create(data: Schema): Promise; + getMultiple(query?: Query): Promise; + getById(id: ID): Promise; + getSingle?(query?: Query): Promise; + create(data: ItemData): Promise; delete(id: ID): Promise; - emplace(id: ID, data: Schema): Promise<[Schema, IsCreated]>; - patch(id: ID, data: Partial): Promise; + emplace(id: ID, data: ItemData): Promise<[ItemData, IsCreated]>; + patch(id: ID, data: Partial): Promise; prepareResource(resource: Resource): void; newId(): Promise; } diff --git a/packages/core/test/utils.ts b/packages/core/test/utils.ts index 8ee3243..dd39fbc 100644 --- a/packages/core/test/utils.ts +++ b/packages/core/test/utils.ts @@ -1,6 +1,5 @@ import {IncomingHttpHeaders, IncomingMessage, OutgoingHttpHeaders, request, RequestOptions} from 'http'; -import {Method} from '../src/backend/common'; -import {DataSource} from '../src/backend/data-source'; +import {Method, DataSource} from '../src/backend'; import {FALLBACK_LANGUAGE, Language} from '../src/common'; interface ClientParams { diff --git a/packages/data-sources/file-jsonl/src/index.ts b/packages/data-sources/file-jsonl/src/index.ts index 433ffb8..eefadd4 100644 --- a/packages/data-sources/file-jsonl/src/index.ts +++ b/packages/data-sources/file-jsonl/src/index.ts @@ -1,14 +1,12 @@ -import {readFile, writeFile} from 'fs/promises'; -import {join} from 'path'; +import { readFile, writeFile } from 'fs/promises'; +import { join } from 'path'; import { Resource, validation as v } from '@modal-sh/yasumi'; import { DataSource, ResourceIdConfig } from '@modal-sh/yasumi/backend'; import assert from 'assert'; -class ResourceNotPreparedError extends Error {} +export class ResourceNotPreparedError extends Error {} -const resourceNotPreparedError = new ResourceNotPreparedError(); - -class ResourceIdNotDesignatedError extends Error {} +export class ResourceIdNotDesignatedError extends Error {} export class JsonLinesDataSource< Schema extends v.BaseSchema = v.BaseSchema, @@ -38,7 +36,7 @@ export class JsonLinesDataSource< } async initialize() { - assert(typeof this.path === 'string', resourceNotPreparedError); + assert(typeof this.path === 'string', new ResourceNotPreparedError()); try { const fileContents = await readFile(this.path, 'utf-8'); @@ -59,14 +57,14 @@ export class JsonLinesDataSource< async newId() { const idConfig = this.resource?.state.shared.get('idConfig') as ResourceIdConfig; - assert(typeof idConfig !== 'undefined', resourceNotPreparedError); + assert(typeof idConfig !== 'undefined', new ResourceNotPreparedError()); const theNewId = await idConfig.generationStrategy(this); return theNewId as string; } async getById(idSerialized: string) { - assert(typeof this.resource !== 'undefined', resourceNotPreparedError); - assert(typeof this.path === 'string', resourceNotPreparedError); + assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); + assert(typeof this.path === 'string', new ResourceNotPreparedError()); const idAttr = this.resource.state.shared.get('idAttr'); assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); @@ -87,8 +85,8 @@ export class JsonLinesDataSource< } async create(data: Data) { - assert(typeof this.resource !== 'undefined', resourceNotPreparedError); - assert(typeof this.path === 'string', resourceNotPreparedError); + assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); + assert(typeof this.path === 'string', new ResourceNotPreparedError()); const idAttr = this.resource.state.shared.get('idAttr'); assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); @@ -115,8 +113,8 @@ export class JsonLinesDataSource< } async delete(idSerialized: string) { - assert(typeof this.resource !== 'undefined', resourceNotPreparedError); - assert(typeof this.path === 'string', resourceNotPreparedError); + assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); + assert(typeof this.path === 'string', new ResourceNotPreparedError()); const idAttr = this.resource.state.shared.get('idAttr'); assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); @@ -135,8 +133,8 @@ export class JsonLinesDataSource< } async emplace(idSerialized: string, dataWithId: Data) { - assert(typeof this.resource !== 'undefined', resourceNotPreparedError); - assert(typeof this.path === 'string', resourceNotPreparedError); + assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); + assert(typeof this.path === 'string', new ResourceNotPreparedError()); const idAttr = this.resource.state.shared.get('idAttr'); assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError()); @@ -171,8 +169,8 @@ export class JsonLinesDataSource< } async patch(idSerialized: string, data: Partial) { - assert(typeof this.resource !== 'undefined', resourceNotPreparedError); - assert(typeof this.path === 'string', resourceNotPreparedError); + assert(typeof this.resource !== 'undefined', new ResourceNotPreparedError()); + assert(typeof this.path === 'string', new ResourceNotPreparedError()); const idAttr = this.resource.state.shared.get('idAttr'); assert(typeof idAttr === 'string', new ResourceIdNotDesignatedError());