npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@forestadmin-experimental/plugin-gcs

v0.0.4

Published

The GCS plugin allows you to work with the files you store on your buckets.

Downloads

17

Readme

The GCS plugin allows you to work with the files you store on your buckets.

To make everything work as expected, you need to install the package @forestadmin-experimental/plugin-gcs.

Note that:

  • The plugin is still experimental. The provided features might be missing you would like to add, don't hesitate to suggest anything by creating a pull request.
  • The plugin allows you to use file widgets on the UI based on some bucket keys you store in your database

Inside this plugin

Some features comes out of the box with this plugin:

  • The possibility to generate signed url to get your file
  • The possibility to upload files to your bucket
  • The possibility to create quick smart action that handles download of many files

Here is a sample describing how to create file fields

const { createAgent } = require('@forestadmin/agent');

const {
  createFileField,
  CreateFileFieldOption,
  addDownloadFilesAction,
  DownloadFilesOptions,
} = require('@forestadmin-experimental/plugin-gcs');

await createAgent<Schema>(Options)
  .addDataSource(DataSourceOptions)
  .customizeCollection('Users', usersCollection => {
    .use<CreateFileFieldOption<Schema, 'Users'>>(createFileField, {
      fieldname: 'drivingLicense',
      gcs: {
        bucketId: 'production-bucket',
        projectId: 'myproject-346344',
        keyFilePath: 'myproject-344513-94fd90cbc66e.json'
      },
      storeAt: (userId, originalFilename) => {
        return `Users/${userId}/identity/${originalFilename}`;
      },
    })
    .use<DownloadFilesOptions<Schema, 'Users'>>(addDownloadFilesAction, {
      gcs: {
        bucketId: 'production-bucket',
        projectId: 'myproject-346344',
        keyFilePath: 'myproject-344513-94fd90cbc66e.json'
      },
      actionName: 'Download all user\'s documents',
      fileName: 'all-users-document.zip',
      fields: ['drivingLicense', 'identity'],
    })
  })

Make a field a file field

The createFileField function allows you to activate the generation of signed urls to preview your files, and activate the upload features.

Here is the list of the available options:

.use(createFileField, {
  /** Name of the field that you want to use as a file-picker on the frontend */
  fieldName: TColumnName<S, N>;

  /**
   * This function allows customizing the string that will be saved in the database.
   * If the objectKeyFromRecord option is not set, the output of that function will also
   * be used as the object key in your bucket.
   *
   * Note that the recordId parameter will _not_ be provided when records are created.
   *
   * storeAt: (recordId, originalFilename, context) => {
   *   return `${context.collection.name}/${recordId ?? 'new-record'}/${originalFilename}`;
   * }
   **/
  storeAt?: (
    recordId: string,
    originalFilename: string,
    context: WriteCustomizationContext<S, N>,
  ) => string | Promise<string>;

  /**
  * This function allows customizing the object key that will be used in your bucket without interfering
  * with what is stored in the database.
  *
  * objectKeyFromRecord: {
  *   extraDependencies: ['firstname', 'lastname'],
  *   mappingFunction: (record, context) => {
  *     return `avatars/${record.firstname}-${record.lastname}.png`;
  *   }
  * };
  */
  objectKeyFromRecord?: {
    extraDependencies?: TFieldName<S, N>[];
    mappingFunction: (
      record: TPartialSimpleRow<S, N>,
      context: CollectionCustomizationContext<S, N>,
    ) => string | Promise<string>;
  };
  

  /** GCS configuration */
  gcs: {
    /** Identifier of your bucket */
    bucketId: string;
  
    /** The project where the bucket resides */
    projectId: string;

    /** Authentication file corresponding to the service account provided by Google */
    keyFilePath: string;
  };
})

Add a download smart action

The addDownloadFilesAction function allows you to add a smart action of type Single in your collection.

The result will be a .zip archive that will be downloaded on the user's computer.

You can either specify the fields that should be added in the archive, or use the getFiles options to specify the bucket keys that should be added in the archive.

Here is the list of the available options:

.use(addDownloadFilesAction, {
  /** 
   * Name of the action that will be displayed in the frontend.
   * 
   * Defaults to `all-files-download.zip`.
   */
  actionName?: string,

  /**
   * Name of archive that will be downloaded on the user's computer.
   * 
   * Defaults to `Download all files`.
   * */
  fileName?: string,

  /** 
   * List of the fields from which bucket keys should be gathered to retrieve the files, and included in teh archive.
   * 
   * This can not be used with the `getFiles` option.
   */
  fields?: string[],

  /**
   * This option allows you to handle complex downloads. Being provided with the context of the underlying smart action,
   * you will be the owner of returning the actual bucket keys you would like to gather files from, and include in the archive.
   * 
   * This can be used with the `fields` option.
   */
  getFiles?: (context: ActionContextSingle<S, N>): Promise<string[]>,
  
  /** GCS configuration */
  gcs: {
    /** Identifier of your bucket */
    bucketId: string;

    /** The project where the bucket resides */
    projectId: string;

    /** Authentication file corresponding to the service account provided by Google */
    keyFilePath: string;
  };
})

TODO

  • objectKeyFromRecord.mappingFunction does not support fields of type array of string
  • file deletion is currently not supported
  • readMode: 'url' | 'proxy' is not supported