FileDb

FileDb is a small type of database. It stores all data for a model into CSV files and create accessor for you.

Installation

hm, yeah. just add this to your Gemfile:

gem 'file_db', '~> 1.1.0'

And then execute:

  $ bundle

Or install it yourself as:

  $ gem install file_db

Huh, ready to use!

Differences to Version 0.7.0

Fieldnames are now in the first line of the table file.

And:

Performance

0.7.0 with 1000 Entries

Action Time in Milliseconds
getting the first 29.194
update record 39.226
create record 74.994
using where with 1 parameter 8.700
using where with 2 parameter 9.775
find 9.507

1.0.0 with 1000 Entries

Action Time in Milliseconds
getting the first 0.031
update record 4.629
create record 3.673
using where with 1 parameter 0.535
using where with 2 parameter 0.337
find 0.030

0.7.0 with 5000 Entries

Action Time in Milliseconds
getting the first 88.852
update record 122.662
create record 284.086
using where with 1 parameter 35.795
using where with 2 parameter 35.125
find 35.881

1.0.0 with 5000 Entries

Action Time in Milliseconds
getting the first 0.037
update record 14.405
create record 9.705
using where with 1 parameter 1.846
using where with 2 parameter 2.027
find 0.019

Usage

Configuration

First configure the storage directory:

FileDb::Configuration.configure data_directory: 'data'

Subdirectory is the default for storing the tables. If you change the configuration, the FileDb will be automaticly create a new database directory and table files if needed.

Let's start with creating a model called User.

class User < FileDb::Model

end

Now we got a user model which actually just storing an id. lets add some columns for this.

class User < FileDb::Model
  columns :name, :email
end

Now lets create some entries.

my_user = User.new name: 'rob'
my_user.save

now the User is stored under data/user.csv. You should find an entry like this one:

1,rob,

You can also use create to create it directly without calling save:

my_user = User.create name: 'rob'

Let's get them user back:

User.find 1
-> #<User:0x00000004651798 @name="rob", @id="1", @email=nil>

you can access all attributes like attr_accessor

User.find(1).name
-> rob

The accessors will be created automaticly for you. Let's find all users named with rob. I think you know how get this to work:

User.where(name: 'rob')
-> [#<User:0x00000004651798 @name="rob", @id="1", @email=nil>]

It's also fine to search with more than one parameter.

User.where(name: 'rob', email: nil)
-> [#<User:0x00000004651798 @name="rob", @id="1", @email=nil>]

For the first User with name rob you can use:

User.where(name: 'rob', email: nil).first
-> #<User:0x00000004651798 @name="rob", @id="1", @email=nil>

or

User.find_by(:name, 'rob')
-> [#<User:0x00000004651798 @name="rob", @id="1", @email=nil>]

You can also use first and last to get the first and last user

User.first
-> #<User:0x00000004651798 @name="rob", @id="1", @email=nil>
User.last
-> #<User:0x00000004651798 @name="rob", @id="1", @email=nil>

Or you use all to get really all users.

User.all
-> [#<User:0x00000004651798 @name="rob", @id="1", @email=nil>]

rename the user:

user = User.find(1)
-> #<User:0x00000004651798 @name="rob", @id="1", @email=nil>
user.name = 'bob'
user.email = '[email protected]'
user.save
User.find(1)
-> #<User:0x00000004651798 @name="bob", @id="1", @[email protected]>

delete a record:

user = User.find(1)
-> #<User:0x00000004651798 @name="rob", @id="1", @email=nil>
user.delete
User.find(1)
-> nil

You want to use another table than user? So just configure it:

class User < FileDb::Model
  columns :name, :email
  set_table_name 'crazy'
end

The MIT License (MIT)

Copyright (c) 2015 Robert Starke

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Questions?

If you have further questions, code smells, hints or a beer, just contact me :)