Add primary key logic

This commit is contained in:
Pierre Burgy 2017-05-04 19:05:41 +02:00
parent 80ea276725
commit 23bc7e9f0c
7 changed files with 55 additions and 21 deletions

View File

@ -117,6 +117,11 @@ module.exports = function(strapi) {
_.forEach(models, (definition, model) => {
globalName = _.upperFirst(_.camelCase(definition.globalId));
// Set the default values to model settings.
_.defaults(definition, {
primaryKey: 'id'
});
// Make sure the model has a table name.
// If not, use the model name.
if (_.isEmpty(definition.tableName)) {

View File

@ -80,6 +80,11 @@ module.exports = function (strapi) {
try {
let collection = strapi.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)];
// Set the default values to model settings.
_.defaults(definition, {
primaryKey: '_id'
});
// Initialize lifecycle callbacks.
const preLifecycle = {
validate: 'beforeCreate',

View File

@ -12,11 +12,12 @@ module.exports = {
find: async(ctx) => {
const model = ctx.params.model;
const primaryKey = strapi.models[model].primaryKey;
const {
limit = 10,
skip = 0,
sort = '_id'
sort = primaryKey
} = ctx.request.query;
const entries = await User
@ -41,26 +42,36 @@ module.exports = {
findOne: async(ctx) => {
const model = ctx.params.model;
const _id = ctx.params.id;
const primaryKey = strapi.models[model].primaryKey;
const params = {};
params[primaryKey] = ctx.params.id;
const entries = await User
.findOne({
_id
});
const entry = await User
.findOne(params);
ctx.body = entries;
ctx.body = entry;
},
update: async(ctx) => {
const model = ctx.params.model;
const primaryKey = strapi.models[model].primaryKey;
const params = {};
params[primaryKey] = ctx.params.id;
const entryUpdated = await User
.update({_id: ctx.request.params.id}, ctx.request.body);
.update(params, ctx.request.body);
ctx.body = entryUpdated;
},
delete: async(ctx) => {
const model = ctx.params.model;
const primaryKey = strapi.models[model].primaryKey;
const params = {};
params[primaryKey] = ctx.params.id;
const entryDeleted = await User
.remove({_id: ctx.request.params.id});
.remove(params);
ctx.body = entryDeleted;
}

View File

@ -14,7 +14,7 @@ import styles from './styles.scss';
class Table extends React.Component { // eslint-disable-line react/prefer-stateless-function
render() {
const tableRows = this.props.records.map((record, key) => {
const destination = this.props.route.path.replace(':slug', this.props.routeParams.slug) + '/' + record.id;
const destination = `${this.props.route.path.replace(':slug', this.props.routeParams.slug)}/${record[this.props.primaryKey]}`;
return (
<TableRow
@ -23,6 +23,7 @@ class Table extends React.Component { // eslint-disable-line react/prefer-statel
headers={this.props.headers}
record={record}
history={this.props.history}
primaryKey={this.props.primaryKey}
/>
);
});
@ -54,6 +55,7 @@ Table.propTypes = {
changeSort: React.PropTypes.func,
sort: React.PropTypes.string,
history: React.PropTypes.object,
primaryKey: React.PropTypes.string,
};
export default Table;

View File

@ -49,7 +49,7 @@ class TableRow extends React.Component { // eslint-disable-line react/prefer-sta
let content = this.getDisplayedValue(header.type, this.props.record[header.name]);
// Display a link if the current column is the `id` column
if (header.name === 'id') {
if (header.name === this.props.primaryKey) {
content = (
<Link to={this.props.destination} className={styles.idLink}>
{this.getDisplayedValue(header.type, this.props.record[header.name])}
@ -89,6 +89,7 @@ TableRow.propTypes = {
record: React.PropTypes.object,
destination: React.PropTypes.string,
history: React.PropTypes.object,
primaryKey: React.PropTypes.string,
};
export default TableRow;

View File

@ -8,7 +8,10 @@
import React from 'react';
import { createStructuredSelector } from 'reselect';
import { loadModels } from './actions';
import { makeSelectModels } from './selectors';
import {
makeSelectModels,
makeSelectLoading,
} from './selectors';
import { connect } from 'react-redux';
import '../../styles/main.scss';
@ -19,16 +22,20 @@ class App extends React.Component { // eslint-disable-line react/prefer-stateles
}
render() {
// Assign plugin component to children
const childrenWithProps = React.Children.map(this.props.children,
(child) => React.cloneElement(child, {
exposedComponents: this.props.exposedComponents
})
);
let content = <div></div>;
if (this.props.models) {
// Assign plugin component to children
content = React.Children.map(this.props.children,
(child) => React.cloneElement(child, {
exposedComponents: this.props.exposedComponents
})
);
}
return (
<div className='content-manager'>
{React.Children.toArray(childrenWithProps)}
{React.Children.toArray(content)}
</div>
);
}
@ -52,6 +59,7 @@ export function mapDispatchToProps(dispatch) {
const mapStateToProps = createStructuredSelector({
models: makeSelectModels(),
loading: makeSelectLoading(),
});
// Wrap the component to inject dispatch and state into it

View File

@ -44,6 +44,7 @@ import {
export class List extends React.Component { // eslint-disable-line react/prefer-stateless-function
componentWillMount() {
this.props.setCurrentModelName(this.props.routeParams.slug.toLowerCase());
this.props.changeSort(this.props.models[this.props.routeParams.slug.toLowerCase()].primaryKey);
this.props.loadRecords();
this.props.loadCount();
}
@ -74,9 +75,9 @@ export class List extends React.Component { // eslint-disable-line react/prefer-
type: value.type,
}));
// Add `id` column
// Add the primary key column
tableHeaders.unshift({
name: 'id',
name: currentModel.primaryKey,
label: 'ID',
type: 'string',
});
@ -90,6 +91,7 @@ export class List extends React.Component { // eslint-disable-line react/prefer-
changeSort={this.props.changeSort}
sort={this.props.sort}
history={this.props.history}
primaryKey={currentModel.primaryKey || 'id'}
/>
);
}