Browse Source

Small fixes

Tidy up some parts of the codebase.
master
TheoryOfNekomata 8 months ago
parent
commit
0b3764267c
3 changed files with 28 additions and 27 deletions
  1. +11
    -7
      packages/core/src/backend/data-source.ts
  2. +1
    -2
      packages/core/test/utils.ts
  3. +16
    -18
      packages/data-sources/file-jsonl/src/index.ts

+ 11
- 7
packages/core/src/backend/data-source.ts View File

@@ -7,16 +7,20 @@ type TotalCount = number;

type DeleteResult = unknown;

export interface DataSource<Schema extends object = object, Query extends object = object, ID extends string = string> {
export interface DataSource<
ItemData extends object = object,
Query extends object = object,
ID extends string = string
> {
initialize(): Promise<unknown>;
getTotalCount?(query?: Query): Promise<TotalCount>;
getMultiple(query?: Query): Promise<Schema[]>;
getById(id: ID): Promise<Schema | null>;
getSingle?(query?: Query): Promise<Schema | null>;
create(data: Schema): Promise<Schema>;
getMultiple(query?: Query): Promise<ItemData[]>;
getById(id: ID): Promise<ItemData | null>;
getSingle?(query?: Query): Promise<ItemData | null>;
create(data: ItemData): Promise<ItemData>;
delete(id: ID): Promise<DeleteResult>;
emplace(id: ID, data: Schema): Promise<[Schema, IsCreated]>;
patch(id: ID, data: Partial<Schema>): Promise<Schema | null>;
emplace(id: ID, data: ItemData): Promise<[ItemData, IsCreated]>;
patch(id: ID, data: Partial<ItemData>): Promise<ItemData | null>;
prepareResource(resource: Resource): void;
newId(): Promise<ID>;
}


+ 1
- 2
packages/core/test/utils.ts View File

@@ -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 {


+ 16
- 18
packages/data-sources/file-jsonl/src/index.ts View File

@@ -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<any>;
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<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());


Loading…
Cancel
Save