Skip to content

Commit

Permalink
debt(sequelize): move attributes to declare
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed May 19, 2022
1 parent fea8b41 commit cf05709
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 277 deletions.
2 changes: 1 addition & 1 deletion .babelrc
@@ -1,6 +1,5 @@
{
"presets": [
"@babel/preset-typescript",
[
"@babel/preset-env",
{
Expand All @@ -11,6 +10,7 @@
]
],
"plugins": [
["@babel/plugin-transform-typescript", { "allowDeclareFields": true }],
"add-module-exports",
"lodash",
"@babel/plugin-proposal-class-properties",
Expand Down
16 changes: 5 additions & 11 deletions docs/adding-new-models.md
Expand Up @@ -52,7 +52,6 @@ module.exports = {
Example: `server/models/MyTable.ts`

```ts
import restoreSequelizeAttributesOnClass from '../lib/restore-sequelize-attributes-on-class';
import sequelize, { DataTypes, Model } from '../lib/sequelize';

// Define all attributes for the model
Expand All @@ -70,16 +69,11 @@ interface MyTableCreateAttributes {
}

class MyTable extends Model<MyTableAttributes, MyTableCreateAttributes> implements MyTableAttributes {
id: number;
MyCollectiveId: number;
createdAt: Date;
updatedAt: Date;
deletedAt: Date;

constructor(...args) {
super(...args);
restoreSequelizeAttributesOnClass(new.target, this);
}
declare id: number;
declare MyCollectiveId: number;
declare createdAt: Date;
declare updatedAt: Date;
declare deletedAt: Date;
}

MyTable.init(
Expand Down
79 changes: 35 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -34,7 +34,6 @@
"@babel/plugin-proposal-class-properties": "7.17.12",
"@babel/plugin-transform-async-to-generator": "7.17.12",
"@babel/preset-env": "7.17.12",
"@babel/preset-typescript": "7.17.12",
"@hyperwatch/hyperwatch": "3.8.2",
"@octokit/auth-oauth-app": "4.3.1",
"@octokit/rest": "18.12.0",
Expand Down Expand Up @@ -129,6 +128,7 @@
"devDependencies": {
"@babel/cli": "^7.16.0",
"@babel/eslint-parser": "^7.16.0",
"@babel/plugin-transform-typescript": "^7.17.12",
"@babel/register": "^7.16.0",
"@types/lodash": "^4.14.171",
"@types/mocha": "^9.0.0",
Expand Down
23 changes: 0 additions & 23 deletions server/lib/restore-sequelize-attributes-on-class.ts

This file was deleted.

16 changes: 5 additions & 11 deletions server/models/CurrencyExchangeRate.ts
@@ -1,20 +1,14 @@
import restoreSequelizeAttributesOnClass from '../lib/restore-sequelize-attributes-on-class';
import sequelize, { DataTypes, Model } from '../lib/sequelize';

/**
* Sequelize model to represent an CurrencyExchangeRate, linked to the `CurrencyExchangeRates` table.
*/
export class CurrencyExchangeRate extends Model {
public readonly id!: number;
public rate!: number;
public from!: string;
public to!: string;
public createdAt!: Date;

constructor(...args) {
super(...args);
restoreSequelizeAttributesOnClass(new.target, this);
}
public declare readonly id: number;
public declare rate: number;
public declare from: string;
public declare to: string;
public declare createdAt: Date;

static getMany(
fromCurrency: string,
Expand Down
16 changes: 5 additions & 11 deletions server/models/ExpenseAttachedFile.ts
Expand Up @@ -3,7 +3,6 @@ import { DataTypes, Model, Transaction } from 'sequelize';

import { diffDBEntries } from '../lib/data';
import { isValidUploadedImage } from '../lib/images';
import restoreSequelizeAttributesOnClass from '../lib/restore-sequelize-attributes-on-class';
import sequelize from '../lib/sequelize';

import models from '.';
Expand All @@ -12,16 +11,11 @@ import models from '.';
* Sequelize model to represent an ExpenseAttachedFile, linked to the `ExpenseAttachedFiles` table.
*/
export class ExpenseAttachedFile extends Model {
public readonly id!: number;
public ExpenseId!: number;
public CreatedByUserId: number;
public url!: string;
public createdAt!: Date;

constructor(...args) {
super(...args);
restoreSequelizeAttributesOnClass(new.target, this);
}
public declare readonly id: number;
public declare ExpenseId: number;
public declare CreatedByUserId: number;
public declare url: string;
public declare createdAt: Date;

/**
* Create an attachment from user-submitted data.
Expand Down
26 changes: 10 additions & 16 deletions server/models/ExpenseItem.ts
Expand Up @@ -3,7 +3,6 @@ import { DataTypes, Model, Transaction } from 'sequelize';

import { diffDBEntries } from '../lib/data';
import { isValidUploadedImage } from '../lib/images';
import restoreSequelizeAttributesOnClass from '../lib/restore-sequelize-attributes-on-class';
import { buildSanitizerOptions, sanitizeHTML } from '../lib/sanitize-html';
import sequelize from '../lib/sequelize';

Expand All @@ -13,24 +12,19 @@ import models from '.';
* Sequelize model to represent an ExpenseItem, linked to the `ExpenseItems` table.
*/
export class ExpenseItem extends Model {
public readonly id!: number;
public ExpenseId!: number;
public CreatedByUserId!: number;
public amount!: number;
public url!: string;
public createdAt!: Date;
public updatedAt!: Date;
public deletedAt: Date;
public incurredAt!: Date;
public description: string;
public declare readonly id: number;
public declare ExpenseId: number;
public declare CreatedByUserId: number;
public declare amount: number;
public declare url: string;
public declare createdAt: Date;
public declare updatedAt: Date;
public declare deletedAt: Date;
public declare incurredAt: Date;
public declare description: string;

private static editableFields = ['amount', 'url', 'description', 'incurredAt'];

constructor(...args) {
super(...args);
restoreSequelizeAttributesOnClass(new.target, this);
}

/**
* Based on `diffDBEntries`, diff two items list to know which ones where
* added, removed or added.
Expand Down
24 changes: 9 additions & 15 deletions server/models/HostApplication.ts
@@ -1,6 +1,5 @@
import { pick } from 'lodash';

import restoreSequelizeAttributesOnClass from '../lib/restore-sequelize-attributes-on-class';
import sequelize, { DataTypes, Model } from '../lib/sequelize';

import models from '.';
Expand All @@ -24,20 +23,15 @@ interface HostApplicationCreationAttributes {
}

export class HostApplication extends Model<HostApplication, HostApplicationCreationAttributes> {
public readonly id!: number;
public CollectiveId!: number;
public HostCollectiveId!: number;
public status!: HostApplicationStatus;
public customData: Record<string, unknown> | null;
public message: string;
public createdAt!: Date;
public updatedAt!: Date;
public deletedAt: Date | null;

constructor(...args) {
super(...args);
restoreSequelizeAttributesOnClass(new.target, this);
}
public declare readonly id: number;
public declare CollectiveId: number;
public declare HostCollectiveId: number;
public declare status: HostApplicationStatus;
public declare customData: Record<string, unknown> | null;
public declare message: string;
public declare createdAt: Date;
public declare updatedAt: Date;
public declare deletedAt: Date | null;

// ---- Static ----

Expand Down
18 changes: 6 additions & 12 deletions server/models/MigrationLog.ts
@@ -1,4 +1,3 @@
import restoreSequelizeAttributesOnClass from '../lib/restore-sequelize-attributes-on-class';
import sequelize, { DataTypes, Model } from '../lib/sequelize';

export enum MigrationLogType {
Expand Down Expand Up @@ -40,17 +39,12 @@ class MigrationLog
extends Model<MigrationLogAttributes, MigrationLogCommonCreateAttributes>
implements MigrationLogAttributes
{
id: number;
type: MigrationLogType;
createdAt: Date;
description: string;
data: MigrationLogData;
CreatedByUserId: number;

constructor(...args) {
super(...args);
restoreSequelizeAttributesOnClass(new.target, this);
}
public declare id: number;
public declare type: MigrationLogType;
public declare createdAt: Date;
public declare description: string;
public declare data: MigrationLogData;
public declare CreatedByUserId: number;

static async getDataForMergeAccounts(
fromAccountId: number,
Expand Down
30 changes: 12 additions & 18 deletions server/models/OAuthAuthorizationCode.ts
@@ -1,4 +1,3 @@
import restoreSequelizeAttributesOnClass from '../lib/restore-sequelize-attributes-on-class';
import sequelize, { DataTypes, Model } from '../lib/sequelize';

import models from '.';
Expand All @@ -25,23 +24,18 @@ class OAuthAuthorizationCode
extends Model<OAuthAuthorizationCodeAttributes, OAuthAuthorizationCodeCreateAttributes>
implements OAuthAuthorizationCodeAttributes
{
id: number;
code: string;
redirectUri: string;
expiresAt: Date;
data: Record<string, unknown>;
createdAt: Date;
updatedAt: Date;
deletedAt?: Date;
ApplicationId: number;
UserId: number;
application: typeof models.Application;
user: typeof models.User;

constructor(...args) {
super(...args);
restoreSequelizeAttributesOnClass(new.target, this);
}
public declare id: number;
public declare code: string;
public declare redirectUri: string;
public declare expiresAt: Date;
public declare data: Record<string, unknown>;
public declare createdAt: Date;
public declare updatedAt: Date;
public declare deletedAt?: Date;
public declare ApplicationId: number;
public declare UserId: number;
public declare application: typeof models.Application;
public declare user: typeof models.User;
}

OAuthAuthorizationCode.init(
Expand Down

0 comments on commit cf05709

Please sign in to comment.