1st passe except query

This commit is contained in:
Alexandre Bodin 2023-09-14 09:22:47 +02:00
parent c56f1863ab
commit bc8f81eee4
5 changed files with 35 additions and 41 deletions

View File

@ -45,7 +45,7 @@ export default class Dialect {
return true;
}
supportsOperator(): boolean {
supportsOperator(operator?: string): boolean {
return true;
}

View File

@ -30,7 +30,7 @@ export const createMetadata = (models: Model[] = []): Metadata => {
},
...model.attributes,
},
lifecycles: model.lifecycles || {},
lifecycles: model.lifecycles ?? ({} as Meta['lifecycles']),
indexes: model.indexes || [],
});
}

View File

@ -107,15 +107,10 @@ export namespace Relation {
export type OneToOne = BaseBidirectional & {
relation: 'oneToOne';
useJoinTable?: boolean;
} & (
| {
joinTable: JoinTable;
}
| {
joinColumn: JoinColumn;
owner?: boolean;
}
);
joinTable?: JoinTable;
joinColumn?: JoinColumn;
owner?: boolean;
};
export type OneToMany = BaseBidirectional & {
relation: 'oneToMany';
@ -124,17 +119,11 @@ export namespace Relation {
export type ManyToOne = BaseBidirectional & {
relation: 'manyToOne';
useJoinTable?: boolean;
} & (
| {
joinTable: JoinTable;
}
| {
joinColumn: JoinColumn;
owner?: boolean;
}
);
joinTable?: JoinTable;
joinColumn?: JoinColumn;
owner?: boolean;
};
export type ManyToMany = BaseBidirectional & {
relation: 'manyToMany';
@ -240,7 +229,7 @@ export interface Model {
tableName: string;
singularName: string;
attributes: Record<string, Attribute>;
lifecycles: Record<string, unknown>;
lifecycles?: Record<Action, SubscriberFn>;
indexes: Index[];
componentLink?: Meta;
columnToAttribute?: Record<string, string>;

View File

@ -2,7 +2,6 @@ import * as types from '../types';
import type { Metadata, Meta, Attribute } from '../metadata/types';
import type { Column, Schema, Table } from './types';
import { isRelationAttribute } from '../metadata/relations';
const createColumn = (name: string, attribute: Attribute): Column => {
const { type, args = [], ...opts } = getColumnType(attribute);
@ -15,7 +14,7 @@ const createColumn = (name: string, attribute: Attribute): Column => {
notNullable: false,
unsigned: false,
...opts,
...(attribute.column || {}),
...('column' in attribute ? attribute.column ?? {} : {}),
};
};
@ -31,8 +30,8 @@ const createTable = (meta: Meta): Table => {
const attribute = meta.attributes[key];
// if (types.isRelation(attribute.type)) {
if (isRelationAttribute(attribute)) {
if (attribute.morphColumn && attribute.owner) {
if (attribute.type === 'relation') {
if ('morphColumn' in attribute && attribute.morphColumn && attribute.owner) {
const { idColumn, typeColumn } = attribute.morphColumn;
table.columns.push(
@ -45,7 +44,12 @@ const createTable = (meta: Meta): Table => {
);
table.columns.push(createColumn(typeColumn.name, { type: 'string' }));
} else if (attribute.joinColumn && attribute.owner && attribute.joinColumn.referencedTable) {
} else if (
'joinColumn' in attribute &&
attribute.joinColumn &&
attribute.owner &&
attribute.joinColumn.referencedTable
) {
// NOTE: we could pass uniquness for oneToOne to avoid creating more than one to one
const { name: columnName, referencedColumn, referencedTable } = attribute.joinColumn;
@ -73,7 +77,7 @@ const createTable = (meta: Meta): Table => {
columns: [columnName],
});
}
} else if (types.isScalar(attribute.type)) {
} else if (types.isScalarAttribute(attribute)) {
const column = createColumn(attribute.columnName || key, attribute);
if (column.unique) {
@ -100,7 +104,7 @@ const createTable = (meta: Meta): Table => {
};
const getColumnType = (attribute: Attribute) => {
if (attribute.columnType) {
if ('columnType' in attribute && attribute.columnType) {
return attribute.columnType;
}

View File

@ -1,32 +1,33 @@
import { Attribute } from '@strapi/typings';
import * as types from '../../types';
import { getJoinTableName } from '../../metadata/relations';
import type { Database } from '../..';
import type { Relation } from '../../metadata/types';
type Link = {
relation: Attribute.Relation;
invRelation: Attribute.Relation;
relation: Relation.Bidirectional & { inversedBy: string };
invRelation: Relation.Bidirectional & { inversedBy: string };
};
const getLinksWithoutMappedBy = (db: Database): Array<Link> => {
const relationsToUpdate = {};
const relationsToUpdate: Record<string, Link> = {};
db.metadata.forEach((contentType) => {
const attributes = contentType.attributes;
// For each relation attribute, add the joinTable name to tablesToUpdate
Object.values(attributes).forEach((attribute) => {
if (!types.isRelation(attribute.type)) return;
if (attribute.type !== 'relation') {
return;
}
if (attribute.inversedBy) {
if ('inversedBy' in attribute && attribute.inversedBy) {
const invRelation = db.metadata.get(attribute.target).attributes[attribute.inversedBy];
// Both relations use inversedBy.
if (invRelation.inversedBy) {
if ('inversedBy' in invRelation && invRelation.inversedBy) {
relationsToUpdate[attribute.joinTable.name] = {
relation: attribute,
invRelation,
relation: attribute as Relation.Bidirectional & { inversedBy: string },
invRelation: invRelation as Relation.Bidirectional & { inversedBy: string },
};
}
}
@ -36,12 +37,12 @@ const getLinksWithoutMappedBy = (db: Database): Array<Link> => {
return Object.values(relationsToUpdate);
};
const isLinkTableEmpty = async (db, linkTableName) => {
const isLinkTableEmpty = async (db: Database, linkTableName: string) => {
// If the table doesn't exist, it's empty
const exists = await db.getSchemaConnection().hasTable(linkTableName);
if (!exists) return true;
const result = await db.getConnection().count('* as count').from(linkTableName);
const result = await db.getConnection().from(linkTableName).count('* as count');
return Number(result[0].count) === 0;
};