Skip to content

Commit

Permalink
feat(computed-column): adds new computed type
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 committed Sep 19, 2021
1 parent 070550b commit d6dd376
Show file tree
Hide file tree
Showing 25 changed files with 86 additions and 20 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Warthog is now on version 3.0! There were a few breaking changes that you should
<summary>Expand for Breaking change details</summary>
<p>

### Adds `computed` attribute to models

A `computed` property is one that is fully managed by the server. A good example is a status flag that is managed by a state machine in the backend. You should not be able to modify this on create or update, but should always be able to read it.

### Switch ID generation library from `shortid` (deprecated) to `nanoid`

Per `shortid` [README](https://github.com/dylang/shortid/blob/master/README.md) it is deprecated.
Expand Down
2 changes: 1 addition & 1 deletion examples/01-simple-model/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-1
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_FILTER_BY_DEFAULT=true
2 changes: 1 addition & 1 deletion examples/03-one-to-many-relationship/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-3
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_FILTER_BY_DEFAULT=true
2 changes: 1 addition & 1 deletion examples/04-many-to-many-relationship/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-4
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_FILTER_BY_DEFAULT=true
2 changes: 1 addition & 1 deletion examples/05-migrations/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-5
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_FILTER_BY_DEFAULT=true
2 changes: 1 addition & 1 deletion examples/06-base-service/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-6
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_FILTER_BY_DEFAULT=true
2 changes: 1 addition & 1 deletion examples/07-feature-flags/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-feature-flag
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_FILTER_BY_DEFAULT=true
2 changes: 1 addition & 1 deletion examples/08-performance/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-8
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_FILTER_BY_DEFAULT=true
2 changes: 1 addition & 1 deletion examples/09-production/.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-9
WARTHOG_DB_HOST=localhost
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_PORT=5432
WARTHOG_DB_USERNAME=postgres
WARTHOG_FILTER_BY_DEFAULT=false
2 changes: 1 addition & 1 deletion examples/10-subscriptions/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-10-subscriptions
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_SUBSCRIPTIONS=true
WARTHOG_FILTER_BY_DEFAULT=true
2 changes: 1 addition & 1 deletion examples/11-transactions/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-11-transactions
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_SUBSCRIPTIONS=true
WARTHOG_FILTER_BY_DEFAULT=true
2 changes: 1 addition & 1 deletion examples/14-base-service-v2/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ PGUSER=postgres
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-14-base-service-v2
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_SUBSCRIPTIONS=true
2 changes: 1 addition & 1 deletion src/cli/templates/new/_env.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WARTHOG_API_BASE_URL=http://localhost:4000
WARTHOG_DB_DATABASE=<%= props.kebabName %>
WARTHOG_DB_HOST=localhost
WARTHOG_DB_LOGGING=all
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_PORT=5432
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_DB_USERNAME=postgres
2 changes: 1 addition & 1 deletion src/cli/templates/new/env.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local_db: &local_db
WARTHOG_DB_DATABASE: warthog-starter-development
WARTHOG_DB_HOST: localhost
WARTHOG_DB_LOGGING: all
WARTHOG_DB_PASSWORD: ''
WARTHOG_DB_PASSWORD: postgres
WARTHOG_DB_PORT: 5432
WARTHOG_DB_SYNCHRONIZE: true
WARTHOG_DB_USERNAME: postgres
Expand Down
2 changes: 1 addition & 1 deletion src/core/tests/dotenv-files/.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ WARTHOG_D=ENV
WARTHOG_API_BASE_URL=http://localhost:4100
WARTHOG_DB_DATABASE=warthog-example-1
WARTHOG_DB_USERNAME=postgres
WARTHOG_DB_PASSWORD=
WARTHOG_DB_PASSWORD=postgres
WARTHOG_DB_SYNCHRONIZE=true
WARTHOG_DB_HOST=localhost
16 changes: 16 additions & 0 deletions src/decorators/OneToOne.ts.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Field } from 'type-graphql';
import { JoinTable, ManyToMany as TypeORMManyToMany } from 'typeorm';
import { composeMethodDecorators, MethodDecoratorFactory } from '../utils';

