How to change name of default attributes strapi v4 - content-management-system

Model has default attributes - timestamps and it looks like this -
{
...
createdAt: '2022-10-06T12:31:48.684Z',
updatedAt: '2022-10-06T12:31:50.587Z',
publishedAt: '2022-10-06T12:31:50.428Z',
...
}
and I would like to change for each model to this
{
...
created_at: '2022-10-06T12:31:48.684Z',
updated_at: '2022-10-06T12:31:50.587Z',
published_at: '2022-10-06T12:31:50.428Z',
...
}

Related

database design for a market

I want to design database for a market with simple and few objects for selling using NodeJS, MongoDB and Mongoose. Because I'm new to MongoDB and NoSQL designs, I need a guide for designing it.
My implementation is here:
var orderSchema = new Schema({
orderId: Schema.Types.ObjectId,
orderType: {
type: String, enum: ['OBJEC1',
'OBJECT2',
//other objects
], default: 'ALBUM'
},
price: { type: String, enum: ['PRICE1', 'PRICE2', 'PRICE3'] },
coverPhoto: { type: String, default: '' },
photos: [{
address: { type: String, default: 'media/uploads' },
}],
orderQuantity: { type: Number, default: 1 },
isChecked: { type: Boolean, default: true },
date: { type: Date, default: Date.now }
});
Besides, I'll save reference of each order to its related user. Am I right, or not? Thanks a lot.
The way you designed your schema based on the logic seems good. One thing is you have used default in all the fields.
First, you should understand that default is optional and default is used only when you want to populate some value during the data is created.
Example: you have default for date field, here it is good to have. You don't want to manually assign a date during processing the data. So only unless your field should have common default value when creation then you go ahead otherwise remove the default field and make sure the data is inserted properly.
you can use required attribute in case some field is mandatory to create a document in the collection. I guess orderType a mandatory field so don't miss it ever during insertion so make it as required: true.
var orderSchema = new Schema({
orderId: {
type: Schema.Types.ObjectId
},
orderType: {
type: String,
enum: ['OBJEC1','OBJECT2']
},
price: {
type: String,
enum: ['PRICE1', 'PRICE2', 'PRICE3']
},
coverPhoto: {
type: String
},
photos: [{
address: {
type: String
}
}],
orderQuantity: {
type: Number
},
isChecked: {
type: Boolean,
default: true
},
date: {
type: Date,
default: Date.now
}
});

Group the documents and also populate the fields

I have a collection like:
MsgSchema = {
user: { type: ObjectID, ref: 'User' },
userAdmin: { type: String, default: 'no' },
body: { type: String },
createdAt: { type: Date, default: Date.now() }
}
I want to get the msg data with userAdmin field yes and by populating the user field and grouping the data by user field, and then sort the data based on Date. I looked at aggregate but couldn't figure out a way to do it.

How can I update my model in a sailsjs Controller like mongodb updates: a complete document replacement

For my purposes, I'd like sailjs's
Collection.update(id, {params}, cb)
to work like mongodb's update, with the entire model getting swapped out. Is there a way to do this through in a sailsjs Controller?
For example: if I have a model that looks like
{ _id: "123...", good: true, bad: false, createdAt: dt, updatedAt: dt }
and my {params} are
{ _id: "123...", good: false }
I'd like the resulting model to be
{ _id: "123...", good: false, createdAt: dt, updatedAt: newdt }

How to save populated Document?

Is it possible to save a populated document?
I am trying to do:
var Group = new Db['Group']();
for (var i=0; i<50; i++)
Db.Members.push({ User: { _id: "521014731e27b1b008000002"}, pseudo: 'John' });
Group.save();
Schemas
var GroupSchemaModel = {
Members: [{
User: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
updated_at: { type: Date, required: true, default: Date.now }
}]
};
I get the error
{ message: 'Cast to ObjectId failed for value "[object Object]" at path "User"',
name: 'CastError',
type: 'ObjectId',
value: { _id: '521014731e27b1b008000002' },
path: 'User' }
This:
User: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
Tells mongoose that User field will be a collection of references of type ObjectId pointing to another collection.
You, on the other hand, are trying to insert an object there:
Db.Members.push({ User: { _id: "521014731e27b1b008000002"}, pseudo: 'John' });
Mongoose tries to cast it to ObjectId and fails. That's apart from the fact that pseudo field isn't in the group schema.
Try this instead:
Db.Members.push({User: mongoose.Types.ObjectId("521014731e27b1b008000002"), updated_at: whatever});

Mongoose - using Populate on an array of ObjectId

I've got a schema that looks a bit like:
var conversationSchema = new Schema({
created: { type: Date, default: Date.now },
updated: { type: Date, default: Date.now },
recipients: { type: [Schema.ObjectId], ref: 'User' },
messages: [ conversationMessageSchema ]
});
So my recipients collection, is a collection of object id's referencing my user schema / collection.
I need to populate these on query, so i'm trying this:
Conversation.findOne({ _id: myConversationId})
.populate('user')
.run(function(err, conversation){
//do stuff
});
But obviously 'user' isn't populating...
Is there a way I can do this?
For anyone else coming across this question.. the OP's code has an error in the schema definition.. it should be:
var conversationSchema = new Schema({
created: { type: Date, default: Date.now },
updated: { type: Date, default: Date.now },
recipients: [{ type: Schema.ObjectId, ref: 'User' }],
messages: [ conversationMessageSchema ]
});
mongoose.model('Conversation', conversationSchema);
Use the name of the schema path instead of the collection name:
Conversation.findOne({ _id: myConversationId})
.populate('recipients') // <==
.exec(function(err, conversation){
//do stuff
});