mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 10:23:34 +00:00
feat: move the document id schema insertion into strapi/strapi
This commit is contained in:
parent
5f48ce84cc
commit
91d6b8ca44
@ -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",
|
||||
|
||||
@ -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 ?? {},
|
||||
|
||||
@ -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';
|
||||
}
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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]!),
|
||||
|
||||
@ -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"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user