Skip to content

Commit

Permalink
feat: Add project and project relations entities (#8421)
Browse files Browse the repository at this point in the history
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
  • Loading branch information
valya and netroy authored Jan 24, 2024
1 parent be99072 commit c85bdc4
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/cli/src/databases/dsl/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ export class Column {
options.type = isPostgres ? 'timestamptz' : 'datetime';
} else if (type === 'json' && isSqlite) {
options.type = 'text';
} else if (type === 'uuid' && isMysql) {
} else if (type === 'uuid') {
// mysql does not support uuid type
options.type = 'varchar(36)';
if (isMysql) options.type = 'varchar(36)';
// we haven't been defining length on "uuid" varchar on sqlite
if (isSqlite) options.type = 'varchar';
}

if ((type === 'varchar' || type === 'timestamp') && length !== 'auto') {
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/src/databases/entities/Project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Column, Entity, OneToMany } from 'typeorm';
import { WithTimestampsAndStringId } from './AbstractEntity';
import type { ProjectRelation } from './ProjectRelation';

export type ProjectType = 'personal' | 'team' | 'public';

@Entity()
export class Project extends WithTimestampsAndStringId {
@Column({ length: 255 })
name: string;

@Column({ length: 36 })
type: ProjectType;

@OneToMany('ProjectRelation', 'project')
projectRelations: ProjectRelation[];
}
28 changes: 28 additions & 0 deletions packages/cli/src/databases/entities/ProjectRelation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm';
import { User } from './User';
import { WithTimestamps } from './AbstractEntity';
import { Project } from './Project';

export type ProjectRole =
| 'project:admin'
| 'project:editor'
| 'project:workflowEditor'
| 'project:viewer';

@Entity()
export class ProjectRelation extends WithTimestamps {
@Column()
role: ProjectRole;

@ManyToOne('User', 'projectRelations')
user: User;

@PrimaryColumn('uuid')
userId: string;

@ManyToOne('Project', 'projectRelations')
project: Project;

@PrimaryColumn()
projectId: string;
}
4 changes: 4 additions & 0 deletions packages/cli/src/databases/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { IPersonalizationSurveyAnswers } from '@/Interfaces';
import type { AuthIdentity } from './AuthIdentity';
import { ownerPermissions, memberPermissions, adminPermissions } from '@/permissions/roles';
import { hasScope, type ScopeOptions, type Scope } from '@n8n/permissions';
import type { ProjectRelation } from './ProjectRelation';

export type GlobalRole = 'global:owner' | 'global:admin' | 'global:member';
export type AssignableRole = Exclude<GlobalRole, 'global:owner'>;
Expand Down Expand Up @@ -85,6 +86,9 @@ export class User extends WithTimestamps implements IUser {
@OneToMany('SharedCredentials', 'user')
sharedCredentials: SharedCredentials[];

@OneToMany('ProjectRelation', 'user')
projectRelations: ProjectRelation[];

@Column({ type: Boolean, default: false })
disabled: boolean;

Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/databases/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { WorkflowStatistics } from './WorkflowStatistics';
import { ExecutionMetadata } from './ExecutionMetadata';
import { ExecutionData } from './ExecutionData';
import { WorkflowHistory } from './WorkflowHistory';
import { Project } from './Project';
import { ProjectRelation } from './ProjectRelation';

export const entities = {
AuthIdentity,
Expand All @@ -41,4 +43,6 @@ export const entities = {
ExecutionMetadata,
ExecutionData,
WorkflowHistory,
Project,
ProjectRelation,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { MigrationContext, ReversibleMigration } from '@db/types';

const tableName = 'project';
const relationTableName = 'project_relation';

export class CreateProject1705928727784 implements ReversibleMigration {
async up({ schemaBuilder: { createTable, column } }: MigrationContext) {
await createTable(tableName).withColumns(
column('id').varchar(36).primary.notNull,
column('name').varchar(255),
column('type').varchar(36),
).withTimestamps;

await createTable(relationTableName)
.withColumns(
column('projectId').varchar(36).primary.notNull,
column('userId').uuid.primary.notNull,
column('role').varchar().notNull,
)
.withIndexOn('projectId')
.withIndexOn('userId')
.withForeignKey('projectId', {
tableName,
columnName: 'id',
onDelete: 'CASCADE',
})
.withForeignKey('userId', {
tableName: 'user',
columnName: 'id',
onDelete: 'CASCADE',
}).withTimestamps;
}

async down({ schemaBuilder: { dropTable } }: MigrationContext) {
await dropTable(relationTableName);
await dropTable(tableName);
}
}
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/mysqldb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { ExecutionSoftDelete1693491613982 } from '../common/1693491613982-Execut
import { AddWorkflowMetadata1695128658538 } from '../common/1695128658538-AddWorkflowMetadata';
import { ModifyWorkflowHistoryNodesAndConnections1695829275184 } from '../common/1695829275184-ModifyWorkflowHistoryNodesAndConnections';
import { AddGlobalAdminRole1700571993961 } from '../common/1700571993961-AddGlobalAdminRole';
import { CreateProject1705928727784 } from '../common/1705928727784-CreateProject';
import { DropRoleMapping1705429061930 } from '../common/1705429061930-DropRoleMapping';

export const mysqlMigrations: Migration[] = [
Expand Down Expand Up @@ -106,5 +107,6 @@ export const mysqlMigrations: Migration[] = [
AddWorkflowMetadata1695128658538,
ModifyWorkflowHistoryNodesAndConnections1695829275184,
AddGlobalAdminRole1700571993961,
CreateProject1705928727784,
DropRoleMapping1705429061930,
];
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/postgresdb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { AddWorkflowMetadata1695128658538 } from '../common/1695128658538-AddWor
import { MigrateToTimestampTz1694091729095 } from './1694091729095-MigrateToTimestampTz';
import { ModifyWorkflowHistoryNodesAndConnections1695829275184 } from '../common/1695829275184-ModifyWorkflowHistoryNodesAndConnections';
import { AddGlobalAdminRole1700571993961 } from '../common/1700571993961-AddGlobalAdminRole';
import { CreateProject1705928727784 } from '../common/1705928727784-CreateProject';
import { DropRoleMapping1705429061930 } from '../common/1705429061930-DropRoleMapping';

export const postgresMigrations: Migration[] = [
Expand Down Expand Up @@ -104,5 +105,6 @@ export const postgresMigrations: Migration[] = [
MigrateToTimestampTz1694091729095,
ModifyWorkflowHistoryNodesAndConnections1695829275184,
AddGlobalAdminRole1700571993961,
CreateProject1705928727784,
DropRoleMapping1705429061930,
];
2 changes: 2 additions & 0 deletions packages/cli/src/databases/migrations/sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { ExecutionSoftDelete1693491613982 } from './1693491613982-ExecutionSoftD
import { AddWorkflowMetadata1695128658538 } from '../common/1695128658538-AddWorkflowMetadata';
import { ModifyWorkflowHistoryNodesAndConnections1695829275184 } from '../common/1695829275184-ModifyWorkflowHistoryNodesAndConnections';
import { AddGlobalAdminRole1700571993961 } from '../common/1700571993961-AddGlobalAdminRole';
import { CreateProject1705928727784 } from '../common/1705928727784-CreateProject';
import { DropRoleMapping1705429061930 } from './1705429061930-DropRoleMapping';

const sqliteMigrations: Migration[] = [
Expand Down Expand Up @@ -100,6 +101,7 @@ const sqliteMigrations: Migration[] = [
AddWorkflowMetadata1695128658538,
ModifyWorkflowHistoryNodesAndConnections1695829275184,
AddGlobalAdminRole1700571993961,
CreateProject1705928727784,
DropRoleMapping1705429061930,
];

Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/databases/repositories/project.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Service } from 'typedi';
import { DataSource, Repository } from 'typeorm';
import { Project } from '../entities/Project';

@Service()
export class ProjectRepository extends Repository<Project> {
constructor(dataSource: DataSource) {
super(Project, dataSource.manager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Service } from 'typedi';
import { DataSource, Repository } from 'typeorm';
import { ProjectRelation } from '../entities/ProjectRelation';

@Service()
export class ProjectRelationRepository extends Repository<ProjectRelation> {
constructor(dataSource: DataSource) {
super(ProjectRelation, dataSource.manager);
}
}

0 comments on commit c85bdc4

Please sign in to comment.