Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add preset feature #34

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/chatty-melons-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"pinorama-presets": minor
"pinorama-studio": minor
"pinorama-types": minor
"pinorama-transport": patch
"pinorama-client": patch
"pinorama-server": patch
---

add preset feature
2 changes: 1 addition & 1 deletion packages/pinorama-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.6",
"@types/node": "^20.14.2",
"pinorama-server": "workspace:*",
"pinorama-types": "workspace:*",
"rimraf": "^5.0.7",
"rollup": "^4.18.0",
"rollup-plugin-dts": "^6.1.1",
Expand Down
26 changes: 16 additions & 10 deletions packages/pinorama-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { PinoramaIntrospection } from "pinorama-types"
import { z } from "zod"
import { setTimeout } from "./platform/node.js"

import type { Results, SearchParams } from "@orama/orama"
import type { OramaPinorama, PinoramaDocument } from "pinorama-server"
import type {
AnyOrama,
Results,
SearchParams,
TypedDocument
} from "@orama/orama"

const clientOptionsSchema = z.object({
url: z.string(),
Expand All @@ -22,7 +27,7 @@ export const defaultClientOptions: Partial<PinoramaClientOptions> = {
backoffMax: 30_000
}

export class PinoramaClient<T extends z.ZodType = z.ZodType> {
export class PinoramaClient<T extends AnyOrama> {
/** Pinorama Server URL */
private url: string

Expand Down Expand Up @@ -85,7 +90,7 @@ export class PinoramaClient<T extends z.ZodType = z.ZodType> {
}
}

public async insert(docs: z.infer<T>[]): Promise<void> {
public async insert(docs: TypedDocument<T>[]): Promise<void> {
await this.retryOperation(async () => {
const response = await fetch(`${this.url}/bulk`, {
method: "POST",
Expand All @@ -99,7 +104,7 @@ export class PinoramaClient<T extends z.ZodType = z.ZodType> {
})
}

public async search(payload: SearchParams<OramaPinorama>) {
public async search(payload: SearchParams<T>) {
try {
const response = await fetch(`${this.url}/search`, {
method: "POST",
Expand All @@ -111,7 +116,7 @@ export class PinoramaClient<T extends z.ZodType = z.ZodType> {
throw new Error("[TODO ERROR]: PinoramaClient.search failed")
}

const json: Results<PinoramaDocument> = await response.json()
const json: Results<TypedDocument<T>> = await response.json()

return json
} catch (error) {
Expand Down Expand Up @@ -139,18 +144,19 @@ export class PinoramaClient<T extends z.ZodType = z.ZodType> {
}
}

public async introspection<T = unknown>(): Promise<T> {
public async introspection() {
try {
const response = await fetch(`${this.url}/introspection`, {
method: "GET",
headers: this.defaultHeaders
})

const json = await response.json()
if (response.status !== 200) {
throw new Error(json.error)
if (!response.ok) {
throw new Error("[TODO ERROR]: PinoramaClient.introspection failed")
}

const json: PinoramaIntrospection<T["schema"]> = await response.json()

return json
} catch (error) {
console.error("error fetching introspection:", error)
Expand Down
33 changes: 33 additions & 0 deletions packages/pinorama-presets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "pinorama-presets",
"version": "0.0.1",
"type": "module",
"types": "./dist/presets/index.d.mts",
"exports": "./dist/presets/index.mjs",
"files": ["dist"],
"scripts": {
"clean": "rimraf dist node_modules",
"build": "tsc"
},
"homepage": "https://github.com/pinoramajs/pinorama#readme",
"bugs": {
"url": "https://github.com/pinoramajs/pinorama/issues"
},
"license": "MIT",
"author": "Francesco Pasqua <[email protected]> (https://cesco.me)",
"repository": {
"type": "git",
"url": "https://github.com/pinoramajs/pinorama.git",
"directory": "packages/pinorama-presets"
},
"devDependencies": {
"@orama/orama": "2.0.19",
"@types/node": "^20.14.2",
"pinorama-types": "workspace:*",
"rimraf": "^5.0.7",
"typescript": "^5.4.5"
},
"publishConfig": {
"access": "public"
}
}
107 changes: 107 additions & 0 deletions packages/pinorama-presets/src/presets/fastify.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { createPreset } from "../utils.mjs"

export const fastify = createPreset(
{
time: "number",
level: "enum",
msg: "string",
pid: "enum",
hostname: "string",
reqId: "string",
req: {
method: "string",
url: "string",
hostname: "string",
remoteAddress: "string",
remotePort: "enum"
},
res: {
statusCode: "enum"
},
responseTime: "number"
},
{
facets: {
level: "enum",
msg: "string",
"req.method": "string",
"req.url": "string",
"res.statusCode": "enum",
"req.hostname": "string",
"req.remoteAddress": "string",
"req.remotePort": "enum",
pid: "enum",
hostname: "string"
},
columns: {
time: true, // pino
level: true, // pino
msg: true, // pino
reqId: true,
"req.method": true,
"req.url": true,
"res.statusCode": true,
"req.hostname": false,
"req.remoteAddress": false,
"req.remotePort": false,
responseTime: false,
pid: false, // pino
hostname: false // pino
},
labels: {
level: [
"Level",
{
10: "TRACE",
20: "DEBUG",
30: "INFO",
40: "WARN",
50: "ERROR",
60: "FATAL"
}
],
time: "Time",
msg: "Message",
pid: "PID",
hostname: "Host",
reqId: "Req. ID",
"req.method": [
"Method",
{
GET: "GET",
POST: "POST",
PUT: "PUT",
PATCH: "PATCH",
DELETE: "DELETE",
HEAD: "HEAD",
OPTIONS: "OPTIONS"
}
],
"req.url": "URL",
"req.hostname": "Req. Host",
"req.remoteAddress": "Address",
"req.remotePort": "Port",
"res.statusCode": "Status",
responseTime: "Res. Time"
},
formatters: {
time: "timestamp"
},
styles: {
time: {
opacity: "0.5"
},
level: [
{},
{
10: { color: "var(--color-gray-500)" },
20: { color: "var(--color-purple-500)" },
30: { color: "var(--color-lime-500)" },
40: { color: "var(--color-yellow-500)" },
50: { color: "var(--color-red-500)" },
60: { color: "var(--color-red-500)" }
}
]
}
}
)
2 changes: 2 additions & 0 deletions packages/pinorama-presets/src/presets/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./fastify.mjs"
export * from "./pino.mjs"
62 changes: 62 additions & 0 deletions packages/pinorama-presets/src/presets/pino.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { createPreset } from "../utils.mjs"

export const pino = createPreset(
{
time: "number",
level: "enum",
msg: "string",
pid: "enum",
hostname: "string"
},
{
facets: {
level: "enum",
msg: "string",
pid: "enum",
hostname: "string"
},
columns: {
time: true,
level: true,
msg: true,
pid: false,
hostname: false
},
labels: {
time: "Time",
level: [
"Level",
{
10: "TRACE",
20: "DEBUG",
30: "INFO",
40: "WARN",
50: "ERROR",
60: "FATAL"
}
],
msg: "Message",
pid: "PID",
hostname: "Host"
},
formatters: {
time: "timestamp"
},
styles: {
time: {
opacity: "0.5"
},
level: [
{},
{
10: { color: "var(--color-gray-500)" },
20: { color: "var(--color-purple-500)" },
30: { color: "var(--color-lime-500)" },
40: { color: "var(--color-yellow-500)" },
50: { color: "var(--color-red-500)" },
60: { color: "var(--color-red-500)" }
}
]
}
}
)
14 changes: 14 additions & 0 deletions packages/pinorama-presets/src/utils.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { AnySchema } from "@orama/orama"
import type { PinoramaIntrospection } from "pinorama-types"

type PinoramaPreset<T extends AnySchema> = {
schema: T
introspection: PinoramaIntrospection<T>
}

export function createPreset<T extends AnySchema>(
schema: T,
introspection: PinoramaIntrospection<T>
): PinoramaPreset<T> {
return { schema, introspection }
}
23 changes: 23 additions & 0 deletions packages/pinorama-presets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"isolatedModules": true,
"strict": true,
"noUncheckedIndexedAccess": true,
"moduleResolution": "NodeNext",
"module": "NodeNext",
"sourceMap": true,
"declaration": true,
"declarationMap": true,
"lib": ["ES2022"],
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules", "dist", "tests"]
}
4 changes: 3 additions & 1 deletion packages/pinorama-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"@orama/plugin-data-persistence": "2.0.19",
"change-case": "^5.4.4",
"fastify": "^4.26.2",
"fastify-plugin": "^4.5.1"
"fastify-plugin": "^4.5.1",
"pinorama-presets": "workspace:*",
"pinorama-types": "workspace:*"
},
"publishConfig": {
"access": "public"
Expand Down
Loading