fastify/help

create repository `fastify/fastify-elasticsearch`

RodrigoDornelles opened this issue ยท 20 comments

Opensearch support would be interesting, I think the community would help create and implement this.

opensearch is a community-driven project with some extra features like improved authentication, ssl, anomaly detection, easier for clusters and many other things in addition to being less obfuscated.

It is unclear what you propose

It is unclear what you propose

just create a github repository for elasticsearch, so that it is published in the npm registry.

to be maintained as an official package, I could send a pull request myself, it would be a few things other than @fastify/elasticsearch.

example

I know you do it in JavaScript with just the typing declared later, but this is code from my own project.

I just copied it and made some changes and I can now use it.

import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { Client, ClientOptions } from '@opensearch-project/opensearch';

interface FastifyElasticsearchOptions {
  namespace?: string;
  healthcheck?: boolean;
  client?: Client;
  node: string
  auth: {
    username: string,
    password: string
  }
}

declare module 'fastify' {
    interface FastifyInstance {
        elastic: Record<string, Client> | Client;
    }
}

async function fastifyElasticsearch(
  fastify: FastifyInstance,
  options: FastifyElasticsearchOptions
): Promise<void> {
  const { namespace, healthcheck } = options;
  delete options.namespace;
  delete options.healthcheck;

  const client = options.client || new Client(options as ClientOptions);

  if (healthcheck !== false) {
    await client.ping();
  }

  if (namespace) {
    if (!fastify.elastic) {
      fastify.decorate('elastic', {});
    }

    if ((fastify.elastic as Record<string, Client>)[namespace] ) {
      throw new Error(`Elasticsearch namespace already used: ${namespace}`);
    }

    (fastify.elastic as Record<string, Client>)[namespace] = client;

    fastify.addHook('onClose', async (instance) => {
      await ((instance.elastic as Record<string, Client>)[namespace]).close();
    });
  } else {
    fastify
      .decorate('elastic', client)
      .addHook('onClose', async (instance) => {
        await (instance.elastic as Client).close();
      });
  }
}

export default function (
  fastify: FastifyInstance,
  opts: FastifyElasticsearchOptions,
  next: (err?: Error) => void
): void {
  fastify.decorate('elastic', {});
  fastify.register(fastifyElasticsearch, opts);
  next();
}

export { fastifyElasticsearch };

@Uzlopak @mcollina @allevo @Fdawgs

You can also create your repository and library and submit it as part of the community plugins

fastify has a rich ecosystem, it looks interesting as an official project.

So... somebody wants to contribute a @fastify/opensearch plugin? I can create a repo if there is a volunteer

So... somebody wants to contribute a @fastify/opensearch plugin? I can create a repo if there is a volunteer

Me! ๐Ÿ™‹

@mcollina

What is it about the current plugin that prevents it from being used or extended?

What is it about the current plugin that prevents it from being used or extended?

the current plugin would leave you with extra dependencies. (adding elasticsearch when you use opensearch) I also suggested perhaps extending it by leaving it up to the user to choose which database it should work with. fastify/fastify-elasticsearch#108

@jsumners

@mcollina
I believe it is private (error 404), at the weekend I believe it will be finished!

the current plugin would leave you with extra dependencies. (adding elasticsearch when you use opensearch) I also suggested perhaps extending it by leaving it up to the user to choose which database it should work with. fastify/fastify-elasticsearch#108

I'm not sure one extra dependency is enough to justify having to maintain a whole new project. We have a very long list of modules to maintain already.

Then @RodrigoDornelles why don't you start adding it to fastify-elasticsearch to begin with?

@mcollina I don't see a problem. That's why I raised the question, I think it would also be good as an improvement to { @fastify/opensearch itself.

@mcollina I finished the project! and it's working with CI!

How can I transfer to Fastify Community?

I've invited you to the org, you should be able to transfer the repo!

I've invited you to the org, you should be able to transfer the repo!

@mcollina There is already a private repository, which I don't have access to, called fastify-opensearch and I can't transfer it because of this!

image

removed

@mcollina transferred!

you can now publish it!

What do you think about making a pull request that automates publishing via github actions, I can add provenance that give greater traceability to the packages, this can be a CI plugin in the repository fastify/workflows in the future, when I better understand the fastify flow.

@RodrigoDornelles while you are it, you might be interested in doing pino-opensearch as well ;).

Published