Schemas
Aligning to well defined schema for clients and backends using AI
Summary
Example: Creating an Object Schema in MongoDB using ChatGPT
Prompt:
Generate a MongoDB schema definition using Mongoose that includes the following standard fields: id, createdDate, lastModifiedDate, createdBy, and lastModifiedBy. The schema should automatically handle the createdDate and lastModifiedDate fields. Consider the bicycle-app-config context.Response:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const BaseSchema = new Schema(
{
_id: {
type: Schema.Types.ObjectId,
auto: true,
},
createdDate: {
type: Date,
default: Date.now,
},
lastModifiedDate: {
type: Date,
default: Date.now,
},
createdBy: {
type: String,
required: true,
},
lastModifiedBy: {
type: String,
required: true,
},
},
{
timestamps: {
createdAt: 'createdDate',
updatedAt: 'lastModifiedDate',
},
}
);
module.exports = BaseSchema;Discussion
Pros:
Cons:
Last updated