Giter VIP home page Giter VIP logo

zngly-graphql-db's Introduction

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

zngly-graphql-db (preview)

WPGraphQL custom database manager

  • Create a model to reflect your desired table
  • During runtime the table is created/updated
  • CRUD access via Graphql or Direct accesss

Installation Via Composer

https://packagist.org/packages/zngly/zngly-graphql-db

composer require zngly/zngly-graphql-db

How To Use

use Zngly\Graphql\Db\ZnglyDb;
use Zngly\Graphql\Db\Model\Field;
use Zngly\Graphql\Db\Model\FieldType;
use Zngly\Graphql\Db\Model\Table;

class NotificationsModel extends Table
{
    public static function table_single_name(): string
    {
        return 'notification';
    }

    public static function table_plural_name(): string
    {
        return "notifications";
    }

    public static function graphql_single_name(): string
    {
        return "Notification";
    }

    public static function graphql_plural_name(): string
    {
        return "Notifications";
    }

    public static function graphql_from_type(): string
    {
        return "RootQuery";
    }

    public static function description(): string
    {
        return "Notifications!";
    }

    public static function fields(): array
    {
        return [
            Field::create()
                ->name("id")
                ->is_id()
                ->primary_key()
                ->description("notification id")
                ->type(FieldType::create()->BIGINT())
                ->not_null()
                ->auto_increment(),
            Field::create()
                ->name('title')
                ->description('notification title')
                ->type(FieldType::create()->TEXT())
                ->not_null()
                ->collate(),
            Field::create()
                ->name('message')
                ->description('notification message')
                ->type(FieldType::create()->TEXT())
                ->not_null()
                ->collate(),
            Field::create()
                ->name('from')
                ->description('form who the notification is from')
                ->type(FieldType::create()->TEXT())
                ->not_null()
                ->collate(),
            Field::create()
                ->name('to')
                ->description('form who the notification is to')
                ->type(FieldType::create()->TEXT())
                ->not_null()
                ->collate(),
        ];
    }
}

new ZnglyDb("0.0.1", [
    new NotificationsModel(),
]);

Graphql Queries & Mutations

query notifications {
	notifications(first: 1100) {
		nodes {
			id
			from
			message
			title
			to
		}
	}
}

mutation create {
	createNotification(input: { title: "new test", to: "admin", message: "review this pls", from: "user" }) {
		notification {
			id
			title
			to
			message
			from
		}
	}
}

mutation delete {
	deleteNotification(input: { id: "16" }) {
		deletedId
		notification {
			title
			id
		}
	}
}

mutation update {
	updateNotification(input: { id: 16, message: "Can you Review this Please." }) {
		notification {
			id
			from
			message
			title
			to
		}
	}
}

How To Access Your Models Manually

// below is an example of how to get a model from the database manager
// you can query, add, update and delete entries for the model
use Zngly\Graphql\Db\Database\DatabaseManager;

$db = DatabaseManager::get_instance();

$notification_db = $db->get("notification");

$nr = rand(1, 100);

// below is the structure of how the notification table actions should be
$notification_db->insert([
    'message' => 'test nr: ' . $nr,
    'from' => 'test' . $nr,
    'to' => 'test' . $nr,
    'title' => 'test' . $nr,
]);

$notification_db->update(1, [
    'message' => 'This is my new updated test',
]);

$notification_db->delete(1);

$notification_db->query([
    'fields' => ['id', 'from', 'to', "message"],
    // 'number' => '1',
    // 'search' => 'hello',
    // 'search_columns' => ['message'],
]);

zngly-graphql-db's People

Contributors

zngly-vlad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.