global-setup.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /** **********************************************************
  2. * Caution
  3. *
  4. * Module aliases by compilerOptions.paths in tsconfig.json
  5. * are NOT available in setup scripts
  6. *********************************************************** */
  7. import 'tsconfig-paths/register';
  8. import mongoose from 'mongoose';
  9. import { initMongooseGlobalSettings, getMongoUri, mongoOptions } from '@growi/core';
  10. // check env
  11. if (process.env.NODE_ENV !== 'test') {
  12. throw new Error('\'process.env.NODE_ENV\' must be \'test\'');
  13. }
  14. module.exports = async() => {
  15. initMongooseGlobalSettings();
  16. mongoose.connect(getMongoUri(), mongoOptions);
  17. // drop database
  18. await mongoose.connection.dropDatabase();
  19. // init DB
  20. const pageCollection = mongoose.connection.collection('pages');
  21. const userCollection = mongoose.connection.collection('users');
  22. // create global user & rootPage
  23. const globalUser = (await userCollection.insertMany([{ name: 'globalUser', username: 'globalUser', email: 'globalUser@example.com' }]))[0];
  24. await userCollection.insertMany([
  25. { name: 'v5DummyUser1', username: 'v5DummyUser1', email: 'v5DummyUser1@example.com' },
  26. { name: 'v5DummyUser2', username: 'v5DummyUser2', email: 'v5DummyUser2@example.com' },
  27. ]);
  28. await pageCollection.insertMany([{
  29. path: '/',
  30. grant: 1,
  31. creator: globalUser,
  32. lastUpdateUser: globalUser,
  33. }]);
  34. await mongoose.disconnect();
  35. };