Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scripts to drop all collections #182

Merged
merged 6 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions discord-bot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions discord-bot/connect.js
Original file line number Diff line number Diff line change
@@ -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);
};
26 changes: 26 additions & 0 deletions discord-bot/dropCollections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

require('dotenv').config();

const connect = require('./connect');
const mongoose = require('./mongoose');

require('./models/bot');

dropCollections().catch(err => {
console.error(err);
process.exit(-1);
});

async function dropCollections() {
await connect();

const collections = await mongoose.connection.listCollections();
for (const collection of collections) {
console.log('Dropping', collection);
await mongoose.connection.dropCollection(collection);
}

console.log('Done');
process.exit(0);
}
26 changes: 2 additions & 24 deletions discord-bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] });
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion discord-bot/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const mongoose = require('mongoose');

mongoose.set('autoCreate', true);
mongoose.set('autoCreate', false);
mongoose.set('autoIndex', false);

const { driver } = require('stargate-mongoose');
Expand Down
6 changes: 4 additions & 2 deletions discord-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"description": "",
"main": "index.js",
"scripts": {
"clean-db": "node ./dropCollections",
"lint": "eslint .",
"test": "mocha test/*.js",
"seed": "node ./seed",
"start": "node ./index.js"
},
"author": "",
Expand All @@ -19,8 +21,8 @@
"discord.js": "14.14.1",
"dotenv": "16.3.1",
"eslint": "8.56.0",
"mongoose": "8.x",
"stargate-mongoose": "0.4.2"
"mongoose": "^8.1",
"stargate-mongoose": "0.4.3"
},
"devDependencies": {
"mocha": "10.2.0",
Expand Down
24 changes: 24 additions & 0 deletions discord-bot/seed.js
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion netlify-functions-ecommerce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
24 changes: 24 additions & 0 deletions netlify-functions-ecommerce/dropCollections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

require('dotenv').config();

const connect = require('./connect');
const mongoose = require('./mongoose');

dropCollections().catch(err => {
console.error(err);
process.exit(-1);
});

async function dropCollections() {
await connect();

const collections = await mongoose.connection.listCollections();
for (const collection of collections) {
console.log('Dropping', collection);
await mongoose.connection.dropCollection(collection);
}

console.log('Done');
process.exit(0);
}
5 changes: 3 additions & 2 deletions netlify-functions-ecommerce/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"license": "MIT",
"dependencies": {
"dotenv": "16.3.1",
"mongoose": "8.x",
"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"
Expand All @@ -23,6 +23,7 @@
},
"scripts": {
"build": "node ./frontend/build",
"clean-db": "node ./dropCollections",
"lint": "eslint .",
"seed": "node ./seed",
"start": "netlify dev --dir public --functions netlify/functions",
Expand Down
16 changes: 13 additions & 3 deletions netlify-functions-ecommerce/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

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();
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({}))
);
const { Product } = models;

await Product.create({
name: 'iPhone 12',
Expand Down
5 changes: 3 additions & 2 deletions typescript-express-reviews/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"scripts": {
"build": "tsc",
"clean-db": "ts-node src/seed/dropCollections",
"seed": "ts-node src/seed/seed",
"start": "ts-node src/api",
"test": "ts-mocha tests/*.test.ts"
Expand All @@ -18,8 +19,8 @@
"bcryptjs": "^2.4.3",
"dotenv": "16.3.1",
"express": "4.x",
"mongoose": "8.x",
"stargate-mongoose": "0.4.2"
"mongoose": "^8.1",
"stargate-mongoose": "0.4.3"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
Expand Down
2 changes: 1 addition & 1 deletion typescript-express-reviews/src/models/mongoose.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mongoose from 'mongoose';

mongoose.set('autoCreate', true);
mongoose.set('autoCreate', false);
mongoose.set('autoIndex', false);

import { driver } from 'stargate-mongoose';
Expand Down
23 changes: 23 additions & 0 deletions typescript-express-reviews/src/seed/dropCollections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import dotenv from 'dotenv';
dotenv.config();

import connect from '../models/connect';
import mongoose from '../models/mongoose';

dropCollections().catch(err => {
console.error(err);
process.exit(-1);
});

async function dropCollections() {
await connect();

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');
process.exit(0);
}
26 changes: 13 additions & 13 deletions typescript-express-reviews/src/seed/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -17,18 +17,18 @@ 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();
}));
// 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);
}));

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
if (!existingCollections.includes(Model.collection.collectionName)) {
await mongoose.connection.createCollection(Model.collection.collectionName);
}
// Then make sure the collection is empty
await Model.deleteMany({});
}

const users = await User.create([
{
Expand Down
Loading