Skip to content

Commit

Permalink
Prevent computed columns from getting into default select, and some o…
Browse files Browse the repository at this point in the history
…ther snake_case related fixes (#404)
  • Loading branch information
romeerez committed Sep 21, 2024
1 parent 59a5892 commit 1bfe969
Show file tree
Hide file tree
Showing 50 changed files with 1,070 additions and 919 deletions.
7 changes: 7 additions & 0 deletions .changeset/smart-trains-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'rake-db': patch
'pqb': patch
'orchid-orm': patch
---

Prevent computed columns from getting into default select, and some other snake_case related fixes (#404)
28 changes: 18 additions & 10 deletions packages/orm/src/baseTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
useTestORM,
} from './test-utils/orm.test-utils';
import path from 'path';
import { getCallerFilePath } from 'orchid-core';
import { getCallerFilePath, pick } from 'orchid-core';
import {
asMock,
assertType,
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('baseTable', () => {
readonly table = 'user';
columns = this.setColumns((t) => ({
id: t.identity().primaryKey(),
createdAt: t.timestamp(),
createdAt: t.name('created_at').timestamp(),
}));
}

Expand All @@ -172,6 +172,7 @@ describe('baseTable', () => {
await db.user.create(userData);

const BaseTable = createBaseTable({
snakeCase: true,
columnTypes: (t) => ({
identity: t.identity,
timestamp() {
Expand Down Expand Up @@ -296,6 +297,7 @@ describe('baseTable', () => {
it('should produce custom SQL for timestamps when updating', () => {
const nowSQL = `now() AT TIME ZONE 'UTC'`;
const BaseTable = createBaseTable({
snakeCase: true,
nowSQL,
});

Expand All @@ -319,7 +321,7 @@ describe('baseTable', () => {
expectSql(
user.find(1).update({}).toSQL(),
`
UPDATE "user" SET "updatedAt" = (now() AT TIME ZONE 'UTC') WHERE "user"."id" = $1
UPDATE "user" SET "updated_at" = (now() AT TIME ZONE 'UTC') WHERE "user"."id" = $1
`,
[1],
);
Expand Down Expand Up @@ -568,7 +570,7 @@ describe('baseTable', () => {
Id: t.name('id').identity().primaryKey(),
Name: t.name('name').text(),
Password: t.name('password').text(),
UserKey: t.name('userKey').text().nullable(),
UserKey: t.name('user_key').text().nullable(),
}));

computed = this.setComputed((q) => ({
Expand Down Expand Up @@ -597,8 +599,8 @@ describe('baseTable', () => {
readonly table = 'profile';
columns = this.setColumns((t) => ({
Id: t.name('id').identity().primaryKey(),
ProfileKey: t.name('profileKey').text(),
UserId: t.name('userId').integer().nullable(),
ProfileKey: t.name('profile_key').text(),
UserId: t.name('user_id').integer().nullable(),
}));

relations = {
Expand All @@ -619,8 +621,14 @@ describe('baseTable', () => {

let userId = 0;
beforeAll(async () => {
userId = await local.user.get('Id').insert(userData);
await local.profile.insert({ ...profileData, UserId: userId });
userId = await local.user
.get('Id')
.insert(pick(userData, ['Name', 'Password', 'UserKey']));

await local.profile.insert({
ProfileKey: profileData.ProfileKey,
UserId: userId,
});
});

describe('select', () => {
Expand Down Expand Up @@ -706,14 +714,14 @@ describe('baseTable', () => {
expectSql(
local.user.toSQL(),
`
SELECT * FROM "user" WHERE ("user"."deletedAt" IS NULL)
SELECT "id", "deleted_at" AS "deletedAt" FROM "user" WHERE ("user"."deleted_at" IS NULL)
`,
);

expectSql(
local.user.includeDeleted().toSQL(),
`
SELECT * FROM "user"
SELECT "id", "deleted_at" AS "deletedAt" FROM "user"
`,
);

Expand Down
6 changes: 3 additions & 3 deletions packages/orm/src/orm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ describe('orm', () => {
q.toSQL(),
`SELECT * FROM (
SELECT
"user"."createdAt",
"user"."created_at" "createdAt",
"user"."name" "alias",
"messagesCount".r "messagesCount"
FROM "user"
LEFT JOIN LATERAL (
SELECT count(*) r
FROM "message" "messages"
WHERE "messages"."authorId" = "user"."id"
AND "messages"."messageKey" = "user"."userKey"
WHERE "messages"."author_id" = "user"."id"
AND "messages"."message_key" = "user"."user_key"
) "messagesCount" ON true
) "user"
WHERE "user"."messagesCount" >= $1`,
Expand Down
92 changes: 46 additions & 46 deletions packages/orm/src/relations/belongsTo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('belongsTo', () => {
`
SELECT ${userSelectAll} FROM "user"
WHERE "user"."id" = $1
AND "user"."userKey" = $2
AND "user"."user_key" = $2
`,
[UserId, 'key'],
);
Expand Down Expand Up @@ -75,8 +75,8 @@ describe('belongsTo', () => {
WHERE EXISTS (
SELECT 1 FROM "profile"
WHERE "profile"."bio" = $1
AND "profile"."userId" = "user"."id"
AND "profile"."profileKey" = "user"."userKey"
AND "profile"."user_id" = "user"."id"
AND "profile"."profile_key" = "user"."user_key"
)
AND "user"."name" = $2
`,
Expand Down Expand Up @@ -112,11 +112,11 @@ describe('belongsTo', () => {
SELECT 1
FROM "postTag"
WHERE "postTag"."tag" = $1
AND "postTag"."postId" = "post"."id"
AND "postTag"."post_id" = "post"."id"
)
AND "post"."body" = $2
AND "post"."userId" = "user"."id"
AND "post"."title" = "user"."userKey"
AND "post"."user_id" = "user"."id"
AND "post"."title" = "user"."user_key"
)
AND "user"."name" = $3
`,
Expand All @@ -139,8 +139,8 @@ describe('belongsTo', () => {
.toSQL(),
`
SELECT ${userSelectAll} FROM "user" "u"
WHERE "u"."id" = "p"."userId"
AND "u"."userKey" = "p"."profileKey"
WHERE "u"."id" = "p"."user_id"
AND "u"."user_key" = "p"."profile_key"
`,
);
});
Expand All @@ -156,8 +156,8 @@ describe('belongsTo', () => {
WHERE EXISTS (
SELECT 1 FROM "user"
WHERE "user"."name" = $1
AND "user"."id" = "p"."userId"
AND "user"."userKey" = "p"."profileKey"
AND "user"."id" = "p"."user_id"
AND "user"."user_key" = "p"."profile_key"
)
`,
['name'],
Expand All @@ -172,8 +172,8 @@ describe('belongsTo', () => {
SELECT ${profileSelectAll} FROM "profile" "p"
WHERE EXISTS (
SELECT 1 FROM "user"
WHERE "user"."id" = "p"."userId"
AND "user"."userKey" = "p"."profileKey"
WHERE "user"."id" = "p"."user_id"
AND "user"."user_key" = "p"."profile_key"
AND "user"."name" = $1
)
`,
Expand All @@ -195,12 +195,12 @@ describe('belongsTo', () => {
SELECT 1 FROM "user" AS "sender"
WHERE EXISTS (
SELECT 1 FROM "profile"
WHERE "profile"."userId" = "sender"."id"
AND "profile"."profileKey" = "sender"."userKey"
WHERE "profile"."user_id" = "sender"."id"
AND "profile"."profile_key" = "sender"."user_key"
AND "profile"."bio" = $1
)
AND "sender"."id" = "m"."authorId"
AND "sender"."userKey" = "m"."messageKey"
AND "sender"."id" = "m"."author_id"
AND "sender"."user_key" = "m"."message_key"
)
`,
['bio'],
Expand All @@ -217,12 +217,12 @@ describe('belongsTo', () => {
SELECT ${messageSelectAll} FROM "message" "m"
WHERE EXISTS (
SELECT 1 FROM "user" AS "sender"
WHERE "sender"."id" = "m"."authorId"
AND "sender"."userKey" = "m"."messageKey"
WHERE "sender"."id" = "m"."author_id"
AND "sender"."user_key" = "m"."message_key"
AND EXISTS (
SELECT 1 FROM "profile"
WHERE "profile"."userId" = "sender"."id"
AND "profile"."profileKey" = "sender"."userKey"
WHERE "profile"."user_id" = "sender"."id"
AND "profile"."profile_key" = "sender"."user_key"
AND "profile"."bio" = $1
)
)
Expand All @@ -248,8 +248,8 @@ describe('belongsTo', () => {
SELECT "p"."bio" "Bio", "user"."name" "Name"
FROM "profile" "p"
JOIN "user"
ON "user"."id" = "p"."userId"
AND "user"."userKey" = "p"."profileKey"
ON "user"."id" = "p"."user_id"
AND "user"."user_key" = "p"."profile_key"
AND "user"."name" = $1
`,
['name'],
Expand Down Expand Up @@ -278,8 +278,8 @@ describe('belongsTo', () => {
JOIN "user" AS "u"
ON "u"."name" = $1
AND "u"."age" = $2
AND "u"."id" = "p"."userId"
AND "u"."userKey" = "p"."profileKey"
AND "u"."id" = "p"."user_id"
AND "u"."user_key" = "p"."profile_key"
`,
['name', 20],
);
Expand All @@ -302,8 +302,8 @@ describe('belongsTo', () => {
SELECT ${userSelectAll}
FROM "user" "u"
WHERE "u"."name" = $1
AND "u"."id" = "profile"."userId"
AND "u"."userKey" = "profile"."profileKey"
AND "u"."id" = "profile"."user_id"
AND "u"."user_key" = "profile"."profile_key"
) "u" ON true
WHERE "u"."Name" = $2
`,
Expand Down Expand Up @@ -336,8 +336,8 @@ describe('belongsTo', () => {
SELECT "user"."id" "Id", "user"."name" "Name"
FROM "user"
WHERE "user"."name" = $1
AND "user"."id" = "p"."userId"
AND "user"."userKey" = "p"."profileKey"
AND "user"."id" = "p"."user_id"
AND "user"."user_key" = "p"."profile_key"
) "user" ON true
ORDER BY "user"."Name" ASC
`,
Expand All @@ -364,9 +364,9 @@ describe('belongsTo', () => {
FROM "user"
WHERE EXISTS (
SELECT 1 FROM "post"
WHERE "post"."id" = "postTag"."postId"
AND "post"."userId" = "user"."id"
AND "post"."title" = "user"."userKey"
WHERE "post"."id" = "postTag"."post_id"
AND "post"."user_id" = "user"."id"
AND "post"."title" = "user"."user_key"
)
) "t"
) "items" ON true
Expand All @@ -391,8 +391,8 @@ describe('belongsTo', () => {
LEFT JOIN LATERAL (
SELECT true r
FROM "user"
WHERE "user"."id" = "p"."userId"
AND "user"."userKey" = "p"."profileKey"
WHERE "user"."id" = "p"."user_id"
AND "user"."user_key" = "p"."profile_key"
) "hasUser" ON true
`,
);
Expand Down Expand Up @@ -425,15 +425,15 @@ describe('belongsTo', () => {
LEFT JOIN LATERAL (
SELECT ${userSelectAll}
FROM "user"
WHERE "user"."id" = "profile"."userId"
AND "user"."userKey" = "profile"."profileKey"
WHERE "user"."id" = "profile"."user_id"
AND "user"."user_key" = "profile"."profile_key"
) "user2" ON true
WHERE "user2"."Name" = $1
AND "profile"."userId" = "user"."id"
AND "profile"."profileKey" = "user"."userKey"
AND "profile"."user_id" = "user"."id"
AND "profile"."profile_key" = "user"."user_key"
) "profile2" ON true
WHERE "user"."id" = "profile"."userId"
AND "user"."userKey" = "profile"."profileKey"
WHERE "user"."id" = "profile"."user_id"
AND "user"."user_key" = "profile"."profile_key"
) "user" ON true
`,
['name'],
Expand Down Expand Up @@ -603,17 +603,17 @@ describe('belongsTo', () => {
readonly table = 'user';
columns = this.setColumns((t) => ({
Id: t.name('id').identity().primaryKey(),
UserKey: t.name('userKey').text(),
UserKey: t.name('user_key').text(),
}));
}

class ProfileTable extends BaseTable {
readonly table = 'profile';
columns = this.setColumns((t) => ({
Id: t.name('id').identity().primaryKey(),
ProfileKey: t.name('profileKey').text(),
ProfileKey: t.name('profile_key').text(),
UserId: t
.name('userId')
.name('user_id')
.integer()
.nullable()
.foreignKey(() => UserTable, 'Id'),
Expand Down Expand Up @@ -648,7 +648,7 @@ describe('belongsTo', () => {
expectSql(
q.toSQL(),
`
INSERT INTO "profile"("id", "userId", "profileKey", "bio")
INSERT INTO "profile"("id", "user_id", "profile_key", "bio")
VALUES ($1, $2, $3, $4)
RETURNING ${profileSelectAll}
`,
Expand Down Expand Up @@ -1524,7 +1524,7 @@ describe('belongsTo', () => {
readonly table = 'profile';
columns = this.setColumns((t) => ({
Id: t.name('id').identity().primaryKey(),
UserId: t.name('userId').integer().nullable(),
UserId: t.name('user_id').integer().nullable(),
}));

relations = {
Expand Down Expand Up @@ -1585,8 +1585,8 @@ describe('belongsTo', () => {
SELECT count(*) = $1
FROM "user"
WHERE "user"."name" IN ($2, $3)
AND "user"."id" = "profile"."userId"
AND "user"."userKey" = "profile"."profileKey"
AND "user"."id" = "profile"."user_id"
AND "user"."user_key" = "profile"."profile_key"
)
`,
[1, 'a', 'b'],
Expand Down
Loading

0 comments on commit 1bfe969

Please sign in to comment.