Skip to content

Commit

Permalink
add sql driver example
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinhVu committed Mar 20, 2023
1 parent 62ba695 commit ce81ae2
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/executor.sql.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
describe('executor', () => {
it('sql driver', () => {
const someNativeDriver = {
execQry (qry) {
return `exec query ${qry.trim()}`
},
execNonQry(nonQry) {
return `exec non query ${nonQry.trim()}`
}
}

const makeEntity = (table) => {
let qry = '';
return {
$() {
let q = qry;
qry = '';
return someNativeDriver.execQry(q)
},
$$() {
let q = qry;
qry = '';
return someNativeDriver.execNonQry(q)
},
select(cols) {
const projection = typeof cols === 'string' ? cols : cols.join(', ')
qry += `SELECT ${projection} from ${table}
`;
return this;
},
where(condition) {
qry +=
`WHERE ${condition}
`;
return this;
}
}
}
const sqlDriver = {
user: makeEntity('user')
}

const rs = sqlDriver.user.select(['fullName', 'username']).where('age > 10').$()
expect(rs).toBe('exec query SELECT fullName, username from user\n' +
'WHERE age > 10')
})
})

0 comments on commit ce81ae2

Please sign in to comment.