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) => { _.forEach(models, (definition, model) => {
globalName = _.upperFirst(_.camelCase(definition.globalId)); globalName = _.upperFirst(_.camelCase(definition.globalId));
// Set the default values to model settings.
_.defaults(definition, {
primaryKey: 'id'
});
// Make sure the model has a table name. // Make sure the model has a table name.
// If not, use the model name. // If not, use the model name.
if (_.isEmpty(definition.tableName)) { if (_.isEmpty(definition.tableName)) {

View File

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

View File

@ -12,11 +12,12 @@ module.exports = {
find: async(ctx) => { find: async(ctx) => {
const model = ctx.params.model; const model = ctx.params.model;
const primaryKey = strapi.models[model].primaryKey;
const { const {
limit = 10, limit = 10,
skip = 0, skip = 0,
sort = '_id' sort = primaryKey
} = ctx.request.query; } = ctx.request.query;
const entries = await User const entries = await User
@ -41,26 +42,36 @@ module.exports = {
findOne: async(ctx) => { findOne: async(ctx) => {
const model = ctx.params.model; 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 const entry = await User
.findOne({ .findOne(params);
_id
});
ctx.body = entries; ctx.body = entry;
}, },
update: async(ctx) => { 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 const entryUpdated = await User
.update({_id: ctx.request.params.id}, ctx.request.body); .update(params, ctx.request.body);
ctx.body = entryUpdated; ctx.body = entryUpdated;
}, },
delete: async(ctx) => { 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 const entryDeleted = await User
.remove({_id: ctx.request.params.id}); .remove(params);
ctx.body = entryDeleted; 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 class Table extends React.Component { // eslint-disable-line react/prefer-stateless-function
render() { render() {
const tableRows = this.props.records.map((record, key) => { 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 ( return (
<TableRow <TableRow
@ -23,6 +23,7 @@ class Table extends React.Component { // eslint-disable-line react/prefer-statel
headers={this.props.headers} headers={this.props.headers}
record={record} record={record}
history={this.props.history} history={this.props.history}
primaryKey={this.props.primaryKey}
/> />
); );
}); });
@ -54,6 +55,7 @@ Table.propTypes = {
changeSort: React.PropTypes.func, changeSort: React.PropTypes.func,
sort: React.PropTypes.string, sort: React.PropTypes.string,
history: React.PropTypes.object, history: React.PropTypes.object,
primaryKey: React.PropTypes.string,
}; };
export default Table; 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]); let content = this.getDisplayedValue(header.type, this.props.record[header.name]);
// Display a link if the current column is the `id` column // Display a link if the current column is the `id` column
if (header.name === 'id') { if (header.name === this.props.primaryKey) {
content = ( content = (
<Link to={this.props.destination} className={styles.idLink}> <Link to={this.props.destination} className={styles.idLink}>
{this.getDisplayedValue(header.type, this.props.record[header.name])} {this.getDisplayedValue(header.type, this.props.record[header.name])}
@ -89,6 +89,7 @@ TableRow.propTypes = {
record: React.PropTypes.object, record: React.PropTypes.object,
destination: React.PropTypes.string, destination: React.PropTypes.string,
history: React.PropTypes.object, history: React.PropTypes.object,
primaryKey: React.PropTypes.string,
}; };
export default TableRow; export default TableRow;

View File

@ -8,7 +8,10 @@
import React from 'react'; import React from 'react';
import { createStructuredSelector } from 'reselect'; import { createStructuredSelector } from 'reselect';
import { loadModels } from './actions'; import { loadModels } from './actions';
import { makeSelectModels } from './selectors'; import {
makeSelectModels,
makeSelectLoading,
} from './selectors';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import '../../styles/main.scss'; import '../../styles/main.scss';
@ -19,16 +22,20 @@ class App extends React.Component { // eslint-disable-line react/prefer-stateles
} }
render() { render() {
// Assign plugin component to children let content = <div></div>;
const childrenWithProps = React.Children.map(this.props.children,
(child) => React.cloneElement(child, { if (this.props.models) {
exposedComponents: this.props.exposedComponents // Assign plugin component to children
}) content = React.Children.map(this.props.children,
); (child) => React.cloneElement(child, {
exposedComponents: this.props.exposedComponents
})
);
}
return ( return (
<div className='content-manager'> <div className='content-manager'>
{React.Children.toArray(childrenWithProps)} {React.Children.toArray(content)}
</div> </div>
); );
} }
@ -52,6 +59,7 @@ export function mapDispatchToProps(dispatch) {
const mapStateToProps = createStructuredSelector({ const mapStateToProps = createStructuredSelector({
models: makeSelectModels(), models: makeSelectModels(),
loading: makeSelectLoading(),
}); });
// Wrap the component to inject dispatch and state into it // 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 export class List extends React.Component { // eslint-disable-line react/prefer-stateless-function
componentWillMount() { componentWillMount() {
this.props.setCurrentModelName(this.props.routeParams.slug.toLowerCase()); 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.loadRecords();
this.props.loadCount(); this.props.loadCount();
} }
@ -74,9 +75,9 @@ export class List extends React.Component { // eslint-disable-line react/prefer-
type: value.type, type: value.type,
})); }));
// Add `id` column // Add the primary key column
tableHeaders.unshift({ tableHeaders.unshift({
name: 'id', name: currentModel.primaryKey,
label: 'ID', label: 'ID',
type: 'string', type: 'string',
}); });
@ -90,6 +91,7 @@ export class List extends React.Component { // eslint-disable-line react/prefer-
changeSort={this.props.changeSort} changeSort={this.props.changeSort}
sort={this.props.sort} sort={this.props.sort}
history={this.props.history} history={this.props.history}
primaryKey={currentModel.primaryKey || 'id'}
/> />
); );
} }