From d2b505383100f7b28c3d441f04542d91f19d44f8 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 9 Jan 2024 10:21:22 -0500 Subject: [PATCH 1/5] add scripts to drop collections to avoid 5 collection max --- .../dropCollections.js | 23 ++++++++++++++ netlify-functions-ecommerce/mongoose.js | 2 +- .../src/models/connect.ts | 2 +- .../src/models/mongoose.ts | 2 +- .../src/seed/dropCollections.ts | 31 +++++++++++++++++++ typescript-express-reviews/src/seed/seed.ts | 17 +++++----- 6 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 netlify-functions-ecommerce/dropCollections.js create mode 100644 typescript-express-reviews/src/seed/dropCollections.ts diff --git a/netlify-functions-ecommerce/dropCollections.js b/netlify-functions-ecommerce/dropCollections.js new file mode 100644 index 0000000..4c80760 --- /dev/null +++ b/netlify-functions-ecommerce/dropCollections.js @@ -0,0 +1,23 @@ +'use strict'; + +require('dotenv').config(); + +const models = require('./models'); +const connect = require('./connect'); + +dropCollections().catch(err => { + console.error(err); + process.exit(-1); +}); + +async function dropCollections() { + await connect(); + + for (const Model of Object.values(models)) { + console.log('Dropping', Model.collection.collectionName); + await Model.db.dropCollection(Model.collection.collectionName); + } + + console.log('Done'); + process.exit(0); +} diff --git a/netlify-functions-ecommerce/mongoose.js b/netlify-functions-ecommerce/mongoose.js index c630af9..e74e509 100644 --- a/netlify-functions-ecommerce/mongoose.js +++ b/netlify-functions-ecommerce/mongoose.js @@ -2,7 +2,7 @@ const mongoose = require('mongoose'); -mongoose.set('autoCreate', true); +mongoose.set('autoCreate', process.env.MONGOOSE_AUTO_CREATE === 'false' ? false : true); mongoose.set('autoIndex', false); mongoose.set('toJSON', { virtuals: true }); mongoose.set('toObject', { virtuals: true }); diff --git a/typescript-express-reviews/src/models/connect.ts b/typescript-express-reviews/src/models/connect.ts index 3413c65..3ddf08a 100644 --- a/typescript-express-reviews/src/models/connect.ts +++ b/typescript-express-reviews/src/models/connect.ts @@ -16,7 +16,7 @@ const astraApplicationToken = process.env.ASTRA_APPLICATION_TOKEN ?? ''; export default async function connect() { if (isAstra) { const uri = createAstraUri(astraDbId, astraRegion, astraKeyspace, astraApplicationToken); - console.log('Connecting to', uri); + console.log('Connecting to Astra:', uri); await mongoose.connect( uri, { isAstra: true } as mongoose.ConnectOptions diff --git a/typescript-express-reviews/src/models/mongoose.ts b/typescript-express-reviews/src/models/mongoose.ts index 5bad01b..79e1419 100644 --- a/typescript-express-reviews/src/models/mongoose.ts +++ b/typescript-express-reviews/src/models/mongoose.ts @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; -mongoose.set('autoCreate', true); +mongoose.set('autoCreate', process.env.MONGOOSE_AUTO_CREATE === 'false' ? false : true); mongoose.set('autoIndex', false); import { driver } from 'stargate-mongoose'; diff --git a/typescript-express-reviews/src/seed/dropCollections.ts b/typescript-express-reviews/src/seed/dropCollections.ts new file mode 100644 index 0000000..141bb88 --- /dev/null +++ b/typescript-express-reviews/src/seed/dropCollections.ts @@ -0,0 +1,31 @@ +import dotenv from 'dotenv'; +dotenv.config(); + +import connect from '../models/connect'; + +import Authentication from '../models/authentication'; +import Review from '../models/review'; +import User from '../models/user'; +import Vehicle from '../models/vehicle'; + +dropCollections().catch(err => { + console.error(err); + process.exit(-1); +}); + +async function dropCollections() { + await connect(); + + for (const Model of [Authentication, Review, User, Vehicle]) { + console.log('Dropping', Model.collection.collectionName); + await Model.db.dropCollection(Model.collection.collectionName).catch(err => { + if (err?.errors?.[0]?.message === 'Request failed with status code 504') { + return; + } + throw err; + }); + } + + console.log('Done'); + process.exit(0); +} diff --git a/typescript-express-reviews/src/seed/seed.ts b/typescript-express-reviews/src/seed/seed.ts index 766065b..59b634c 100644 --- a/typescript-express-reviews/src/seed/seed.ts +++ b/typescript-express-reviews/src/seed/seed.ts @@ -21,14 +21,15 @@ async function run() { await Promise.all(Object.values(mongoose.connection.models).map(async Model => { await Model.init(); })); - // Then drop all collections to clear db - await Promise.all(Object.values(mongoose.connection.models).map(async Model => { - await mongoose.connection.dropCollection(Model.collection.collectionName); - })); - // Then recreate all collections. Workaround for lack of `dropDatabase()` and `deleteMany()` - await Promise.all(Object.values(mongoose.connection.models).map(async Model => { - await mongoose.connection.createCollection(Model.collection.collectionName); - })); + for (const Model of Object.values(mongoose.connection.models)) { + console.log('Resetting collection', Model.collection.collectionName); + // Then drop all collections to clear db. + // Ignore errors because sometimes `dropCollection()` times out even though the collection is dropped + await mongoose.connection.dropCollection(Model.collection.collectionName).catch(() => {}); + // Then recreate all collections. Workaround for lack of `dropDatabase()` and `deleteMany()` + // Ignore errors because `createCollection()` sometimes times out. + await mongoose.connection.createCollection(Model.collection.collectionName).catch(() => {}); + } const users = await User.create([ { From 3a2c4793a89bac11495779374f14e6e30eb36354 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 10 Jan 2024 16:36:53 -0500 Subject: [PATCH 2/5] add drop collections scripts --- discord-bot/dropCollections.js | 46 +++++++++++++++++++ discord-bot/mongoose.js | 2 +- discord-bot/package.json | 1 + .../dropCollections.js | 7 ++- netlify-functions-ecommerce/package.json | 1 + typescript-express-reviews/package.json | 1 + 6 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 discord-bot/dropCollections.js diff --git a/discord-bot/dropCollections.js b/discord-bot/dropCollections.js new file mode 100644 index 0000000..5011aca --- /dev/null +++ b/discord-bot/dropCollections.js @@ -0,0 +1,46 @@ +'use strict'; + +require('dotenv').config(); + +const { createAstraUri } = require('stargate-mongoose'); +const mongoose = require('./mongoose'); + +require('./models/bot'); + +dropCollections().catch(err => { + console.error(err); + process.exit(-1); +}); + +async function dropCollections() { + let uri = ''; + let jsonApiConnectOptions = {}; + if (process.env.IS_ASTRA === 'true') { + uri = createAstraUri(process.env.ASTRA_DBID, process.env.ASTRA_REGION, process.env.ASTRA_KEYSPACE, process.env.ASTRA_APPLICATION_TOKEN); + jsonApiConnectOptions = { + isAstra: true + }; + } else { + uri = process.env.JSON_API_URL; + jsonApiConnectOptions = { + username: process.env.JSON_API_AUTH_USERNAME, + password: process.env.JSON_API_AUTH_PASSWORD, + authUrl: process.env.JSON_API_AUTH_URL + }; + } + console.log('Connecting to', uri); + await mongoose.connect(uri, jsonApiConnectOptions); + + for (const Model of Object.values(mongoose.models)) { + console.log('Dropping', Model.collection.collectionName); + await Model.db.dropCollection(Model.collection.collectionName).catch(err => { + if (err?.errors?.[0]?.message === 'Request failed with status code 504') { + return; + } + throw err; + }); + } + + console.log('Done'); + process.exit(0); +} diff --git a/discord-bot/mongoose.js b/discord-bot/mongoose.js index 01b16cf..3ccf714 100644 --- a/discord-bot/mongoose.js +++ b/discord-bot/mongoose.js @@ -2,7 +2,7 @@ const mongoose = require('mongoose'); -mongoose.set('autoCreate', true); +mongoose.set('autoCreate', process.env.MONGOOSE_AUTO_CREATE === 'false' ? false : true); mongoose.set('autoIndex', false); const { driver } = require('stargate-mongoose'); diff --git a/discord-bot/package.json b/discord-bot/package.json index 50ff0c2..ed8cb78 100644 --- a/discord-bot/package.json +++ b/discord-bot/package.json @@ -4,6 +4,7 @@ "description": "", "main": "index.js", "scripts": { + "clean-db": "env MONGOOSE_AUTO_CREATE=false node ./dropCollections", "lint": "eslint .", "test": "mocha test/*.js", "start": "node ./index.js" diff --git a/netlify-functions-ecommerce/dropCollections.js b/netlify-functions-ecommerce/dropCollections.js index 4c80760..960feff 100644 --- a/netlify-functions-ecommerce/dropCollections.js +++ b/netlify-functions-ecommerce/dropCollections.js @@ -15,7 +15,12 @@ async function dropCollections() { for (const Model of Object.values(models)) { console.log('Dropping', Model.collection.collectionName); - await Model.db.dropCollection(Model.collection.collectionName); + await Model.db.dropCollection(Model.collection.collectionName).catch(err => { + if (err?.errors?.[0]?.message === 'Request failed with status code 504') { + return; + } + throw err; + }); } console.log('Done'); diff --git a/netlify-functions-ecommerce/package.json b/netlify-functions-ecommerce/package.json index bd31baa..bdced06 100644 --- a/netlify-functions-ecommerce/package.json +++ b/netlify-functions-ecommerce/package.json @@ -23,6 +23,7 @@ }, "scripts": { "build": "node ./frontend/build", + "clean-db": "env MONGOOSE_AUTO_CREATE=false node ./dropCollections", "lint": "eslint .", "seed": "node ./seed", "start": "netlify dev --dir public --functions netlify/functions", diff --git a/typescript-express-reviews/package.json b/typescript-express-reviews/package.json index 2177638..8f482b3 100644 --- a/typescript-express-reviews/package.json +++ b/typescript-express-reviews/package.json @@ -6,6 +6,7 @@ "license": "MIT", "scripts": { "build": "tsc", + "clean-db": "env MONGOOSE_AUTO_CREATE=false ts-node src/seed/dropCollections", "seed": "ts-node src/seed/seed", "start": "ts-node src/api", "test": "ts-mocha tests/*.test.ts" From caf9d2b6d7ac8e7606665a5453965cf58212f50c Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 18 Jan 2024 10:28:45 -0500 Subject: [PATCH 3/5] disable autoCreate to avoid dropping/recreating collections all the time, use deleteMany to truncate collections --- discord-bot/package.json | 2 +- netlify-functions-ecommerce/README.md | 2 +- netlify-functions-ecommerce/mongoose.js | 2 +- netlify-functions-ecommerce/package.json | 2 +- netlify-functions-ecommerce/seed.js | 12 ++++++++---- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/discord-bot/package.json b/discord-bot/package.json index 4fd24bc..b042d04 100644 --- a/discord-bot/package.json +++ b/discord-bot/package.json @@ -20,7 +20,7 @@ "discord.js": "14.14.1", "dotenv": "16.3.1", "eslint": "8.56.0", - "mongoose": "8.x", + "mongoose": "^8.1", "stargate-mongoose": "0.4.2" }, "devDependencies": { diff --git a/netlify-functions-ecommerce/README.md b/netlify-functions-ecommerce/README.md index 3a6e870..1cd5f91 100644 --- a/netlify-functions-ecommerce/README.md +++ b/netlify-functions-ecommerce/README.md @@ -32,7 +32,7 @@ Make sure you have a local stargate instance running as described on the [main p ### running the example 1. Run `npm install` -2. Run `npm run seed` +2. Run `npm run seed` to create all collections and insert sample data 3. Run `npm run build` to compile the frontend 4. (Optional) set `STRIPE_SECRET_KEY` to a test Stripe API key in your `.env` file. This will allow you to enable Stripe checkout. 5. Run `npm start` diff --git a/netlify-functions-ecommerce/mongoose.js b/netlify-functions-ecommerce/mongoose.js index e74e509..d0f2713 100644 --- a/netlify-functions-ecommerce/mongoose.js +++ b/netlify-functions-ecommerce/mongoose.js @@ -2,7 +2,7 @@ const mongoose = require('mongoose'); -mongoose.set('autoCreate', process.env.MONGOOSE_AUTO_CREATE === 'false' ? false : true); +mongoose.set('autoCreate', false); mongoose.set('autoIndex', false); mongoose.set('toJSON', { virtuals: true }); mongoose.set('toObject', { virtuals: true }); diff --git a/netlify-functions-ecommerce/package.json b/netlify-functions-ecommerce/package.json index ea80823..50e654d 100644 --- a/netlify-functions-ecommerce/package.json +++ b/netlify-functions-ecommerce/package.json @@ -4,7 +4,7 @@ "license": "MIT", "dependencies": { "dotenv": "16.3.1", - "mongoose": "8.x", + "mongoose": "^8.1", "sinon": "17.0.1", "stargate-mongoose": "0.4.2", "stripe": "14.12.0", diff --git a/netlify-functions-ecommerce/seed.js b/netlify-functions-ecommerce/seed.js index 155ee75..2ed9dfa 100644 --- a/netlify-functions-ecommerce/seed.js +++ b/netlify-functions-ecommerce/seed.js @@ -2,15 +2,19 @@ require('./config'); -const { Product } = require('./models'); +const models = require('./models'); const connect = require('./connect'); -const mongoose = require('./mongoose'); async function createProducts() { await connect(); - await Product.db.dropCollection('products'); - await Product.createCollection(); + await Promise.all( + Object.values(models).map(Model => Model.createCollection()) + ); + await Promise.all( + Object.values(models).map(Model => Model.deleteMany({})) + ); + const { Product } = models; await Product.create({ name: 'iPhone 12', From e63b2525810031127c3941275ec9275e14bfff51 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 18 Jan 2024 10:55:56 -0500 Subject: [PATCH 4/5] typescript-express-reviews: disable autoCreate to avoid dropping/recreating collections all the time, use deleteMany to truncate collections --- typescript-express-reviews/package.json | 2 +- typescript-express-reviews/src/models/mongoose.ts | 2 +- typescript-express-reviews/src/seed/seed.ts | 15 +++++---------- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/typescript-express-reviews/package.json b/typescript-express-reviews/package.json index 8674fca..9c75d46 100644 --- a/typescript-express-reviews/package.json +++ b/typescript-express-reviews/package.json @@ -19,7 +19,7 @@ "bcryptjs": "^2.4.3", "dotenv": "16.3.1", "express": "4.x", - "mongoose": "8.x", + "mongoose": "^8.1", "stargate-mongoose": "0.4.2" }, "devDependencies": { diff --git a/typescript-express-reviews/src/models/mongoose.ts b/typescript-express-reviews/src/models/mongoose.ts index 79e1419..35741a7 100644 --- a/typescript-express-reviews/src/models/mongoose.ts +++ b/typescript-express-reviews/src/models/mongoose.ts @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; -mongoose.set('autoCreate', process.env.MONGOOSE_AUTO_CREATE === 'false' ? false : true); +mongoose.set('autoCreate', false); mongoose.set('autoIndex', false); import { driver } from 'stargate-mongoose'; diff --git a/typescript-express-reviews/src/seed/seed.ts b/typescript-express-reviews/src/seed/seed.ts index 59b634c..b5e04aa 100644 --- a/typescript-express-reviews/src/seed/seed.ts +++ b/typescript-express-reviews/src/seed/seed.ts @@ -17,18 +17,13 @@ run().catch(err => { async function run() { await connect(); - // First wait for autoCreate to finish - await Promise.all(Object.values(mongoose.connection.models).map(async Model => { - await Model.init(); - })); + for (const Model of Object.values(mongoose.connection.models)) { console.log('Resetting collection', Model.collection.collectionName); - // Then drop all collections to clear db. - // Ignore errors because sometimes `dropCollection()` times out even though the collection is dropped - await mongoose.connection.dropCollection(Model.collection.collectionName).catch(() => {}); - // Then recreate all collections. Workaround for lack of `dropDatabase()` and `deleteMany()` - // Ignore errors because `createCollection()` sometimes times out. - await mongoose.connection.createCollection(Model.collection.collectionName).catch(() => {}); + // First ensure the collection exists + await mongoose.connection.createCollection(Model.collection.collectionName); + // Then make sure the collection is empty + await Model.deleteMany({}); } const users = await User.create([ From a96953f3a69c65aadaab7ad878709bfa64954a79 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 18 Jan 2024 17:22:59 -0500 Subject: [PATCH 5/5] avoid using autoCreate, instead rely on seed scripts to create collection --- discord-bot/README.md | 7 ++-- discord-bot/connect.js | 28 ++++++++++++++++ discord-bot/dropCollections.js | 32 ++++--------------- discord-bot/index.js | 26 ++------------- discord-bot/mongoose.js | 2 +- discord-bot/package.json | 5 +-- discord-bot/seed.js | 24 ++++++++++++++ .../dropCollections.js | 14 +++----- netlify-functions-ecommerce/package.json | 4 +-- netlify-functions-ecommerce/seed.js | 12 +++++-- typescript-express-reviews/package.json | 4 +-- .../src/seed/dropCollections.ts | 18 +++-------- typescript-express-reviews/src/seed/seed.ts | 8 +++-- 13 files changed, 97 insertions(+), 87 deletions(-) create mode 100644 discord-bot/connect.js create mode 100644 discord-bot/seed.js diff --git a/discord-bot/README.md b/discord-bot/README.md index 7b106be..264ed02 100644 --- a/discord-bot/README.md +++ b/discord-bot/README.md @@ -10,9 +10,10 @@ Make sure you have a local Stargate instance running as described on the [main p ## Running This Example 1. Create a `.env` file with the keys from `.env.example` and the values from the developer portal and your Discord server.* -1. Run npm install -1. Run node ./deploy-commands.js -1. Run node ./index.js +1. Run `npm install` +1. Run `npm run seed` to create the Bot collection in Astra +1. Run `node ./deploy-commands.js` +1. Run `node ./index.js` * `DISCORD_GUILD_ID`: go into your server's settings and click on "Widget". The "Server ID" is the `guildId`. See screenshot 1 below. * `DISCORD_CLIENT_ID`: go to the [Discord developer portal](https://discord.com/developers/applications), click your bot, and then click "OAuth2". The "Application ID" is the `clientId`. See screenshot 2 below. diff --git a/discord-bot/connect.js b/discord-bot/connect.js new file mode 100644 index 0000000..b0e8abe --- /dev/null +++ b/discord-bot/connect.js @@ -0,0 +1,28 @@ +'use strict'; + +const { createAstraUri } = require('stargate-mongoose'); +const mongoose = require('./mongoose'); + +module.exports = async function connect() { + let uri = ''; + let jsonApiConnectOptions = {}; + if (process.env.IS_ASTRA === 'true') { + uri = createAstraUri( + process.env.ASTRA_API_ENDPOINT, + process.env.ASTRA_APPLICATION_TOKEN, + process.env.ASTRA_NAMESPACE + ); + jsonApiConnectOptions = { + isAstra: true + }; + } else { + uri = process.env.JSON_API_URL; + jsonApiConnectOptions = { + username: process.env.JSON_API_AUTH_USERNAME, + password: process.env.JSON_API_AUTH_PASSWORD, + authUrl: process.env.JSON_API_AUTH_URL + }; + } + console.log('Connecting to', uri); + await mongoose.connect(uri, jsonApiConnectOptions); +}; \ No newline at end of file diff --git a/discord-bot/dropCollections.js b/discord-bot/dropCollections.js index 5011aca..affe27c 100644 --- a/discord-bot/dropCollections.js +++ b/discord-bot/dropCollections.js @@ -2,7 +2,7 @@ require('dotenv').config(); -const { createAstraUri } = require('stargate-mongoose'); +const connect = require('./connect'); const mongoose = require('./mongoose'); require('./models/bot'); @@ -13,32 +13,12 @@ dropCollections().catch(err => { }); async function dropCollections() { - let uri = ''; - let jsonApiConnectOptions = {}; - if (process.env.IS_ASTRA === 'true') { - uri = createAstraUri(process.env.ASTRA_DBID, process.env.ASTRA_REGION, process.env.ASTRA_KEYSPACE, process.env.ASTRA_APPLICATION_TOKEN); - jsonApiConnectOptions = { - isAstra: true - }; - } else { - uri = process.env.JSON_API_URL; - jsonApiConnectOptions = { - username: process.env.JSON_API_AUTH_USERNAME, - password: process.env.JSON_API_AUTH_PASSWORD, - authUrl: process.env.JSON_API_AUTH_URL - }; - } - console.log('Connecting to', uri); - await mongoose.connect(uri, jsonApiConnectOptions); + await connect(); - for (const Model of Object.values(mongoose.models)) { - console.log('Dropping', Model.collection.collectionName); - await Model.db.dropCollection(Model.collection.collectionName).catch(err => { - if (err?.errors?.[0]?.message === 'Request failed with status code 504') { - return; - } - throw err; - }); + const collections = await mongoose.connection.listCollections(); + for (const collection of collections) { + console.log('Dropping', collection); + await mongoose.connection.dropCollection(collection); } console.log('Done'); diff --git a/discord-bot/index.js b/discord-bot/index.js index 685ab51..e7c5dfe 100644 --- a/discord-bot/index.js +++ b/discord-bot/index.js @@ -5,11 +5,9 @@ require('dotenv').config(); // Require the necessary discord.js classes const { Client, Collection, GatewayIntentBits } = require('discord.js'); const assert = require('assert'); +const connect = require('./connect'); const fs = require('fs'); const path = require('node:path'); -const mongoose = require('./mongoose'); - -const { createAstraUri } = require('stargate-mongoose'); // Create a new client instance const client = new Client({ intents: [GatewayIntentBits.Guilds] }); @@ -54,27 +52,7 @@ client.on('interactionCreate', async interaction => { run(); async function run() { - let uri = ''; - let jsonApiConnectOptions = {}; - if (process.env.IS_ASTRA === 'true') { - uri = createAstraUri( - process.env.ASTRA_API_ENDPOINT, - process.env.ASTRA_APPLICATION_TOKEN, - process.env.ASTRA_NAMESPACE - ); - jsonApiConnectOptions = { - isAstra: true - }; - } else { - uri = process.env.JSON_API_URL; - jsonApiConnectOptions = { - username: process.env.JSON_API_AUTH_USERNAME, - password: process.env.JSON_API_AUTH_PASSWORD, - authUrl: process.env.JSON_API_AUTH_URL - }; - } - console.log('Connecting to', uri); - await mongoose.connect(uri, jsonApiConnectOptions); + await connect(); // Login to Discord with your client's token client.login(token); } diff --git a/discord-bot/mongoose.js b/discord-bot/mongoose.js index 3ccf714..50e5b2b 100644 --- a/discord-bot/mongoose.js +++ b/discord-bot/mongoose.js @@ -2,7 +2,7 @@ const mongoose = require('mongoose'); -mongoose.set('autoCreate', process.env.MONGOOSE_AUTO_CREATE === 'false' ? false : true); +mongoose.set('autoCreate', false); mongoose.set('autoIndex', false); const { driver } = require('stargate-mongoose'); diff --git a/discord-bot/package.json b/discord-bot/package.json index b042d04..dbefcd8 100644 --- a/discord-bot/package.json +++ b/discord-bot/package.json @@ -4,9 +4,10 @@ "description": "", "main": "index.js", "scripts": { - "clean-db": "env MONGOOSE_AUTO_CREATE=false node ./dropCollections", + "clean-db": "node ./dropCollections", "lint": "eslint .", "test": "mocha test/*.js", + "seed": "node ./seed", "start": "node ./index.js" }, "author": "", @@ -21,7 +22,7 @@ "dotenv": "16.3.1", "eslint": "8.56.0", "mongoose": "^8.1", - "stargate-mongoose": "0.4.2" + "stargate-mongoose": "0.4.3" }, "devDependencies": { "mocha": "10.2.0", diff --git a/discord-bot/seed.js b/discord-bot/seed.js new file mode 100644 index 0000000..498dcf2 --- /dev/null +++ b/discord-bot/seed.js @@ -0,0 +1,24 @@ +'use strict'; + +require('dotenv').config(); + +const Bot = require('./models/bot'); +const connect = require('./connect'); +const mongoose = require('./mongoose'); + +seed().catch(err => { + console.error(err); + process.exit(-1); +}); + +async function seed() { + await connect(); + + const existingCollections = await mongoose.connection.listCollections(); + if (!existingCollections.includes(Bot.collection.collectionName)) { + await Bot.createCollection(); + } + + console.log('Done'); + process.exit(0); +} diff --git a/netlify-functions-ecommerce/dropCollections.js b/netlify-functions-ecommerce/dropCollections.js index 960feff..2a56192 100644 --- a/netlify-functions-ecommerce/dropCollections.js +++ b/netlify-functions-ecommerce/dropCollections.js @@ -2,8 +2,8 @@ require('dotenv').config(); -const models = require('./models'); const connect = require('./connect'); +const mongoose = require('./mongoose'); dropCollections().catch(err => { console.error(err); @@ -13,14 +13,10 @@ dropCollections().catch(err => { async function dropCollections() { await connect(); - for (const Model of Object.values(models)) { - console.log('Dropping', Model.collection.collectionName); - await Model.db.dropCollection(Model.collection.collectionName).catch(err => { - if (err?.errors?.[0]?.message === 'Request failed with status code 504') { - return; - } - throw err; - }); + const collections = await mongoose.connection.listCollections(); + for (const collection of collections) { + console.log('Dropping', collection); + await mongoose.connection.dropCollection(collection); } console.log('Done'); diff --git a/netlify-functions-ecommerce/package.json b/netlify-functions-ecommerce/package.json index 50e654d..133ecab 100644 --- a/netlify-functions-ecommerce/package.json +++ b/netlify-functions-ecommerce/package.json @@ -6,7 +6,7 @@ "dotenv": "16.3.1", "mongoose": "^8.1", "sinon": "17.0.1", - "stargate-mongoose": "0.4.2", + "stargate-mongoose": "0.4.3", "stripe": "14.12.0", "vanillatoasts": "1.6.0", "webpack": "5.x" @@ -23,7 +23,7 @@ }, "scripts": { "build": "node ./frontend/build", - "clean-db": "env MONGOOSE_AUTO_CREATE=false node ./dropCollections", + "clean-db": "node ./dropCollections", "lint": "eslint .", "seed": "node ./seed", "start": "netlify dev --dir public --functions netlify/functions", diff --git a/netlify-functions-ecommerce/seed.js b/netlify-functions-ecommerce/seed.js index 2ed9dfa..499619d 100644 --- a/netlify-functions-ecommerce/seed.js +++ b/netlify-functions-ecommerce/seed.js @@ -4,13 +4,19 @@ require('./config'); const models = require('./models'); const connect = require('./connect'); +const mongoose = require('./mongoose'); async function createProducts() { await connect(); - await Promise.all( - Object.values(models).map(Model => Model.createCollection()) - ); + const existingCollections = await mongoose.connection.listCollections(); + for (const Model of Object.values(models)) { + if (existingCollections.includes(Model.collection.collectionName)) { + continue; + } + console.log('Creating', Model.collection.collectionName); + await Model.createCollection(); + } await Promise.all( Object.values(models).map(Model => Model.deleteMany({})) ); diff --git a/typescript-express-reviews/package.json b/typescript-express-reviews/package.json index 9c75d46..c363f08 100644 --- a/typescript-express-reviews/package.json +++ b/typescript-express-reviews/package.json @@ -6,7 +6,7 @@ "license": "MIT", "scripts": { "build": "tsc", - "clean-db": "env MONGOOSE_AUTO_CREATE=false ts-node src/seed/dropCollections", + "clean-db": "ts-node src/seed/dropCollections", "seed": "ts-node src/seed/seed", "start": "ts-node src/api", "test": "ts-mocha tests/*.test.ts" @@ -20,7 +20,7 @@ "dotenv": "16.3.1", "express": "4.x", "mongoose": "^8.1", - "stargate-mongoose": "0.4.2" + "stargate-mongoose": "0.4.3" }, "devDependencies": { "@types/bcryptjs": "^2.4.2", diff --git a/typescript-express-reviews/src/seed/dropCollections.ts b/typescript-express-reviews/src/seed/dropCollections.ts index 141bb88..81ede6a 100644 --- a/typescript-express-reviews/src/seed/dropCollections.ts +++ b/typescript-express-reviews/src/seed/dropCollections.ts @@ -2,11 +2,7 @@ import dotenv from 'dotenv'; dotenv.config(); import connect from '../models/connect'; - -import Authentication from '../models/authentication'; -import Review from '../models/review'; -import User from '../models/user'; -import Vehicle from '../models/vehicle'; +import mongoose from '../models/mongoose'; dropCollections().catch(err => { console.error(err); @@ -16,14 +12,10 @@ dropCollections().catch(err => { async function dropCollections() { await connect(); - for (const Model of [Authentication, Review, User, Vehicle]) { - console.log('Dropping', Model.collection.collectionName); - await Model.db.dropCollection(Model.collection.collectionName).catch(err => { - if (err?.errors?.[0]?.message === 'Request failed with status code 504') { - return; - } - throw err; - }); + const collections = await mongoose.connection.listCollections() as unknown as string[]; + for (const collection of collections) { + console.log('Dropping', collection); + await mongoose.connection.dropCollection(collection); } console.log('Done'); diff --git a/typescript-express-reviews/src/seed/seed.ts b/typescript-express-reviews/src/seed/seed.ts index b5e04aa..4272987 100644 --- a/typescript-express-reviews/src/seed/seed.ts +++ b/typescript-express-reviews/src/seed/seed.ts @@ -2,7 +2,7 @@ import dotenv from 'dotenv'; dotenv.config(); import connect from '../models/connect'; -import mongoose from 'mongoose'; +import mongoose from '../models/mongoose'; import Authentication from '../models/authentication'; import Review from '../models/review'; @@ -18,10 +18,14 @@ run().catch(err => { async function run() { await connect(); + const existingCollections = await mongoose.connection.listCollections() as unknown as string[]; + for (const Model of Object.values(mongoose.connection.models)) { console.log('Resetting collection', Model.collection.collectionName); // First ensure the collection exists - await mongoose.connection.createCollection(Model.collection.collectionName); + if (!existingCollections.includes(Model.collection.collectionName)) { + await mongoose.connection.createCollection(Model.collection.collectionName); + } // Then make sure the collection is empty await Model.deleteMany({}); }