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

133 lines
3.8 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)
* For a react-native database, we use @react-native-async-storage/async-storage
*
* This version is the browser version
*/
const localforage = require('localforage')
3 years ago
const { callbackify } = require('util')
// 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
const existsAsync = async filename => {
try {
const value = await store.getItem(filename)
if (value !== null) return true // Even if value is undefined, localforage returns null
return false
} catch (error) {
return false
}
}
3 years ago
const exists = callbackify(existsAsync)
const renameAsync = async (filename, newFilename) => {
try {
const value = await store.getItem(filename)
if (value === null) await store.removeItem(newFilename)
else {
3 years ago
await store.setItem(newFilename, value)
await store.removeItem(filename)
}
3 years ago
} catch (err) {
console.warn('An error happened while renaming, skip')
}
}
3 years ago
const rename = callbackify(renameAsync)
const writeFileAsync = async (filename, contents, options) => {
// Options do not matter in browser setup
3 years ago
try {
await store.setItem(filename, contents)
} catch (error) {
console.warn('An error happened while writing, skip')
}
}
3 years ago
const writeFile = callbackify(writeFileAsync)
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')
}
}
3 years ago
const appendFile = callbackify(appendFileAsync)
const readFileAsync = async (filename, options) => {
try {
return (await store.getItem(filename)) || ''
} catch (error) {
console.warn('An error happened while reading, skip')
return ''
}
}
3 years ago
const readFile = callbackify(readFileAsync)
const unlinkAsync = async filename => {
try {
await store.removeItem(filename)
} catch (error) {
console.warn('An error happened while unlinking, skip')
}
}
3 years ago
const unlink = callbackify(unlinkAsync)
// Nothing to do, no directories will be used on the browser
3 years ago
const mkdirAsync = (dir, options) => Promise.resolve()
const mkdir = callbackify(mkdirAsync)
4 years ago
// Nothing to do, no data corruption possible in the browser
3 years ago
const ensureDatafileIntegrityAsync = (filename) => Promise.resolve()
const ensureDatafileIntegrity = callbackify(ensureDatafileIntegrityAsync)
3 years ago
const crashSafeWriteFileLinesAsync = async (filename, lines) => {
lines.push('') // Add final new line
3 years ago
await writeFileAsync(filename, lines.join('\n'))
}
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