feat: port basic functionality to elixir
This commit is contained in:
16
priv/repo/migrations/20250913202135_create_categories.exs
Normal file
16
priv/repo/migrations/20250913202135_create_categories.exs
Normal file
@@ -0,0 +1,16 @@
|
||||
defmodule ComponentsElixir.Repo.Migrations.CreateCategories do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:categories) do
|
||||
add :name, :string, null: false
|
||||
add :description, :text
|
||||
add :parent_id, references(:categories, on_delete: :delete_all)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
create index(:categories, [:parent_id])
|
||||
create unique_index(:categories, [:name, :parent_id])
|
||||
end
|
||||
end
|
||||
25
priv/repo/migrations/20250913202150_create_components.exs
Normal file
25
priv/repo/migrations/20250913202150_create_components.exs
Normal file
@@ -0,0 +1,25 @@
|
||||
defmodule ComponentsElixir.Repo.Migrations.CreateComponents do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:components) do
|
||||
add :name, :string, null: false
|
||||
add :description, :text
|
||||
add :keywords, :string
|
||||
add :position, :string
|
||||
add :count, :integer, default: 0
|
||||
add :datasheet_url, :string
|
||||
add :image_filename, :string
|
||||
add :category_id, references(:categories, on_delete: :restrict), null: false
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
create index(:components, [:category_id])
|
||||
create index(:components, [:name])
|
||||
|
||||
# Full-text search indexes for PostgreSQL
|
||||
execute "CREATE INDEX components_search_idx ON components USING gin(to_tsvector('english', name || ' ' || coalesce(description, '') || ' ' || coalesce(keywords, '')))",
|
||||
"DROP INDEX components_search_idx"
|
||||
end
|
||||
end
|
||||
@@ -9,3 +9,136 @@
|
||||
#
|
||||
# We recommend using the bang functions (`insert!`, `update!`
|
||||
# and so on) as they will fail if something goes wrong.
|
||||
|
||||
alias ComponentsElixir.{Repo, Inventory}
|
||||
alias ComponentsElixir.Inventory.{Category, Component}
|
||||
|
||||
# Clear existing data
|
||||
Repo.delete_all(Component)
|
||||
Repo.delete_all(Category)
|
||||
|
||||
# Create categories
|
||||
{:ok, resistors} = Inventory.create_category(%{name: "Resistors", description: "Various types of resistors"})
|
||||
{:ok, capacitors} = Inventory.create_category(%{name: "Capacitors", description: "Electrolytic, ceramic, and film capacitors"})
|
||||
{:ok, semiconductors} = Inventory.create_category(%{name: "Semiconductors", description: "ICs, transistors, diodes"})
|
||||
{:ok, connectors} = Inventory.create_category(%{name: "Connectors", description: "Headers, terminals, plugs"})
|
||||
|
||||
# Create subcategories
|
||||
{:ok, _through_hole_resistors} = Inventory.create_category(%{
|
||||
name: "Through-hole",
|
||||
description: "Traditional leaded resistors",
|
||||
parent_id: resistors.id
|
||||
})
|
||||
|
||||
{:ok, _smd_resistors} = Inventory.create_category(%{
|
||||
name: "SMD/SMT",
|
||||
description: "Surface mount resistors",
|
||||
parent_id: resistors.id
|
||||
})
|
||||
|
||||
{:ok, _ceramic_caps} = Inventory.create_category(%{
|
||||
name: "Ceramic",
|
||||
description: "Ceramic disc and multilayer capacitors",
|
||||
parent_id: capacitors.id
|
||||
})
|
||||
|
||||
{:ok, _electrolytic_caps} = Inventory.create_category(%{
|
||||
name: "Electrolytic",
|
||||
description: "Polarized electrolytic capacitors",
|
||||
parent_id: capacitors.id
|
||||
})
|
||||
|
||||
# Create sample components
|
||||
sample_components = [
|
||||
%{
|
||||
name: "1kΩ Resistor (1/4W)",
|
||||
description: "Carbon film resistor, 5% tolerance",
|
||||
keywords: "resistor carbon film 1k ohm",
|
||||
position: "A1-1",
|
||||
count: 150,
|
||||
category_id: resistors.id
|
||||
},
|
||||
%{
|
||||
name: "10kΩ Resistor (1/4W)",
|
||||
description: "Carbon film resistor, 5% tolerance",
|
||||
keywords: "resistor carbon film 10k ohm",
|
||||
position: "A1-2",
|
||||
count: 200,
|
||||
category_id: resistors.id
|
||||
},
|
||||
%{
|
||||
name: "100μF Electrolytic Capacitor",
|
||||
description: "25V electrolytic capacitor, radial leads",
|
||||
keywords: "capacitor electrolytic 100uf microfarad",
|
||||
position: "B2-1",
|
||||
count: 50,
|
||||
category_id: capacitors.id
|
||||
},
|
||||
%{
|
||||
name: "0.1μF Ceramic Capacitor",
|
||||
description: "50V ceramic disc capacitor",
|
||||
keywords: "capacitor ceramic 100nf nanofarad disc",
|
||||
position: "B2-2",
|
||||
count: 300,
|
||||
category_id: capacitors.id
|
||||
},
|
||||
%{
|
||||
name: "ATmega328P-PU",
|
||||
description: "8-bit AVR microcontroller, DIP-28 package",
|
||||
keywords: "microcontroller avr atmega328 arduino",
|
||||
position: "C3-1",
|
||||
count: 10,
|
||||
datasheet_url: "https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf",
|
||||
category_id: semiconductors.id
|
||||
},
|
||||
%{
|
||||
name: "LM358 Op-Amp",
|
||||
description: "Dual operational amplifier, DIP-8 package",
|
||||
keywords: "opamp operational amplifier lm358 dual",
|
||||
position: "C3-2",
|
||||
count: 25,
|
||||
category_id: semiconductors.id
|
||||
},
|
||||
%{
|
||||
name: "2N2222 NPN Transistor",
|
||||
description: "General purpose NPN transistor, TO-92 package",
|
||||
keywords: "transistor npn 2n2222 to92",
|
||||
position: "C3-3",
|
||||
count: 40,
|
||||
category_id: semiconductors.id
|
||||
},
|
||||
%{
|
||||
name: "2.54mm Pin Headers",
|
||||
description: "Male pin headers, 40 pins, break-away",
|
||||
keywords: "header pins male 2.54mm breakaway",
|
||||
position: "D4-1",
|
||||
count: 20,
|
||||
category_id: connectors.id
|
||||
},
|
||||
%{
|
||||
name: "JST-XH 2-pin Connector",
|
||||
description: "2-pin JST-XH connector with housing",
|
||||
keywords: "jst xh connector 2pin housing",
|
||||
position: "D4-2",
|
||||
count: 30,
|
||||
category_id: connectors.id
|
||||
},
|
||||
%{
|
||||
name: "470Ω Resistor (1/4W)",
|
||||
description: "Carbon film resistor, 5% tolerance, commonly used for LEDs",
|
||||
keywords: "resistor carbon film 470 ohm led current limiting",
|
||||
position: "A1-3",
|
||||
count: 100,
|
||||
category_id: resistors.id
|
||||
}
|
||||
]
|
||||
|
||||
Enum.each(sample_components, fn component_attrs ->
|
||||
{:ok, _component} = Inventory.create_component(component_attrs)
|
||||
end)
|
||||
|
||||
IO.puts("Seeded database with categories and sample components!")
|
||||
IO.puts("Categories: #{length(Inventory.list_categories())}")
|
||||
IO.puts("Components: #{length(Inventory.list_components())}")
|
||||
IO.puts("")
|
||||
IO.puts("Login with password: changeme (or set AUTH_PASSWORD environment variable)")
|
||||
|
||||
Reference in New Issue
Block a user