Wednesday, November 13, 2024

docker host name/address

if don't wanna use the ip address

docker get IP address command:

docker inspect -f '{{range. NetworkSettings. Networks}}{{. IPAddress}}{{end}}' container_name_or_id

use host.docker.internal

 


Monday, November 11, 2024

.Net Core connect to postgresql

 1. appsettings.json

  "ConnectionStrings": {
    "DefaultConnection": ""
  }

2. Program.cs
builder.Services.AddDbContext<ApiDbCopntext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));

3. Models/ folder
add Table Class
namespace ReactAPIApp.Server.Models
{
    public class Driver
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
        public int DriverNumber { get; set; }
    }
}

4. Add Context for example add Data folder

Data/ApiDbContext.cs

using Microsoft.EntityFrameworkCore;

using ReactAPIApp.Server.Models;

namespace ReactAPIApp.Server.Data
{
    public class ApiDbContext : DbContext
    {
        public ApiDbContext(DbContextOptions<ApiDbContext> options) : base(options){}
        public DbSet<Driver> Drivers { get; set; }
    }
}

5. run migrate command
if from package manager console
PM> Add-Migration initial

if from package manager console

Friday, November 8, 2024

js function 严格模式 use strict

 禁止function 内部的this指向winodows

class 中的function,都已默认开启了严格模式



Tuesday, October 29, 2024

.Net seed migration

dbcontext command lines:

  1. Using the Package Manager Console in Visual Studio:

Add-Migration initial 

remove-database

update-database

  1. Using the .NET Command Line Interface (.NET CLI):
dotnet ef migrations add Initial
dotnet ef database update

Saturday, July 20, 2024

Multi column searching

 scopeSearch

collect(explode(' ', $terms))->filter()->each(function ($tiem)use ($query) {

    $term = '%' . $term . '%';

    $query->where(function($query) use ($term) {

        $query->where('first_name', 'like', $term)

            ->orWhere('last_name', 'like', $term)

            ->orWhereHas('company', function($query) use ($term) {

                $query->where('name', like', $term);

            });

    });

});

to SQL it is



SELECT * FROM 'users'

WHERE 'first_name' LIKE '%bill%'

    OR 'last_name' LIKE '%bill%'

    OR EXISTS (

        SELECT * FROM 'companies'

        WHERE 'users'.'company_id' = 'companies'.'id'

    )

Optimizing Circular Relationships

 model/Comment.php

public function isAuthor() {// n + 1 issue

     return $this-->feature->comments->first()->user_id === $this->user_id;

}

Improves

in controllor

$feature->load('comments.user', 'comments.feature.comments');


Improves more

$feature->load('comments.user');

$feature->comments->each->setRelation('feature', $feature); 

Calculate totals using conditional aggregates

select 
    count(case when status = 'Requested' then 1 end) as requested,
    count(case when status = 'Planned' then 1 end) as planned,
    count(case when status = 'Completed' then 1 end) and completed,
from features

on laravel

$status = Feature::toBase() // toBase we don't want to the Feature model return back, but instead we want collection of different totals
    ->selectRaw("count(case when status = 'Requested' then 1 end) as requested")
    ->selectRaw("count(case when status = 'Planned' then 1 end) as planned")
    ->selectRaw("count(case when status = 'Completed' then 1 end) as completed")
    ->first();

we also can improve to // filter will allow you to use where statement
$status = Feature::toBase()
    ->selectRaw("count(*) filter (where status = 'Requested') as requested")
    ->selectRaw(("count(*) filter (where status = 'Planned') as planned")
    ->selectRaw(("count(*) filter (where status = 'Completed') as completed")
    ->first();