36 Commits

Author SHA1 Message Date
c54780d2f0 Support Nim 2.x, compatibility with waterpark.
- Nim 2.x has moved the DB connectors outside the standard library to
  the `db_connector` package.
- Refactor the pooling implementation and macro expectations to use the
  `withConnection` name instead of `withConn`. This change allows a
  caller to use a [waterpark](https://github.com/guzba/waterpark) pool
  instance instead of the builtin pool instance. Waterpark provides
  better support for multi-threaded environments. The builtin pooling
  mechanism may be deprecated in favor of waterpark in the future.
- Add the `getModelIfItExists` generated proc to the list of standard
  procs we generate. This is a flavour of `getModel` that returns an
  `Option` instead of raising an exception when there is no model for
  the given id.
- Change `PaginationParams#orderBy` to accept a `seq[string]` to allow
  for sorting on multiple fields.
2025-01-03 07:49:21 -06:00
fb74d84cb7 Map names to db ident names for columns passed for ordering in paginated queries. 2.2.0 2023-08-09 09:16:10 -05:00
fbd20de71f Add createOrUpdateRecord and record method generators.
`createOrUpdateRecord` implements upsert: update an existing record if
it exists or create a new record if not. A new error `DbUpdateError` was
added to be raised when an existing record does exist but was not able
to be updated.
2023-08-09 09:13:12 -05:00
540d0d2f67 Fix missing import in pooling implementation. 2.1.1 2023-02-04 19:04:50 -06:00
a05555ee67 WIP - Initial stab at making it generic to support db_sqlite. 2.1.0 2022-11-03 16:38:14 -05:00
454fc8c47a Add support for pagination in functions returning multiple records. 2.0.0 2022-09-08 12:52:21 -05:00
004e3c19bb Correctly pluralize tables names in getAll and findWhere generated procedures. 1.0.4 2022-09-08 10:40:59 -05:00
4e0a173c76 Remove hidden dependency on timeutils (private repo). 1.0.3 2022-09-06 09:50:24 -05:00
bd5d8c98b8 Update README to match main fiber_orm docs. 2022-09-04 12:59:35 -05:00
7d45346bb6 Add generated documentations (for use with GitHub pages). 2022-09-03 21:32:22 -05:00
a3dbb0bbbc Flesh out documentation with a worked example. 2022-09-03 20:53:37 -05:00
9bf3c4f3ec Initial stab at better documentation. 2022-09-02 23:25:52 -05:00
1d7c955805 Bugfix: don't try to cull connections from an empty pool.
The first step in culling connections was to take a subset of the pool's
connections from index 0 to numToCull. This leads to an error if
numToCull is also zero.
1.0.2
2022-06-04 10:46:00 -05:00
9625ac6a5e withPool executes provided statement block in a try/finally to ensure the connection is released. 2022-04-25 18:22:34 -05:00
d4540a1de7 Make column ordering explicit in createRecord return.
The Nim [Row][nim-row] implementation only supports positional
identification of columns. In other words, there is nothing to tell us
which column is in which position. Because of this, we always create SQL
statements which explicitly name the columns we wish to receive so that
we know the order of columns and can rebuild models appropriately.

`createRule` wasn't doing this but naively using `RETURNING *`. This
still works as long as the field ordering in the Nim model class match
the default column ordering returned by the database, but confuses
columns otherwise. This fixes that by specifying explicitly the column
ordering as we do in other places.

[nim-row]: https://nim-lang.org/docs/db_postgres.html#Row
1.0.1
2022-03-11 12:54:14 -06:00
3e19b3628d Use a connection provider rather than long-lived connections.
The previous implementation expected to work with a context object that
contained a field `conn` that represented a valid database connection.
However, this required the caller to manage the connection's lifecycle
(close, renew, etc.). Now we expect to receive a context object that
provides a `withConn` procedure or template that accepts a statement
block and provides a `conn` variable to that code block. For example:

    createRecord(db: DbContext): Record =
        # withConn must be defined for DbContext
        db.withConn:
          # conn must be injected into the statement block context
          conn.exec(sql("INSERT INTO..."))

In addition, this change provides a connection pooling mechanism
(`DbConnPool`) as a default implementation for this hypothetical
DbContext. There is also a new function `initPool` that will create an
DbConnPool instance.

Callers of this library can modify their DbContext objects to extend
DbConnPool or simply be a type alias of DbConnPool.
1.0.0
2022-02-07 11:38:37 -06:00
8aad3cdb79 Make the logging namespace GC-safe. 0.3.6 2022-01-22 20:23:30 -06:00
f7791b6f60 Add logging statments (behind namespaced logger). 0.3.5 2022-01-13 16:22:15 -06:00
279d9aa7fd Expose a number of useful utility methods and macros. 0.3.4 2021-08-02 05:54:56 -05:00
d90372127b Further fix for ISO8601 date parsing.
Recognize versions of timestamps with 'T' as the date/time separator.
For example, compare:

    '2021-08-01 23:14:00-05:00'
    '2021-08-01T23:14:00-05:00'

This commit adds support for the second flavor (and it's variations).
0.3.3
2021-08-01 23:14:18 -05:00
2b78727356 Fix for PostgreSQL timestamp with timezone fields.
The previous fix for PostgreSQL timestamp fields matched fields with and
without timezones, but did not properly parse values from fields that
included the timezone. Now we check for the presence of the timezone in
the date string and choose a format string to parse it correctly.
0.3.2
2021-07-05 11:24:06 -05:00
445c86f97e Shuffle variable declarations to make functions GC-safe. 0.3.1 2021-07-02 20:34:29 -05:00
126167fdaf Fix name mapping in field lookups generation. 2021-07-02 20:16:49 -05:00
ff0c5e5305 Update to support Nim 1.4.x+ 2021-04-02 13:58:08 -05:00
bdd62cad66 Add support for float data type. 0.3.0 2020-02-16 21:50:43 -06:00
b496b10578 Bump version number for 0.2.0 release. 0.2.0 2020-02-09 04:22:59 -06:00
c6430baa9a Support more ID types, allow creating records with known IDs.
* Previously all ID types needed to have the `isZero` function defined.
  This was named after the UUID.isZero function but does not describe
  the general case. Non-numeric is types will not be "zero," they will
  be "empty." Renamed to `isEmpty` and implemented basic versions of
  this for `int`, `string`, and `UUID`.

* Previously newly created records were not allowed to have ids set.
  Fiber required them to be set in the database and the caller to
  retrieve the newly generated ID from the newly created record. This is
  not really a decision that Fiber should make in a general case. There
  are valid reasons for both possibilities (creating the id in the
  application code vs. creating the id in the database layer). As a
  more general-purpose solution Fiber now leaves this to the caller.

  If the caller provides an id value, Fiber will include it in the
  INSERT statement. If the id is unset, Fiber will not include it at
  all, allowing it to be generated in the database.
2020-01-02 18:55:46 -06:00
cd52c9860d Add support for enum values. 2020-01-02 18:55:38 -06:00
af755a8a8d Round out support for Option type model fields.
The Option type has two forms depending on the type of the wrapped value
(see https://nim-lang.org/docs/options.html#Option). We only supported
one of these previously. This commit adds support for the other type as
well.

Additionally, this fixes a compile error that was introduced into the
use of `isSome` in the generated code after Nim 1.0.
2020-01-02 18:51:38 -06:00
1f57e0dccc Fix support for PostgreSQL timestamp fields.
PostgreSQL uses a format similar to IS8601 but allows values expressed
with tenths or hundreths of seconds rather than just milliseconds.
`2020-01-01 12:34:98.3+00` as opposed to `2020-01-01 12:34:98.300+00`,
for example. The `times` module in the Nim stdlib supports only
milliseconds with exactly three digits. To bridge this gap we detect the
two unsupported cases and pad the fractional seconds out to millisecond
precision.
2020-01-02 18:46:54 -06:00
61e06842af Use the more intelligent pluralization method we built when generating findXsByField lookup functions. 2020-01-02 18:46:03 -06:00
934bb26cf3 Make the DB type object generic (not PmApiDb). 2020-01-02 18:44:44 -06:00
126c4f1c7c Make record not found error messages more descriptive. 2020-01-02 18:42:48 -06:00
56a257be2e Bump package version. 0.1.2 2019-12-25 20:36:03 -06:00
35af299fdc Rename module from 'fiber_orm_nim' to 'fiber_orm'. 0.1.1 2019-12-25 18:51:00 -06:00
f84849ecde Initial commit of Nim Fiber ORM
Initial code based on common db module developed as part of the Personal
Measure project.
0.1.0
2019-12-25 18:07:23 -06:00