diff --git a/packages/core/package.json b/packages/core/package.json index 6ff2344..cc2df5c 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -48,5 +48,47 @@ "negotiator": "^0.6.3", "tsx": "^4.7.1", "valibot": "^0.30.0" + }, + "types": "./dist/types/common/index.d.ts", + "main": "./dist/cjs/production/index.js", + "module": "./dist/esm/production/index.js", + "exports": { + ".": { + "development": { + "require": "./dist/cjs/development/index.js", + "import": "./dist/esm/development/index.js" + }, + "require": "./dist/cjs/production/index.js", + "import": "./dist/esm/production/index.js", + "types": "./dist/types/common/index.d.ts" + }, + "./backend": { + "development": { + "require": "./dist/cjs/development/backend.js", + "import": "./dist/esm/development/backend.js" + }, + "require": "./dist/cjs/production/backend.js", + "import": "./dist/esm/production/backend.js", + "types": "./dist/types/backend/index.d.ts" + }, + "./client": { + "development": { + "require": "./dist/cjs/development/client.js", + "import": "./dist/esm/development/client.js" + }, + "require": "./dist/cjs/production/client.js", + "import": "./dist/esm/production/client.js", + "types": "./dist/types/client/index.d.ts" + } + }, + "typesVersions": { + "*": { + "backend": [ + "./dist/types/backend/index.d.ts" + ], + "client": [ + "./dist/types/client/index.d.ts" + ] + } } } diff --git a/packages/core/src/backend/data-sources/file-jsonl.ts b/packages/core/src/backend/data-sources/file-jsonl.ts deleted file mode 100644 index ce3220d..0000000 --- a/packages/core/src/backend/data-sources/file-jsonl.ts +++ /dev/null @@ -1,270 +0,0 @@ -import {readFile, writeFile} from 'fs/promises'; -import {join} from 'path'; -import {DataSource as DataSourceInterface, ResourceIdConfig} from '../data-source'; -import {Resource} from '../../common'; -import * as v from 'valibot'; - -declare module '../../common' { - interface Resource< - Schema extends v.BaseSchema = v.BaseSchema, - CurrentName extends string = string, - CurrentRouteName extends string = string, - CurrentIdAttr extends string = string, - IdSchema extends v.BaseSchema = v.BaseSchema - > { - dataSource?: DataSourceInterface; - id( - newIdAttr: NewIdAttr, - params: ResourceIdConfig - ): Resource; - fullText(fullTextAttr: string): this; - } -} - -export class DataSource> implements DataSourceInterface { - private path?: string; - - private resource?: Resource; - - data: T[] = []; - - constructor(private readonly baseDir = '') { - // noop - } - - prepareResource< - Schema extends v.BaseSchema = v.BaseSchema, - CurrentName extends string = string, - CurrentRouteName extends string = string - >(resource: Resource) { - this.path = join(this.baseDir, `${resource.state.routeName}.jsonl`); - resource.dataSource = resource.dataSource ?? this; - const originalResourceId = resource.id; - resource.id = (newIdAttr: NewIdAttr, params: ResourceIdConfig) => { - originalResourceId(newIdAttr, params); - return resource as Resource; - }; - this.resource = resource; - } - - async initialize() { - if (typeof this.path !== 'string') { - throw new Error('Resource not prepared.'); - } - - try { - const fileContents = await readFile(this.path, 'utf-8'); - const lines = fileContents.split('\n'); - this.data = lines.filter((l) => l.trim().length > 0).map((l) => JSON.parse(l)); - } catch (err) { - await writeFile(this.path, ''); - } - } - - async getTotalCount() { - return this.data.length; - } - - async getMultiple() { - return [...this.data]; - } - - async newId() { - const idConfig = this.resource?.state.shared.get('idConfig') as ResourceIdConfig; - if (typeof idConfig === 'undefined') { - throw new Error('Resource not prepared.'); - } - - const theNewId = await idConfig.generationStrategy(this); - return theNewId as string; - } - - async getById(idSerialized: string) { - if (typeof this.resource === 'undefined') { - throw new Error('Resource not prepared.'); - } - - if (typeof this.path !== 'string') { - throw new Error('Resource not prepared.'); - } - - const theIdAttr = this.resource.state.shared.get('idAttr'); - if (typeof theIdAttr === 'undefined') { - throw new Error('Resource not prepared.'); - } - const idAttr = theIdAttr as string; - const theIdConfigRaw = this.resource.state.shared.get('idConfig'); - if (typeof theIdConfigRaw === 'undefined') { - throw new Error('Resource not prepared.'); - } - const theIdConfig = theIdConfigRaw as ResourceIdConfig; - - const id = theIdConfig.deserialize(idSerialized); - const foundData = this.data.find((s) => s[idAttr] === id); - - if (foundData) { - return { - ...foundData - }; - } - - return null; - } - - async create(data: T) { - if (typeof this.resource === 'undefined') { - throw new Error('Resource not prepared.'); - } - - if (typeof this.path !== 'string') { - throw new Error('Resource not prepared.'); - } - - const theIdAttr = this.resource.state.shared.get('idAttr'); - if (typeof theIdAttr === 'undefined') { - throw new Error('Resource not prepared.'); - } - const idAttr = theIdAttr as string; - const theIdConfigRaw = this.resource.state.shared.get('idConfig'); - if (typeof theIdConfigRaw === 'undefined') { - throw new Error('Resource not prepared.'); - } - const theIdConfig = theIdConfigRaw as ResourceIdConfig; - - const newData = { - ...data - } as Record; - - if (idAttr in newData) { - newData[idAttr] = theIdConfig.deserialize(newData[idAttr] as string); - } - - const newCollection = [ - ...this.data, - newData - ]; - - await writeFile(this.path, newCollection.map((d) => JSON.stringify(d)).join('\n')); - - return data as T; - } - - async delete(idSerialized: string) { - if (typeof this.resource === 'undefined') { - throw new Error('Resource not prepared.'); - } - - if (typeof this.path !== 'string') { - throw new Error('Resource not prepared.'); - } - - const theIdAttr = this.resource.state.shared.get('idAttr'); - if (typeof theIdAttr === 'undefined') { - throw new Error('Resource not prepared.'); - } - const idAttr = theIdAttr as string; - const theIdConfigRaw = this.resource.state.shared.get('idConfig'); - if (typeof theIdConfigRaw === 'undefined') { - throw new Error('Resource not prepared.'); - } - const theIdConfig = theIdConfigRaw as ResourceIdConfig; - - const oldDataLength = this.data.length; - - const id = theIdConfig.deserialize(idSerialized); - const newData = this.data.filter((s) => !(s[idAttr] === id)); - - await writeFile(this.path, newData.map((d) => JSON.stringify(d)).join('\n')); - - return oldDataLength !== newData.length; - } - - async emplace(idSerialized: string, dataWithId: T) { - if (typeof this.resource === 'undefined') { - throw new Error('Resource not prepared.'); - } - - if (typeof this.path !== 'string') { - throw new Error('Resource not prepared.'); - } - - const theIdAttr = this.resource.state.shared.get('idAttr'); - if (typeof theIdAttr === 'undefined') { - throw new Error('Resource not prepared.'); - } - const idAttr = theIdAttr as string; - const theIdConfigRaw = this.resource.state.shared.get('idConfig'); - if (typeof theIdConfigRaw === 'undefined') { - throw new Error('Resource not prepared.'); - } - const theIdConfig = theIdConfigRaw as ResourceIdConfig; - - const existing = await this.getById(idSerialized); - const id = theIdConfig.deserialize(idSerialized); - const { [idAttr]: idFromResource, ...data } = dataWithId; - const dataToEmplace = { - ...data, - [idAttr]: id, - } as T; - - if (existing) { - const newData = this.data.map((d) => { - if (d[idAttr] === id) { - return dataToEmplace; - } - - return d; - }); - - await writeFile(this.path, newData.map((d) => JSON.stringify(d)).join('\n')); - - return [dataToEmplace, false] as [T, boolean]; - } - - const newData = await this.create(dataToEmplace); - return [newData, true] as [T, boolean]; - } - - async patch(idSerialized: string, data: Partial) { - if (typeof this.resource === 'undefined') { - throw new Error('Resource not prepared.'); - } - - if (typeof this.path !== 'string') { - throw new Error('Resource not prepared.'); - } - - const theIdAttr = this.resource.state.shared.get('idAttr'); - if (typeof theIdAttr === 'undefined') { - throw new Error('Resource not prepared.'); - } - const idAttr = theIdAttr as string; - const theIdConfigRaw = this.resource.state.shared.get('idConfig'); - if (typeof theIdConfigRaw === 'undefined') { - throw new Error('Resource not prepared.'); - } - const theIdConfig = theIdConfigRaw as ResourceIdConfig; - - const existing = await this.getById(idSerialized); - if (!existing) { - return null; - } - - const newItem = { - ...existing, - ...data, - } - - const id = theIdConfig.deserialize(idSerialized); - const newData = this.data.map((d) => { - if (d[idAttr] === id) { - return newItem; - } - - return d; - }); - - await writeFile(this.path, newData.map((d) => JSON.stringify(d)).join('\n')); - return newItem as T; - } -} diff --git a/packages/core/src/backend/data-sources/index.ts b/packages/core/src/backend/data-sources/index.ts deleted file mode 100644 index 36d820d..0000000 --- a/packages/core/src/backend/data-sources/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * as jsonlFile from './file-jsonl'; diff --git a/packages/core/src/backend/index.ts b/packages/core/src/backend/index.ts index ab74d0d..1243f85 100644 --- a/packages/core/src/backend/index.ts +++ b/packages/core/src/backend/index.ts @@ -1,2 +1,3 @@ export * from './core'; -export * as dataSources from './data-sources'; +export * from './common'; +export * from './data-source'; diff --git a/packages/core/src/backend/servers/http/core.ts b/packages/core/src/backend/servers/http/core.ts index d7e3469..30afe25 100644 --- a/packages/core/src/backend/servers/http/core.ts +++ b/packages/core/src/backend/servers/http/core.ts @@ -27,11 +27,12 @@ import {decorateRequestWithMethod} from './decorators/method'; import {decorateRequestWithUrl} from './decorators/url'; import {ErrorPlainResponse, PlainResponse} from './response'; import EventEmitter from 'events'; +import {DataSource} from '../../data-source'; type RequiredResource = Required>['resource']; interface ResourceWithDataSource extends Omit { - dataSource: Required>['dataSource']; + dataSource: DataSource; } interface ResourceRequestContext extends Omit { diff --git a/packages/core/src/common/resource.ts b/packages/core/src/common/resource.ts index 9adfb4f..ddceabe 100644 --- a/packages/core/src/common/resource.ts +++ b/packages/core/src/common/resource.ts @@ -1,5 +1,6 @@ import * as v from 'valibot'; import {PatchContentType} from './media-type'; +import {DataSource, ResourceIdConfig} from '../backend'; export const CAN_PATCH_VALID_VALUES = ['merge', 'delta'] as const; @@ -33,12 +34,14 @@ type CanPatch = boolean | Partial | CanPatchSpec[]; export interface Resource< Schema extends v.BaseSchema = v.BaseSchema, CurrentName extends string = string, - CurrentRouteName extends string = string + CurrentRouteName extends string = string, + CurrentIdAttr extends string = string, + IdSchema extends v.BaseSchema = v.BaseSchema > { schema: Schema; state: ResourceState; - name(n: NewName): Resource; - route(n: NewRouteName): Resource; + name(n: NewName): Resource; + route(n: NewRouteName): Resource; canFetchCollection(b?: boolean): this; canFetchItem(b?: boolean): this; canCreate(b?: boolean): this; @@ -46,6 +49,11 @@ export interface Resource< canEmplace(b?: boolean): this; canDelete(b?: boolean): this; relatedTo(resource: Resource): this; + dataSource?: DataSource; + id( + newIdAttr: NewIdAttr, + params: ResourceIdConfig + ): Resource; } export const resource = < @@ -121,7 +129,7 @@ export const resource = < resourceState.shared.set('idConfig', config); return this; }, - fullText(attrName) { + fullText(attrName: string) { const fullTextAttrs = (resourceState.shared.get('fullText') ?? new Set()) as Set; fullTextAttrs.add(attrName); resourceState.shared.set('fullText', fullTextAttrs); diff --git a/packages/core/test/features/decorators.test.ts b/packages/core/test/features/decorators.test.ts index 0194924..09d8b2e 100644 --- a/packages/core/test/features/decorators.test.ts +++ b/packages/core/test/features/decorators.test.ts @@ -1,41 +1,26 @@ -import {describe, afterAll, afterEach, beforeAll, beforeEach, it} from 'vitest'; -import {mkdtemp, rm} from 'fs/promises'; -import {join} from 'path'; -import {tmpdir} from 'os'; -import {application, resource, Resource, ResourceType, validation as v} from '../../src/common'; -import {Backend, dataSources} from '../../src/backend'; -import {autoIncrement} from '../fixtures'; -import {RequestContext} from '../../src/backend/common'; +import {describe, afterAll, beforeAll, it} from 'vitest'; +import {Application, application, resource, Resource, validation as v} from '../../src/common'; +import {Backend, DataSource, RequestContext} from '../../src/backend'; +import {createTestClient, DummyDataSource, dummyGenerationStrategy, TEST_LANGUAGE, TestClient} from '../utils'; const PORT = 3001; const HOST = '127.0.0.1'; const BASE_PATH = '/api'; const ACCEPT = 'application/json'; const ACCEPT_LANGUAGE = 'en'; +const ACCEPT_CHARSET = 'utf-8'; const CONTENT_TYPE_CHARSET = 'utf-8'; const CONTENT_TYPE = ACCEPT; describe('decorators', () => { - let baseDir: string; - beforeAll(async () => { - try { - baseDir = await mkdtemp(join(tmpdir(), 'yasumi-')); - } catch { - // noop - } - }); - afterAll(async () => { - try { - await rm(baseDir, { - recursive: true, - }); - } catch { - // noop - } - }); - let Piano: Resource; - beforeEach(() => { + let app: Application; + let dataSource: DataSource; + let backend: Backend; + let server: ReturnType; + let client: TestClient; + + beforeAll(() => { Piano = resource(v.object( { brand: v.string() @@ -45,30 +30,38 @@ describe('decorators', () => { .name('Piano' as const) .route('pianos' as const) .id('id' as const, { - generationStrategy: autoIncrement, + generationStrategy: dummyGenerationStrategy, serialize: (id) => id?.toString() ?? '0', deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0, schema: v.number(), }); - }); - let server: ReturnType; - beforeEach(() => { - const app = application({ + app = application({ name: 'piano-service', }) + .language(TEST_LANGUAGE) .resource(Piano); - const backend = app - .createBackend({ - dataSource: new dataSources.jsonlFile.DataSource(baseDir), - }) - .throwsErrorOnDeletingNotFound(); + dataSource = new DummyDataSource(); + + backend = app.createBackend({ + dataSource, + }); server = backend.createHttpServer({ basePath: BASE_PATH }); + client = createTestClient({ + host: HOST, + port: PORT, + }) + .acceptMediaType(ACCEPT) + .acceptLanguage(ACCEPT_LANGUAGE) + .acceptCharset(ACCEPT_CHARSET) + .contentType(CONTENT_TYPE) + .contentCharset(CONTENT_TYPE_CHARSET); + return new Promise((resolve, reject) => { server.on('error', (err) => { reject(err); @@ -84,7 +77,7 @@ describe('decorators', () => { }); }); - afterEach(() => new Promise((resolve, reject) => { + afterAll(() => new Promise((resolve, reject) => { server.close((err) => { if (err) { reject(err); diff --git a/packages/core/test/fixtures.ts b/packages/core/test/fixtures.ts deleted file mode 100644 index 93c1f24..0000000 --- a/packages/core/test/fixtures.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {DataSource} from '../src/backend/data-source'; - -export const autoIncrement = async (dataSource: DataSource) => { - const data = await dataSource.getMultiple() as Record[]; - - const highestId = data.reduce( - (highestId, d) => (Number(d.id) > highestId ? Number(d.id) : highestId), - -Infinity - ); - - if (Number.isFinite(highestId)) { - return (highestId + 1); - } - - return 1; -}; diff --git a/packages/core/test/handlers/http/default.test.ts b/packages/core/test/handlers/http/default.test.ts index 56182ab..8e0196d 100644 --- a/packages/core/test/handlers/http/default.test.ts +++ b/packages/core/test/handlers/http/default.test.ts @@ -9,7 +9,7 @@ import { vi, } from 'vitest'; import {constants} from 'http2'; -import {Backend} from '../../../src/backend'; +import {Backend, DataSource} from '../../../src/backend'; import { application, resource, @@ -17,9 +17,7 @@ import { Resource, Application, } from '../../../src/common'; -import { autoIncrement } from '../../fixtures'; -import {createTestClient, DummyDataSource, TEST_LANGUAGE, TestClient} from '../../utils'; -import {DataSource} from '../../../src/backend/data-source'; +import {createTestClient, DummyDataSource, dummyGenerationStrategy, TEST_LANGUAGE, TestClient} from '../../utils'; const PORT = 3000; const HOST = '127.0.0.1'; @@ -48,7 +46,7 @@ describe('happy path', () => { .name('Piano' as const) .route('pianos' as const) .id('id' as const, { - generationStrategy: autoIncrement, + generationStrategy: dummyGenerationStrategy, serialize: (id) => id?.toString() ?? '0', deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0, schema: v.number(), diff --git a/packages/core/test/handlers/http/error-handling.test.ts b/packages/core/test/handlers/http/error-handling.test.ts index 0d17cdd..094ea60 100644 --- a/packages/core/test/handlers/http/error-handling.test.ts +++ b/packages/core/test/handlers/http/error-handling.test.ts @@ -8,11 +8,16 @@ import { it, vi, } from 'vitest'; import {constants} from 'http2'; -import {Backend} from '../../../src/backend'; +import {Backend, DataSource} from '../../../src/backend'; import {application, resource, validation as v, Resource, Application, Delta} from '../../../src/common'; -import { autoIncrement } from '../../fixtures'; -import {createTestClient, TestClient, DummyDataSource, DummyError, TEST_LANGUAGE} from '../../utils'; -import {DataSource} from '../../../src/backend/data-source'; +import { + createTestClient, + TestClient, + DummyDataSource, + DummyError, + TEST_LANGUAGE, + dummyGenerationStrategy, +} from '../../utils'; const PORT = 3001; const HOST = '127.0.0.1'; @@ -41,7 +46,7 @@ describe('error handling', () => { .name('Piano' as const) .route('pianos' as const) .id('id' as const, { - generationStrategy: autoIncrement, + generationStrategy: dummyGenerationStrategy, serialize: (id) => id?.toString() ?? '0', deserialize: (id) => Number.isFinite(Number(id)) ? Number(id) : 0, schema: v.number(), diff --git a/packages/core/test/utils.ts b/packages/core/test/utils.ts index b3d7b17..8ee3243 100644 --- a/packages/core/test/utils.ts +++ b/packages/core/test/utils.ts @@ -143,6 +143,8 @@ export const createTestClient = (options: Omit Promise.resolve(); + export class DummyError extends Error {} export class DummyDataSource implements DataSource { diff --git a/packages/data-source-file-jsonl/.gitignore b/packages/data-source-file-jsonl/.gitignore new file mode 100644 index 0000000..53992de --- /dev/null +++ b/packages/data-source-file-jsonl/.gitignore @@ -0,0 +1,107 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.production +.env.development + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and *not* Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +.npmrc diff --git a/packages/data-source-file-jsonl/LICENSE b/packages/data-source-file-jsonl/LICENSE new file mode 100644 index 0000000..5a4fdfd --- /dev/null +++ b/packages/data-source-file-jsonl/LICENSE @@ -0,0 +1,7 @@ +MIT License Copyright (c) 2024 TheoryOfNekomata + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/packages/data-source-file-jsonl/package.json b/packages/data-source-file-jsonl/package.json new file mode 100644 index 0000000..34a78e6 --- /dev/null +++ b/packages/data-source-file-jsonl/package.json @@ -0,0 +1,49 @@ +{ + "name": "data-source-file-jsonl", + "version": "0.0.0", + "files": [ + "dist", + "src" + ], + "engines": { + "node": ">=16" + }, + "license": "MIT", + "keywords": [ + "pridepack" + ], + "devDependencies": { + "@types/node": "^20.11.0", + "pridepack": "2.6.0", + "tslib": "^2.6.2", + "typescript": "^5.3.3", + "vitest": "^1.2.0" + }, + "scripts": { + "prepublishOnly": "pridepack clean && pridepack build", + "build": "pridepack build", + "type-check": "pridepack check", + "clean": "pridepack clean", + "watch": "pridepack watch", + "start": "pridepack start", + "dev": "pridepack dev", + "test": "vitest" + }, + "dependencies": { + "@modal-sh/yasumi": "*" + }, + "private": false, + "description": "JSON lines file data source for yasumi.", + "repository": { + "url": "", + "type": "git" + }, + "homepage": "", + "bugs": { + "url": "" + }, + "author": "TheoryOfNekomata ", + "publishConfig": { + "access": "public" + } +} diff --git a/packages/data-source-file-jsonl/pridepack.json b/packages/data-source-file-jsonl/pridepack.json new file mode 100644 index 0000000..0bc7a8f --- /dev/null +++ b/packages/data-source-file-jsonl/pridepack.json @@ -0,0 +1,3 @@ +{ + "target": "es2018" +} diff --git a/packages/data-source-file-jsonl/src/index.ts b/packages/data-source-file-jsonl/src/index.ts new file mode 100644 index 0000000..918f880 --- /dev/null +++ b/packages/data-source-file-jsonl/src/index.ts @@ -0,0 +1,252 @@ +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'; + +export class JsonLinesDataSource> implements DataSource { + private path?: string; + + private resource?: Resource; + + data: Data[] = []; + + constructor(private readonly baseDir = '') { + // noop + } + + prepareResource< + Schema extends v.BaseSchema = v.BaseSchema, + CurrentName extends string = string, + CurrentRouteName extends string = string + >(resource: Resource) { + this.path = join(this.baseDir, `${resource.state.routeName}.jsonl`); + resource.dataSource = resource.dataSource ?? this; + const originalResourceId = resource.id; + resource.id = (newIdAttr: NewIdAttr, params: ResourceIdConfig) => { + originalResourceId(newIdAttr, params); + return resource as Resource; + }; + this.resource = resource; + } + + async initialize() { + if (typeof this.path !== 'string') { + throw new Error('Resource not prepared.'); + } + + try { + const fileContents = await readFile(this.path, 'utf-8'); + const lines = fileContents.split('\n'); + this.data = lines.filter((l) => l.trim().length > 0).map((l) => JSON.parse(l)); + } catch (err) { + await writeFile(this.path, ''); + } + } + + async getTotalCount() { + return this.data.length; + } + + async getMultiple() { + return [...this.data]; + } + + async newId() { + const idConfig = this.resource?.state.shared.get('idConfig') as ResourceIdConfig; + if (typeof idConfig === 'undefined') { + throw new Error('Resource not prepared.'); + } + + const theNewId = await idConfig.generationStrategy(this); + return theNewId as string; + } + + async getById(idSerialized: string) { + if (typeof this.resource === 'undefined') { + throw new Error('Resource not prepared.'); + } + + if (typeof this.path !== 'string') { + throw new Error('Resource not prepared.'); + } + + const theIdAttr = this.resource.state.shared.get('idAttr'); + if (typeof theIdAttr === 'undefined') { + throw new Error('Resource not prepared.'); + } + const idAttr = theIdAttr as string; + const theIdConfigRaw = this.resource.state.shared.get('idConfig'); + if (typeof theIdConfigRaw === 'undefined') { + throw new Error('Resource not prepared.'); + } + const theIdConfig = theIdConfigRaw as ResourceIdConfig; + + const id = theIdConfig.deserialize(idSerialized); + const foundData = this.data.find((s) => s[idAttr] === id); + + if (foundData) { + return { + ...foundData + }; + } + + return null; + } + + async create(data: Data) { + if (typeof this.resource === 'undefined') { + throw new Error('Resource not prepared.'); + } + + if (typeof this.path !== 'string') { + throw new Error('Resource not prepared.'); + } + + const theIdAttr = this.resource.state.shared.get('idAttr'); + if (typeof theIdAttr === 'undefined') { + throw new Error('Resource not prepared.'); + } + const idAttr = theIdAttr as string; + const theIdConfigRaw = this.resource.state.shared.get('idConfig'); + if (typeof theIdConfigRaw === 'undefined') { + throw new Error('Resource not prepared.'); + } + const theIdConfig = theIdConfigRaw as ResourceIdConfig; + + const newData = { + ...data + } as Record; + + if (idAttr in newData) { + newData[idAttr] = theIdConfig.deserialize(newData[idAttr] as string); + } + + const newCollection = [ + ...this.data, + newData + ]; + + await writeFile(this.path, newCollection.map((d) => JSON.stringify(d)).join('\n')); + + return data as Data; + } + + async delete(idSerialized: string) { + if (typeof this.resource === 'undefined') { + throw new Error('Resource not prepared.'); + } + + if (typeof this.path !== 'string') { + throw new Error('Resource not prepared.'); + } + + const theIdAttr = this.resource.state.shared.get('idAttr'); + if (typeof theIdAttr === 'undefined') { + throw new Error('Resource not prepared.'); + } + const idAttr = theIdAttr as string; + const theIdConfigRaw = this.resource.state.shared.get('idConfig'); + if (typeof theIdConfigRaw === 'undefined') { + throw new Error('Resource not prepared.'); + } + const theIdConfig = theIdConfigRaw as ResourceIdConfig; + + const oldDataLength = this.data.length; + + const id = theIdConfig.deserialize(idSerialized); + const newData = this.data.filter((s) => !(s[idAttr] === id)); + + await writeFile(this.path, newData.map((d) => JSON.stringify(d)).join('\n')); + + return oldDataLength !== newData.length; + } + + async emplace(idSerialized: string, dataWithId: Data) { + if (typeof this.resource === 'undefined') { + throw new Error('Resource not prepared.'); + } + + if (typeof this.path !== 'string') { + throw new Error('Resource not prepared.'); + } + + const theIdAttr = this.resource.state.shared.get('idAttr'); + if (typeof theIdAttr === 'undefined') { + throw new Error('Resource not prepared.'); + } + const idAttr = theIdAttr as string; + const theIdConfigRaw = this.resource.state.shared.get('idConfig'); + if (typeof theIdConfigRaw === 'undefined') { + throw new Error('Resource not prepared.'); + } + const theIdConfig = theIdConfigRaw as ResourceIdConfig; + + const existing = await this.getById(idSerialized); + const id = theIdConfig.deserialize(idSerialized); + const { [idAttr]: idFromResource, ...data } = dataWithId; + const dataToEmplace = { + ...data, + [idAttr]: id, + } as Data; + + if (existing) { + const newData = this.data.map((d) => { + if (d[idAttr] === id) { + return dataToEmplace; + } + + return d; + }); + + await writeFile(this.path, newData.map((d) => JSON.stringify(d)).join('\n')); + + return [dataToEmplace, false] as [Data, boolean]; + } + + const newData = await this.create(dataToEmplace); + return [newData, true] as [Data, boolean]; + } + + async patch(idSerialized: string, data: Partial) { + if (typeof this.resource === 'undefined') { + throw new Error('Resource not prepared.'); + } + + if (typeof this.path !== 'string') { + throw new Error('Resource not prepared.'); + } + + const theIdAttr = this.resource.state.shared.get('idAttr'); + if (typeof theIdAttr === 'undefined') { + throw new Error('Resource not prepared.'); + } + const idAttr = theIdAttr as string; + const theIdConfigRaw = this.resource.state.shared.get('idConfig'); + if (typeof theIdConfigRaw === 'undefined') { + throw new Error('Resource not prepared.'); + } + const theIdConfig = theIdConfigRaw as ResourceIdConfig; + + const existing = await this.getById(idSerialized); + if (!existing) { + return null; + } + + const newItem = { + ...existing, + ...data, + } + + const id = theIdConfig.deserialize(idSerialized); + const newData = this.data.map((d) => { + if (d[idAttr] === id) { + return newItem; + } + + return d; + }); + + await writeFile(this.path, newData.map((d) => JSON.stringify(d)).join('\n')); + return newItem as Data; + } +} diff --git a/packages/data-source-file-jsonl/test/index.test.ts b/packages/data-source-file-jsonl/test/index.test.ts new file mode 100644 index 0000000..441ca94 --- /dev/null +++ b/packages/data-source-file-jsonl/test/index.test.ts @@ -0,0 +1,8 @@ +import { describe, it, expect } from 'vitest'; +import add from '../src'; + +describe('blah', () => { + it('works', () => { + expect(add(1, 1)).toEqual(2); + }); +}); diff --git a/packages/data-source-file-jsonl/tsconfig.json b/packages/data-source-file-jsonl/tsconfig.json new file mode 100644 index 0000000..74083d7 --- /dev/null +++ b/packages/data-source-file-jsonl/tsconfig.json @@ -0,0 +1,23 @@ +{ + "exclude": ["node_modules"], + "include": ["src", "types"], + "compilerOptions": { + "module": "ESNext", + "lib": ["ESNext"], + "importHelpers": true, + "declaration": true, + "sourceMap": true, + "rootDir": "./src", + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "moduleResolution": "bundler", + "jsx": "react", + "esModuleInterop": true, + "target": "es2018", + "useDefineForClassFields": false, + "declarationMap": true + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0ad4dc7..5e174e4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -37,6 +37,28 @@ importers: specifier: ^1.4.0 version: 1.5.0(@types/node@20.12.7) + packages/data-source-file-jsonl: + dependencies: + '@modal-sh/yasumi': + specifier: '*' + version: link:../core + devDependencies: + '@types/node': + specifier: ^20.11.0 + version: 20.12.7 + pridepack: + specifier: 2.6.0 + version: 2.6.0(tslib@2.6.2)(typescript@5.4.5) + tslib: + specifier: ^2.6.2 + version: 2.6.2 + typescript: + specifier: ^5.3.3 + version: 5.4.5 + vitest: + specifier: ^1.2.0 + version: 1.5.0(@types/node@20.12.7) + packages: /@esbuild/aix-ppc64@0.19.12: