From 189cde8411685d76c1f6938f1d6dbf35bb36ec88 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 27 Aug 2024 15:41:31 -0400 Subject: [PATCH] working _id queries and save() re: stargate/data-api#1359 --- discord-bot/README.md | 4 +- discord-bot/commands/createdocument.js | 2 +- discord-bot/models/bot.js | 7 +-- discord-bot/test/countdocuments.test.js | 2 +- discord-bot/test/createdocument.test.js | 2 +- discord-bot/test/setup.js | 2 +- netlify-functions-ecommerce/README.md | 28 +++++------ netlify-functions-ecommerce/models.js | 49 +++++++++---------- .../netlify/functions/addToCart.js | 6 +-- .../netlify/functions/checkout.js | 8 +-- .../netlify/functions/confirmOrder.js | 14 +++--- .../netlify/functions/getCart.js | 2 +- .../netlify/functions/removeFromCart.js | 4 +- .../test/addToCart.test.js | 10 ++-- .../test/checkout.test.js | 6 +-- .../test/fixtures/createCart.js | 2 +- .../test/fixtures/createOrder.js | 3 +- .../test/fixtures/createProducts.js | 2 +- .../test/getCart.test.js | 4 +- .../test/removeFromCart.test.js | 16 +++--- typescript-express-reviews/README.md | 16 +++--- .../src/models/authentication.ts | 7 +-- .../src/models/review.ts | 15 ++---- typescript-express-reviews/src/models/user.ts | 7 +-- .../src/models/vehicle.ts | 7 +-- typescript-express-reviews/src/seed/seed.ts | 4 +- 26 files changed, 101 insertions(+), 128 deletions(-) diff --git a/discord-bot/README.md b/discord-bot/README.md index f5452de8..3d072c67 100644 --- a/discord-bot/README.md +++ b/discord-bot/README.md @@ -54,7 +54,7 @@ Create table: ``` CREATE TABLE bots ( - id text, + "_id" text, name text, - PRIMARY KEY (id)); + PRIMARY KEY ("_id")); ``` \ No newline at end of file diff --git a/discord-bot/commands/createdocument.js b/discord-bot/commands/createdocument.js index 3c096b69..254f41e9 100644 --- a/discord-bot/commands/createdocument.js +++ b/discord-bot/commands/createdocument.js @@ -7,7 +7,7 @@ module.exports = { data: new SlashCommandBuilder().setName('createdocument').setDescription('creates a document'), async execute(interaction) { console.log(new Date(), 'createdocument'); - await Bot.insertMany([{ name: 'I am a document' }]); + await Bot.create({ name: 'I am a document' }); await interaction.reply('done!'); } }; \ No newline at end of file diff --git a/discord-bot/models/bot.js b/discord-bot/models/bot.js index b3ea7b91..078d3248 100644 --- a/discord-bot/models/bot.js +++ b/discord-bot/models/bot.js @@ -3,13 +3,8 @@ const mongoose = require('../mongoose'); const botSchema = new mongoose.Schema({ - id: { - type: mongoose.Types.ObjectId, - required: true, - default: () => new mongoose.Types.ObjectId() - }, name: String -}, { _id: false, versionKey: false }); +}, { versionKey: false }); const Bot = mongoose.model('Bot', botSchema); diff --git a/discord-bot/test/countdocuments.test.js b/discord-bot/test/countdocuments.test.js index a34cab68..c35ba943 100644 --- a/discord-bot/test/countdocuments.test.js +++ b/discord-bot/test/countdocuments.test.js @@ -10,7 +10,7 @@ describe('countdocuments', function() { it('returns the number of bot documents', async function() { const docs = await Bot.find({}); for (const doc of docs) { - await Bot.deleteOne({ id: doc.id }); + await Bot.deleteOne({ _id: doc._id }); } await Bot.insertMany({ name: 'test' }); diff --git a/discord-bot/test/createdocument.test.js b/discord-bot/test/createdocument.test.js index 061aba35..ffe231e4 100644 --- a/discord-bot/test/createdocument.test.js +++ b/discord-bot/test/createdocument.test.js @@ -10,7 +10,7 @@ describe('createdocument', function() { it('inserts a new document', async function() { let docs = await Bot.find({}); for (const doc of docs) { - await Bot.deleteOne({ id: doc.id }); + await Bot.deleteOne({ _id: doc._id }); } const interaction = { diff --git a/discord-bot/test/setup.js b/discord-bot/test/setup.js index 5bd33e30..569e3968 100644 --- a/discord-bot/test/setup.js +++ b/discord-bot/test/setup.js @@ -18,7 +18,7 @@ before(async function() { const docs = await Bot.find({}); for (const doc of docs) { - await Bot.deleteOne({ id: doc.id }); + await Bot.deleteOne({ _id: doc._id }); } }); diff --git a/netlify-functions-ecommerce/README.md b/netlify-functions-ecommerce/README.md index 889c1f38..eda3641a 100644 --- a/netlify-functions-ecommerce/README.md +++ b/netlify-functions-ecommerce/README.md @@ -77,26 +77,26 @@ Using test ``` CREATE TABLE products ( - id text, + "_id" text, name text, price decimal, image text, description text, - PRIMARY KEY (id)); + PRIMARY KEY ("_id")); CREATE TABLE orders ( - id text, - total decimal, - name text, - paymentMethod text, - items text, - PRIMARY KEY (id)); + "_id" text, + total decimal, + name text, + "paymentMethod" text, + items text, + PRIMARY KEY ("_id")); CREATE TABLE carts ( - id text, - items text, - orderId text, - total decimal, - stripeSessionId text, - PRIMARY KEY (id)); + "_id" text, + items text, + "orderId" text, + total decimal, + "stripeSessionId" text, + PRIMARY KEY ("_id")); ``` \ No newline at end of file diff --git a/netlify-functions-ecommerce/models.js b/netlify-functions-ecommerce/models.js index 0c4005c7..eeda37e7 100644 --- a/netlify-functions-ecommerce/models.js +++ b/netlify-functions-ecommerce/models.js @@ -3,32 +3,29 @@ const mongoose = require('./mongoose'); const productSchema = new mongoose.Schema({ - id: { - type: mongoose.Types.ObjectId, - required: true, - default: () => new mongoose.Types.ObjectId() - }, name: String, price: Number, image: String, description: String -}, { _id: false, versionKey: false }); +}, { versionKey: false }); const Product = mongoose.model('Product', productSchema); module.exports.Product = Product; const orderSchema = new mongoose.Schema({ - id: { - type: mongoose.Types.ObjectId, - required: true, - default: () => new mongoose.Types.ObjectId() + items: { + type: String, + get(v) { + return v == null ? v : JSON.parse(v); + }, + set(v) { + if (v == null) { + return v; + } + return typeof v === 'string' ? v : JSON.stringify(v); + }, }, - items: [{ - _id: false, - productId: { type: mongoose.ObjectId, required: true, ref: 'Product' }, - quantity: { type: Number, required: true, validate: v => v > 0 } - }], total: { type: Number, default: 0 @@ -37,22 +34,24 @@ const orderSchema = new mongoose.Schema({ type: String }, paymentMethod: { - id: String, - brand: String, - last4: String + type: String, + get(v) { + return v == null ? v : JSON.parse(v); + }, + set(v) { + if (v == null) { + return v; + } + return typeof v === 'string' ? v : JSON.stringify(v); + }, } -}, { _id: false, versionKey: false }); +}, { versionKey: false }); const Order = mongoose.model('Order', orderSchema); module.exports.Order = Order; const cartSchema = new mongoose.Schema({ - id: { - type: mongoose.Types.ObjectId, - required: true, - default: () => new mongoose.Types.ObjectId() - }, items: { type: String, get(v) { @@ -68,7 +67,7 @@ const cartSchema = new mongoose.Schema({ orderId: { type: mongoose.ObjectId, ref: 'Order' }, total: Number, stripeSessionId: { type: String } -}, { _id: false, versionKey: false, timestamps: false, toObject: { getters: true }, toJSON: { getters: true } }); +}, { versionKey: false, timestamps: false, toObject: { getters: true }, toJSON: { getters: true } }); cartSchema.virtual('numItems').get(function numItems() { if (this.items == null) { diff --git a/netlify-functions-ecommerce/netlify/functions/addToCart.js b/netlify-functions-ecommerce/netlify/functions/addToCart.js index b990600d..22195aed 100644 --- a/netlify-functions-ecommerce/netlify/functions/addToCart.js +++ b/netlify-functions-ecommerce/netlify/functions/addToCart.js @@ -13,7 +13,7 @@ const handler = async(event) => { if (event.body.cartId) { // get the document containing the specified cartId const cart = await Cart. - findOne({ id: event.body.cartId }). + findOne({ _id: event.body.cartId }). setOptions({ sanitizeFilter: true }); if (cart == null) { @@ -31,7 +31,7 @@ const handler = async(event) => { for (const product of event.body.items) { const exists = cart.items?.find(item => item?.productId?.toString() === product?.productId?.toString()); if (!exists) { - if (products.find(p => product?.productId?.toString() === p?.id?.toString())) { + if (products.find(p => product?.productId?.toString() === p?._id?.toString())) { cart.items = [...(cart.items || []), product]; } } else { @@ -46,7 +46,7 @@ const handler = async(event) => { return { statusCode: 200, body: JSON.stringify({ cart: null }) }; } - await Cart.updateOne({ id: cart.id }, cart.getChanges()); + await cart.save(); return { statusCode: 200, body: JSON.stringify(cart) }; } else { // If no cartId, create a new cart diff --git a/netlify-functions-ecommerce/netlify/functions/checkout.js b/netlify-functions-ecommerce/netlify/functions/checkout.js index 16eeb392..f396720a 100644 --- a/netlify-functions-ecommerce/netlify/functions/checkout.js +++ b/netlify-functions-ecommerce/netlify/functions/checkout.js @@ -11,14 +11,14 @@ const handler = async(event) => { event.body = JSON.parse(event.body || {}); await connect(); const cart = await Cart. - findOne({ id: event.body.cartId }). + findOne({ _id: event.body.cartId }). setOptions({ sanitizeFilter: true }). orFail(); const stripeProducts = { line_items: [] }; let total = 0; for (let i = 0; i < cart.items.length; i++) { - const product = await Product.findOne({ id: cart.items[i].productId }); + const product = await Product.findOne({ _id: cart.items[i].productId }); stripeProducts.line_items.push({ price_data: { currency: 'usd', @@ -35,7 +35,7 @@ const handler = async(event) => { cart.total = total; if (process.env.STRIPE_SECRET_KEY === 'test') { - await Cart.updateOne({ id: cart.id }, cart.getChanges()); + await cart.save(); return { statusCode: 200, body: JSON.stringify({ cart: cart, url: '/order-confirmation' }) @@ -50,7 +50,7 @@ const handler = async(event) => { }); cart.stripeSessionId = session.id; - await Cart.updateOne({ id: cart.id }, cart.getChanges()); + await cart.save(); return { statusCode: 200, diff --git a/netlify-functions-ecommerce/netlify/functions/confirmOrder.js b/netlify-functions-ecommerce/netlify/functions/confirmOrder.js index 7d06809b..2969674f 100644 --- a/netlify-functions-ecommerce/netlify/functions/confirmOrder.js +++ b/netlify-functions-ecommerce/netlify/functions/confirmOrder.js @@ -11,12 +11,12 @@ const handler = async(event) => { event.body = JSON.parse(event.body || {}); await connect(); const cart = await Cart. - findOne({ id: event.body.cartId }). + findOne({ _id: event.body.cartId }). setOptions({ sanitizeFilter: true }). orFail(); if (cart.orderId) { - const order = await Order.findOne({ id: cart.orderId }).orFail(); + const order = await Order.findOne({ _id: cart.orderId }).orFail(); return { statusCode: 200, @@ -35,8 +35,8 @@ const handler = async(event) => { } }); - cart.orderId = order.id; - await Cart.updateOne({ id: cart.id }, cart.getChanges()); + cart.orderId = order._id; + await cart.save(); return { statusCode: 200, @@ -55,7 +55,7 @@ const handler = async(event) => { const intent = await stripe.paymentIntents.retrieve(session.payment_intent); const paymentMethod = await stripe.paymentMethods.retrieve(intent['payment_method']); - const [order] = await Order.insertMany({ + const order = await Order.create({ items: cart.items, name: session['customer_details'].name, total: +(session['amount_total'] / 100).toFixed(2), @@ -64,8 +64,8 @@ const handler = async(event) => { null }); - cart.orderId = order.id; - await Cart.updateOne({ id: cart.id }, cart.getChanges()); + cart.orderId = order._id; + await cart.save(); return { statusCode: 200, diff --git a/netlify-functions-ecommerce/netlify/functions/getCart.js b/netlify-functions-ecommerce/netlify/functions/getCart.js index 7f240088..a88f3164 100644 --- a/netlify-functions-ecommerce/netlify/functions/getCart.js +++ b/netlify-functions-ecommerce/netlify/functions/getCart.js @@ -10,7 +10,7 @@ const handler = async(event) => { await connect(); // get the document containing the specified cartId const cart = await Cart. - findOne({ id: event.queryStringParameters.cartId }). + findOne({ _id: event.queryStringParameters.cartId }). setOptions({ sanitizeFilter: true }); return { statusCode: 200, body: JSON.stringify({ cart }) }; } catch (error) { diff --git a/netlify-functions-ecommerce/netlify/functions/removeFromCart.js b/netlify-functions-ecommerce/netlify/functions/removeFromCart.js index 70daf707..2ccb60ed 100644 --- a/netlify-functions-ecommerce/netlify/functions/removeFromCart.js +++ b/netlify-functions-ecommerce/netlify/functions/removeFromCart.js @@ -9,7 +9,7 @@ const handler = async(event) => { try { event.body = JSON.parse(event.body || {}); await connect(); - const cart = await Cart.findOne({ id: event.body.cartId }); + const cart = await Cart.findOne({ _id: event.body.cartId }); const index = cart.items.findIndex((item) => item.productId.toString() == event.body.item.productId.toString() ); @@ -32,7 +32,7 @@ const handler = async(event) => { ...cart.items.slice(index + 1) ]; } - await Cart.updateOne({ id: cart.id }, cart.getChanges()); + await cart.save(); return { statusCode: 200, body: JSON.stringify(cart) }; } catch (error) { console.log(error); diff --git a/netlify-functions-ecommerce/test/addToCart.test.js b/netlify-functions-ecommerce/test/addToCart.test.js index accc44bb..fc23af88 100644 --- a/netlify-functions-ecommerce/test/addToCart.test.js +++ b/netlify-functions-ecommerce/test/addToCart.test.js @@ -40,8 +40,8 @@ describe('Add to Cart', function() { body: { cartId: cart.id, items: [ - { productId: products[0].id, quantity: 2 }, - { productId: products[1].id, quantity: 1 } + { productId: products[0]._id, quantity: 2 }, + { productId: products[1]._id, quantity: 1 } ] } }; @@ -58,10 +58,10 @@ describe('Add to Cart', function() { const { cart } = await fixtures.createCart({ products: [] }); const params = { body: { - cartId: cart.id, + cartId: cart._id, items: [ - { productId: products[0].id, quantity: 2 }, - { productId: products[1].id, quantity: 1 } + { productId: products[0]._id, quantity: 2 }, + { productId: products[1]._id, quantity: 1 } ] } }; diff --git a/netlify-functions-ecommerce/test/checkout.test.js b/netlify-functions-ecommerce/test/checkout.test.js index 8cbea524..7b207ad9 100644 --- a/netlify-functions-ecommerce/test/checkout.test.js +++ b/netlify-functions-ecommerce/test/checkout.test.js @@ -22,8 +22,8 @@ describe('Checkout', function() { body: { cartId: null, items: [ - { productId: products[0].id, quantity: 2 }, - { productId: products[1].id, quantity: 1 } + { productId: products[0]._id, quantity: 2 }, + { productId: products[1]._id, quantity: 1 } ] } }; @@ -33,7 +33,7 @@ describe('Checkout', function() { assert(result.body); assert(result.body.items.length); - params.body.cartId = result.body.id; + params.body.cartId = result.body._id; sinon.stub(stripe.paymentIntents, 'retrieve').returns({ status: 'succeeded', id: '123', brand: 'visa', last4: '1234' }); sinon.stub(stripe.paymentMethods, 'retrieve').returns({ status: 'succeeded', id: '123', brand: 'visa', last4: '1234' }); sinon.stub(stripe.checkout.sessions, 'create').returns({ diff --git a/netlify-functions-ecommerce/test/fixtures/createCart.js b/netlify-functions-ecommerce/test/fixtures/createCart.js index 2bc1ae48..71948dc3 100644 --- a/netlify-functions-ecommerce/test/fixtures/createCart.js +++ b/netlify-functions-ecommerce/test/fixtures/createCart.js @@ -3,6 +3,6 @@ const { Cart } = require('../../models'); module.exports = async function createCart(params) { - const [cart] = await Cart.insertMany({ items: params.items }); + const cart = await Cart.create({ items: params.items }); return { cart }; }; diff --git a/netlify-functions-ecommerce/test/fixtures/createOrder.js b/netlify-functions-ecommerce/test/fixtures/createOrder.js index 9f513678..3a28148d 100644 --- a/netlify-functions-ecommerce/test/fixtures/createOrder.js +++ b/netlify-functions-ecommerce/test/fixtures/createOrder.js @@ -3,7 +3,6 @@ const { Order } = require('../../models'); module.exports = async function createOrder(params) { - - const [order] = await Order.insertMany(params.order); + const [order] = await Order.create(params.order); return { order }; }; diff --git a/netlify-functions-ecommerce/test/fixtures/createProducts.js b/netlify-functions-ecommerce/test/fixtures/createProducts.js index a5d63085..421c2aac 100644 --- a/netlify-functions-ecommerce/test/fixtures/createProducts.js +++ b/netlify-functions-ecommerce/test/fixtures/createProducts.js @@ -3,7 +3,7 @@ const { Product } = require('../../models'); module.exports = async function createProducts(params) { - const products = await Product.insertMany(params.product); + const products = await Product.create(params.product); return { products }; }; diff --git a/netlify-functions-ecommerce/test/getCart.test.js b/netlify-functions-ecommerce/test/getCart.test.js index aaa17ddd..70476b8a 100644 --- a/netlify-functions-ecommerce/test/getCart.test.js +++ b/netlify-functions-ecommerce/test/getCart.test.js @@ -11,12 +11,12 @@ describe('Get the cart given an id', function() { const params = { queryStringParameters: { - cartId: cart.id + cartId: cart._id } }; const findCart = await getCart(params); assert.equal(findCart.statusCode, 200); findCart.body = JSON.parse(findCart.body); - assert.equal(findCart.body.cart.id, cart.id.toString()); + assert.equal(findCart.body.cart._id, cart._id.toString()); }); }); diff --git a/netlify-functions-ecommerce/test/removeFromCart.test.js b/netlify-functions-ecommerce/test/removeFromCart.test.js index 7b637874..2b23276e 100644 --- a/netlify-functions-ecommerce/test/removeFromCart.test.js +++ b/netlify-functions-ecommerce/test/removeFromCart.test.js @@ -20,8 +20,8 @@ describe('Remove From Cart', function() { body: { cartId: null, items: [ - { productId: products[0].id, quantity: 2 }, - { productId: products[1].id, quantity: 1 } + { productId: products[0]._id, quantity: 2 }, + { productId: products[1]._id, quantity: 1 } ] } }; @@ -32,9 +32,9 @@ describe('Remove From Cart', function() { assert.equal(result.body.items.length, 2); const newParams = { body: { - cartId: result.body.id, + cartId: result.body._id, item: { - productId: products[0].id + productId: products[0]._id } } }; @@ -51,8 +51,8 @@ describe('Remove From Cart', function() { body: { cartId: null, items: [ - { productId: products[0].id, quantity: 2 }, - { productId: products[1].id, quantity: 1 } + { productId: products[0]._id, quantity: 2 }, + { productId: products[1]._id, quantity: 1 } ] } }; @@ -63,9 +63,9 @@ describe('Remove From Cart', function() { assert(result.body.items.length); const newParams = { body: { - cartId: result.body.id, + cartId: result.body._id, item: { - productId: products[0].id, + productId: products[0]._id, quantity: 1 } } diff --git a/typescript-express-reviews/README.md b/typescript-express-reviews/README.md index f8fa2264..0178440f 100644 --- a/typescript-express-reviews/README.md +++ b/typescript-express-reviews/README.md @@ -36,38 +36,38 @@ Make sure you have Node.js 14 or higher and a local Stargate instance running as ``` CREATE TABLE authentications ( - id text, + "_id" text, type text, "userId" text, secret text, - PRIMARY KEY (id)); + PRIMARY KEY ("_id")); CREATE TABLE reviews ( - id text, + "_id" text, rating int, text text, "userId" text, "vehicleId" text, "createdAt" decimal, "updatedAt" decimal, - PRIMARY KEY (id)); + PRIMARY KEY ("_id")); CREATE TABLE users ( - id text, + "_id" text, email text, "firstName" text, "lastName" text, - PRIMARY KEY (id)); + PRIMARY KEY ("_id")); CREATE TABLE vehicles ( - id text, + "_id" text, make text, model text, year int, images text, "numReviews" int, "averageReview" decimal, - PRIMARY KEY (id)); + PRIMARY KEY ("_id")); CREATE INDEX ON reviews ("vehicleId"); CREATE INDEX ON users (email); diff --git a/typescript-express-reviews/src/models/authentication.ts b/typescript-express-reviews/src/models/authentication.ts index 8e8224c2..5dac8678 100644 --- a/typescript-express-reviews/src/models/authentication.ts +++ b/typescript-express-reviews/src/models/authentication.ts @@ -1,11 +1,6 @@ import mongoose from './mongoose'; const schema = new mongoose.Schema({ - id: { - type: mongoose.Types.ObjectId, - required: true, - default: () => new mongoose.Types.ObjectId() - }, type: { type: String, required: true, @@ -14,7 +9,7 @@ const schema = new mongoose.Schema({ }, userId: { type: mongoose.Types.ObjectId, required: true }, secret: { type: String, required: true } -}, { _id: false, versionKey: false }); +}, { versionKey: false }); const Authentication = mongoose.model('Authentication', schema); diff --git a/typescript-express-reviews/src/models/review.ts b/typescript-express-reviews/src/models/review.ts index bc47b887..3ccea0d7 100644 --- a/typescript-express-reviews/src/models/review.ts +++ b/typescript-express-reviews/src/models/review.ts @@ -1,11 +1,6 @@ import mongoose from './mongoose'; const schema = new mongoose.Schema({ - id: { - type: mongoose.Types.ObjectId, - required: true, - default: () => new mongoose.Types.ObjectId() - }, rating: { type: Number, required: true, @@ -32,19 +27,19 @@ const schema = new mongoose.Schema({ type: Number, default: () => Date.now() } -}, { _id: false, versionKey: false, timestamps: true }); +}, { versionKey: false, timestamps: true }); schema.virtual('user', { ref: 'User', - localField: 'user_id', - foreignField: 'id', + localField: 'userId', + foreignField: '_id', justOne: true }); schema.virtual('vehicle', { ref: 'Vehicle', localField: 'vehicleId', - foreignField: 'id', + foreignField: '_id', justOne: true }); @@ -54,7 +49,7 @@ schema.pre('save', async function updateVehicleRating() { } const vehicle = await mongoose.model('Vehicle').findOne({ id: this.vehicleId }).orFail(); vehicle.numReviews += 1; - const vehicleReviews = await mongoose.model('Review').find({ vehicle_id: this.vehicleId }); + const vehicleReviews = await mongoose.model('Review').find({ vehicleId: this.vehicleId }); const reviewRatings = vehicleReviews.map((entry) => entry.rating); reviewRatings.push(this.rating); const average = calculateAverage(reviewRatings); diff --git a/typescript-express-reviews/src/models/user.ts b/typescript-express-reviews/src/models/user.ts index 47be6d06..f863a899 100644 --- a/typescript-express-reviews/src/models/user.ts +++ b/typescript-express-reviews/src/models/user.ts @@ -1,11 +1,6 @@ import mongoose from './mongoose'; const schema = new mongoose.Schema({ - id: { - type: mongoose.Types.ObjectId, - required: true, - default: () => new mongoose.Types.ObjectId() - }, email: { type: String, required: true, @@ -19,7 +14,7 @@ const schema = new mongoose.Schema({ type: String, required: true } -}, { _id: false, versionKey: false }); +}, { versionKey: false }); schema.virtual('displayName').get(function() { return this.firstName + ' ' + this.lastName.slice(0, 1) + '.'; diff --git a/typescript-express-reviews/src/models/vehicle.ts b/typescript-express-reviews/src/models/vehicle.ts index 50dec515..d614bdd6 100644 --- a/typescript-express-reviews/src/models/vehicle.ts +++ b/typescript-express-reviews/src/models/vehicle.ts @@ -3,11 +3,6 @@ import mongoose from './mongoose'; const imagesSchemaType = new mongoose.Schema.Types.Array('images', { type: [String] }); const schema = new mongoose.Schema({ - id: { - type: mongoose.Types.ObjectId, - required: true, - default: () => new mongoose.Types.ObjectId() - }, make: { type: String, required: true @@ -43,7 +38,7 @@ const schema = new mongoose.Schema({ required: true, default: 0 } -}, { _id: false, versionKey: false }); +}, { versionKey: false }); const Vehicle = mongoose.model('Vehicle', schema); diff --git a/typescript-express-reviews/src/seed/seed.ts b/typescript-express-reviews/src/seed/seed.ts index 1532411e..02b7f337 100644 --- a/typescript-express-reviews/src/seed/seed.ts +++ b/typescript-express-reviews/src/seed/seed.ts @@ -58,7 +58,7 @@ async function run() { } const vehicles = await Vehicle.insertMany([ { - id: '0'.repeat(24), + _id: '0'.repeat(24), make: 'Tesla', model: 'Model S', year: 2022, @@ -70,7 +70,7 @@ async function run() { averageReviews: 0 }, { - id: '1'.repeat(24), + _id: '1'.repeat(24), make: 'Porsche', model: 'Taycan', year: 2022,