Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PostgreSQL camel case table rows #88

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/query/drivers/pg/PGQueryPostProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const pgQueryPostProcessor = (
if (tokenizedQuery[i + 1] === "?") {
// If the MySQL style "??" is used then pass the parameter in directly as
// PostgreSQL doesn't support column parameter injection
outputQuery += queryParameters[parameterCount]
outputQuery += `"${queryParameters[parameterCount]}"`
parameterCount++
i = i + 1
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/query/drivers/sql/TableRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export abstract class TableRepository<

if (store instanceof PGStore || store instanceof PGTransaction) {
this.postProcessor = pgQueryPostProcessor
this.tableName = `"${this.tableName}"`
}
}

Expand Down Expand Up @@ -76,7 +75,7 @@ export abstract class TableRepository<
} else {
let query = `
INSERT INTO
?? (${Object.keys(filteredPayload).map(() => `"??"`)})
?? (${Object.keys(filteredPayload).map(() => `??`)})
VALUES
(${Object.keys(filteredPayload).map(() => "?")})
RETURNING ??
Expand Down
110 changes: 110 additions & 0 deletions tests/query/drivers/TableRepository.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { expect } from "chai"
import { PGStore } from "../../../src/datastore"
import { Container, inject, injectable } from "inversify"
import { TableRepository } from "../../../src/query"

interface TestModel {
testColumn: string
}

@injectable()
class TestRepository extends TableRepository<TestModel, "testColumn"> {
constructor(@inject(PGStore) store: PGStore) {
super(store, "tableTest", ["testColumn"], "testColumn")
}
}

describe("TableRepository", () => {
const store = new PGStore({
host: process.env.PG_HOST,
port: process.env.PG_PORT ? Number(process.env.PG_PORT) : undefined,
database: process.env.PG_DATABASE,
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
})
const container: Container = new Container()

beforeEach(async () => {
await store.startup(container)
await store.query(
`CREATE TABLE "tableTest" (
"testColumn" VARCHAR
)`,
[]
)
})

afterEach(async () => {
await store.query(`DROP TABLE "tableTest"`, [])

await store.shutdown(container)
})

it("should read all table records", async () => {
let tableRepository = new TestRepository(store)
let values = await tableRepository.read({})

expect(values.length).to.equal(0)
})

it("should insert a record and read it", async () => {
let tableRepository = new TestRepository(store)
await tableRepository.create({
testColumn: "val",
})

let values = await tableRepository.read({
testColumn: "val",
})
expect(values.length).to.equal(1)
})

it("should update a record", async () => {
let tableRepository = new TestRepository(store)
await tableRepository.create({
testColumn: "val",
})

await tableRepository.update(
{
testColumn: "new-val",
},
{
testColumn: "val",
}
)

let values = await tableRepository.read({
testColumn: "new-val",
})
expect(values.length).to.equal(1)
})

it("should delete a record", async () => {
let tableRepository = new TestRepository(store)
await tableRepository.create({
testColumn: "val",
})
await tableRepository.create({
testColumn: "other",
})

let createdValues = await tableRepository.read({
testColumn: "val",
})
expect(createdValues.length).to.equal(1)

await tableRepository.delete({
testColumn: "val",
})

let deletedValues = await tableRepository.read({
testColumn: "val",
})
let remainingValues = await tableRepository.read({
testColumn: "other",
})
expect(deletedValues.length).to.equal(0)
expect(remainingValues.length).to.equal(1)
})
})