feat: move the document id schema insertion into strapi/strapi

This commit is contained in:
Marc-Roig 2023-11-06 10:05:06 +01:00
parent 5f48ce84cc
commit 91d6b8ca44
No known key found for this signature in database
GPG Key ID: FB4E2C43A0BEE249
6 changed files with 16 additions and 14 deletions

View File

@ -40,7 +40,6 @@
"watch": "pack-up watch"
},
"dependencies": {
"@paralleldrive/cuid2": "2.2.2",
"@strapi/utils": "4.14.5",
"date-fns": "2.30.0",
"debug": "4.3.4",

View File

@ -1,5 +1,4 @@
import _ from 'lodash/fp';
import { createId } from '@paralleldrive/cuid2';
import * as types from '../utils/types';
import {
@ -41,18 +40,12 @@ export const createMetadata = (models: Model[] = []): Metadata => {
throw new Error('The attribute "id" is reserved and cannot be used in a model');
}
const documentIdAttribute: Meta['attributes'] =
model.modelType === 'contentType'
? { documentId: { type: 'string', default: createId } }
: {};
metadata.add({
...model,
attributes: {
id: {
type: 'increments',
},
...documentIdAttribute,
...model.attributes,
},
lifecycles: model.lifecycles ?? {},

View File

@ -223,8 +223,4 @@ export interface Model {
indexes?: Index[];
foreignKeys?: ForeignKey[];
lifecycles?: Partial<Record<Action, SubscriberFn>>;
// TODO: remove these once we add documentId outside the DB layer
options?: Record<string, unknown>;
modelName?: string;
modelType?: 'contentType' | 'component';
}

View File

@ -87,6 +87,7 @@
"dependencies": {
"@koa/cors": "3.4.3",
"@koa/router": "10.1.1",
"@paralleldrive/cuid2": "2.2.2",
"@strapi/admin": "4.14.5",
"@strapi/data-transfer": "4.14.5",
"@strapi/database": "4.14.5",

View File

@ -1,5 +1,6 @@
import { DatabaseConfig } from '@strapi/database';
import { Schema, Attribute } from '@strapi/types';
import { createId } from '@paralleldrive/cuid2';
const transformAttribute = (attribute: Attribute.Any) => {
switch (attribute.type) {
@ -18,15 +19,27 @@ const transformAttribute = (attribute: Attribute.Any) => {
};
export const transformContentTypesToModels = (
contentTypes: Pick<Schema.ContentType, 'uid' | 'modelName' | 'collectionName' | 'attributes'>[]
contentTypes: Pick<
Schema.ContentType,
'uid' | 'modelName' | 'collectionName' | 'attributes' | 'modelType'
>[]
): DatabaseConfig['models'] => {
return contentTypes.map((contentType) => {
// Add document id to content types
// @ts-expect-error - `default` function is not typed into `Attribute`
// as it is not documented
const documentIdAttribute: Record<string, Attribute.Any> =
contentType.modelType === 'contentType'
? { documentId: { type: 'string', default: createId } }
: {};
const model = {
...contentType,
// reuse new model def
singularName: contentType.modelName,
tableName: contentType.collectionName!,
attributes: {
...documentIdAttribute,
...Object.keys(contentType.attributes! || {}).reduce((attrs, attrName) => {
return Object.assign(attrs, {
[attrName]: transformAttribute(contentType.attributes[attrName]!),

View File

@ -7739,7 +7739,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@strapi/database@workspace:packages/core/database"
dependencies:
"@paralleldrive/cuid2": "npm:2.2.2"
"@strapi/pack-up": "workspace:*"
"@strapi/utils": "npm:4.14.5"
date-fns: "npm:2.30.0"
@ -8470,6 +8469,7 @@ __metadata:
dependencies:
"@koa/cors": "npm:3.4.3"
"@koa/router": "npm:10.1.1"
"@paralleldrive/cuid2": "npm:2.2.2"
"@strapi/admin": "npm:4.14.5"
"@strapi/data-transfer": "npm:4.14.5"
"@strapi/database": "npm:4.14.5"