Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async createOneToOneRelation(currentTable: string, gqlField: InputModelFieldContext, tableName: string):
Promise {
let fieldname = `${currentTable}Id`
if (gqlField.hasDirectives && gqlField.annotations.OneToOne.field) {
fieldname = gqlField.annotations.OneToOne.field;
}
if (!gqlField.isArray) {
// tslint:disable-next-line: no-parameter-reassignment
tableName = gqlField.type.toLowerCase();
const hasColumn = await this.dbConnection.schema.hasColumn(
tableName,
fieldname,
);
if (hasColumn) {
logger.info('skipping relation creation');
} else {
await this.dbConnection.schema.alterTable(
tableName,
(table: Knex.TableBuilder) => {
table
.integer(fieldname)
.unique()
.unsigned();
table
.foreign(fieldname)
.references('id')
.inTable(currentTable);
},
);
}
} else {
public async createManyToManyRelation(currentTable: string, gqlField: InputModelFieldContext):
Promise {
const newTable = `${currentTable}_${gqlField.type.toLowerCase()}`;
// tslint:disable-next-line: await-promise
const hasTable = await this.dbConnection.schema.hasTable(newTable);
if (gqlField.isArray) {
if (hasTable) {
logger.info('skipping rela tion creation');
} else {
const tableOne = gqlField.type.toLowerCase();
const tableTwo = currentTable;
const fieldOne = `${tableOne}Id`;
const fieldTwo = `${currentTable}Id`;
await this.dbConnection.schema.createTable(
newTable,
(table: Knex.TableBuilder) => {
table.increments();
table.integer(fieldOne).unsigned();
table
.foreign(fieldOne)
.references('id')
.inTable(tableOne);
table.integer(fieldTwo).unsigned();
table
public async createOneToManyRelation(currentTable: string, gqlField: InputModelFieldContext, tableName: string):
Promise {
let fieldname = `${currentTable}Id`
if (gqlField.hasDirectives && gqlField.annotations.OneToMany.field) {
fieldname = gqlField.annotations.OneToMany.field;
}
if (gqlField.isArray) {
// tslint:disable-next-line: no-parameter-reassignment
tableName = gqlField.type.toLowerCase();
// tslint:disable-next-line: await-promise
const hasColumn = await this.dbConnection.schema.hasColumn(
tableName,
fieldname,
);
if (hasColumn) {
logger.info('skipping relation creation');
} else {
await this.dbConnection.schema.alterTable(
tableName,
(table: Knex.TableBuilder) => {
table.integer(fieldname).unsigned();
table
.foreign(fieldname)
.references('id')
.inTable(currentTable);
},
);
}
} else {
throw new Error(
'Incorrect syntax declaration. Declaration should be an array.',
);
public async updateDatabaseResources(
context: DatabaseContextProvider,
types: InputModelTypeContext[],
changes: Change[]
): Promise {
if (changes.length > 0) {
logger.info(`Updating database schema`)
for (const change of changes) {
const parts = change.path.split('.');
const changedType = {
name: parts[0],
field: parts[1],
}
const gqlType = types.find((t: InputModelTypeContext) => t.name === changedType.name);
const tableName = context.getFieldName(gqlType);
if (change.type === ChangeType.FieldAdded) {
await this.addField(tableName, changedType.field, gqlType);
public async createDatabaseRelations(context: DatabaseContextProvider, types: InputModelTypeContext[]):
Promise {
logger.info("Creating relations")
for (const gqlType of types) {
const tableName = context.getFieldName(gqlType);
const currentTable = tableName;
for (const gqlField of gqlType.fields) {
if (gqlField.isType) {
if (Relation.manyToMany in gqlField.annotations) {
await this.createManyToManyRelation(currentTable, gqlField);
} else if (
Relation.oneToMany in gqlField.annotations ||
gqlField.isArray
) {
await this.createOneToManyRelation(
currentTable,
gqlField,
tableName,
);
private async addTable(tableName: string, t: InputModelTypeContext) {
const hasTable = await this.dbConnection.schema.hasTable(tableName);
if (hasTable) {
logger.info(`Table already exists! Cannot add table ${tableName}`);
} else {
logger.info(`Creating table ${tableName}`);
await this.dbConnection.schema.createTable(
tableName,
(table: Knex.TableBuilder) => {
table.increments();
for (const field of t.fields) {
const method = this.primitiveTypesMapping[field.type];
if (method) {
table[method](field.name);
}
}
table.timestamps();
},
);
}
}
private async addField(tableName: string, field: string, t: InputModelTypeContext) {
const hasTable = await this.dbConnection.schema.hasTable(tableName);
const gqlField = t.fields.find((f: InputModelFieldContext) => f.name === field);
if (!hasTable) {
logger.info(`Table does not exist! Cannot add field to table ${tableName}`);
} else {
logger.info(`Adding field '${gqlField.name}' to table '${tableName}'`);
await this.dbConnection.schema.alterTable(
tableName,
(table: Knex.TableBuilder) => {
const method = this.primitiveTypesMapping[gqlField.type];
if (method) {
table[method](gqlField.name);
}
}
);
}
}
private async addTable(tableName: string, t: InputModelTypeContext) {
const hasTable = await this.dbConnection.schema.hasTable(tableName);
if (hasTable) {
logger.info(`Table already exists! Cannot add table ${tableName}`);
} else {
logger.info(`Creating table ${tableName}`);
await this.dbConnection.schema.createTable(
tableName,
(table: Knex.TableBuilder) => {
table.increments();
for (const field of t.fields) {
const method = this.primitiveTypesMapping[field.type];
if (method) {
table[method](field.name);
}
}
table.timestamps();
},
);