Skip to content

Commit

Permalink
fixes #1036
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Jun 15, 2024
1 parent a65a7f3 commit d45a8fa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
31 changes: 24 additions & 7 deletions src/util/object-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,20 @@ export function isArrayBufferOrView(
}

export function isPlainObject(obj: unknown): obj is Record<string, unknown> {
return (
isObject(obj) &&
!Array.isArray(obj) &&
!isDate(obj) &&
!isBuffer(obj) &&
!isArrayBufferOrView(obj)
)
if (!isObject(obj) || getTag(obj) !== '[object Object]') {
return false
}

if (Object.getPrototypeOf(obj) === null) {
return true
}

let proto = obj
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
}

return Object.getPrototypeOf(obj) === proto
}

export function getLast<T>(arr: ArrayLike<T>): T | undefined {
Expand Down Expand Up @@ -169,3 +176,13 @@ function compareGenericObjects(

return true
}

const toString = Object.prototype.toString

function getTag(value: unknown): string {
if (value == null) {
return value === undefined ? '[object Undefined]' : '[object Null]'
}

return toString.call(value)
}
44 changes: 37 additions & 7 deletions test/node/src/camel-case.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { CamelCasePlugin, Generated, Kysely, RawBuilder, sql } from '../../../'
import {
CamelCasePlugin,
Generated,
Kysely,
ParseJSONResultsPlugin,
RawBuilder,
sql,
} from '../../../'

import {
destroyTest,
Expand Down Expand Up @@ -78,7 +85,7 @@ for (const dialect of DIALECTS) {

// Can't run this test on SQLite because we can't access the same database
// from the other Kysely instance.
if (dialect === 'postgres' || dialect === 'mysql' || dialect === 'mssql') {
if (dialect !== 'sqlite') {
it('should have created the table and its columns in snake_case', async () => {
const result = await sql<any>`select * from camel_person`.execute(
ctx.db,
Expand Down Expand Up @@ -262,18 +269,41 @@ for (const dialect of DIALECTS) {
})
})

it('should respect maintainNestedObjectKeys', async () => {
const data = await camelDb
.withoutPlugins()
.withPlugin(new CamelCasePlugin({ maintainNestedObjectKeys: true }))
it('should map nested objects by default', async () => {
let db = camelDb.withoutPlugins()

if (dialect === 'mssql' || dialect === 'sqlite') {
db = db.withPlugin(new ParseJSONResultsPlugin())
}

db = db.withPlugin(new CamelCasePlugin())

const data = await db
.selectFrom('camelPerson')
.selectAll()
.executeTakeFirstOrThrow()

expect(data.preferences).to.eql({
disableEmails: true,
})
})

it('should respect maintainNestedObjectKeys', async () => {
let db = camelDb.withoutPlugins()

if (dialect === 'mssql' || dialect === 'sqlite') {
data.preferences = JSON.parse(data.preferences.toString())
db = db.withPlugin(new ParseJSONResultsPlugin())
}

db = db.withPlugin(
new CamelCasePlugin({ maintainNestedObjectKeys: true }),
)

const data = await db
.selectFrom('camelPerson')
.selectAll()
.executeTakeFirstOrThrow()

expect(data.preferences).to.eql({
disable_emails: true,
})
Expand Down

0 comments on commit d45a8fa

Please sign in to comment.