Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 724d02b

Browse files
committed
feat: utils for properly getting Datastore and its options
1 parent edc7530 commit 724d02b

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/migration-templates.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
module.exports = {
44
indexJs: `'use strict'
55
6-
const Datastore = require('datastore-fs')
7-
const log = require('debug')('jsipfs-repo-migrations:migration-{{version}}')
6+
const utils = require('../../src/utils')
7+
const log = require('debug')('repo-migrations:migration-{{version}}')
88
99
async function migrate(repoPath, options, isBrowser) {
10-
const store = new Datastore(repoPath, {extension: '', createIfMissing: false})
10+
const { StorageBackend, storageOptions } = utils.getDatastoreAndOptions(options, 'root')
11+
const store = new StorageBackend(repoPath, storageOptions)
1112
store.open()
1213
1314
try {
@@ -18,7 +19,8 @@ async function migrate(repoPath, options, isBrowser) {
1819
}
1920
2021
async function revert(repoPath, options, isBrowser) {
21-
const store = new Datastore(repoPath, {extension: '', createIfMissing: false})
22+
const { StorageBackend, storageOptions } = utils.getDatastoreAndOptions(options, 'root')
23+
const store = new StorageBackend(repoPath, storageOptions)
2224
store.open()
2325
2426
try {

src/utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
const Datastore = require('datastore-fs')
4+
5+
exports.getDatastoreAndOptions = function getDatastoreAndOptions(options, key, defaultDatastore = Datastore) {
6+
let StorageBackend, storageBackendOptions
7+
if (options !== undefined
8+
&& options['storageBackends'] !== undefined
9+
&& options['storageBackends'][key] !== undefined
10+
) {
11+
StorageBackend = options['storageBackends'][key]
12+
} else {
13+
StorageBackend = defaultDatastore
14+
}
15+
16+
if (options !== undefined
17+
&& options['storageBackendOptions'] !== undefined
18+
&& options['storageBackendOptions'][key] !== undefined
19+
) {
20+
storageBackendOptions = options['storageBackendOptions'][key]
21+
} else {
22+
storageBackendOptions = {}
23+
}
24+
25+
return {
26+
StorageBackend: StorageBackend,
27+
storageOptions: storageBackendOptions
28+
}
29+
}

0 commit comments

Comments
 (0)