-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update drizzle peer version and add pg test (#18)
- Loading branch information
1 parent
142fe50
commit bea7bda
Showing
8 changed files
with
450 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pnpm 8.15.2 | ||
nodejs lts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "drizzle-cursor", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Utils for Drizzle ORM cursor based pagination", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
|
@@ -21,7 +21,8 @@ | |
"db:studio": "drizzle-kit studio", | ||
"db:check": "drizzle-kit check:sqlite", | ||
"test:example": "tsx test/example.ts", | ||
"test:example:sqlite": "tsx test/example.sqlite.ts" | ||
"test:example:sqlite": "tsx test/example.sqlite.ts", | ||
"test:example:pg": "tsx test/example.pg.ts" | ||
}, | ||
"keywords": [ | ||
"drizzle", | ||
|
@@ -35,16 +36,18 @@ | |
"author": "Santiago Montoya <[email protected]>", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"drizzle-orm": ">= 0.29.1 < 1" | ||
"drizzle-orm": ">= 0.33.0 < 1" | ||
}, | ||
"devDependencies": { | ||
"@changesets/cli": "^2.27.1", | ||
"@electric-sql/pglite": "^0.2.3", | ||
"@faker-js/faker": "^8.4.1", | ||
"@total-typescript/ts-reset": "^0.5.1", | ||
"@types/better-sqlite3": "^7.6.9", | ||
"@types/node": "^20.11.17", | ||
"base-x": "^4.0.0", | ||
"better-sqlite3": "^9.4.1", | ||
"dayjs": "^1.11.13", | ||
"dotenv": "^16.4.4", | ||
"drizzle-kit": "^0.20.14", | ||
"superjson": "^2.2.1", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import { | ||
pgTable, | ||
text, | ||
serial, | ||
varchar, | ||
time, | ||
timestamp, | ||
date, | ||
} from "drizzle-orm/pg-core"; | ||
import { PGlite } from "@electric-sql/pglite"; | ||
import { drizzle } from "drizzle-orm/pglite"; | ||
import { migrate } from "drizzle-orm/pglite/migrator"; | ||
import path from "node:path"; | ||
import { da, faker } from "@faker-js/faker"; | ||
import generateCursor from "../src"; | ||
import { sql } from "drizzle-orm"; | ||
import dayjs from "dayjs"; | ||
|
||
const usersTable = pgTable("users", { | ||
id: serial("id").primaryKey(), | ||
slug: varchar("slug").unique(), | ||
title: varchar("title"), | ||
content: text("content"), | ||
time1: time("time1"), | ||
time2: time("time2", { withTimezone: true }), | ||
time3: time("time3", { precision: 6 }), | ||
time4: time("time4", { precision: 6, withTimezone: true }), | ||
time5: time("time5").defaultNow(), | ||
timestamp1: timestamp("timestamp1"), | ||
timestamp2: timestamp("timestamp2", { withTimezone: true }), | ||
timestamp3: timestamp("timestamp3", { precision: 6 }), | ||
timestamp4: timestamp("timestamp4", { precision: 6, withTimezone: true }), | ||
timestamp5: timestamp("timestamp5").defaultNow(), | ||
date1: date("date1"), | ||
date2: date("date2", { mode: "string" }), | ||
date3: date("date3", { mode: "date" }), | ||
date4: date("date4").defaultNow(), | ||
date5: date("date5", { mode: "string" }).defaultNow(), | ||
date6: date("date6", { mode: "date" }).defaultNow(), | ||
}); | ||
|
||
const schema = { | ||
users: usersTable, | ||
} as const; | ||
|
||
const client = new PGlite(); | ||
|
||
export const db = drizzle(client, { schema, logger: true }); | ||
const migrationsFolder = path.join(__dirname, "example.pg/migrations"); | ||
|
||
async function main() { | ||
await migrate(db, { migrationsFolder }); | ||
|
||
await db.execute(sql` | ||
CREATE TABLE IF NOT EXISTS users ( | ||
id SERIAL PRIMARY KEY NOT NULL, | ||
slug TEXT UNIQUE, | ||
title TEXT, | ||
content TEXT, | ||
time1 TIME, | ||
time2 TIME WITH TIME ZONE, | ||
time3 TIME(6), | ||
time4 TIME(6) WITH TIME ZONE, | ||
time5 TIME DEFAULT NOW(), | ||
timestamp1 TIMESTAMP, | ||
timestamp2 TIMESTAMP WITH TIME ZONE, | ||
timestamp3 TIMESTAMP(6), | ||
timestamp4 TIMESTAMP(6) WITH TIME ZONE, | ||
timestamp5 TIMESTAMP DEFAULT NOW(), | ||
date1 DATE, | ||
date2 TEXT, | ||
date3 DATE, | ||
date4 DATE DEFAULT NOW(), | ||
date5 TEXT DEFAULT NOW(), | ||
date6 DATE DEFAULT NOW() | ||
); | ||
`); | ||
|
||
const items: (typeof usersTable.$inferInsert)[] = Array.from( | ||
{ length: 100 }, | ||
(_, k) => { | ||
return { | ||
slug: faker.lorem.slug(), | ||
title: faker.lorem.words(), | ||
content: faker.lorem.sentences(), | ||
time1: faker.datatype.boolean() | ||
? null | ||
: dayjs(faker.date.anytime()).format("HH:mm:ss.SSS"), | ||
time2: faker.datatype.boolean() | ||
? null | ||
: dayjs(faker.date.anytime()).format("HH:mm:ss.SSS"), | ||
time3: faker.datatype.boolean() | ||
? null | ||
: dayjs(faker.date.anytime()).format("HH:mm:ss.SSS"), | ||
time4: faker.datatype.boolean() | ||
? null | ||
: dayjs(faker.date.anytime()).format("HH:mm:ss.SSS"), | ||
time5: faker.datatype.boolean() | ||
? null | ||
: dayjs(faker.date.anytime()).format("HH:mm:ss.SSS"), | ||
timestamp1: faker.datatype.boolean() | ||
? null | ||
: faker.date.past({ years: 10 }), | ||
timestamp2: faker.datatype.boolean() | ||
? null | ||
: faker.date.past({ years: 10 }), | ||
timestamp3: faker.datatype.boolean() | ||
? null | ||
: faker.date.past({ years: 10 }), | ||
timestamp4: faker.datatype.boolean() | ||
? null | ||
: faker.date.past({ years: 10 }), | ||
timestamp5: faker.datatype.boolean() | ||
? null | ||
: faker.date.past({ years: 10 }), | ||
date1: faker.datatype.boolean() | ||
? null | ||
: dayjs(faker.date.past({ years: 10 })).format("YYYY-MM-DD"), | ||
date2: faker.datatype.boolean() | ||
? null | ||
: dayjs(faker.date.past({ years: 10 })).format("YYYY-MM-DD"), | ||
date3: faker.datatype.boolean() ? null : faker.date.past({ years: 10 }), | ||
}; | ||
}, | ||
); | ||
|
||
await db.insert(usersTable).values(items); | ||
|
||
// const allItems = await db.query.users.findMany(); | ||
// console.table(allItems); | ||
|
||
const cursor = generateCursor({ | ||
primaryCursor: { key: "id", schema: usersTable.id, order: "ASC" }, | ||
cursors: [ | ||
{ key: "time1", schema: usersTable.time1, order: "ASC" }, | ||
{ key: "time2", schema: usersTable.time2, order: "ASC" }, | ||
{ key: "time3", schema: usersTable.time3, order: "ASC" }, | ||
{ key: "time4", schema: usersTable.time4, order: "ASC" }, | ||
{ key: "time5", schema: usersTable.time5, order: "ASC" }, | ||
{ key: "timestamp1", schema: usersTable.timestamp1, order: "ASC" }, | ||
{ key: "timestamp2", schema: usersTable.timestamp2, order: "ASC" }, | ||
{ key: "timestamp3", schema: usersTable.timestamp3, order: "ASC" }, | ||
{ key: "timestamp4", schema: usersTable.timestamp4, order: "ASC" }, | ||
{ key: "timestamp5", schema: usersTable.timestamp5, order: "ASC" }, | ||
{ key: "date1", schema: usersTable.date1, order: "ASC" }, | ||
{ key: "date2", schema: usersTable.date2, order: "ASC" }, | ||
{ key: "date3", schema: usersTable.date3, order: "ASC" }, | ||
], | ||
}); | ||
|
||
const PAGE_SIZE = 10; | ||
|
||
const data1 = await db | ||
.select() | ||
.from(usersTable) | ||
.orderBy(...cursor.orderBy) | ||
.where(cursor.where()) | ||
.limit(PAGE_SIZE); | ||
console.table(data1); | ||
|
||
const last1 = data1.at(-1); | ||
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | ||
console.table(last1); | ||
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | ||
|
||
const data2 = await db | ||
.select() | ||
.from(usersTable) | ||
.orderBy(...cursor.orderBy) | ||
.where(cursor.where(last1)) | ||
.limit(PAGE_SIZE); | ||
|
||
console.table(data2); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("❌ test failed"); | ||
console.error(err); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"version": "5", | ||
"dialect": "postgresql", | ||
"entries": [ | ||
] | ||
} |
Oops, something went wrong.