w-orm-reladb
v1.0.59
Published
An operator for relational database in nodejs.
Maintainers
Readme
w-orm-reladb
An operator for relational database in nodejs.
Documentation
To view documentation or get support, visit docs.
insert with option.returnList
An aggregate count answers "how many rows were new", but not which ones — and the latter is what deduplication actually needs, when only new rows should trigger an expensive downstream action. Setting returnList hands back the per row verdict that insert already computes internally:
let rs = await w.insert(data, { returnList: true })
// => [ { n: 1, nInserted: 1, ok: 1 }, { n: 1, nInserted: 0, ok: 1 }, ... ]
let fresh = data.filter((v, k) => rs[k].nInserted === 1)The returned array is the same length as the input and in the same order. Each element carries n: 1 and ok: 1 — any failure in insert is a batch level error and rejects, so a per row element never reports a failure. rs.filter(v => v.nInserted === 1).length equals the nInserted of the default aggregate form, and invalid input returns [] instead of the aggregate empty result.
The switch is static: the shape is decided solely by the value you write at the call site, never by the data or by what happened at run time. Treat the two values as two separate contracts and do not share result handling code between call sites that use different ones.
insert and insertBulk
Both write rows that do not yet exist, and both return { n, nInserted, ok }. They differ in what happens on a conflict, so insertBulk is not a faster insert:
| Situation | insert | insertBulk |
|---|---|---|
| Primary key already exists | that row is skipped, the batch still returns ok: 1 | the whole call rejects, and not a single row is written |
| Duplicate primary keys inside one batch | only the first one counts toward nInserted | treated as a conflict, the whole call rejects |
| nInserted | rows actually inserted, 0 ≤ nInserted ≤ n | equals n whenever the call succeeds |
| Use it for | ordinary writes against a table that may already hold data | bulk import where no conflict is expected |
With no conflict the two are indistinguishable, so you may swap them under that assumption — and if the assumption is wrong, insertBulk tells you by rejecting rather than silently skipping.
insertBulk is considerably faster here, because insert has to write row by row to report an exact nInserted (measured on this machine, Windows 11 / Node 24 / sequelize 6):
| Rows | sqlite insert → insertBulk | mssql insert → insertBulk |
|---|---|---|
| 1000 | 5549 ms → 25 ms (226x) | 6802 ms → 190 ms (36x) |
| 5000 | 29825 ms → 50 ms (592x) | 44116 ms → 489 ms (90x) |
The all-or-nothing guarantee is provided by a transaction, not by the batch statement alone: mssql splits a large batch into several statements because of its bind parameter limit, so without one an interrupted batch would leave earlier rows behind. When you pass option.transaction, a SAVEPOINT is used instead, so a failure rolls back only this call and leaves the rest of your transaction untouched.
Closed instances
init() hands you an instance you can share across calls through option.instance, and instance.close() ends it. Once closed, every function rejects immediately with a message naming the closed state — including selectByPk, which does not fall back to its usual null. This matters because a closed instance and "no such row" would otherwise look identical, and a deduplication caller reading that as "not present yet" would re-run the whole batch downstream. Do not keep using an instance after closing it; call init() again for a new one.
Note also that a plain call made without option.instance opens and closes its own connection, so it ends any instance you are holding. Do not mix the two styles against the same WOrmReladb object.
Concurrency
The scope of the atomicity guarantee, by range:
| Range | Guaranteed | Provided by |
|---|---|---|
| Cross process — several processes against the same database | Yes | The database itself. insert and save write through single conditional statements, and the primary key unique constraint decides the winner. Independent of opt.useStable. |
| Single process — several parallel calls in one process | Yes when opt.useStable is true (the default) | A built-in queue that runs one operation at a time. |
| Single process, with opt.useStable set to false | No | — |
Do not issue parallel calls from one process while opt.useStable is false. Each instance holds one shared connection, opened at the start of a call and closed at its end, so a second call running in parallel closes the connection the first one is still using. Measured on 30 parallel calls in a single process: sqlite raised SQLITE_MISUSE: Database handle is closed and rejected 12 of the batches, mssql reported ConnectionManager.getConnection was called after the connection manager was closed! as a per row failure; both ended with 29 of the 30 rows written, so writes are silently lost. Keep the default true, or serialize the calls yourself (for example await them one by one).
Cross process was measured with 2 independent processes against the same 20 primary keys: nInserted summed to exactly 20 with 20 rows in the table, and with each process writing a different column to those keys all 40 operations returned ok: 1 with both columns preserved on all 20 rows. sqlite was repeated over 6 rounds to rule out timing dependent SQLITE_BUSY. Platform: Windows 11, Node 24, sequelize 6, MSSQL 2022.
Before Installation
If you need to use encrypted sqlite, you need to manually install
@journeyapps/sqlcipher, as follows:
- Open visual studio code by system administrator.
- Open the project folder, and need to make sure the words of path are
asciifor Python 2.7.- Install
windows-build-toolsinto npm global first, and specify withvs2015. Use command to install:npm i -g windows-build-tools --vs2015.- Install
@journeyapps/sqlciphersecond, use command to install:npm i @journeyapps/sqlcipher.
Installation
Using npm(ES6 module):
Note:
@journeyapps/sqlcipheris not compiled into the *.umd file by default, and it is not tied to the dependents for general use in package.json.
npm i w-orm-reladbExample for mssql
Link: [dev source code]
import wo from 'w-orm-reladb'
let username = 'username'
let password = 'password'
let opt = {
url: `mssql://${username}:${password}@localhost:1433`,
db: 'worm',
cl: 'users',
fdModels: './models',
// modelType: 'json',
// autoGenPk: false,
}
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)'
},
]
async function test() {
//w
let w = wo(opt)
//createStorage, create table for mssql
await w.createStorage()
console.log('createStorage')
//genModelsByDB, disable if got models
// await w.genModelsByDB({
// username,
// password,
// dialect: 'mssql', //default
// host: 'localhost', //default
// port: 1433, //default
// db: opt.db,
// fdModels: opt.fdModels,
// })
//on
w.on('change', function(mode, data, res) {
console.log('change', mode)
})
w.on('error', function(mode, data, err) {
console.log('error', mode, err)
})
//delAll
await w.delAll()
.then(function(msg) {
console.log('delAll then', msg)
})
.catch(function(msg) {
console.log('delAll catch', msg)
})
//insert
await w.insert(rs)
.then(function(msg) {
console.log('insert then', msg)
})
.catch(function(msg) {
console.log('insert catch', msg)
})
//save
await w.save(rsm, { autoInsert: false })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
//select all
let ss = await w.select()
console.log('select all', ss)
//select
let so = await w.select({ id: 'id-rosemary' })
console.log('select', so)
//select by $and, $gt, $lt
let spa = await w.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
console.log('select by $and, $gt, $lt', spa)
//select by $or, $gte, $lte
let spb = await w.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 w.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
let sr = await w.select({ name: { $regex: 'PeT', $options: '$i' } })
console.log('selectReg', sr)
//del
let d = []
if (ss) {
d = ss.filter(function(v) {
return v.name !== 'kettle'
})
}
await w.del(d)
.then(function(msg) {
console.log('del then', msg)
})
.catch(function(msg) {
console.log('del catch', msg)
})
}
test()
// createStorage
// change delAll
// delAll then { n: 0, nDeleted: 0, ok: 1 }
// change insert
// insert then { n: 3, nInserted: 3, ok: 1 }
// 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 } //autoInsert=false
// { n: 1, nInserted: 1, nModified: 0, ok: 1 } //autoInsert=true
// ]
// select all [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
// { id: '{random id}', name: 'kettle', value: 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: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
// selectReg [
// { id: 'id-peter', name: 'peter(modify)', value: 123 }
// ]
// change del
// del then [
// { n: 1, nDeleted: 1, ok: 1 },
// { n: 1, nDeleted: 1, ok: 1 }
// ]Example of commit transaction for mssql
Link: [dev source code]
import wo from 'w-orm-reladb'
let username = 'username'
let password = 'password'
let opt = {
url: `mssql://${username}:${password}@localhost:1433`,
db: 'worm',
cl: 'users',
fdModels: './models',
// modelType: 'json',
// autoGenPk: false,
}
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)'
},
]
async function testCommit() {
//w
let w = wo(opt)
//createStorage, create table for mssql
await w.createStorage()
console.log('createStorage')
//genModelsByDB, disable if got models
// await w.genModelsByDB({
// username,
// password,
// dialect: 'mssql', //default
// host: 'localhost', //default
// port: 1433, //default
// db: opt.db,
// fdModels: opt.fdModels,
// })
//on
w.on('change', function(mode, data, res) {
console.log('change', mode)
})
w.on('error', function(mode, data, err) {
console.log('error', mode, err)
})
//delAll
await w.delAll()
.then(function(msg) {
console.log('delAll then', msg)
})
.catch(function(msg) {
console.log('delAll catch', msg)
})
//connState
let instance = await w.init()
let transaction = await w.genTransaction()
let connState = {
instance, //可由外部初始化共用instance, 可單獨使用不另外給transaction
transaction, //若外部使用共用之transaction, 亦需使用共用instance
}
console.log('init')
//insert
await w.insert(rs, connState)
.then(function(msg) {
console.log('insert then', msg)
})
.catch(function(msg) {
console.log('insert catch', msg)
})
//save
await w.save(rsm, { ...connState, autoInsert: false })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
//del
await w.del({ id: 'id-rosemary' }, connState)
.then(function(msg) {
console.log('del then', msg)
})
.catch(function(msg) {
console.log('del catch', msg)
})
//select all
let ssBeforeCommit = await w.select(null, connState)
console.log('select all (before commit)', ssBeforeCommit) //此時select可查到暫時有效的數據
// => [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
//commit
await transaction.commit()
console.log('commit')
//close
await instance.close()
console.log('close')
//select all
let ssFinal = await w.select()
console.log('select all (final)', ssFinal)
// => [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
//check
let rPeter = ssFinal.filter((v) => {
return v.name === 'peter(modify)'
})
let bPeter = rPeter?.[0]?.value === 123
let rRosemary = ssFinal.filter((v) => {
return v.name === 'rosemary(modify)'
})
let bRosemary = rRosemary?.[0]?.value === 123.456
let rKettle = ssFinal.filter((v) => {
return v.name === 'kettle'
})
let bKettle = rKettle?.[0]?.value === 456
if (bPeter && !bRosemary && bKettle) {
console.log('commit success')
}
else {
console.log('commit error')
}
}
testCommit()
// createStorage
// change delAll
// delAll then { n: 2, ok: 1 }
// init
// change insert
// insert then { n: 3, nInserted: 3, ok: 1 }
// 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 } //autoInsert=false
// { n: 1, nInserted: 1, nModified: 0, ok: 1 } //autoInsert=true
// ]
// change del
// del then [
// { n: 1, nDeleted: 1, ok: 1 }
// ]
// select all (before commit) [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
// commit
// close
// select all (final) [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
// commit successExample of rollback transaction for mssql
Link: [dev source code]
import wo from 'w-orm-reladb'
let username = 'username'
let password = 'password'
let opt = {
url: `mssql://${username}:${password}@localhost:1433`,
db: 'worm',
cl: 'users',
fdModels: './models',
// modelType: 'json',
// autoGenPk: false,
}
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)'
},
]
async function testRollback() {
//w
let w = wo(opt)
//createStorage, create table for mssql
await w.createStorage()
console.log('createStorage')
//genModelsByDB, disable if got models
// await w.genModelsByDB({
// username,
// password,
// dialect: 'mssql', //default
// host: 'localhost', //default
// port: 1433, //default
// db: opt.db,
// fdModels: opt.fdModels,
// })
//on
w.on('change', function(mode, data, res) {
console.log('change', mode)
})
w.on('error', function(mode, data, err) {
console.log('error', mode, err)
})
//delAll
await w.delAll()
.then(function(msg) {
console.log('delAll then', msg)
})
.catch(function(msg) {
console.log('delAll catch', msg)
})
//connState
let instance = await w.init()
let transaction = await w.genTransaction()
let connState = {
instance, //可由外部初始化共用instance, 可單獨使用不另外給transaction
transaction, //若外部使用共用之transaction, 亦需使用共用instance
}
console.log('init')
//insert
await w.insert(rs, connState)
.then(function(msg) {
console.log('insert then', msg)
})
.catch(function(msg) {
console.log('insert catch', msg)
})
//save
await w.save(rsm, { ...connState, autoInsert: false })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
//del
await w.del({ id: 'id-rosemary' }, connState)
.then(function(msg) {
console.log('del then', msg)
})
.catch(function(msg) {
console.log('del catch', msg)
})
//select all
let ssBeforeRollback = await w.select(null, connState)
console.log('select all (before rollback)', ssBeforeRollback) //此時select可查到暫時有效的數據
// => [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
//rollback
await transaction.rollback()
console.log('rollback')
//close
await instance.close()
console.log('close')
//select all
let ssFinal = await w.select()
console.log('select all (final)', ssFinal)
// => []
//check
let rPeter = ssFinal.filter((v) => {
return v.name === 'peter(modify)'
})
let bPeter = rPeter?.[0]?.value === 123
let rRosemary = ssFinal.filter((v) => {
return v.name === 'rosemary(modify)'
})
let bRosemary = rRosemary?.[0]?.value === 123.456
let rKettle = ssFinal.filter((v) => {
return v.name === 'kettle'
})
let bKettle = rKettle?.[0]?.value === 456
if (!bPeter && !bRosemary && !bKettle) {
console.log('rollback success')
}
else {
console.log('rollback error')
}
}
testRollback()
// createStorage
// change delAll
// delAll then { n: 0, nDeleted: 0, ok: 1 }
// init
// change insert
// insert then { n: 3, nInserted: 3, ok: 1 }
// 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 } //autoInsert=false
// { n: 1, nInserted: 1, nModified: 0, ok: 1 } //autoInsert=true
// ]
// change del
// del then [
// { n: 1, nDeleted: 1, ok: 1 }
// ]
// select all (before rollback) [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
// rollback
// close
// select all (final) []
// rollback successExample for sqlite
Link: [dev source code]
import fs from 'fs'
import wo from 'w-orm-reladb'
let username = 'username'
let password = 'password'
let opt = {
url: `sqlite://${username}:${password}`,
db: 'worm',
cl: 'users',
fdModels: './models',
// modelType: 'json',
// autoGenPk: false,
storage: './worm.sqlite',
}
//因worm.sqlite可能為加密數據, 若有切換useEncryption時得先刪除, 再通過createStorage重新產生
if (fs.existsSync(opt.storage)) {
fs.unlinkSync(opt.storage)
}
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)'
},
]
async function test() {
//測試sqlite
//w
let w = wo(opt)
//genModelsByDB, disable if got models
// await w.genModelsByDB({
// username,
// password,
// dialect: 'mssql', //default
// host: 'localhost', //default
// port: 1433, //default
// db: opt.db,
// fdModels: opt.fdModels,
// })
//createStorage, create table for sqlite
await w.createStorage()
console.log('createStorage')
//on
w.on('change', function(mode, data, res) {
console.log('change', mode)
})
w.on('error', function(mode, data, err) {
console.log('error', mode, err)
})
//delAll
await w.delAll()
.then(function(msg) {
console.log('delAll then', msg)
})
.catch(function(msg) {
console.log('delAll catch', msg)
})
//insert
await w.insert(rs)
.then(function(msg) {
console.log('insert then', msg)
})
.catch(function(msg) {
console.log('insert catch', msg)
})
//save
await w.save(rsm, { autoInsert: false })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
//select all
let ss = await w.select()
console.log('select all', ss)
//select
let so = await w.select({ id: 'id-rosemary' })
console.log('select', so)
//select by $and, $gt, $lt
let spa = await w.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
console.log('select by $and, $gt, $lt', spa)
//select by $or, $gte, $lte
let spb = await w.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 w.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
let sr = await w.select({ name: { $regex: 'PeT', $options: '$i' } })
console.log('selectReg', sr)
//del
let d = []
if (ss) {
d = ss.filter(function(v) {
return v.name !== 'kettle'
})
}
await w.del(d)
.then(function(msg) {
console.log('del then', msg)
})
.catch(function(msg) {
console.log('del catch', msg)
})
}
test()
// createStorage
// change delAll
// delAll then { n: 0, nDeleted: 0, ok: 1 }
// change insert
// insert then { n: 3, nInserted: 3, ok: 1 }
// 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 } //autoInsert=false
// { n: 1, nInserted: 1, nModified: 0, ok: 1 } //autoInsert=true
// ]
// select all [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
// { id: '{random id}', name: 'kettle', value: 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: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
// selectReg [
// { id: 'id-peter', name: 'peter(modify)', value: 123 }
// ]
// change del
// del then [
// { n: 1, nDeleted: 1, ok: 1 },
// { n: 1, nDeleted: 1, ok: 1 }
// ]Example commit transaction for sqlite
Link: [dev source code]
import fs from 'fs'
import wo from 'w-orm-reladb'
let username = 'username'
let password = 'password'
let opt = {
url: `sqlite://${username}:${password}`,
db: 'worm',
cl: 'users',
fdModels: './models',
// modelType: 'json',
// autoGenPk: false,
storage: './worm.sqlite',
}
//因worm.sqlite可能為加密數據, 若有切換useEncryption時得先刪除, 再通過createStorage重新產生
if (fs.existsSync(opt.storage)) {
fs.unlinkSync(opt.storage)
}
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)'
},
]
async function testCommit() {
//w
let w = wo(opt)
//createStorage, create table for sqlite
await w.createStorage()
console.log('createStorage')
//genModelsByDB, disable if got models
// await w.genModelsByDB({
// username,
// password,
// dialect: 'mssql', //default
// host: 'localhost', //default
// port: 1433, //default
// db: opt.db,
// fdModels: opt.fdModels,
// })
//on
w.on('change', function(mode, data, res) {
console.log('change', mode)
})
w.on('error', function(mode, data, err) {
console.log('error', mode, err)
})
//delAll
await w.delAll()
.then(function(msg) {
console.log('delAll then', msg)
})
.catch(function(msg) {
console.log('delAll catch', msg)
})
//connState
let instance = await w.init()
let transaction = await w.genTransaction()
let connState = {
instance, //可由外部初始化共用instance, 可單獨使用不另外給transaction
transaction, //若外部使用共用之transaction, 亦需使用共用instance
}
console.log('init')
//insert
await w.insert(rs, connState)
.then(function(msg) {
console.log('insert then', msg)
})
.catch(function(msg) {
console.log('insert catch', msg)
})
//save
await w.save(rsm, { ...connState, autoInsert: false })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
//del
await w.del({ id: 'id-rosemary' }, connState)
.then(function(msg) {
console.log('del then', msg)
})
.catch(function(msg) {
console.log('del catch', msg)
})
//select all
let ssBeforeCommit = await w.select(null, connState)
console.log('select all (before commit)', ssBeforeCommit) //此時select可查到暫時有效的數據
// => [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
//commit
await transaction.commit()
console.log('commit')
//close
await instance.close()
console.log('close')
//select all
let ssFinal = await w.select()
console.log('select all (final)', ssFinal)
// => [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
//check
let rPeter = ssFinal.filter((v) => {
return v.name === 'peter(modify)'
})
let bPeter = rPeter?.[0]?.value === 123
let rRosemary = ssFinal.filter((v) => {
return v.name === 'rosemary(modify)'
})
let bRosemary = rRosemary?.[0]?.value === 123.456
let rKettle = ssFinal.filter((v) => {
return v.name === 'kettle'
})
let bKettle = rKettle?.[0]?.value === 456
if (bPeter && !bRosemary && bKettle) {
console.log('commit success')
}
else {
console.log('commit error')
}
}
testCommit()
// createStorage
// change delAll
// delAll then { n: 0, nDeleted: 0, ok: 1 }
// init
// change insert
// insert then { n: 3, nInserted: 3, ok: 1 }
// 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 } //autoInsert=false
// { n: 1, nInserted: 1, nModified: 0, ok: 1 } //autoInsert=true
// ]
// change del
// del then [
// { n: 1, nDeleted: 1, ok: 1 }
// ]
// select all (before commit) [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
// commit
// close
// select all (final) [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
// commit successExample rollback transaction for sqlite
Link: [dev source code]
import fs from 'fs'
import wo from 'w-orm-reladb'
let username = 'username'
let password = 'password'
let opt = {
url: `sqlite://${username}:${password}`,
db: 'worm',
cl: 'users',
fdModels: './models',
// modelType: 'json',
// autoGenPk: false,
storage: './worm.sqlite',
}
//因worm.sqlite可能為加密數據, 若有切換useEncryption時得先刪除, 再通過createStorage重新產生
if (fs.existsSync(opt.storage)) {
fs.unlinkSync(opt.storage)
}
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)'
},
]
async function testRollback() {
//w
let w = wo(opt)
//createStorage, create table for sqlite
await w.createStorage()
console.log('createStorage')
//genModelsByDB, disable if got models
// await w.genModelsByDB({
// username,
// password,
// dialect: 'mssql', //default
// host: 'localhost', //default
// port: 1433, //default
// db: opt.db,
// fdModels: opt.fdModels,
// })
//on
w.on('change', function(mode, data, res) {
console.log('change', mode)
})
w.on('error', function(mode, data, err) {
console.log('error', mode, err)
})
//delAll
await w.delAll()
.then(function(msg) {
console.log('delAll then', msg)
})
.catch(function(msg) {
console.log('delAll catch', msg)
})
//connState
let instance = await w.init()
let transaction = await w.genTransaction()
let connState = {
instance, //可由外部初始化共用instance, 可單獨使用不另外給transaction
transaction, //若外部使用共用之transaction, 亦需使用共用instance
}
console.log('init')
//insert
await w.insert(rs, connState)
.then(function(msg) {
console.log('insert then', msg)
})
.catch(function(msg) {
console.log('insert catch', msg)
})
//save
await w.save(rsm, { ...connState, autoInsert: false })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
//del
await w.del({ id: 'id-rosemary' }, connState)
.then(function(msg) {
console.log('del then', msg)
})
.catch(function(msg) {
console.log('del catch', msg)
})
//select all
let ssBeforeRollback = await w.select(null, connState)
console.log('select all (before rollback)', ssBeforeRollback) //此時select可查到暫時有效的數據
// => [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
//rollback
await transaction.rollback()
console.log('rollback')
//close
await instance.close()
console.log('close')
//select all
let ssFinal = await w.select()
console.log('select all (final)', ssFinal)
// => []
//check
let rPeter = ssFinal.filter((v) => {
return v.name === 'peter(modify)'
})
let bPeter = rPeter?.[0]?.value === 123
let rRosemary = ssFinal.filter((v) => {
return v.name === 'rosemary(modify)'
})
let bRosemary = rRosemary?.[0]?.value === 123.456
let rKettle = ssFinal.filter((v) => {
return v.name === 'kettle'
})
let bKettle = rKettle?.[0]?.value === 456
if (!bPeter && !bRosemary && !bKettle) {
console.log('rollback success')
}
else {
console.log('rollback error')
}
}
testRollback()
// createStorage
// change delAll
// delAll then { n: 0, nDeleted: 0, ok: 1 }
// init
// change insert
// insert then { n: 3, nInserted: 3, ok: 1 }
// 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 } //autoInsert=false
// { n: 1, nInserted: 1, nModified: 0, ok: 1 } //autoInsert=true
// ]
// change del
// del then [
// { n: 1, nDeleted: 1, ok: 1 }
// ]
// select all (before rollback) [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
// rollback
// close
// select all (final) []
// rollback successExample of sqlcipher for sqlite
Link: [dev source code]
import fs from 'fs'
import wo from 'w-orm-reladb'
let username = 'username'
let password = 'password'
let opt = {
url: `sqlite://${username}:${password}`,
db: 'worm',
cl: 'users',
fdModels: './models',
// modelType: 'json',
// autoGenPk: false,
storage: './worm.sqlite',
useEncryption: true,
}
//因worm.sqlite可能為加密數據, 若有切換useEncryption時得先刪除, 再通過createStorage重新產生
if (fs.existsSync(opt.storage)) {
fs.unlinkSync(opt.storage)
}
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)'
},
]
async function test() {
//測試加密sqlite
//安裝@journeyapps/sqlcipher方式:
//1.visual studio code得使用系統管理員權限開啟
//2.開啟專案資料夾, 確定路徑內不能含有中文, 否則python2.7無法接受
//3.先安裝windows-build-tools並指定安裝vs2015, 使用指令安裝至全域: npm i -g windows-build-tools --vs2015
//4.若切換或重新安裝nodejs, 因全域環境不同, 記得得要重裝windows-build-tools
//5.安裝@journeyapps/sqlcipher: npm i @journeyapps/sqlcipher
//w
let w = wo(opt)
//genModelsByDB, disable if got models
// await w.genModelsByDB({
// username,
// password,
// dialect: 'mssql', //default
// host: 'localhost', //default
// port: 1433, //default
// db: opt.db,
// fdModels: opt.fdModels,
// })
//createStorage, create table for sqlite
await w.createStorage()
console.log('createStorage')
//on
w.on('change', function(mode, data, res) {
console.log('change', mode)
})
w.on('error', function(mode, data, err) {
console.log('error', mode, err)
})
//delAll
await w.delAll()
.then(function(msg) {
console.log('delAll then', msg)
})
.catch(function(msg) {
console.log('delAll catch', msg)
})
//insert
await w.insert(rs)
.then(function(msg) {
console.log('insert then', msg)
})
.catch(function(msg) {
console.log('insert catch', msg)
})
//save
await w.save(rsm, { autoInsert: false })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
//select all
let ss = await w.select()
console.log('select all', ss)
//select
let so = await w.select({ id: 'id-rosemary' })
console.log('select', so)
//select by $and, $gt, $lt
let spa = await w.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
console.log('select by $and, $gt, $lt', spa)
//select by $or, $gte, $lte
let spb = await w.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 w.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
let sr = await w.select({ name: { $regex: 'PeT', $options: '$i' } })
console.log('selectReg', sr)
//del
let d = []
if (ss) {
d = ss.filter(function(v) {
return v.name !== 'kettle'
})
}
await w.del(d)
.then(function(msg) {
console.log('del then', msg)
})
.catch(function(msg) {
console.log('del catch', msg)
})
}
test()
// createStorage
// change delAll
// delAll then { n: 0, nDeleted: 0, ok: 1 }
// change insert
// insert then { n: 3, nInserted: 3, ok: 1 }
// 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 } //autoInsert=false
// { n: 1, nInserted: 1, nModified: 0, ok: 1 } //autoInsert=true
// ]
// select all [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
// { id: '{random id}', name: 'kettle', value: 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: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
// selectReg [
// { id: 'id-peter', name: 'peter(modify)', value: 123 }
// ]
// change del
// del then [
// { n: 1, nDeleted: 1, ok: 1 },
// { n: 1, nDeleted: 1, ok: 1 }
// ]Example for genModelsByTabs
Link: [dev source code]
import wo from 'w-orm-reladb'
let username = 'username'
let password = 'password'
let opt = {
url: `mssql://${username}:${password}@localhost:1433`,
db: 'worm',
cl: 'users',
fdModels: './models',
// modelType: 'json',
// autoGenPk: false,
}
let fd = opt.fdModels
let tabs = {
tb1: {
id: {
type: 'STRING', //主鍵不能使用TEXT
pk: true,
},
title: 'TEXT',
price: 'DOUBLE',
isActive: 'INTEGER',
},
tb2: {
sid: {
type: 'STRING', //主鍵不能使用TEXT
pk: true,
},
name: 'TEXT',
size: 'DOUBLE',
age: 'INTEGER',
},
tb3: {
keyINTEGER: 'INTEGER',
keyBIGINT: 'BIGINT',
keyFLOAT: 'FLOAT', //精確度7位
keyDOUBLE: 'DOUBLE', //精確度15~16位
keyDECIMAL: 'DECIMAL', //精確度28~29位
keyDATE: 'DATE',
keyBOOLEAN: 'BOOLEAN',
keySTRING: 'STRING',
keyTEXT: 'TEXT',
},
}
async function test() {
//測試使用數據tabs並呼叫genModelsByTabs來產生models
//w
let w = wo(opt)
//genModelsByTabs, 預設產生js格式的設定檔
w.genModelsByTabs(fd, tabs)
//genModelsByTabs, 產生json格式的設定檔
w.genModelsByTabs(fd, tabs, { type: 'json' })
}
test()
// generate file: ./models/tb1.js
// generate file: ./models/tb2.js
// generate file: ./models/tb3.js
// generate file: ./models/tb1.json
// generate file: ./models/tb2.json
// generate file: ./models/tb3.json
// tb1.json
// {
// table: 'tb1',
// fields: {
// id: {
// type: 'DataTypes.STRING',
// primaryKey: true,
// allowNull: false,
// autoIncrement: false,
// comment: null,
// },
// title: {
// type: 'DataTypes.TEXT',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// price: {
// type: 'DataTypes.DOUBLE',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// isActive: {
// type: 'DataTypes.INTEGER',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// },
// options: {
// tableName: 'tb1',
// },
// }
// tb1.js
// module.exports = function(sequelize, DataTypes) {
// return sequelize.define('tb1', {
// "id": {
// "type": DataTypes.STRING,
// "primaryKey": true,
// "allowNull": false,
// "autoIncrement": false,
// "comment": null
// },
// "title": {
// "type": DataTypes.TEXT,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "price": {
// "type": DataTypes.DOUBLE,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "isActive": {
// "type": DataTypes.INTEGER,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// }
// }, {
// tableName: 'tb1'
// });
// };
// tb2.json
// {
// table: 'tb2',
// fields: {
// sid: {
// type: 'DataTypes.STRING',
// primaryKey: true,
// allowNull: false,
// autoIncrement: false,
// comment: null,
// },
// name: {
// type: 'DataTypes.TEXT',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// size: {
// type: 'DataTypes.DOUBLE',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// age: {
// type: 'DataTypes.INTEGER',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// },
// options: {
// tableName: 'tb2',
// },
// }
// tb2.js
// module.exports = function(sequelize, DataTypes) {
// return sequelize.define('tb2', {
// "sid": {
// "type": DataTypes.STRING,
// "primaryKey": true,
// "allowNull": false,
// "autoIncrement": false,
// "comment": null
// },
// "name": {
// "type": DataTypes.TEXT,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "size": {
// "type": DataTypes.DOUBLE,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "age": {
// "type": DataTypes.INTEGER,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// }
// }, {
// tableName: 'tb2'
// });
// };
// tb3.json
// {
// table: 'tb3',
// fields: {
// keyINTEGER: {
// type: 'DataTypes.INTEGER',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// keyBIGINT: {
// type: 'DataTypes.BIGINT',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// keyFLOAT: {
// type: 'DataTypes.FLOAT',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// keyDOUBLE: {
// type: 'DataTypes.DOUBLE',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// keyDECIMAL: {
// type: 'DataTypes.DECIMAL',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// keyDATE: {
// type: 'DataTypes.DATE',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// keyBOOLEAN: {
// type: 'DataTypes.BOOLEAN',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// keySTRING: {
// type: 'DataTypes.STRING',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// keyTEXT: {
// type: 'DataTypes.TEXT',
// primaryKey: false,
// allowNull: true,
// autoIncrement: false,
// comment: null,
// },
// },
// options: {
// tableName: 'tb3',
// },
// }
// tb3.js
// module.exports = function(sequelize, DataTypes) {
// return sequelize.define('tb3', {
// "keyINTEGER": {
// "type": DataTypes.INTEGER,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "keyBIGINT": {
// "type": DataTypes.BIGINT,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "keyFLOAT": {
// "type": DataTypes.FLOAT,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "keyDOUBLE": {
// "type": DataTypes.DOUBLE,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "keyDECIMAL": {
// "type": DataTypes.DECIMAL,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "keyDATE": {
// "type": DataTypes.DATE,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "keyBOOLEAN": {
// "type": DataTypes.BOOLEAN,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "keySTRING": {
// "type": DataTypes.STRING,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// },
// "keyTEXT": {
// "type": DataTypes.TEXT,
// "primaryKey": false,
// "allowNull": true,
// "autoIncrement": false,
// "comment": null
// }
// }, {
// tableName: 'tb3'
// });
// };