mirror of https://github.com/seald/nedb
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.
87 lines
2.9 KiB
87 lines
2.9 KiB
4 years ago
|
/**
|
||
|
* 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)
|
||
3 years ago
|
* For a react-native database, we use @react-native-async-storage/async-storage
|
||
4 years ago
|
*
|
||
|
* 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) => {
|
||
4 years ago
|
// 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) => {
|
||
4 years ago
|
// 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) => {
|
||
4 years ago
|
// 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) => {
|
||
4 years ago
|
// 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) => {
|
||
4 years ago
|
// 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) => {
|
||
4 years ago
|
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
|
|
||
4 years ago
|
// Nothing to do, no data corruption possible in the browser
|
||
|
const ensureDatafileIntegrity = (filename, callback) => callback(null)
|
||
4 years ago
|
|
||
3 years ago
|
const crashSafeWriteFileLines = (filename, lines, callback) => {
|
||
3 years ago
|
lines.push('') // Add final new line
|
||
3 years ago
|
writeFile(filename, lines.join('\n'), callback)
|
||
|
}
|
||
|
|
||
4 years ago
|
// Interface
|
||
|
module.exports.exists = exists
|
||
|
module.exports.rename = rename
|
||
|
module.exports.writeFile = writeFile
|
||
3 years ago
|
module.exports.crashSafeWriteFileLines = crashSafeWriteFileLines
|
||
4 years ago
|
module.exports.appendFile = appendFile
|
||
|
module.exports.readFile = readFile
|
||
|
module.exports.unlink = unlink
|
||
|
module.exports.mkdir = mkdir
|
||
|
module.exports.ensureDatafileIntegrity = ensureDatafileIntegrity
|