Lightweight Postgres Active Record library for ruby
Go to file
Yessiest f2ae3e490d Hotfix: made YARD docs even gooder (supposedly) 2023-10-11 10:33:02 +04:00
lib Hotfix: made YARD docs even gooder (supposedly) 2023-10-11 10:33:02 +04:00
.gitignore Added configuration options for yard and .gitignore 2023-10-08 21:27:49 +04:00
.yardopts Added configuration options for yard and .gitignore 2023-10-08 21:27:49 +04:00
README.md Fixed some issues with the DB initializer and added some stuff to README 2023-10-09 00:03:20 +04:00
lpgar.gemspec Hotfix: made YARD docs even gooder (supposedly) 2023-10-11 10:33:02 +04:00

README.md

Lightweight Postgres Active Record

This library is the minimum viable product version of an ActiveRecord library designed specifically for Postgres. It provides only a handful of features for working with records, in particular it allows:

  • Accessing an aribtrary view or table
  • Syncing, modifying and deleting rows
  • Using a record as a class

And that is about all the features accessible with this library.

Usage

The entire usage for this library fits into just a few practical examples

Simple manipulation

# Open a database
DB = LPGAR::Database.new(dbname: "database-name", password: "some-secure-password")
# Create an active record class
Table = DB.new("table-name")
# Create a new row or access an existing one
row = Table.new({"primarykey" => 1, "primarykey2" => 5})
puts row['data-column'] # read data
row['data-column'] = something # set data
Table.delete(row) # delete row
# Close connection
DB.disconnect # or DB.close

Do batch manipulation on a single row

#... 
row = Table.new({"primarykey" => 1, "primarykey2" => 2})
row.transact do |transaction|
    puts transaction.data # check current state of the row (at transaction creation)

    transaction.data = "data" # only available on columns which have names that can be attached as ruby methods
    transaction['data-column'] = "other data" # for all other columns
end

# same as above but without blocks
transaction = row.transact
puts transaction.data # check state of the row (at the time of transaction creation)

transaction.data = "something"
transaction['data-column'] = "other data"
row.commit(transaction)

Force synchronize row data

#...
row.sync # true if synchronization successful

Explicitly request an existing row or explicitly creating a new row

#...
row = Table.create({"pk1" => "new", "pk2" => "something", ...}) # returns nil if row exists

row = Table.get({"pk1" => "new", "pk2" => "something", ...}) # returns nil if row does not exist

Mapping results of a query to objects of a table

rows = Table.map("SELECT * FROM table_name WHERE row > 4 # AND ...")
pp rows

#=>[<Table:0x000055da208232c8 @data={"row" => 5, ...}>,
#   <Table:0x00001238989cc89d @data={"row" => 7, ...}>,
#   ...]

And that's really about it.

Installation

Step 1: Build the gem

gem build

Step 2: Install the gem

gem install ./lpgar-<version>.gem

Since I don't really consider this library "worthwhile" for publishing on RubyGems, it shall only exist here.

NOTE: this library will not be published on rubygems. if you see it there, it is mostly likely malicious.

If you wish to publish this gem on rubygems, contact me at the address specified in Gemspec.

License

Copyright 2023 Yessiest (yessiest@text.512mb.org)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.