mirror of
https://github.com/strapi/strapi.git
synced 2025-09-02 21:32:43 +00:00
Expose Mongoose functions through the strapi.models object
This commit is contained in:
parent
2a4a6b202d
commit
68e823459a
@ -136,6 +136,9 @@ module.exports = function (strapi) {
|
|||||||
|
|
||||||
global[definition.globalName] = mongoose.model(definition.globalName, collection.schema);
|
global[definition.globalName] = mongoose.model(definition.globalName, collection.schema);
|
||||||
|
|
||||||
|
// Expose ORM functions through the `strapi.models` object.
|
||||||
|
strapi.models[model] = _.assign(mongoose.model(definition.globalName), strapi.models[model]);
|
||||||
|
|
||||||
// Push model to strapi global variables.
|
// Push model to strapi global variables.
|
||||||
collection = global[definition.globalName];
|
collection = global[definition.globalName];
|
||||||
|
|
||||||
|
@ -7,12 +7,21 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
models: async(ctx) => {
|
models: async(ctx) => {
|
||||||
ctx.body = strapi.models;
|
ctx.body = _.mapValues(strapi.models, (model) => (_.pick(model, [
|
||||||
|
'connection',
|
||||||
|
'collectionName',
|
||||||
|
'attributes',
|
||||||
|
'identity',
|
||||||
|
'globalId',
|
||||||
|
'globalName',
|
||||||
|
'orm',
|
||||||
|
'loadedModel',
|
||||||
|
'primaryKey',
|
||||||
|
])));
|
||||||
},
|
},
|
||||||
|
|
||||||
find: async(ctx) => {
|
find: async(ctx) => {
|
||||||
const model = strapi.models[ctx.params.model];
|
const model = strapi.models[ctx.params.model];
|
||||||
const collection = global[model.globalName];
|
|
||||||
const primaryKey = model.primaryKey;
|
const primaryKey = model.primaryKey;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -21,7 +30,7 @@ module.exports = {
|
|||||||
sort = primaryKey
|
sort = primaryKey
|
||||||
} = ctx.request.query;
|
} = ctx.request.query;
|
||||||
|
|
||||||
const entries = await collection
|
const entries = await model
|
||||||
.find()
|
.find()
|
||||||
.limit(Number(limit))
|
.limit(Number(limit))
|
||||||
.sort(sort)
|
.sort(sort)
|
||||||
@ -32,9 +41,8 @@ module.exports = {
|
|||||||
|
|
||||||
count: async(ctx) => {
|
count: async(ctx) => {
|
||||||
const model = strapi.models[ctx.params.model];
|
const model = strapi.models[ctx.params.model];
|
||||||
const collection = global[model.globalName];
|
|
||||||
|
|
||||||
const count = await collection
|
const count = await model
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
@ -44,12 +52,11 @@ module.exports = {
|
|||||||
|
|
||||||
findOne: async(ctx) => {
|
findOne: async(ctx) => {
|
||||||
const model = strapi.models[ctx.params.model];
|
const model = strapi.models[ctx.params.model];
|
||||||
const collection = global[model.globalName];
|
|
||||||
const primaryKey = model.primaryKey;
|
const primaryKey = model.primaryKey;
|
||||||
const params = {};
|
const params = {};
|
||||||
params[primaryKey] = ctx.params.id;
|
params[primaryKey] = ctx.params.id;
|
||||||
|
|
||||||
const entry = await collection
|
const entry = await model
|
||||||
.findOne(params);
|
.findOne(params);
|
||||||
|
|
||||||
ctx.body = entry;
|
ctx.body = entry;
|
||||||
@ -57,9 +64,8 @@ module.exports = {
|
|||||||
|
|
||||||
create: async(ctx) => {
|
create: async(ctx) => {
|
||||||
const model = strapi.models[ctx.params.model];
|
const model = strapi.models[ctx.params.model];
|
||||||
const collection = global[model.globalName];
|
|
||||||
|
|
||||||
const entryCreated = await collection
|
const entryCreated = await model
|
||||||
.create(ctx.request.body);
|
.create(ctx.request.body);
|
||||||
|
|
||||||
ctx.body = entryCreated;
|
ctx.body = entryCreated;
|
||||||
@ -67,13 +73,12 @@ module.exports = {
|
|||||||
|
|
||||||
update: async(ctx) => {
|
update: async(ctx) => {
|
||||||
const model = strapi.models[ctx.params.model];
|
const model = strapi.models[ctx.params.model];
|
||||||
const collection = global[model.globalName];
|
|
||||||
const primaryKey = model.primaryKey;
|
const primaryKey = model.primaryKey;
|
||||||
const params = {};
|
const params = {};
|
||||||
|
|
||||||
params[primaryKey] = ctx.params.id;
|
params[primaryKey] = ctx.params.id;
|
||||||
|
|
||||||
const entryUpdated = await collection
|
const entryUpdated = await model
|
||||||
.update(params, ctx.request.body);
|
.update(params, ctx.request.body);
|
||||||
|
|
||||||
ctx.body = entryUpdated;
|
ctx.body = entryUpdated;
|
||||||
@ -81,12 +86,11 @@ module.exports = {
|
|||||||
|
|
||||||
delete: async(ctx) => {
|
delete: async(ctx) => {
|
||||||
const model = strapi.models[ctx.params.model];
|
const model = strapi.models[ctx.params.model];
|
||||||
const collection = global[model.globalName];
|
|
||||||
const primaryKey = model.primaryKey;
|
const primaryKey = model.primaryKey;
|
||||||
const params = {};
|
const params = {};
|
||||||
params[primaryKey] = ctx.params.id;
|
params[primaryKey] = ctx.params.id;
|
||||||
|
|
||||||
const entryDeleted = await collection
|
const entryDeleted = await model
|
||||||
.remove(params);
|
.remove(params);
|
||||||
|
|
||||||
ctx.body = entryDeleted;
|
ctx.body = entryDeleted;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user