The JavaScript Database, for Node.js, nw.js, electron and the browser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nedb/browser-version/lib/storage.browser.js

289 lines
8.0 KiB

/**
* Way data is stored for this database
*
3 years ago
* This version is the browser version and uses [localforage]{@link https://github.com/localForage/localForage} which chooses the best option depending on user browser (IndexedDB then WebSQL then localStorage).
* @module storageBrowser
3 years ago
* @see module:storage
* @see module:storageReactNative
*/
const localforage = require('localforage')
const { callbackify } = require('util') // TODO: util is not a dependency, this would fail if util is not polyfilled
// Configure localforage to display NeDB name for now. Would be a good idea to let user use his own app name
const store = localforage.createInstance({
name: 'NeDB',
storeName: 'nedbdata'
})
/**
3 years ago
* Returns Promise<true> if file exists.
*
* Async version of {@link module:storageBrowser.exists}.
* @param {string} file
* @return {Promise<boolean>}
* @async
* @alias module:storageBrowser.existsAsync
3 years ago
* @see module:storageBrowser.exists
*/
const existsAsync = async file => {
3 years ago
try {
const value = await store.getItem(file)
3 years ago
if (value !== null) return true // Even if value is undefined, localforage returns null
return false
} catch (error) {
return false
}
}
/**
* @callback module:storageBrowser~existsCallback
* @param {boolean} exists
*/
/**
3 years ago
* Callback returns true if file exists.
* @function
* @param {string} file
* @param {module:storageBrowser~existsCallback} cb
* @alias module:storageBrowser.exists
*/
3 years ago
const exists = callbackify(existsAsync)
/**
3 years ago
* Async version of {@link module:storageBrowser.rename}.
* @param {string} oldPath
* @param {string} newPath
* @return {Promise<void>}
* @alias module:storageBrowser.renameAsync
* @async
3 years ago
* @see module:storageBrowser.rename
*/
const renameAsync = async (oldPath, newPath) => {
3 years ago
try {
const value = await store.getItem(oldPath)
if (value === null) await store.removeItem(newPath)
else {
await store.setItem(newPath, value)
await store.removeItem(oldPath)
}
3 years ago
} catch (err) {
console.warn('An error happened while renaming, skip')
}
}
/**
* Moves the item from one path to another
* @function
* @param {string} oldPath
* @param {string} newPath
* @param {NoParamCallback} c
* @return {void}
* @alias module:storageBrowser.rename
*/
3 years ago
const rename = callbackify(renameAsync)
/**
3 years ago
* Async version of {@link module:storageBrowser.writeFile}.
* @param {string} file
* @param {string} data
* @param {object} [options]
* @return {Promise<void>}
* @alias module:storageBrowser.writeFileAsync
* @async
3 years ago
* @see module:storageBrowser.writeFile
*/
const writeFileAsync = async (file, data, options) => {
// Options do not matter in browser setup
3 years ago
try {
await store.setItem(file, data)
3 years ago
} catch (error) {
console.warn('An error happened while writing, skip')
}
}
/**
* Saves the item at given path
* @function
* @param {string} path
* @param {string} data
* @param {object} options
* @param {function} callback
* @alias module:storageBrowser.writeFile
*/
3 years ago
const writeFile = callbackify(writeFileAsync)
/**
3 years ago
* Async version of {@link module:storageBrowser.appendFile}.
* @function
* @param {string} filename
* @param {string} toAppend
* @param {object} [options]
* @return {Promise<void>}
* @alias module:storageBrowser.appendFileAsync
* @async
3 years ago
* @see module:storageBrowser.appendFile
*/
3 years ago
const appendFileAsync = async (filename, toAppend, options) => {
// Options do not matter in browser setup
3 years ago
try {
const contents = (await store.getItem(filename)) || ''
await store.setItem(filename, contents + toAppend)
} catch (error) {
console.warn('An error happened appending to file writing, skip')
}
}
/**
* Append to the item at given path
* @function
* @param {string} filename
* @param {string} toAppend
* @param {object} [options]
* @param {function} callback
* @alias module:storageBrowser.appendFile
*/
3 years ago
const appendFile = callbackify(appendFileAsync)
/**
3 years ago
* Async version of {@link module:storageBrowser.readFile}.
* @function
* @param {string} filename
* @param {object} [options]
* @return {Promise<Buffer>}
* @alias module:storageBrowser.readFileAsync
* @async
3 years ago
* @see module:storageBrowser.readFile
*/
3 years ago
const readFileAsync = async (filename, options) => {
try {
return (await store.getItem(filename)) || ''
} catch (error) {
console.warn('An error happened while reading, skip')
return ''
}
}
/**
* Read data at given path
* @function
* @param {string} filename
* @param {object} options
* @param {function} callback
* @alias module:storageBrowser.readFile
*/
3 years ago
const readFile = callbackify(readFileAsync)
/**
3 years ago
* Async version of {@link module:storageBrowser.unlink}.
* @function
* @param {string} filename
* @return {Promise<void>}
* @async
* @alias module:storageBrowser.unlinkAsync
3 years ago
* @see module:storageBrowser.unlink
*/
3 years ago
const unlinkAsync = async filename => {
try {
await store.removeItem(filename)
} catch (error) {
console.warn('An error happened while unlinking, skip')
}
}
/**
* Remove the data at given path
* @function
* @param {string} path
* @param {function} callback
* @alias module:storageBrowser.unlink
*/
3 years ago
const unlink = callbackify(unlinkAsync)
/**
3 years ago
* Shim for {@link module:storage.mkdirAsync}, nothing to do, no directories will be used on the browser.
* @function
* @param {string} path
* @param {object} [options]
* @return {Promise<void|string>}
* @alias module:storageBrowser.mkdirAsync
* @async
*/
const mkdirAsync = (path, options) => Promise.resolve()
/**
3 years ago
* Shim for {@link module:storage.mkdir}, nothing to do, no directories will be used on the browser.
* @function
* @param {string} path
* @param {object} options
* @param {function} callback
* @alias module:storageBrowser.mkdir
*/
3 years ago
const mkdir = callbackify(mkdirAsync)
/**
3 years ago
* Shim for {@link module:storage.ensureDatafileIntegrityAsync}, nothing to do, no data corruption possible in the browser.
* @param {string} filename
* @return {Promise<void>}
* @alias module:storageBrowser.ensureDatafileIntegrityAsync
*/
3 years ago
const ensureDatafileIntegrityAsync = (filename) => Promise.resolve()
/**
3 years ago
* Shim for {@link module:storage.ensureDatafileIntegrity}, nothing to do, no data corruption possible in the browser.
* @function
* @param {string} filename
* @param {NoParamCallback} callback signature: err
* @alias module:storageBrowser.ensureDatafileIntegrity
*/
3 years ago
const ensureDatafileIntegrity = callbackify(ensureDatafileIntegrityAsync)
/**
3 years ago
* Async version of {@link module:storageBrowser.crashSafeWriteFileLines}.
* @param {string} filename
* @param {string[]} lines
* @return {Promise<void>}
* @alias module:storageBrowser.crashSafeWriteFileLinesAsync
3 years ago
* @see module:storageBrowser.crashSafeWriteFileLines
*/
3 years ago
const crashSafeWriteFileLinesAsync = async (filename, lines) => {
lines.push('') // Add final new line
3 years ago
await writeFileAsync(filename, lines.join('\n'))
}
/**
* Fully write or rewrite the datafile, immune to crashes during the write operation (data will not be lost)
* @function
* @param {string} filename
* @param {string[]} lines
* @param {NoParamCallback} [callback] Optional callback, signature: err
* @alias module:storageBrowser.crashSafeWriteFileLines
*/
3 years ago
const crashSafeWriteFileLines = callbackify(crashSafeWriteFileLinesAsync)
// Interface
module.exports.exists = exists
3 years ago
module.exports.existsAsync = existsAsync
module.exports.rename = rename
3 years ago
module.exports.renameAsync = renameAsync
module.exports.writeFile = writeFile
3 years ago
module.exports.writeFileAsync = writeFileAsync
module.exports.crashSafeWriteFileLines = crashSafeWriteFileLines
3 years ago
module.exports.crashSafeWriteFileLinesAsync = crashSafeWriteFileLinesAsync
module.exports.appendFile = appendFile
3 years ago
module.exports.appendFileAsync = appendFileAsync
module.exports.readFile = readFile
3 years ago
module.exports.readFileAsync = readFileAsync
module.exports.unlink = unlink
3 years ago
module.exports.unlinkAsync = unlinkAsync
module.exports.mkdir = mkdir
3 years ago
module.exports.mkdirAsync = mkdirAsync
module.exports.ensureDatafileIntegrity = ensureDatafileIntegrity
3 years ago
module.exports.ensureDatafileIntegrityAsync = ensureDatafileIntegrityAsync