Skip to content

Commit

Permalink
feat(postgres): use jsonb column type by default
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Aug 9, 2020
1 parent 252c52f commit b6c0578
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 350 deletions.
5 changes: 2 additions & 3 deletions ROADMAP.md
Expand Up @@ -16,6 +16,7 @@ discuss specifics.
- Slow query log
- Leverage async iterators for collections (node 10+)
- Multi tenant support ([schema per tenant](https://dzone.com/articles/spring-boot-hibernate-multitenancy-implementation))
- Use `bigint` type natively in `BigIntType` (node 12+)

## Planned changes for v4

Expand Down Expand Up @@ -45,9 +46,7 @@ discuss specifics.
- [x] Remove `autoFlush` option
- [x] Drop default value for platform `type` (currently defaults to `mongodb`)
- [x] Remove `IdEntity/UuidEntity/MongoEntity` interfaces
- [ ] Rename `wrappedReference` to `reference` (keep `wrappedReference` supported)
- [ ] Use `bigint` type natively in `BigIntType`
- [ ] Use `jsonb` as default for object columns in postgres
- [x] Use `jsonb` as default for object columns in postgres

## Docs

Expand Down
2 changes: 1 addition & 1 deletion packages/postgresql/package.json
Expand Up @@ -43,7 +43,7 @@
"compile": "tsc -p tsconfig.build.json"
},
"dependencies": {
"pg": "^8.0.0",
"pg": "^8.0.3",
"@mikro-orm/core": "^4.0.0-alpha.0",
"@mikro-orm/knex": "^4.0.0-alpha.0"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/postgresql/src/PostgreSqlSchemaHelper.ts
Expand Up @@ -14,8 +14,8 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
text: ['text'],
Date: ['timestamptz(?)', 'timestamp(?)', 'datetime(?)', 'timestamp with time zone', 'timestamp without time zone', 'datetimetz', 'time', 'date', 'timetz', 'datetz'],
date: ['timestamptz(?)', 'timestamp(?)', 'datetime(?)', 'timestamp with time zone', 'timestamp without time zone', 'datetimetz', 'time', 'date', 'timetz', 'datetz'],
object: ['json'],
json: ['json'],
object: ['jsonb', 'json'],
json: ['jsonb', 'json'],
uuid: ['uuid'],
enum: ['text'], // enums are implemented as text columns with check constraints
};
Expand Down
12 changes: 6 additions & 6 deletions tests/__snapshots__/SchemaGenerator.test.ts.snap
Expand Up @@ -364,7 +364,7 @@ exports[`SchemaGenerator generate schema from metadata [postgres]: postgres-crea
"set names 'utf8';
set session_replication_role = 'replica';
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null default null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" json null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null default null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" jsonb null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");
alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");
create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");
Expand All @@ -378,7 +378,7 @@ create table \\"address2\\" (\\"author_id\\" int4 not null, \\"value\\" varchar(
alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");
alter table \\"address2\\" add constraint \\"address2_author_id_unique\\" unique (\\"author_id\\");
create table \\"book2\\" (\\"uuid_pk\\" varchar(36) not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" float null, \\"double\\" double precision null, \\"meta\\" json null, \\"author_id\\" int4 not null, \\"publisher_id\\" int4 null);
create table \\"book2\\" (\\"uuid_pk\\" varchar(36) not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" float null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int4 not null, \\"publisher_id\\" int4 null);
alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");
create table \\"book_tag2\\" (\\"id\\" bigserial primary key, \\"name\\" varchar(50) not null);
Expand Down Expand Up @@ -503,7 +503,7 @@ drop table if exists \\"book2_to_book_tag2\\" cascade;
drop table if exists \\"book_to_tag_unordered\\" cascade;
drop table if exists \\"publisher2_to_test2\\" cascade;
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null default null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" json null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null default null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" jsonb null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");
alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");
create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");
Expand All @@ -517,7 +517,7 @@ create table \\"address2\\" (\\"author_id\\" int4 not null, \\"value\\" varchar(
alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");
alter table \\"address2\\" add constraint \\"address2_author_id_unique\\" unique (\\"author_id\\");
create table \\"book2\\" (\\"uuid_pk\\" varchar(36) not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" float null, \\"double\\" double precision null, \\"meta\\" json null, \\"author_id\\" int4 not null, \\"publisher_id\\" int4 null);
create table \\"book2\\" (\\"uuid_pk\\" varchar(36) not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" float null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int4 not null, \\"publisher_id\\" int4 null);
alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");
create table \\"book_tag2\\" (\\"id\\" bigserial primary key, \\"name\\" varchar(50) not null);
Expand Down Expand Up @@ -1020,7 +1020,7 @@ exports[`SchemaGenerator update empty schema from metadata [postgres]: postgres-
"set names 'utf8';
set session_replication_role = 'replica';
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null default null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" json null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create table \\"author2\\" (\\"id\\" serial primary key, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"updated_at\\" timestamptz(3) not null default current_timestamp(3), \\"name\\" varchar(255) not null, \\"email\\" varchar(255) not null, \\"age\\" int4 null default null, \\"terms_accepted\\" bool not null default false, \\"optional\\" bool null, \\"identities\\" jsonb null, \\"born\\" date null, \\"born_time\\" time(0) null, \\"favourite_book_uuid_pk\\" varchar(36) null, \\"favourite_author_id\\" int4 null);
create index \\"custom_email_index_name\\" on \\"author2\\" (\\"email\\");
alter table \\"author2\\" add constraint \\"custom_email_unique_name\\" unique (\\"email\\");
create index \\"author2_terms_accepted_index\\" on \\"author2\\" (\\"terms_accepted\\");
Expand All @@ -1034,7 +1034,7 @@ create table \\"address2\\" (\\"author_id\\" int4 not null, \\"value\\" varchar(
alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\");
alter table \\"address2\\" add constraint \\"address2_author_id_unique\\" unique (\\"author_id\\");
create table \\"book2\\" (\\"uuid_pk\\" varchar(36) not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" float null, \\"double\\" double precision null, \\"meta\\" json null, \\"author_id\\" int4 not null, \\"publisher_id\\" int4 null);
create table \\"book2\\" (\\"uuid_pk\\" varchar(36) not null, \\"created_at\\" timestamptz(3) not null default current_timestamp(3), \\"title\\" varchar(255) null default '', \\"perex\\" text null, \\"price\\" float null, \\"double\\" double precision null, \\"meta\\" jsonb null, \\"author_id\\" int4 not null, \\"publisher_id\\" int4 null);
alter table \\"book2\\" add constraint \\"book2_pkey\\" primary key (\\"uuid_pk\\");
create table \\"book_tag2\\" (\\"id\\" bigserial primary key, \\"name\\" varchar(50) not null);
Expand Down
4 changes: 2 additions & 2 deletions tests/postgre-schema.sql
Expand Up @@ -19,7 +19,7 @@ drop table if exists "publisher2_to_test2" cascade;
drop table if exists "label2" cascade;
drop table if exists "new_table" cascade;

create table "author2" ("id" serial primary key, "created_at" timestamptz(3) not null default current_timestamp(3), "updated_at" timestamptz(3) not null default current_timestamp(3), "name" varchar(255) not null, "email" varchar(255) not null, "age" int4 null, "terms_accepted" bool not null default false, "optional" bool null, "identities" json null, "born" date null, "born_time" time(0) null, "favourite_book_uuid_pk" varchar(36) null, "favourite_author_id" int4 null);
create table "author2" ("id" serial primary key, "created_at" timestamptz(3) not null default current_timestamp(3), "updated_at" timestamptz(3) not null default current_timestamp(3), "name" varchar(255) not null, "email" varchar(255) not null, "age" int4 null, "terms_accepted" bool not null default false, "optional" bool null, "identities" jsonb null, "born" date null, "born_time" time(0) null, "favourite_book_uuid_pk" varchar(36) null, "favourite_author_id" int4 null);
alter table "author2" add constraint "custom_email_unique_name" unique ("email");
create index "author2_born_index" on "author2" ("born");
create index "born_time_idx" on "author2" ("born_time");
Expand All @@ -33,7 +33,7 @@ create table "address2" ("author_id" int4 not null check ("author_id" > 0), "val
alter table "address2" add constraint "address2_pkey" primary key ("author_id");
alter table "address2" add constraint "address2_author_id_unique" unique ("author_id");

create table "book2" ("uuid_pk" varchar(36) not null, "created_at" timestamptz(3) not null default current_timestamp(3), "title" varchar(255) null default '', "perex" text null, "price" float null, "double" numeric null, "meta" json null, "author_id" int4 not null, "publisher_id" int4 null, "foo" varchar(255) null);
create table "book2" ("uuid_pk" varchar(36) not null, "created_at" timestamptz(3) not null default current_timestamp(3), "title" varchar(255) null default '', "perex" text null, "price" float null, "double" numeric null, "meta" jsonb null, "author_id" int4 not null, "publisher_id" int4 null, "foo" varchar(255) null);
alter table "book2" add constraint "book2_pkey" primary key ("uuid_pk");

create table "book_tag2" ("id" bigserial primary key, "name" varchar(50) not null);
Expand Down

0 comments on commit b6c0578

Please sign in to comment.