kzvezdarov/ircdd — pull request #81 by kzvezdarov (2014-11-05 15:07:34+00:00)
Title: Db init

Resolves #76 .

This turned out to be a much larger refactoring of the database module.

### Database init
**Summary**: Extracted the database initialization code from the server.
**Reason**: Generally it is not a good idea to give the client application such wide access over the database.
**Details**: For now the restriction is fairly symbolic - nothing is stopping you from calling `r.db_drop` from somewhere in the server code. Looking into enabling actual client restrictions on rdb.
**Note**: This means that you have to init the database by hand whenever you want to run the full server. To do this, after all containers start, from your host machine:
`rethinkdb init -d ircdd/scripts/dev-config/rethinkdb_base_export`
That will initialize the database with two tables and some data. Looking into either automating this or making it a one-time thing.

### Added primary key usage
**Summary**: Started using primary keys with all data.
**Reason**: Primary keys make your life easier, safer, and should always be used.
**Details**: Decided to keep the default `id` field as the primary key field. The user and group documents set the name/nickname that they are initialized with as their primary key.

### Fixed queries
**Summary**: Simplified the `rdb` queries.
**Reason**: Quality of life.
**Details**: Because the queries were not using the documents' primary key mechanism, they were a needlessly complicated set of nested filters. After refactoring document creation to use primary keys, the rest of the queries were refactored with that in mind and are as a result simpler and cleaner.

### Fixed the model's methods
**Summary**: Methods now have sensible default values for optional fields, asserts for non-optionals.
**Reason**: It was possible to create users with empty nicknames, message lists that are actually strings.
**Details**: Arguments that cannot be empty/None are asserted as such. Optional arguments that can be skipped in the method call are given sensible default values, e.g. the message list of a group is an empty array.

### Fixed connection model
**Summary**: All queries share the same connection.
**Reason**: Make concurrent code that calls into the database module easier to debug, potentially easier management of hot reloading the configuration (e.g. switching database host).
**Details**: Just using the same connection for all queries, which is established on server startup. Failing to connect should crash hard.