A modular database migration tool for Deno inspired by Laravel

Nessie

A modular database migration tool for Deno inspired by Laravel. Currently supports PostgreSQL, MySQL and SQLite.

If you would like to see your DB flavor supported, take a look at how to make a client plugin with examples in the clients folder or in the section How to make a client.

See documentation for the query builder.

See documentation for the clients.

Nessie is available through:

Usage

  • init: Generates a nessie.config.ts file

    deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie/cli.ts init

  • make [name]: Create migration

    deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie/cli.ts make create_users

  • make:seed [name]: Create seed

    deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie/cli.ts make:seed create_users

  • migrate [amount?]: Run migration - will migrate your migrations in your migration folder (sorted by timestamp) newer than the latest migration in your db. Amount defines how many migrations, defaults to all available if not set.

    deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts migrate

    deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts migrate 1

    deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts migrate -c ./nessie.config.ts

  • rollback [amount?]: Rollback - will rollback your migrations. Amount defines how many migrations, defaults to 1 if not set.

    deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts rollback

    deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts rollback 2

    deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts rollback all

  • seed [matcher?]: Seed - will seed your database. Optional matcher will match all files in your seed folder by string literal or RegExp.

    deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts seed

    deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts seed seed_file.js

    deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts seed ".+.ts"

Flags

  • -c, --config: Path to config file, will default to ./nessie.config.ts
  • -d, --debug: Enables verbose output

Contributing

All contributions are welcome, make sure to read the contribution guideline.

Uses

Examples

nessie.config.ts with all default values

import { ClientPostgreSQL } from "./clients/ClientPostgreSQL.ts";

const nessieOptions = {
  migrationFolder: "./db/migrations",
  seedFolder: "./db/seeds",
};

const connectionOptions = {
  database: "nessie",
  hostname: "localhost",
  port: 5432,
  user: "root",
  password: "pwd",
};

export default {
  client: new ClientPostgreSQL(nessieOptions, connectionOptions),
  exposeQueryBuilder: false,
};

Minimal example of a migration file

import { Migration } from "https://deno.land/x/nessie/mod.ts";

export const up: Migration = () => {
  return "CREATE TABLE table1 (id int);";
};

export const down: Migration = () => {
  return "DROP TABLE table1";
};

Using the native query builder (exposeQueryBuilder: true)

import { Migration } from "https://deno.land/x/nessie/mod.ts";
import { Schema } from "https://deno.land/x/nessie/qb.ts";

export const up: Migration<Schema> = ({ queryBuilder }) => {
  queryBuilder.create("users", (table) => {
    table.id();
    table.string("name", 100).nullable();
    table.boolean("is_true").default("false");
    table.custom("custom_column int default 1");
    table.timestamps();
  });

  queryBuilder.queryString(
    "INSERT INTO users VALUES (DEFAULT, 'Deno', true, 2, DEFAULT, DEFAULT);",
  )

  return queryBuilder.query
};

export const down: Migration<Schema> = ({ queryBuilder }) => {
  return queryBuilder.drop("users");
};

Seed file

import { Seed } from "https://deno.land/x/nessie/mod.ts";

export const run: Seed = () => {
  return "INSERT INTO testTable VALUES (1)"
};

See the example folder for more

How to make a client

A client needs to extend AbstractClient and implement the ClientI interface.

query: Takes a query string or array of query strings and sends them of to the batabase for execution. Should return whatever the database responds.

prepare: Will be run when the migration or rollback commands are executed. This should create the connection, set up the nessie_migrations table and prepare the database for incoming migrations.

migrate: Takes a number as an optional input, will default to all files if not set. Will run Math.min(amount, numberOfFiles) migration files. Only handles the up method.

rollback: Takes a number as an optional input, will default to 1 if not set. Will run Math.min(amount, numberOfFiles) migration files. Only handles the down method.

seed: Takes an optional matcher as input. Matcher can be regex or string. Will seed the database. Handles the run method in seed files.

close: Will be the last method run before the program is finished. This should close the database connection.

Download Details:

Author: halvardssm

Source Code: https://github.com/halvardssm/deno-nessie

#deno #nodejs #javascript #node

What is GEEK

Buddha Community

A modular database migration tool for Deno inspired by Laravel
Seamus  Quitzon

Seamus Quitzon

1595205213

How to perform migration rollback in laravel

As we know that laravel migration provides very simple way to create database table structure. We need to create migration file and write table structure then migrate that migration. Sometimes we need to rollback that migration. So here we will discuss about the migration rollback in laravel.

We can run the rollback artisan command to rollback on a particular step. We can execute the following artisan command.

php artisan migrate:rollback --step=1

Every time when we will rollback, we will get the last batch of migration.

**Note: **This rollback command will work on laravel 5.3 or above version. For the version below 5.3, there is no command available for migration rollback in laravel.

We can also use the following command to rollback and re migrate.

php artisan migrate:refresh --step=2

It will rollback and remigrate last two migration.

You can also checkout the article for executing single migration by clicking on the link below.

How to migrate single migration in laravel

