w-orm-level
v1.0.24
Published
An operator for level in nodejs.
Maintainers
Readme
w-orm-level
An operator for level in nodejs.
Keypoint
level 無條件寫入、無比較並交換亦無交易,故 insert 的「檢查主鍵不存在與寫入」與 save 的「查找主鍵與更新或插入」,其原子性由本套件自行提供,且跨行程併發無法支援。
LevelDB 對資料庫目錄持有 OS 層之獨佔鎖(LOCK 檔),一個目錄同時只能由一個實例開啟:本行程持有期間,其他行程(以及同一行程內指向同一目錄的第二個實例)一律於開啟時失敗,錯誤碼為 LEVEL_DATABASE_NOT_OPEN。
故跨行程併發不是「保證失效」而是「無從發生」——不會有兩個寫入者同時操作同一目錄,也就不存在計數失準或資料損毀之風險,後開者是明確失敗而非靜默錯亂。
Documentation
To view documentation or get support, visit docs.
Installation
Using npm(ES6 module):
npm i w-orm-levelExample for collection
Link: [dev source code]
import WOrm from './src/WOrmLevel.mjs'
//import WOrm from './dist/w-orm-level.umd.js'
// import w from 'wsemi'
// w.fsDeleteFolder('./_db')
let opt = {
url: './_db',
db: 'worm',
cl: 'users',
}
let rs = [
{
id: 'id-peter',
name: 'peter',
value: 123,
},
{
id: 'id-rosemary',
name: 'rosemary',
value: 123.456,
},
{
id: '',
name: 'kettle',
value: 456,
},
]
let rsm = [
{
id: 'id-peter',
name: 'peter(modify)'
},
{
id: 'id-rosemary',
name: 'rosemary(modify)'
},
{
id: '',
name: 'kettle(modify)'
},
]
let rsa = [
{
id: 'id-rosemary',
name: 'rosemary',
value: 654.321,
},
]
async function test() {
//wo
let wo = WOrm(opt)
//on
wo.on('change', function(mode, data, res) {
console.log('change', mode)
})
wo.on('error', function(mode, data, err) {
console.log('error', mode, err)
})
//delAll
await wo.delAll()
.then(function(msg) {
console.log('delAll then', msg)
})
.catch(function(msg) {
console.log('delAll catch', msg)
})
//insert
await wo.insert(rs)
.then(function(msg) {
console.log('insert then', msg)
})
.catch(function(msg) {
console.log('insert catch', msg)
})
//insert by returnList, 回傳與輸入等長保序之逐筆結果, nInserted為1即該筆為新增
await wo.insert([{ id: 'id-peter' }, { id: 'id-new' }], { returnList: true })
.then(function(msg) {
console.log('insert(returnList) then', msg)
})
.catch(function(msg) {
console.log('insert(returnList) catch', msg)
})
//insertBulk, 全有全無, 任一筆主鍵已存在即整批reject且不寫入任何一筆
await wo.insertBulk([{ id: 'id-bulk1' }, { id: 'id-bulk2' }])
.then(function(msg) {
console.log('insertBulk then', msg)
})
.catch(function(msg) {
console.log('insertBulk catch', msg.toString())
})
//insertBulk by existed id
await wo.insertBulk([{ id: 'id-bulk3' }, { id: 'id-peter' }])
.then(function(msg) {
console.log('insertBulk(existed) then', msg)
})
.catch(function(msg) {
console.log('insertBulk(existed) catch', msg.toString())
})
//save
await wo.save(rsm, { autoInsert: false })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
//selectByPk
let sp = await wo.selectByPk('id-rosemary')
console.log('selectByPk', sp)
//selectByPk by non-existed id
let spn = await wo.selectByPk('id-non-existed')
console.log('selectByPk(non-existed)', spn)
//del by id-new, id-bulk1, id-bulk2
await wo.del([{ id: 'id-new' }, { id: 'id-bulk1' }, { id: 'id-bulk2' }])
.then(function(msg) {
console.log('del(clean) then', msg)
})
.catch(function(msg) {
console.log('del(clean) catch', msg)
})
//select all
let ss = await wo.select()
console.log('select all', ss)
//select
let so = await wo.select({ id: 'id-rosemary' })
console.log('select', so)
//select by $and, $gt, $lt
let spa = await wo.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
console.log('select by $and, $gt, $lt', spa)
//select by $or, $gte, $lte
let spb = await wo.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
console.log('select by $or, $gte, $lte', spb)
//select by $or, $and, $ne, $in, $nin
let spc = await wo.select({ '$or': [{ '$and': [{ value: { '$ne': 123 } }, { value: { '$in': [123, 321, 123.456, 456] } }, { value: { '$nin': [456, 654] } }] }, { '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 400 } }] }] })
console.log('select by $or, $and, $ne, $in, $nin', spc)
// //select by regex //mingo不支援regex
// let sr = await wo.select({ name: { $regex: 'PeT', $options: '$i' } })
// console.log('selectReg', sr)
//save
await wo.save(rsa, { autoInsert: true })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
//del
let d = ss.filter(function(v) {
return v.name === 'kettle'
})
await wo.del(d)
.then(function(msg) {
console.log('del then', msg)
})
.catch(function(msg) {
console.log('del catch', msg)
})
//del by invalid id, 未帶有效主鍵者為該筆ok為0並附err, 整批仍resolve
await wo.del([{ name: 'no-id' }])
.then(function(msg) {
console.log('del(invalid id) then', msg)
})
.catch(function(msg) {
console.log('del(invalid id) catch', msg)
})
//delAll by find
await wo.delAll({ value: { '$gte': 600 } })
.then(function(msg) {
console.log('delAll(find) then', msg)
})
.catch(function(msg) {
console.log('delAll(find) catch', msg)
})
//close, level對資料庫目錄為獨佔鎖, 用畢須關閉方能令該目錄再被開啟
await wo.close()
}
test()
// change delAll
// delAll then { n: 0, nDeleted: 0, ok: 1 }
// change insert
// insert then { n: 3, nInserted: 3, ok: 1 }
// change insert
// insert(returnList) then [ { n: 1, nInserted: 0, ok: 1 }, { n: 1, nInserted: 1, ok: 1 } ]
// change insertBulk
// insertBulk then { n: 2, nInserted: 2, ok: 1 }
// error insertBulk can not insertBulk by existed id[id-peter]
// insertBulk(existed) catch Error: can not insertBulk by existed id[id-peter]
// change save
// save then [
// { n: 1, nInserted: 0, nModified: 1, ok: 1 },
// { n: 1, nInserted: 0, nModified: 1, ok: 1 },
// { n: 0, nInserted: 0, nModified: 0, ok: 1 }
// ]
// selectByPk { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 }
// selectByPk(non-existed) null
// change del
// del(clean) then [
// { n: 1, nDeleted: 1, ok: 1 },
// { n: 1, nDeleted: 1, ok: 1 },
// { n: 1, nDeleted: 1, ok: 1 }
// ]
// select all [
// {
// id: {random id},
// name: 'kettle',
// value: 456
// },
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 }
// ]
// select [ { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 } ]
// select by $and, $gt, $lt [ { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 } ]
// select by $or, $gte, $lte [
// {
// id: {random id},
// name: 'kettle',
// value: 456
// }
// ]
// select by $or, $and, $ne, $in, $nin [
// {
// id: {random id},
// name: 'kettle',
// value: 456
// },
// { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 }
// ]
// change save
// save then [ { n: 1, nInserted: 0, nModified: 1, ok: 1 } ]
// change del
// del then [ { n: 1, nDeleted: 1, ok: 1 } ]
// error del can not delete by invalid id[]
// change del
// del(invalid id) then [ { n: 0, nDeleted: 0, ok: 0, err: 'can not delete by invalid id[]' } ]
// change delAll
// delAll(find) then { n: 1, nDeleted: 1, ok: 1 }