Browse Source

Extract JSONL sample data source

Put data source to its own package.
master
TheoryOfNekomata 7 months ago
parent
commit
ee290a7a1f
19 changed files with 575 additions and 341 deletions
  1. +42
    -0
      packages/core/package.json
  2. +0
    -270
      packages/core/src/backend/data-sources/file-jsonl.ts
  3. +0
    -1
      packages/core/src/backend/data-sources/index.ts
  4. +2
    -1
      packages/core/src/backend/index.ts
  5. +2
    -1
      packages/core/src/backend/servers/http/core.ts
  6. +12
    -4
      packages/core/src/common/resource.ts
  7. +31
    -38
      packages/core/test/features/decorators.test.ts
  8. +0
    -16
      packages/core/test/fixtures.ts
  9. +3
    -5
      packages/core/test/handlers/http/default.test.ts
  10. +10
    -5
      packages/core/test/handlers/http/error-handling.test.ts
  11. +2
    -0
      packages/core/test/utils.ts
  12. +107
    -0
      packages/data-source-file-jsonl/.gitignore
  13. +7
    -0
      packages/data-source-file-jsonl/LICENSE
  14. +49
    -0
      packages/data-source-file-jsonl/package.json
  15. +3
    -0
      packages/data-source-file-jsonl/pridepack.json
  16. +252
    -0
      packages/data-source-file-jsonl/src/index.ts
  17. +8
    -0
      packages/data-source-file-jsonl/test/index.test.ts
  18. +23
    -0
      packages/data-source-file-jsonl/tsconfig.json
  19. +22
    -0
      pnpm-lock.yaml

+ 42
- 0
packages/core/package.json View File

@@ -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"
]
}
}
}

+ 0
- 270
packages/core/src/backend/data-sources/file-jsonl.ts View File

@@ -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 extends CurrentIdAttr, TheIdSchema extends IdSchema>(
newIdAttr: NewIdAttr,
params: ResourceIdConfig<TheIdSchema>
): Resource<Schema, CurrentName, CurrentRouteName, NewIdAttr, TheIdSchema>;
fullText(fullTextAttr: string): this;
}
}

export class DataSource<T extends Record<string, string>> implements DataSourceInterface<T> {
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<Schema, CurrentName, CurrentRouteName>) {
this.path = join(this.baseDir, `${resource.state.routeName}.jsonl`);
resource.dataSource = resource.dataSource ?? this;
const originalResourceId = resource.id;
resource.id = <NewIdAttr extends string, NewIdSchema extends v.BaseSchema>(newIdAttr: NewIdAttr, params: ResourceIdConfig<NewIdSchema>) => {
originalResourceId(newIdAttr, params);
return resource as Resource<Schema, CurrentName, CurrentRouteName, NewIdAttr, NewIdSchema>;
};
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<any>;
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<any>;

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<any>;

const newData = {
...data
} as Record<string, unknown>;

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<any>;

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<any>;

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<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<any>;

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;
}
}

+ 0
- 1
packages/core/src/backend/data-sources/index.ts View File

@@ -1 +0,0 @@
export * as jsonlFile from './file-jsonl';

+ 2
- 1
packages/core/src/backend/index.ts View File

@@ -1,2 +1,3 @@
export * from './core';
export * as dataSources from './data-sources';
export * from './common';
export * from './data-source';

+ 2
- 1
packages/core/src/backend/servers/http/core.ts View File

@@ -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<Pick<RequestContext, 'resource'>>['resource'];

interface ResourceWithDataSource extends Omit<RequiredResource, 'dataSource'> {
dataSource: Required<Pick<RequiredResource, 'dataSource'>>['dataSource'];
dataSource: DataSource;
}

