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.js

86 lines
2.9 KiB

/**
* Way data is stored for this database
* For a Node.js/Node Webkit database it's the file system
* For a browser-side database it's localforage, which uses the best backend available (IndexedDB then WebSQL then localStorage)
*
* This version is the browser version
*/
const localforage = require('localforage')
// 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'
})
4 years ago
const exists = (filename, cback) => {
// eslint-disable-next-line node/handle-callback-err
store.getItem(filename, (err, value) => {
if (value !== null) return cback(true) // Even if value is undefined, localforage returns null
else return cback(false)
})
}
4 years ago
const rename = (filename, newFilename, callback) => {
// eslint-disable-next-line node/handle-callback-err
store.getItem(filename, (err, value) => {
if (value === null) store.removeItem(newFilename, () => callback())
else {
store.setItem(newFilename, value, () => {
store.removeItem(filename, () => callback())
})
}
})
}
4 years ago
const writeFile = (filename, contents, options, callback) => {
// Options do not matter in browser setup
if (typeof options === 'function') { callback = options }
store.setItem(filename, contents, () => callback())
}
4 years ago
const appendFile = (filename, toAppend, options, callback) => {
// Options do not matter in browser setup
if (typeof options === 'function') { callback = options }
// eslint-disable-next-line node/handle-callback-err
store.getItem(filename, (err, contents) => {
contents = contents || ''
contents += toAppend
store.setItem(filename, contents, () => callback())
})
}
4 years ago
const readFile = (filename, options, callback) => {
// Options do not matter in browser setup
if (typeof options === 'function') { callback = options }
// eslint-disable-next-line node/handle-callback-err
store.getItem(filename, (err, contents) => callback(null, contents || ''))
}
4 years ago
const unlink = (filename, callback) => {
store.removeItem(filename, () => callback())
}
// Nothing to do, no directories will be used on the browser
4 years ago
const mkdir = (dir, options, callback) => callback()
4 years ago
// Nothing to do, no data corruption possible in the browser
const ensureDatafileIntegrity = (filename, callback) => callback(null)
const crashSafeWriteFileLines = (filename, lines, callback) => {
lines.push('') // Add final new line
writeFile(filename, lines.join('\n'), callback)
}
// Interface
module.exports.exists = exists
module.exports.rename = rename
module.exports.writeFile = writeFile
module.exports.crashSafeWriteFileLines = crashSafeWriteFileLines
module.exports.appendFile = appendFile
module.exports.readFile = readFile
module.exports.unlink = unlink
module.exports.mkdir = mkdir
module.exports.ensureDatafileIntegrity = ensureDatafileIntegrity