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

directus-hook-library

v1.0.2

Published

A collection of customizable hooks for Directus

Downloads

3

Readme

Directus Hook Library

NPM version

A collection of customizable hooks for Directus. This is not an extension, but a library of scripts that could be used inside a Directus hook extension.

Installation & Usage

First create a Directus Extension and during setup choose the extension type hook.

Inside the extension folder install directus-hook-library:

npm install directus-hook-library

Import it in src/index.ts, like:

import { setProjectSettingsFromEnvVars } from "directus-hook-library";

Have a look at the examples below.

Tip: You can use multiple of these hook scripts inside the same Directus hook.

Hooks & Examples

deleteUnusedM2OItems

Used to delete related M2O items that loose their relation and should not be kept, which is not possible via directus itself. This makes sense for a M2O relation that is used like a O2O relation.

Delete all oneCollection items that loose their relationship to a manyCollections item.

(!) Important: You have to specify a (hidden) reverse relationship O2M in your oneCollection inside Directus to make this work.

// src/index.ts
import { defineHook } from "@directus/extensions-sdk";
import { deleteUnusedM2OItems } from "directus-hook-library";

export default defineHook((register, context) => {
    deleteUnusedM2OItems(register, context, {
        oneCollection: "meta_infos",
        manyCollections: {
            pages: "pages",
            posts: "posts",
        },
    });
});

deleteUnusedM2AItems

Used to delete related M2A items that loose their relation and should not be kept, which is not possible via directus itself.

Goes through all junctionCollections and stores the keys for each found anyCollection item. Then deletes all anyCollections items that are not included in the stored keys.

// src/index.ts
import { defineHook } from "@directus/extensions-sdk";
import {
    toJunctionCollectionM2A,
    toAnyCollectionM2A,
    deleteUnusedM2AItems,
} from "directus-hook-library";

export default defineHook((register, context) => {
    deleteUnusedM2AItems(register, context, {
        anyCollections: [
            "gallery",
            "video",
            "definition_list",
            "related_content",
        ].map(toAnyCollectionM2A),
        junctionCollections: ["page_editor_nodes", "post_editor_nodes"].map(
            toJunctionCollectionM2A
        ),
    });
});

toAnyCollectionM2A ... maps the array elements to objects of type AnyCollection with a default key field of “id”.

toJunctionCollectionM2A ... maps the array elements to objects of type JunctionCollection with a default foreignKey field of “item” and a default foreignCollection field of “collection”

preventDeletingM2AItems

Used for M2A items to prevent their deletion, which is not possible via directus itself.

Goes through all junctionCollections and searches for the relatedCollection items to delete. If found, prevents deletion.

Find M2A items by searching the schema for “one_allowed_collections” and look for collections that are deletable/reachable by the app/user (many_collection goes to the junctionCollections)

// src/index.ts
import { defineHook } from "@directus/extensions-sdk";
import {
    toJunctionCollectionM2A,
    preventDeletingM2AItems,
} from "directus-hook-library";

export default defineHook((register, context) => {
    preventDeletingM2AItems(register, context, {
        relatedCollections: ["video"],
        junctionCollections: ["page_editor_nodes", "post_editor_nodes"].map(
            toJunctionCollectionM2A
        ),
    });
});

toJunctionCollectionM2A ... maps the array elements to objects of type JunctionCollection with a default foreignKey field of “item” and a default foreignCollection field of “collection”

replaceDeletedUserReferences

This replaces the reference to a deleted user with a reference to the current user in the directus_files collection.

// src/index.ts
import { defineHook } from "@directus/extensions-sdk";
import { replaceDeletedUserReferences } from "directus-hook-library";

export default defineHook((register, context) => {
    replaceDeletedUserReferences(register, context);
});

resetFieldsHiddenByOption

Set fields to null that have a value but are hidden by a condition.

// src/index.ts
import { defineHook } from "@directus/extensions-sdk";
import { resetFieldsHiddenByOption } from "directus-hook-library";

export default defineHook((register, context) => {
    resetFieldsHiddenByOption(register, context, {
        collection: "conditional",
        optionsField: "detail",
        resetGroups: [
            {
                not: ["yes"],
                nullify: ["title", "description"],
            },
            {
                not: ["no"],
                nullify: ["external_link"],
            },
        ],
    });
});

setProjectSettingsFromEnvVars

Used for setting project settings from ENV vars like, PROJECT_URL.

This overwrites the values for settings in the Project Settings when starting Directus.

// src/index.ts
import { defineHook } from "@directus/extensions-sdk";
import { setProjectSettingsFromEnvVars } from "directus-hook-library";

export default defineHook((register, context) => {
    setProjectSettingsFromEnvVars(register, context, [
        "project_name",
        "project_descriptor",
        "project_url",
    ]);
});

For ENV variables like:

PROJECT_NAME=Directus
PROJECT_DESCRIPTOR=Hook
PROJECT_URL=http://localhost:3000