interface ResourceRequestContext extends Omit<RequestContext, 'resource'> {


+ 12
- 4
packages/core/src/common/resource.ts View File

@@ -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<CanPatchObject> | 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<CurrentName, CurrentRouteName>;
name<NewName extends CurrentName>(n: NewName): Resource<Schema, NewName, CurrentRouteName>;
route<NewRouteName extends CurrentRouteName>(n: NewRouteName): Resource<Schema, CurrentName, NewRouteName>;
name<NewName extends CurrentName>(n: NewName): Resource<Schema, NewName, CurrentRouteName, CurrentIdAttr, IdSchema>;
route<NewRouteName extends CurrentRouteName>(n: NewRouteName): Resource<Schema, CurrentName, NewRouteName, CurrentIdAttr, IdSchema>;
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<RelatedSchema extends v.BaseSchema>(resource: Resource<RelatedSchema>): this;
dataSource?: DataSource;
id<NewIdAttr extends CurrentIdAttr, TheIdSchema extends IdSchema>(
newIdAttr: NewIdAttr,
params: ResourceIdConfig<TheIdSchema>
): Resource<Schema, CurrentName, CurrentRouteName, NewIdAttr, TheIdSchema>;
}

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<string>;
fullTextAttrs.add(attrName);
resourceState.shared.set('fullText', fullTextAttrs);


+ 31
- 38
packages/core/test/features/decorators.test.ts View File

@@ -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<Backend['createHttpServer']>;
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<Backend['createHttpServer']>;
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<void>((resolve, reject) => {
afterAll(() => new Promise<void>((resolve, reject) => {
server.close((err) => {
if (err) {
reject(err);


+ 0
- 16
packages/core/test/fixtures.ts View File

@@ -1,16 +0,0 @@
import {DataSource} from '../src/backend/data-source';

export const autoIncrement = async (dataSource: DataSource) => {
const data = await dataSource.getMultiple() as Record<string, string>[];

const highestId = data.reduce<number>(
(highestId, d) => (Number(d.id) > highestId ? Number(d.id) : highestId),
-Infinity
);

if (Number.isFinite(highestId)) {
return (highestId + 1);
}

return 1;
};

+ 3
- 5
packages/core/test/handlers/http/default.test.ts View File

@@ -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(),


+ 10
- 5
packages/core/test/handlers/http/error-handling.test.ts View File

@@ -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(),


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

@@ -143,6 +143,8 @@ export const createTestClient = (options: Omit<RequestOptions, 'method' | 'path'
return client;
};

export const dummyGenerationStrategy = () => Promise.resolve();

export class DummyError extends Error {}

export class DummyDataSource implements DataSource {


+ 107
- 0
packages/data-source-file-jsonl/.gitignore View File

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

+ 7
- 0
packages/data-source-file-jsonl/LICENSE View File

@@ -0,0 +1,7 @@
MIT License Copyright (c) 2024 TheoryOfNekomata <allan.crisostomo@outlook.com>

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.

+ 49
- 0
packages/data-source-file-jsonl/package.json View File

@@ -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 <allan.crisostomo@outlook.com>",
"publishConfig": {
"access": "public"
}
}

+ 3
- 0
packages/data-source-file-jsonl/pridepack.json View File

@@ -0,0 +1,3 @@
{
"target": "es2018"
}

+ 252
- 0
packages/data-source-file-jsonl/src/index.ts View File

@@ -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<Data extends Record<string, string>> implements DataSource<Data> {
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<Schema, CurrentName, CurrentRouteName>) {
this.path = join(this.baseDir, `${resource.state.routeName}.jsonl`);
resource.dataSource = resource.dataSource ?? this;
const originalResourceId = resource.id;
resource.id = <NewIdAttr extends string, NewIdSchema extends v.BaseSchema>(newIdAttr: NewIdAttr, params: ResourceIdConfig<NewIdSchema>) => {
originalResourceId(newIdAttr, params);
return resource as Resource<Schema, CurrentName, CurrentRouteName, NewIdAttr, NewIdSchema>;
};
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<any>;
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<any>;

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<any>;

const newData = {
...data
} as Record<string, unknown>;

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<any>;

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<any>;

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<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<any>;

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;
}
}

+ 8
- 0
packages/data-source-file-jsonl/test/index.test.ts View File

@@ -0,0 +1,8 @@
import { describe, it, expect } from 'vitest';
import add from '../src';

describe('blah', () => {
it('works', () => {
expect(add(1, 1)).toEqual(2);
});
});

+ 23
- 0
packages/data-source-file-jsonl/tsconfig.json View File

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

+ 22
- 0
pnpm-lock.yaml View File

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


Loading…
Cancel
Save