-
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: add parse()/serialize() and change .where()
- Loading branch information
1 parent
e0788a4
commit 144a174
Showing
18 changed files
with
1,932 additions
and
1,778 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"nuxt.isNuxtApp": false | ||
} |
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
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
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,62 @@ | ||
import { SQL, and, asc, desc, eq, gt, lt, or } from "drizzle-orm"; | ||
|
||
import type { CursorConfig } from "./types"; | ||
import { generateSubArrays } from "./utils"; | ||
import { parse } from "./parse"; | ||
import { serialize } from "./serialize"; | ||
|
||
export const generateCursor = (config: CursorConfig) => { | ||
const { cursors = [], primaryCursor } = config; | ||
const orderBy: Array<SQL> = []; | ||
for (const { order = "ASC", schema } of [...cursors, primaryCursor]) { | ||
const fn = order === "ASC" ? asc : desc; | ||
const sql = fn(schema); | ||
orderBy.push(sql); | ||
} | ||
return { | ||
orderBy, | ||
where: (lastPreviousItemData?: Record<string, unknown> | string | null) => { | ||
if (!lastPreviousItemData) { | ||
return undefined; | ||
} | ||
|
||
const data = | ||
typeof lastPreviousItemData === "string" | ||
? parse(config, lastPreviousItemData) | ||
: lastPreviousItemData; | ||
|
||
if (!data) { | ||
return undefined; | ||
} | ||
|
||
const matrix = generateSubArrays([...cursors, primaryCursor]); | ||
|
||
const ors: Array<SQL> = []; | ||
for (const posibilities of matrix) { | ||
const ands: Array<SQL> = []; | ||
for (const cursor of posibilities) { | ||
const lastValue = cursor === posibilities?.at(-1); | ||
const { order = "ASC", schema, key } = cursor; | ||
const fn = order === "ASC" ? gt : lt; | ||
const sql = !lastValue | ||
? eq(schema, data[key]) | ||
: fn(schema, data[key]); | ||
ands.push(sql); | ||
} | ||
const _and = and(...ands); | ||
if (!_and) { | ||
continue; | ||
} | ||
ors.push(_and); | ||
} | ||
const where = or(...ors); | ||
|
||
return where; | ||
}, | ||
parse: (cursor: string) => parse(config, cursor), | ||
serialize: (data?: Record<string, unknown> | null) => | ||
serialize(config, data), | ||
}; | ||
}; | ||
|
||
export default generateCursor; |
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,55 +1,5 @@ | ||
import { AnyColumn, SQL, and, asc, desc, eq, gt, lt, or } from "drizzle-orm"; | ||
|
||
import { generateSubArrays } from "./utils"; | ||
|
||
export type Cursor = { order?: "ASC" | "DESC"; key: string; schema: AnyColumn }; | ||
|
||
export type CursorConfig = { | ||
primaryCursor: Cursor; | ||
cursors?: Array<Cursor>; | ||
}; | ||
|
||
export const generateCursor = ( | ||
{ cursors = [], primaryCursor }: CursorConfig, | ||
lastPreviousItemData?: Record<string, any> | ||
) => { | ||
const orderBy: Array<SQL> = []; | ||
for (const { order = "ASC", schema } of [...cursors, primaryCursor]) { | ||
const fn = order === "ASC" ? asc : desc; | ||
const sql = fn(schema); | ||
orderBy.push(sql); | ||
} | ||
|
||
if (!lastPreviousItemData) { | ||
return { | ||
orderBy, | ||
where: undefined, | ||
}; | ||
} | ||
|
||
const matrix = generateSubArrays([...cursors, primaryCursor]); | ||
|
||
const ors: Array<SQL> = []; | ||
for (const posibilities of matrix) { | ||
const ands: Array<SQL> = []; | ||
for (const cursor of posibilities) { | ||
const lastValue = cursor === posibilities?.at(-1); | ||
const { order = "ASC", schema, key } = cursor; | ||
const fn = order === "ASC" ? gt : lt; | ||
const sql = !lastValue | ||
? eq(schema, lastPreviousItemData[key]) | ||
: fn(schema, lastPreviousItemData[key]); | ||
ands.push(sql); | ||
} | ||
const _and = and(...ands); | ||
if (!_and) { | ||
continue; | ||
} | ||
ors.push(_and); | ||
} | ||
const where = or(...ors); | ||
return { | ||
orderBy, | ||
where, | ||
}; | ||
}; | ||
export * from "./types"; | ||
export * from "./generateCursor"; | ||
export { generateCursor as default } from "./generateCursor"; | ||
export { parse } from "./parse"; | ||
export { serialize } from "./serialize"; |
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,26 @@ | ||
import "@total-typescript/ts-reset"; | ||
|
||
import type { CursorConfig } from "./types"; | ||
|
||
export function parse< | ||
T extends Record<string, unknown> = Record<string, unknown> | ||
>( | ||
{ primaryCursor, cursors = [] }: CursorConfig, | ||
cursor?: string | null | ||
): T | null { | ||
if (!cursor) { | ||
return null; | ||
} | ||
|
||
const keys = [primaryCursor, ...cursors].map((cursor) => cursor.key); | ||
const data = JSON.parse(atob(cursor)) as T; | ||
|
||
const item = keys.reduce((acc, key) => { | ||
const value = data[key]; | ||
acc[key] = value; | ||
return acc; | ||
}, {} as Record<string, unknown>); | ||
return item as T; | ||
} | ||
|
||
export default parse; |
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,25 @@ | ||
import "@total-typescript/ts-reset"; | ||
|
||
import type { CursorConfig } from "./types"; | ||
|
||
export function serialize< | ||
T extends Record<string, unknown> = Record<string, unknown> | ||
>( | ||
{ primaryCursor, cursors = [] }: CursorConfig, | ||
data?: T | null | ||
): string | null { | ||
if (!data) { | ||
return null; | ||
} | ||
|
||
const keys = [primaryCursor, ...cursors].map((cursor) => cursor.key); | ||
const item = keys.reduce((acc, key) => { | ||
const value = data[key]; | ||
acc[key] = value; | ||
return acc; | ||
}, {} as Record<string, unknown>); | ||
|
||
return btoa(JSON.stringify(item)); | ||
} | ||
|
||
export default serialize; |
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,8 @@ | ||
import type { AnyColumn } from "drizzle-orm"; | ||
|
||
export type Cursor = { order?: "ASC" | "DESC"; key: string; schema: AnyColumn }; | ||
|
||
export type CursorConfig = { | ||
primaryCursor: Cursor; | ||
cursors?: Array<Cursor>; | ||
}; |
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
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
Oops, something went wrong.