Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use db model instead of content type #19563

Merged
merged 6 commits into from Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/content-manager/package.json
Expand Up @@ -56,6 +56,7 @@
"qs": "6.11.1"
},
"devDependencies": {
"@strapi/database": "workspace:*",
"@strapi/pack-up": "workspace:*",
"@types/jest": "29.5.2",
"@types/lodash": "^4.14.191",
Expand Down

This file was deleted.

This file was deleted.

14 changes: 10 additions & 4 deletions packages/core/content-manager/server/src/history/index.ts
@@ -1,9 +1,9 @@
import type { Plugin } from '@strapi/types';
import { controllers } from './controllers';
import { services } from './services';
import { contentTypes } from './content-types';
import { routes } from './routes';
import { getService } from './utils';
import { historyVersion } from './models/history-version';

/**
* Check once if the feature is enabled (both license info & feature flag) before loading it,
Expand All @@ -13,22 +13,28 @@ const getFeature = (): Partial<Plugin.LoadedPlugin> => {
// TODO: add license check here when it's ready on the license registry
if (strapi.features.future.isEnabled('history')) {
return {
register({ strapi }) {
strapi.get('models').add(historyVersion);
},
bootstrap({ strapi }) {
// Start recording history and saving history versions
getService(strapi, 'history').init();
},
controllers,
services,
contentTypes,
routes,
};
}

/**
* Keep returning contentTypes to avoid losing the data if the feature is disabled,
* Keep registering the model to avoid losing the data if the feature is disabled,
* or if the license expires.
*/
return { contentTypes };
return {
register({ strapi }) {
strapi.get('models').add(historyVersion);
},
};
};

export default getFeature();
@@ -0,0 +1,48 @@
import type { Model } from '@strapi/database';
import { HISTORY_VERSION_UID } from '../constants';

const historyVersion: Model = {
uid: HISTORY_VERSION_UID,
tableName: 'strapi_history_versions',
singularName: 'history-version',
attributes: {
id: {
type: 'increments',
},
contentType: {
type: 'string',
column: { notNullable: true },
},
relatedDocumentId: {
type: 'string',
column: { notNullable: true },
},
locale: {
type: 'string',
},
status: {
type: 'enumeration',
enum: ['draft', 'published', 'modified'],
},
data: {
type: 'json',
},
schema: {
type: 'json',
},
createdAt: {
type: 'datetime',
default: () => new Date(),
},
// FIXME: joinTable should be optional
// @ts-expect-error database model is not yet updated to support useJoinTable
createdBy: {
type: 'relation',
relation: 'oneToOne',
target: 'admin::user',
useJoinTable: false,
},
Comment on lines +39 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary to use a join table for the created by field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be? Here it's false, and for normal content types it's false as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't select the right section 😓 I think the attribute is correctly set up and will not use a join table, but there is a message about jointable being optional and didn't get why 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. The comment is because TS wanted us to provide a joinTable property (not sure about the name exactly). But it shouldn't be required when useJoinTable is false. The type isn't smart enough for that yet, that's why we expect a TS error here

},
};

export { historyVersion };
2 changes: 0 additions & 2 deletions packages/core/content-manager/server/src/index.ts
Expand Up @@ -5,7 +5,6 @@ import routes from './routes';
import policies from './policies';
import controllers from './controllers';
import services from './services';
import history from './history';

export default () => {
return {
Expand All @@ -16,6 +15,5 @@ export default () => {
routes,
policies,
services,
contentTypes: history.contentTypes,
};
};
2 changes: 2 additions & 0 deletions packages/core/database/src/types/index.ts
Expand Up @@ -6,6 +6,7 @@ export type ID = string | number;
export interface ColumnInfo {
unsigned?: boolean;
defaultTo?: unknown;
notNullable?: boolean;
}

export type Attribute = ScalarAttribute | RelationalAttribute;
Expand Down Expand Up @@ -34,6 +35,7 @@ export interface BaseAttribute {
args: unknown[];
};
searchable?: boolean;
enum?: string[];
}

export interface ScalarAttribute extends BaseAttribute {
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Expand Up @@ -9478,7 +9478,7 @@ __metadata:
languageName: unknown
linkType: soft

"@strapi/database@npm:4.20.1, @strapi/database@workspace:packages/core/database":
"@strapi/database@npm:4.20.1, @strapi/database@workspace:*, @strapi/database@workspace:packages/core/database":
version: 0.0.0-use.local
resolution: "@strapi/database@workspace:packages/core/database"
dependencies:
Expand Down Expand Up @@ -9778,6 +9778,7 @@ __metadata:
resolution: "@strapi/plugin-content-manager@workspace:packages/core/content-manager"
dependencies:
"@sindresorhus/slugify": "npm:1.1.0"
"@strapi/database": "workspace:*"
"@strapi/pack-up": "workspace:*"
"@strapi/types": "npm:4.20.1"
"@strapi/utils": "npm:4.20.1"
Expand Down