chore: builds version tables
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { pgEnum } from 'drizzle-orm/pg-core';
|
||||
import toSnakeCase from 'to-snake-case';
|
||||
import { SanitizedCollectionConfig } from 'payload/dist/collections/config/types';
|
||||
import type { Init } from 'payload/dist/database/types';
|
||||
import { buildVersionGlobalFields } from 'payload/dist/versions/buildGlobalFields';
|
||||
import { buildVersionCollectionFields } from 'payload/dist/versions/buildCollectionFields';
|
||||
import { buildTable } from './schema/build';
|
||||
import type { PostgresAdapter } from './types';
|
||||
|
||||
@@ -16,22 +19,52 @@ export const init: Init = async function init(this: PostgresAdapter) {
|
||||
}
|
||||
|
||||
this.payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {
|
||||
const tableName = toSnakeCase(collection.slug);
|
||||
|
||||
buildTable({
|
||||
adapter: this,
|
||||
buildRelationships: true,
|
||||
fields: collection.fields,
|
||||
tableName: collection.slug,
|
||||
tableName,
|
||||
timestamps: collection.timestamps,
|
||||
});
|
||||
|
||||
if (collection.versions) {
|
||||
const versionsTableName = `_${tableName}_versions`;
|
||||
const versionFields = buildVersionCollectionFields(collection);
|
||||
|
||||
buildTable({
|
||||
adapter: this,
|
||||
buildRelationships: true,
|
||||
fields: versionFields,
|
||||
tableName: versionsTableName,
|
||||
timestamps: true,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
this.payload.config.globals.forEach((global) => {
|
||||
const tableName = toSnakeCase(global.slug);
|
||||
|
||||
buildTable({
|
||||
adapter: this,
|
||||
buildRelationships: true,
|
||||
fields: global.fields,
|
||||
tableName: global.slug,
|
||||
tableName,
|
||||
timestamps: false,
|
||||
});
|
||||
|
||||
if (global.versions) {
|
||||
const versionsTableName = `_${tableName}_versions`;
|
||||
const versionFields = buildVersionGlobalFields(global);
|
||||
|
||||
buildTable({
|
||||
adapter: this,
|
||||
buildRelationships: true,
|
||||
fields: versionFields,
|
||||
tableName: versionsTableName,
|
||||
timestamps: true,
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -43,7 +43,6 @@ export const buildTable = ({
|
||||
tableName,
|
||||
timestamps,
|
||||
}: Args): Result => {
|
||||
const formattedTableName = toSnakeCase(tableName);
|
||||
const columns: Record<string, AnyPgColumnBuilder> = baseColumns;
|
||||
const indexes: Record<string, (cols: GenericColumns) => IndexBuilder> = {};
|
||||
|
||||
@@ -84,8 +83,8 @@ export const buildTable = ({
|
||||
indexes,
|
||||
localesColumns,
|
||||
localesIndexes,
|
||||
newTableName: formattedTableName,
|
||||
parentTableName: formattedTableName,
|
||||
newTableName: tableName,
|
||||
parentTableName: tableName,
|
||||
relationships,
|
||||
}));
|
||||
|
||||
@@ -94,7 +93,7 @@ export const buildTable = ({
|
||||
columns.updatedAt = timestamp('updated_at').defaultNow().notNull();
|
||||
}
|
||||
|
||||
const table = pgTable(formattedTableName, columns, (cols) => {
|
||||
const table = pgTable(tableName, columns, (cols) => {
|
||||
const extraConfig = Object.entries(baseExtraConfig).reduce((config, [key, func]) => {
|
||||
config[key] = func(cols);
|
||||
return config;
|
||||
@@ -106,10 +105,10 @@ export const buildTable = ({
|
||||
}, extraConfig);
|
||||
});
|
||||
|
||||
adapter.tables[formattedTableName] = table;
|
||||
adapter.tables[tableName] = table;
|
||||
|
||||
if (hasLocalizedField) {
|
||||
const localeTableName = `${formattedTableName}_locales`;
|
||||
const localeTableName = `${tableName}_locales`;
|
||||
localesColumns.id = serial('id').primaryKey();
|
||||
localesColumns._locale = adapter.enums._locales('_locale').notNull();
|
||||
localesColumns._parentID = parentIDColumnMap[idColType]('_parent_id').references(() => table.id, { onDelete: 'cascade' }).notNull();
|
||||
@@ -158,7 +157,7 @@ export const buildTable = ({
|
||||
relationshipColumns[`${relationTo}ID`] = parentIDColumnMap[colType](`${formattedRelationTo}_id`).references(() => adapter.tables[formattedRelationTo].id);
|
||||
});
|
||||
|
||||
const relationshipsTableName = `${formattedTableName}_relationships`;
|
||||
const relationshipsTableName = `${tableName}_relationships`;
|
||||
|
||||
relationshipsTable = pgTable(relationshipsTableName, relationshipColumns, (cols) => {
|
||||
const result: Record<string, unknown> = {};
|
||||
@@ -220,7 +219,7 @@ export const buildTable = ({
|
||||
return result;
|
||||
});
|
||||
|
||||
adapter.relations[`relations_${formattedTableName}`] = tableRelations;
|
||||
adapter.relations[`relations_${tableName}`] = tableRelations;
|
||||
|
||||
return { arrayBlockRelations };
|
||||
};
|
||||
|
||||
@@ -73,7 +73,7 @@ export const traverseFields = ({
|
||||
targetIndexes = localesIndexes;
|
||||
}
|
||||
|
||||
if (field.unique || field.index) {
|
||||
if ((field.unique || field.index) && !['array', 'blocks', 'relationship', 'upload', 'group'].includes(field.type)) {
|
||||
targetIndexes[`${field.name}Idx`] = createIndex({ columnName, name: field.name, unique: field.unique });
|
||||
}
|
||||
}
|
||||
|
||||
15
test/postgres/collections/Pages.ts
Normal file
15
test/postgres/collections/Pages.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { CollectionConfig } from '../../../src/collections/config/types';
|
||||
|
||||
export const Pages: CollectionConfig = {
|
||||
slug: 'pages',
|
||||
versions: {
|
||||
drafts: true,
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'slug',
|
||||
type: 'text',
|
||||
localized: true,
|
||||
},
|
||||
],
|
||||
};
|
||||
11
test/postgres/collections/People.ts
Normal file
11
test/postgres/collections/People.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { CollectionConfig } from '../../../src/collections/config/types';
|
||||
|
||||
export const People: CollectionConfig = {
|
||||
slug: 'people',
|
||||
fields: [
|
||||
{
|
||||
name: 'fullName',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -2,6 +2,8 @@ import { buildConfigWithDefaults } from '../buildConfigWithDefaults';
|
||||
import { LocalizedArrays } from './collections/LocalizedArrays';
|
||||
import { LocalizedBlocks } from './collections/LocalizedBlocks';
|
||||
import { LocalizedGroups } from './collections/LocalizedGroups';
|
||||
import { Pages } from './collections/Pages';
|
||||
import { People } from './collections/People';
|
||||
import { Posts } from './collections/Posts';
|
||||
import { MainMenu } from './globals/MainMenu';
|
||||
|
||||
@@ -10,26 +12,9 @@ const config = buildConfigWithDefaults({
|
||||
LocalizedArrays,
|
||||
LocalizedBlocks,
|
||||
LocalizedGroups,
|
||||
Pages,
|
||||
People,
|
||||
Posts,
|
||||
{
|
||||
slug: 'pages',
|
||||
fields: [
|
||||
{
|
||||
name: 'slug',
|
||||
type: 'text',
|
||||
localized: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
slug: 'people',
|
||||
fields: [
|
||||
{
|
||||
name: 'fullName',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
globals: [
|
||||
MainMenu,
|
||||
|
||||
Reference in New Issue
Block a user