Hi - firstly, thanks for a great library.
Issue
When using generated columns which are NOT NULL
, those columns are required in the Initializer interface but should in some cases be omitted or made optional.
Details
I've recently started using IDENTITY
columns rather than SEQUENCE
or SERIAL
for a number of reasons (see StackOverflow).
However when you use IDENTITY
as below, the generated UsersInitializer
requires the user_id
column. Similarly, the (slightly contrived) full_name
column, is also required.
In the case of user_id
this column will always be generated as it is defined as GENERATED ALWAYS
, and attempting to insert a value will result in an error. Given that it is always generated, it should probably be omitted from the interface. In the case of full_name
, as it is defined as GENERATED BY DEFAULT
, the column will be generated unless another value is provided and should probably be optional.
CREATE TABLE users (
user_id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
first_name text NOT NULL,
last_name text NOT NULL,
email text NOT NULL,
full_name text NOT NULL GENERATED BY DEFAULT AS (first_name || ' ' || last_name) STORED,
);
export interface UsersInitializer {
/** Primary key. Index: pk_users */
userId: UsersId;
firstName: string;
lastName: string;
email: string;
fullName: string;
}
Suggestion
The applicable postgres infoschema columns are below. If the generated column is an identity, identity_generation
is set and is_generated
is 'NULL', and vice versa.
is_identity: 'YES' | 'NO',
identity_generation: 'ALWAYS' | 'BY DEFAULT' | 'NULL' ,
is_generated: 'ALWAYS' | 'BY DEFAULT' | 'NULL',
generation_expression: string
I would suggest, in addition to defaultValue
in generateProperty.ts, an additional property signifying a generated column is added, something like isGenerated
that can be either be always
or default
. In the case of always
, that column would be omitted from the Initializer, and in the case of default
, that column would be optional. The result would be something like below:
CREATE TABLE users (
user_id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
first_name text NOT NULL,
last_name text NOT NULL,
email text NOT NULL,
full_name text NOT NULL GENERATED BY DEFAULT AS (first_name || ' ' || last_name) STORED,
);
export interface UsersInitializer {
firstName: string;
lastName: string;
email: string;
fullName?: string;
}
Thanks!