#laravel #how to perform rollback migration in laravel #laravel migration rollback #migration refresh in laravel #migration rollback batch in laravel #migration rollback for one specific migration #migration rollback in laravel

Fredy  Larson

Fredy Larson

1595202210

How to migrate single migration in laravel

Sometimes while working on a laravel application, we just need to migrate only single or specific migration. If we run normal migrate command it will migrate all the migrations written in the application. Here i will let you know to migrate single migration in laravel.

Migrations play very important role for aplication’s database structure. We just need to create migrations in the application for each table which you want and whenever we migrate these migrations. New table structure would be created.

For new developers or setting up new environment, we do not need to backup of table structure. We will just need to run the migrations. It will automatically create desired tables in the database connected from the laravel application.

We can run all the migrations using the command below.

php artisan migrate

Now if we want to run only one migration or a specific migration, we will need to define a path parameter and will need to specify the path of that migration file which we want to run like below.

php artisan migrate --path=/database/migrations/my_migration.php

It will migrate only my_migration.php file. Here, you will need to replace file name from your own migration file.

You can also learn how to add column in existing daatbase table through migration by clicking on the below.

#laravel #how to migrate single migration file #laravel migration #laravel single migration #migrate specific migration in laravel

Fredy  Larson

Fredy Larson

1595205933

Laravel create model migration and controller in a single command

In this tutorial, you will learn how to create a controller Or resource controller and model using the command line (CLI). You will also learn how to create a controller and model using one command only. This example tutorial also work with laravel version.

Let’s use the following ways to Create controller, model and migration laravel in one command in laravel:

1. Create model command

Use the php artisan make model for creating a model using the command line (CLI) :

 php artisan make:model Photo

This command is to create the photo model. After the created the model, it looks like this:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
    //
}

2. Create Controller command

Use the php artisan make:controller command for creating a controller using this command line:

 php artisan make:controller PhotoController

This command will create controller named photoController. It looks like as follow:

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
class PhotoController extends Controller
{
}

3. Create a Resource Controller Command

To create the resource controller in laravel, use the following command:

php artisan make:controller PhotoController --resource

PHP artisan make controller resource command creates a resource controller. It has already created some methods like index, update, edit, destroy, etc. It looks like this:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PhotoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

4. Command For Create Model and Controller

Use the php artisan make:model -mc for creating a controller and model, you can use this command as follow:

 php artisan make:model -mc Photo

This single command has been created as a photo controller and model.

5. Laravel make:model with migration and controller

If you want to create controller and model, so you can execute php artisan make:model -mc for creating a controller and model in command prompt:

 php artisan make:model Product -mcr

This single command has been created as a Product controller and model.

In this tutorial, you have successfully learned how to create a controller and model. Also, learn how to create a model and resource controller using one command.

#laravel #create controller resource and model in a command #laravel artisan command #laravel create model #laravel single command for model and migration #migration and controller

Laravel 8 Database Seeder Example

Today we will see Laravel 8 Database Seeder Example, as we all know laravel framework provide many functionalities to user to reduce developer’s time for developing website, here we will see How to create database seeder in Laravel 8.

Many times you have requirement to add some default records or entry to login or add form details etc. there are no problem to add manual data in database one or two times but it is very challenging task to add data manually each and every time.

Laravel 8 Database Seeder Example

https://websolutionstuff.com/post/laravel-8-database-seeder-example


Read Also : Read Also : Laravel 8 CRUD Operation Example

https://websolutionstuff.com/post/laravel-8-crud-operation-example


Read Also : Read Also : Create Dummy Data Using Laravel Tinker

https://websolutionstuff.com/post/create-dummy-data-using-laravel-tinker

#laravel 8 database seeder example #laravel #database #seeder #database seeders in laravel 8 #laravel 8 seeder example

Seamus  Quitzon

Seamus Quitzon

1595201363

Php how to delete multiple rows through checkbox using ajax in laravel

First thing, we will need a table and i am creating products table for this example. So run the following query to create table.

CREATE TABLE `products` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
 `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
 `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
 `updated_at` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Next, we will need to insert some dummy records in this table that will be deleted.

INSERT INTO `products` (`name`, `description`) VALUES

('Test product 1', 'Product description example1'),

('Test product 2', 'Product description example2'),

('Test product 3', 'Product description example3'),

('Test product 4', 'Product description example4'),

('Test product 5', 'Product description example5');

Now we are redy to create a model corresponding to this products table. Here we will create Product model. So let’s create a model file Product.php file under app directory and put the code below.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'name','description'
    ];
}

Step 2: Create Route

Now, in this second step we will create some routes to handle the request for this example. So opeen routes/web.php file and copy the routes as given below.

routes/web.php

Route::get('product', 'ProductController@index');
Route::delete('product/{id}', ['as'=>'product.destroy','uses'=>'ProductController@destroy']);
Route::delete('delete-multiple-product', ['as'=>'product.multiple-delete','uses'=>'ProductController@deleteMultiple']);

#laravel #delete multiple rows in laravel using ajax #laravel ajax delete #laravel ajax multiple checkbox delete #laravel delete multiple rows #laravel delete records using ajax #laravel multiple checkbox delete rows #laravel multiple delete