generated from productdevbookcom/ts-bundle-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drizzle.ts
85 lines (76 loc) · 2.03 KB
/
drizzle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { unSearch } from 'unsearch'
import type { Column } from 'drizzle-orm'
import {
and,
asc,
desc,
eq,
gt,
gte,
ilike,
like,
lt,
lte,
ne,
or,
} from 'drizzle-orm'
import { pgTable, serial, varchar } from 'drizzle-orm/pg-core'
import postgres from 'postgres'
import { drizzle } from 'drizzle-orm/postgres-js'
const queryClient = postgres('postgres://postgres:[email protected]:5432/db')
const db = drizzle(queryClient)
export const user = pgTable('user', {
id: serial('id').primaryKey(),
username: varchar('username').unique(),
name: varchar('name'),
email: varchar('email'),
})
const columKeys = {
username: user.username,
email: user.email,
id: user.id,
name: user.name,
}
const scopeTags = {
'=': (key: Column, value: string) => eq(key, value),
'!=': (key: Column, value: string) => ne(key, value),
'>': (key: Column, value: string) => gt(key, value),
'>=': (key: Column, value: string) => gte(key, value),
'<': (key: Column, value: string) => lt(key, value),
'<=': (key: Column, value: string) => lte(key, value),
'<like>': (key: Column, value: string) => like(key, `%${value}%`),
'<ilike>': (key: Column, value: string) => ilike(key, `%${value}%`),
'OR': (...args: any[]) => or(...args),
'AND': (...args: any[]) => and(...args),
'asc': (key: Column) => asc(key),
'desc': (key: Column) => desc(key),
}
const scopeTagsArray = ['OR', 'AND', '<like>', '<ilike>', '>=', '<=', '>', '<', '!=', '=']
async function search(query: string) {
const { config } = await unSearch({
columKeys,
scopeTags,
scopeTagsArray,
search: query,
default: {
filterText: Object.keys(columKeys).map((key) => {
return {
column: columKeys[key as keyof typeof columKeys],
filter: '<ilike>',
key,
}
}),
},
})
const data = db.select().from(user)
.where(
or(
...config._data?.wheres || [],
),
)
.orderBy(
...config._data?.orderBy || [],
)
console.warn(data)
}
search('username:foo AND email:bar')