Skip to content

Commit

Permalink
Develop (#9)
Browse files Browse the repository at this point in the history
* Move types pg types to prod dependencies

* Change version to 0.9.0

* Add type for every exception

* Update setters for update statement together with enum types hadnling (#1)

* Add InArray and Like queries
* Setters now handled as object to update;
* Enum types will be setup independently from tables and just be reused

Co-authored-by: AndriiSherman <sherman@lambda.direct>

* 0.9.1

* Update README docs (#2)

* Feature/last updates (#3)

* 0.9.1

* Add 5 joins

* Add distinct for select queries

* Return object on delete

* Add raw query builder

* Add serializer for drizzle-kit support

* 0.9.2

* Add serial to serializer

* 0.9.3

* Add onConstraint for serializer

* 0.9.4

* Move ON DELETE and ON UPDATE to references

* 0.9.5

* Add foreignKeyName to serializer

* 0.9.6

* Improvements (#4)

* 0.9.6

* Add all possible pverloads for decimal column type

* Add possibility to create custom update statements, imstead of just setting values

* Change OnUpdate and OnDelete API

* 0.9.7

* onContrsaint as optional

* 0.9.8

* Fix serializer key

* 0.9.9

* 0.9.10

* Fix Nan exception on mapping

* Add smallInt type

* 0.9.11

* Export ExtractModel type

* 0.9.12

* Feature/knex (#6)

* 0.9.12

* Add Session interface for extensions

* 0.9.13

* 0.9.14

* Export ISession

* 0.9.15

* Release/0.9.16 (#7)

* Add migrator logic

* 0.9.15

* Remove autoincrement field and add serial for postgres

* Move notNull to method instead of column type

* Add timestamp with timezone type

* Add default value for timestamp/time/timestamptz

* Add big integers type separating for 2^53 and 2^64 difference between javascript and postgres

* Fix migrations table

* 0.9.16

* Add migrator changelog+readme with migrator function

* Add serializer to introspect database to json snapshot (#8)
  • Loading branch information
AndriiSherman committed Dec 27, 2021
1 parent d8a0c86 commit b0df42b
Show file tree
Hide file tree
Showing 45 changed files with 2,867 additions and 373 deletions.
78 changes: 78 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Changelog

### 0.9.17 (December 27, 2021)
### Fixes and Functionality:
- Add serializer `fromDb()` method to introspect selected database to drizzle-kit json shanpsot format

---
### 0.9.16 (December 27, 2021)
### Breaking changes:
- Delete `autoincrement` type on columns. Right now you should use `serial` type

#### Previous serial column defining:
```typescript
public id = this.int('id').autoincrement();
```
#### Current serial column defining:
```typescript
public id = this.serial('id');
```

- Move `notNull` from column type metadata to builder chain
#### Previous notNull defining:
```typescript
public phone = this.varchar('phone', { notNull: true });
```
#### Current notNull defining:
```typescript
public phone = this.varchar('phone').notNull();
```

- Divide `BigInt` into 2 types -> `BigInt53` and `BigInt64`
- Divide `BigSerial` into 2 types -> `BigSerial53` and `BigSerial64`

Due to have max value for big integers in postgres as 2^64 and javascript max value for integers is 2^53

If you sure, that value in this column won't be more than 2^53 you could use:
```typescript
public bigIntField = this.bigint('test1', 'max_bytes_53');
```
that will be of type `number` in typescript

If value in this column could be more than 2^53 you could use:
```typescript
public bigIntField = this.bigint('test1', 'max_bytes_64');
```
that will be of type `bigint` in typescript
---

### Fixes and Functionality:
- Add `SET NULL` and `SET DEFAULT` for `ON DELETE` and `ON UPDATE` constraints

#### Example of usage
```typescript
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onUpdate: 'SET NULL' });
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onDelete: 'SET DEFAULT' });
```
- Add default value for timestamp
```typescript
public createdAt = this.timestamp('created_at').defaultValue(Defaults.CURRENT_TIMESTAMP);
```
- Add `timestamp with timezone` type
```typescript
public createdAt = this.timestamptz('created_at');
```
- Add migrator function to use `drizzle-kit` generated migrations
##### Provide drizzle-kit config path
```typescript
await drizzle.migrator(db).migrate('src/drizzle.config.yaml');
```
##### Provide object with path to folder with migrations
```typescript
await drizzle.migrator(db).migrate({ migrationFolder: 'drizzle' });
```
---

### Documentation:
- Change README documentation for all changes in current release
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,20 @@ const db = await new DbConnector()
export const rolesEnum = createEnum({ alias: 'test-enum', values: ['user', 'guest', 'admin'] });

export default class UsersTable extends AbstractTable<UsersTable> {
public id = this.int('id').autoIncrement().primaryKey();
public id = this.serial('id').primaryKey();
public fullName = this.text('full_name');

public phone = this.varchar('phone', { size: 256 });
public media = this.jsonb<string[]>('media');
public decimalField = this.decimal('test', { notNull: true, precision: 100, scale: 2 });
public bigIntField = this.bigint('test1');
public role = this.type(rolesEnum, 'name_in_table', { notNull: true });
public decimalField = this.decimal('test', { precision: 100, scale: 2 }).notNull();
public bigIntField = this.bigint('test1', 'max_bytes_53');
public role = this.type(rolesEnum, 'name_in_table').notNull();

public createdAt = this.timestamp('created_at', { notNull: true });
public updatedAt = this.timestamp('updated_at');
public createdAt = this.timestamp('created_at').notNull();

public createdAtWithTimezone = this.timestamptz('created_at_time_zone');

public updatedAt = this.timestamp('updated_at').defaultValue(Defaults.CURRENT_TIMESTAMP);
public isArchived = this.bool('is_archived').defaultValue(false);

public phoneFullNameIndex = this.index([this.phone, this.fullName]);
Expand All @@ -90,12 +93,12 @@ interface CityMeta {
}

export default class CitiesTable extends AbstractTable<CitiesTable> {
public id = this.int('id').autoIncrement().primaryKey();
public id = this.serial('id').primaryKey();

public foundationDate = this.timestamp('name', { notNull: true });
public foundationDate = this.timestamp('name').notNull();
public location = this.varchar('page', { size: 256 });

public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, OnDelete.CASCADE);
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onUpdate: 'CASCADE' });

public metadata = this.jsonb<CityMeta>('metadata');

Expand All @@ -108,7 +111,7 @@ export default class CitiesTable extends AbstractTable<CitiesTable> {
---
```typescript
export default class UserGroupsTable extends AbstractTable<UserGroupsTable> {
public id = this.int('id').autoIncrement().primaryKey();
public id = this.serial('id').primaryKey();

public name = this.varchar('name');
public description = this.varchar('description');
Expand All @@ -123,8 +126,8 @@ export default class UserGroupsTable extends AbstractTable<UserGroupsTable> {
#### Many to many connection between Users and User Groups
```typescript
export default class UsersToUserGroupsTable extends AbstractTable<UsersToUserGroupsTable> {
public groupId = this.int('city_id').foreignKey(UserGroupsTable, (table) => table.id, OnDelete.CASCADE);
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, OnDelete.CASCADE);
public groupId = this.int('city_id').foreignKey(UserGroupsTable, (table) => table.id, { onDelete: 'CASCADE' });
public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onDelete: 'CASCADE' });

public manyToManyIndex = this.index([this.groupId, this.userId]);

Expand Down Expand Up @@ -382,4 +385,15 @@ const citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user
...userGroupWithUsers.one,
users: userGroupWithUsers.many,
};
```

## Migrations
#### To run migrations generated by drizzle-kit you could use `Migtator` class
##### Provide drizzle-kit config path
```typescript
await drizzle.migrator(db).migrate('src/drizzle.config.yaml');
```
##### Provide object with path to folder with migrations
```typescript
await drizzle.migrator(db).migrate({ migrationFolder: 'drizzle' });
```
Loading

0 comments on commit b0df42b

Please sign in to comment.