MongoS3LibraryStorage is an implementation of the @lumieducation/h5p-server!ILibraryStorage interface that stores the metadata of libraries in MongoDB and the files in S3.
There is another very similar storage class MongoLibraryStorage, which doesn't use S3 and might be better in many use cases, in particular if you S3 service is too slow or if you pay per request.
The implementation depends on these npm packages:
You must add them manually to your application using npm install aws-sdk mongodb
!
You must import the storage implementation:
import {
MongoS3LibraryStorage,
initS3,
initMongo
} from '@lumieducation/h5p-mongos3';
Initialize the storage implementation like this:
const storage = new MongoLibraryStorage(
initS3({
credentials: {
accessKeyId: 's3accesskey', // optional if env. variable is set
secretAccessKey: 's3accesssecret' // optional if env. variable is set
},
endpoint: 'http://127.0.0.1:9000', // optional if env. variable is set
region: 'us-east-1', // optional if env. variable is set
forcePathStyle: true
}),
(
await initMongo(
'mongodb://127.0.0.1:27017', // optional if env. variable is set
'testdb1', // optional if env. variable is set
'root', // optional if env. variable is set
'h5pnodejs' // optional if env. variable is set
)
).collection('h5p'),
{ s3Bucket: 'h5plibrarybucket' }
);
await storage.createIndexes();
You can safely call createIndexes()
every time you start you application, as
MongoDB checks if indexes already exist before it creates new ones.
aws-sdk
npm
package.mongodb
npm package.h5p
to any name you want. If the
collection doesn't exist yet, it will be automatically created.h5plibrarybucket
to any name you want, but you
must specify one. You must create the bucket manually before you can use it.aws-sdk
,
so you can set any custom configuration values you want.maxKeyLength
to the value you need.
It defaults to 1024.The example Express application can be configured to use the MongoDB library storage by setting the environment variables from above and these additional variables:
An example call would be:
MONGODB_URL="mongodb://127.0.0.1:27017" MONGODB_DB=testdb1 MONGODB_USER=root MONGODB_PASSWORD=h5pnodejs LIBRARYSTORAGE=mongos3 LIBRARY_MONGO_COLLECTION=h5p LIBRARY_AWS_S3_BUCKET=h5plibrarybucket npm start
The method MongoLibraryStorage.migrate can be called when you move to a new major version of MongoLibraryStorage:
await storage.migrate(/*from major version*/ 9, /*to major version*/ 10);
Calling this method will migrate the MongoDB collection data. Note that there is no versioning of the MongoDB collection inside MongoDB. It's your job to decide when to call the migration!
There are automated tests in
/test/implementation/db/MongoS3LibraryStorage.test.ts
.
However, these tests will not be called automatically when you run npm run test
or other test calls. The reason is that the tests require a running
MongoDB and S3 instance and thus need more extensive setup. To manually execute
the tests call npm run test:h5p-mongos3
.
To quickly get a functioning MongoDB instance, you can use the Docker Compose file in the scripts directory like this (you obviously must install Docker and Docker Compose first):
docker-compose -f scripts/mongo-s3-docker-compose.yml up -d
This will start a MongoDB server and MinIO instance in containers. Note that the instances will now be started when your system boots. To stop them from doing this and completely wipe all files from your system, execute:
docker-compose -f scripts/mongo-s3-docker-compose.yml down -v