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; return true;
} }
supportsOperator(): boolean { supportsOperator(operator?: string): boolean {
return true; return true;
} }

View File

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

View File

@ -107,15 +107,10 @@ export namespace Relation {
export type OneToOne = BaseBidirectional & { export type OneToOne = BaseBidirectional & {
relation: 'oneToOne'; relation: 'oneToOne';
useJoinTable?: boolean; useJoinTable?: boolean;
} & ( joinTable?: JoinTable;
| { joinColumn?: JoinColumn;
joinTable: JoinTable; owner?: boolean;
} };
| {
joinColumn: JoinColumn;
owner?: boolean;
}
);
export type OneToMany = BaseBidirectional & { export type OneToMany = BaseBidirectional & {
relation: 'oneToMany'; relation: 'oneToMany';
@ -124,17 +119,11 @@ export namespace Relation {
export type ManyToOne = BaseBidirectional & { export type ManyToOne = BaseBidirectional & {
relation: 'manyToOne'; relation: 'manyToOne';
useJoinTable?: boolean; useJoinTable?: boolean;
} & ( joinTable?: JoinTable;
| { joinColumn?: JoinColumn;
joinTable: JoinTable; owner?: boolean;
} };
| {
joinColumn: JoinColumn;
owner?: boolean;
}
);
export type ManyToMany = BaseBidirectional & { export type ManyToMany = BaseBidirectional & {
relation: 'manyToMany'; relation: 'manyToMany';
@ -240,7 +229,7 @@ export interface Model {
tableName: string; tableName: string;
singularName: string; singularName: string;
attributes: Record<string, Attribute>; attributes: Record<string, Attribute>;
lifecycles: Record<string, unknown>; lifecycles?: Record<Action, SubscriberFn>;
indexes: Index[]; indexes: Index[];
componentLink?: Meta; componentLink?: Meta;
columnToAttribute?: Record<string, string>; 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 { Metadata, Meta, Attribute } from '../metadata/types';
import type { Column, Schema, Table } from './types'; import type { Column, Schema, Table } from './types';
import { isRelationAttribute } from '../metadata/relations';
const createColumn = (name: string, attribute: Attribute): Column => { const createColumn = (name: string, attribute: Attribute): Column => {
const { type, args = [], ...opts } = getColumnType(attribute); const { type, args = [], ...opts } = getColumnType(attribute);
@ -15,7 +14,7 @@ const createColumn = (name: string, attribute: Attribute): Column => {
notNullable: false, notNullable: false,
unsigned: false, unsigned: false,
...opts, ...opts,
...(attribute.column || {}), ...('column' in attribute ? attribute.column ?? {} : {}),
}; };
}; };
@ -31,8 +30,8 @@ const createTable = (meta: Meta): Table => {
const attribute = meta.attributes[key]; const attribute = meta.attributes[key];
// if (types.isRelation(attribute.type)) { // if (types.isRelation(attribute.type)) {
if (isRelationAttribute(attribute)) { if (attribute.type === 'relation') {
if (attribute.morphColumn && attribute.owner) { if ('morphColumn' in attribute && attribute.morphColumn && attribute.owner) {
const { idColumn, typeColumn } = attribute.morphColumn; const { idColumn, typeColumn } = attribute.morphColumn;
table.columns.push( table.columns.push(
@ -45,7 +44,12 @@ const createTable = (meta: Meta): Table => {
); );
table.columns.push(createColumn(typeColumn.name, { type: 'string' })); 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 // NOTE: we could pass uniquness for oneToOne to avoid creating more than one to one
const { name: columnName, referencedColumn, referencedTable } = attribute.joinColumn; const { name: columnName, referencedColumn, referencedTable } = attribute.joinColumn;
@ -73,7 +77,7 @@ const createTable = (meta: Meta): Table => {
columns: [columnName], columns: [columnName],
}); });
} }
} else if (types.isScalar(attribute.type)) { } else if (types.isScalarAttribute(attribute)) {
const column = createColumn(attribute.columnName || key, attribute); const column = createColumn(attribute.columnName || key, attribute);
if (column.unique) { if (column.unique) {
@ -100,7 +104,7 @@ const createTable = (meta: Meta): Table => {
}; };
const getColumnType = (attribute: Attribute) => { const getColumnType = (attribute: Attribute) => {
if (attribute.columnType) { if ('columnType' in attribute && attribute.columnType) {
return 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 { getJoinTableName } from '../../metadata/relations';
import type { Database } from '../..'; import type { Database } from '../..';
import type { Relation } from '../../metadata/types';
type Link = { type Link = {
relation: Attribute.Relation; relation: Relation.Bidirectional & { inversedBy: string };
invRelation: Attribute.Relation; invRelation: Relation.Bidirectional & { inversedBy: string };
}; };
const getLinksWithoutMappedBy = (db: Database): Array<Link> => { const getLinksWithoutMappedBy = (db: Database): Array<Link> => {
const relationsToUpdate = {}; const relationsToUpdate: Record<string, Link> = {};
db.metadata.forEach((contentType) => { db.metadata.forEach((contentType) => {
const attributes = contentType.attributes; const attributes = contentType.attributes;
// For each relation attribute, add the joinTable name to tablesToUpdate // For each relation attribute, add the joinTable name to tablesToUpdate
Object.values(attributes).forEach((attribute) => { 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]; const invRelation = db.metadata.get(attribute.target).attributes[attribute.inversedBy];
// Both relations use inversedBy. // Both relations use inversedBy.
if (invRelation.inversedBy) { if ('inversedBy' in invRelation && invRelation.inversedBy) {
relationsToUpdate[attribute.joinTable.name] = { relationsToUpdate[attribute.joinTable.name] = {
relation: attribute, relation: attribute as Relation.Bidirectional & { inversedBy: string },
invRelation, invRelation: invRelation as Relation.Bidirectional & { inversedBy: string },
}; };
} }
} }
@ -36,12 +37,12 @@ const getLinksWithoutMappedBy = (db: Database): Array<Link> => {
return Object.values(relationsToUpdate); 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 // If the table doesn't exist, it's empty
const exists = await db.getSchemaConnection().hasTable(linkTableName); const exists = await db.getSchemaConnection().hasTable(linkTableName);
if (!exists) return true; 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; return Number(result[0].count) === 0;
}; };