// Note: for many to many relationships, you need to set one item as the "JoinTable"
// therefore, we have 2 separate decorators. Just make sure to add one to one table and
// One to the other in the relationship
export function OneToOne(parentType: any, joinFunc: any, options: any = {}): any {
const factories = [
JoinTable() as MethodDecoratorFactory,
Field(() => [parentType()], { nullable: true, ...options }) as MethodDecoratorFactory,
TypeORMManyToMany(parentType, joinFunc, options) as MethodDecoratorFactory
];

return composeMethodDecorators(...factories);
}
3 changes: 2 additions & 1 deletion src/metadata/metadata-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export type FieldType =

export interface DecoratorCommonOptions {
apiOnly?: boolean;
computed?: boolean; // Means the back end will manage this column fully. Can read it, but no writing
dbOnly?: boolean;
description?: string;
editable?: boolean;
filter?: boolean | WhereOperator[];
nullable?: boolean;
readonly?: boolean;
readonly?: boolean; // Setting this will set it readonly with TypeORM meaning you can't use update methods at all
sort?: boolean;
writeonly?: boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class SchemaGenerator {
const modelColumns = this.getColumnsForModel(model);

modelColumns.forEach((column: ColumnMetadata) => {
if (column.readonly) {
if (column.readonly || column.computed) {
return;
}
let graphQLDataType = this.columnToGraphQLDataType(column);
Expand Down Expand Up @@ -279,7 +279,7 @@ export class SchemaGenerator {

const modelColumns = this.getColumnsForModel(model);
modelColumns.forEach((column: ColumnMetadata) => {
if (column.readonly) {
if (column.readonly || column.computed) {
return;
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/functional/__snapshots__/schema.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type KitchenSink {
integerField: Int!
booleanField: Boolean!
floatField: Float!
computedColumn: String!
jsonField: JSONObject
idField: ID
stringEnumField: StringEnum
Expand Down Expand Up @@ -212,6 +213,8 @@ enum KitchenSinkOrderByInput {
booleanField_DESC
floatField_ASC
floatField_DESC
computedColumn_ASC
computedColumn_DESC
idField_ASC
idField_DESC
stringEnumField_ASC
Expand Down Expand Up @@ -323,6 +326,11 @@ input KitchenSinkWhereInput {
floatField_lt: Float
floatField_lte: Float
floatField_in: [Float!]
computedColumn_eq: String
computedColumn_contains: String
computedColumn_startsWith: String
computedColumn_endsWith: String
computedColumn_in: [String!]
jsonField_json: JSONObject
idField_eq: ID
idField_in: [ID!]
Expand Down
8 changes: 8 additions & 0 deletions src/test/generated/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export type KitchenSinkOrderByInput = 'id_ASC' |
'booleanField_DESC' |
'floatField_ASC' |
'floatField_DESC' |
'computedColumn_ASC' |
'computedColumn_DESC' |
'idField_ASC' |
'idField_DESC' |
'stringEnumField_ASC' |
Expand Down Expand Up @@ -285,6 +287,11 @@ export interface KitchenSinkWhereInput {
floatField_lt?: Float | null
floatField_lte?: Float | null
floatField_in?: Float[] | Float | null
computedColumn_eq?: String | null
computedColumn_contains?: String | null
computedColumn_startsWith?: String | null
computedColumn_endsWith?: String | null
computedColumn_in?: String[] | String | null
jsonField_json?: JSONObject | null
idField_eq?: ID_Input | null
idField_in?: ID_Output[] | ID_Output | null
Expand Down Expand Up @@ -439,6 +446,7 @@ export interface KitchenSink {
integerField: Int
booleanField: Boolean
floatField: Float
computedColumn: String
jsonField?: JSONObject | null
idField?: ID_Output | null
stringEnumField?: StringEnum | null
Expand Down
18 changes: 18 additions & 0 deletions src/test/generated/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ export enum KitchenSinkOrderByEnum {
floatField_ASC = "floatField_ASC",
floatField_DESC = "floatField_DESC",

computedColumn_ASC = "computedColumn_ASC",
computedColumn_DESC = "computedColumn_DESC",

idField_ASC = "idField_ASC",
idField_DESC = "idField_DESC",

Expand Down Expand Up @@ -339,6 +342,21 @@ export class KitchenSinkWhereInput {
@TypeGraphQLField(() => [Float], { nullable: true })
floatField_in?: number[];

@TypeGraphQLField({ nullable: true })
computedColumn_eq?: string;

@TypeGraphQLField({ nullable: true })
computedColumn_contains?: string;

@TypeGraphQLField({ nullable: true })
computedColumn_startsWith?: string;

@TypeGraphQLField({ nullable: true })
computedColumn_endsWith?: string;

@TypeGraphQLField(() => [String], { nullable: true })
computedColumn_in?: string[];

@TypeGraphQLField(() => GraphQLJSONObject, { nullable: true })
jsonField_json?: JsonObject;

Expand Down
8 changes: 8 additions & 0 deletions src/test/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ type KitchenSink {
integerField: Int!
booleanField: Boolean!
floatField: Float!
computedColumn: String!
jsonField: JSONObject
idField: ID
stringEnumField: StringEnum
Expand Down Expand Up @@ -209,6 +210,8 @@ enum KitchenSinkOrderByInput {
booleanField_DESC
floatField_ASC
floatField_DESC
computedColumn_ASC
computedColumn_DESC
idField_ASC
idField_DESC
stringEnumField_ASC
Expand Down Expand Up @@ -320,6 +323,11 @@ input KitchenSinkWhereInput {
floatField_lt: Float
floatField_lte: Float
floatField_in: [Float!]
computedColumn_eq: String
computedColumn_contains: String
computedColumn_startsWith: String
computedColumn_endsWith: String
computedColumn_in: [String!]
jsonField_json: JSONObject
idField_eq: ID
idField_in: [ID!]
Expand Down
3 changes: 3 additions & 0 deletions src/test/modules/kitchen-sink/kitchen-sink.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export class KitchenSink extends BaseModel {
@FloatField()
floatField?: number;

@StringField({ computed: true, default: 'computed' })
computedColumn?: string;

@JSONField({ nullable: true })
jsonField?: JsonObject;

Expand Down
2 changes: 1 addition & 1 deletion src/test/server-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getStandardEnvironmentVariables(): StringMap {
WARTHOG_DB_MIGRATIONS_DIR: './tmp/test/migrations',
WARTHOG_DB_OVERRIDE: 'true', // Set so that we can do DB stuff outside of NODE_ENV=development
WARTHOG_DB_USERNAME: 'postgres',
WARTHOG_DB_PASSWORD: '',
WARTHOG_DB_PASSWORD: 'postgres',
WARTHOG_DB_SYNCHRONIZE: 'true',
WARTHOG_FILTER_BY_DEFAULT: 'true',
WARTHOG_GENERATED_FOLDER: './src/test/generated',
Expand Down
4 changes: 2 additions & 2 deletions tools/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ NODE_ENV=test ./src/test/codegen-test-files.sh

if [ -z "$SKIP_DB_CREATION" ]
then
NODE_ENV=test PGUSER=postgres ./bin/warthog db:drop
NODE_ENV=test PGUSER=postgres ./bin/warthog db:create
NODE_ENV=test PGUSER=postgres WARTHOG_DB_PASSWORD=postgres ./bin/warthog db:drop
NODE_ENV=test PGUSER=postgres WARTHOG_DB_PASSWORD=postgres ./bin/warthog db:create
fi

# Forward command line args to the jest command
Expand Down

0 comments on commit d6dd376

Please sign in to comment.