Skip to content

Commit

Permalink
use new stargate-mongoose setDriver() syntax for better TypeScript su…
Browse files Browse the repository at this point in the history
…pport
  • Loading branch information
vkarpov15 committed Jan 14, 2025
1 parent ca2d372 commit b492052
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
8 changes: 2 additions & 6 deletions typescript-express-reviews/src/models/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const isAstra = process.env.IS_ASTRA ?? '';
const dataAPIURI = process.env.DATA_API_URI ?? '';
const username = process.env.DATA_API_AUTH_USERNAME ?? '';
const password = process.env.DATA_API_AUTH_PASSWORD ?? '';
const authUrl = process.env.DATA_API_AUTH_URL ?? '';

const astraAPIEndpoint = process.env.ASTRA_API_ENDPOINT ?? '';
const astraNamespace = process.env.ASTRA_NAMESPACE ?? '';
Expand All @@ -22,16 +21,13 @@ export default async function connect() {
console.log('Connecting to', uri);
await mongoose.connect(
uri,
{ isAstra: true, level: 'fatal' } as mongoose.ConnectOptions
{ isAstra: true }
);
} else {
console.log('Connecting to', dataAPIURI);
const featureFlags = process.env.DATA_API_TABLES ? ['Feature-Flag-tables'] : [];
await mongoose.connect(
dataAPIURI,
{ username, password, authUrl, featureFlags } as mongoose.ConnectOptions
{ username, password }
);
}
}


11 changes: 5 additions & 6 deletions typescript-express-reviews/src/models/mongoose.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import mongoose from 'mongoose';

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

import { driver } from 'stargate-mongoose';
mongoose.setDriver(driver);

export default mongoose;
const mongooseInstance = new mongoose.Mongoose().setDriver(driver);
mongooseInstance.set('autoCreate', false);
mongooseInstance.set('autoIndex', false);

export default mongooseInstance;
1 change: 0 additions & 1 deletion typescript-express-reviews/src/seed/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ async function run() {
.then(collections => collections.map(c => c.name));

for (const Model of Object.values(mongoose.connection.models)) {

// First ensure the collection exists
if (!existingCollections.includes(Model.collection.collectionName)) {
console.log('Creating collection', Model.collection.collectionName);
Expand Down
21 changes: 10 additions & 11 deletions typescript-express-reviews/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dotenv.config({

import { after, before } from 'mocha';
import connect from '../src/models/connect';
import mongoose from 'mongoose';
import mongoose from '../src/models/mongoose';
import { driver, tableDefinitionFromSchema } from 'stargate-mongoose';

import Authentication from '../src/models/authentication';
Expand All @@ -22,21 +22,20 @@ before(async function() {

await connect();

const connection: driver.Connection = mongoose.connection as unknown as driver.Connection;
if (!process.env.IS_ASTRA) {
await connection.createNamespace(connection.namespace as string);
await mongoose.connection.createNamespace(mongoose.connection.namespace as string);
}

if (process.env.DATA_API_TABLES) {
await connection.dropTable('authentications');
await connection.dropTable('reviews');
await connection.dropTable('users');
await connection.dropTable('vehicles');
await mongoose.connection.dropTable('authentications');
await mongoose.connection.dropTable('reviews');
await mongoose.connection.dropTable('users');
await mongoose.connection.dropTable('vehicles');

await connection.createTable('authentications', tableDefinitionFromSchema(Authentication.schema));
await connection.createTable('reviews', tableDefinitionFromSchema(Review.schema));
await connection.createTable('users', tableDefinitionFromSchema(User.schema));
await connection.createTable('vehicles', tableDefinitionFromSchema(Vehicle.schema));
await mongoose.connection.createTable('authentications', tableDefinitionFromSchema(Authentication.schema));
await mongoose.connection.createTable('reviews', tableDefinitionFromSchema(Review.schema));
await mongoose.connection.createTable('users', tableDefinitionFromSchema(User.schema));
await mongoose.connection.createTable('vehicles', tableDefinitionFromSchema(Vehicle.schema));
} else {
// Make sure all collections are created in Stargate, _after_ calling
// `connect()`. stargate-mongoose doesn't currently support buffering on
Expand Down

0 comments on commit b492052

Please sign in to comment.