Skip to content

Commit

Permalink
Merge branch 'master' into fix-mysql-create-index-using
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov authored Sep 18, 2023
2 parents 4407884 + 41cbc8b commit 1f993f2
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions test/node/src/json-traversal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ for (const dialect of DIALECTS.filter((dialect) => dialect !== 'mssql')) {
await destroyJSONTest(ctx)
})

if (dialect !== 'postgres') {
if (dialect === 'mysql' || dialect === 'sqlite') {
describe('JSON Path syntax ($)', () => {
const jsonOperator = dialect === 'mysql' ? '->$' : '->>$'

Expand Down Expand Up @@ -345,10 +345,41 @@ for (const dialect of DIALECTS.filter((dialect) => dialect !== 'mssql')) {
expect(results).to.have.length(1)
expect(results[0].profile.auth.login_count).to.equal(12)
})

it(`should execute a query with column${jsonOperator}.key.key in order by clause`, async () => {
const query = ctx.db
.selectFrom('person_metadata')
.orderBy(
(eb) =>
eb.ref('profile', jsonOperator).key('auth').key('login_count'),
'desc'
)
.selectAll()

testSql(query, dialect, {
postgres: NOT_SUPPORTED,
mysql: {
parameters: [],
sql: "select * from `person_metadata` order by `profile`->'$.auth.login_count' desc",
},
mssql: NOT_SUPPORTED,
sqlite: {
parameters: [],
sql: `select * from "person_metadata" order by "profile"->>'$.auth.login_count' desc`,
},
})

const results = await query.execute()

expect(results).to.have.length(3)
expect(results[0].profile.auth.login_count).to.equal(14)
expect(results[1].profile.auth.login_count).to.equal(13)
expect(results[2].profile.auth.login_count).to.equal(12)
})
})
}

if (dialect !== 'mysql') {
if (dialect === 'postgres' || dialect === 'sqlite') {
describe('PostgreSQL-style syntax (->->->>)', () => {
const jsonOperator = dialect === 'postgres' ? '->' : '->>'

Expand Down Expand Up @@ -624,6 +655,37 @@ for (const dialect of DIALECTS.filter((dialect) => dialect !== 'mssql')) {
expect(results).to.have.length(1)
expect(results[0].profile.auth.login_count).to.equal(12)
})

it(`should execute a query with column->key${jsonOperator}key in order by clause`, async () => {
const query = ctx.db
.selectFrom('person_metadata')
.orderBy(
(eb) =>
eb.ref('profile', jsonOperator).key('auth').key('login_count'),
'desc'
)
.selectAll()

testSql(query, dialect, {
postgres: {
parameters: [],
sql: `select * from "person_metadata" order by "profile"->'auth'->'login_count' desc`,
},
mysql: NOT_SUPPORTED,
mssql: NOT_SUPPORTED,
sqlite: {
parameters: [],
sql: `select * from "person_metadata" order by "profile"->'auth'->>'login_count' desc`,
},
})

const results = await query.execute()

expect(results).to.have.length(3)
expect(results[0].profile.auth.login_count).to.equal(14)
expect(results[1].profile.auth.login_count).to.equal(13)
expect(results[2].profile.auth.login_count).to.equal(12)
})
})
}
})
Expand Down

0 comments on commit 1f993f2

Please sign in to comment.