Skip to content

Commit c70f9d2

Browse files
committed
Add examples of matching and sorting to apps/drizzle
1 parent 932e06b commit c70f9d2

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

apps/drizzle/src/protect.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,22 @@ const config: EncryptConfig = {
77
users: {
88
email_encrypted: {
99
cast_as: 'text',
10-
indexes: { ore: {}, match: {}, unique: {} },
10+
indexes: {
11+
ore: {},
12+
match: {
13+
tokenizer: {
14+
kind: 'ngram',
15+
token_length: 3
16+
},
17+
token_filters: [
18+
{
19+
kind: 'downcase'
20+
}
21+
],
22+
k: 6,
23+
m: 2048
24+
},
25+
unique: {} },
1126
},
1227
},
1328
},

apps/drizzle/src/select.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,42 @@ import { bindIfParam, sql } from 'drizzle-orm';
66
import type { BinaryOperator, SQL, SQLWrapper } from 'drizzle-orm';
77
import { parseArgs } from 'node:util'
88

9-
const getEmail = () => {
9+
const getArgs = () => {
1010
const { values, positionals } = parseArgs({
1111
args: process.argv,
1212
options: {
13-
email: {
13+
filter: {
1414
type: 'string',
1515
},
16+
op: {
17+
type: 'string',
18+
default: 'match'
19+
},
1620
},
1721
strict: true,
1822
allowPositionals: true,
1923
})
2024

21-
return values.email
25+
return values
2226
}
2327

24-
const email = getEmail()
28+
const {filter, op} = getArgs()
2529

26-
if (!email) {
30+
if (!filter) {
2731
throw new Error('Email is required')
2832
}
2933

34+
const fnForOp: (op: string) => BinaryOperator = op => {
35+
switch (op) {
36+
case 'match':
37+
return csMatch
38+
case 'eq':
39+
return csEq
40+
default:
41+
throw new Error(`unknown op: ${op}`)
42+
}
43+
}
44+
3045
const csEq: BinaryOperator = (left: SQLWrapper, right: unknown): SQL => {
3146
return sql`cs_unique_v1(${left}) = cs_unique_v1(${bindIfParam(right, left)})`;
3247
};
@@ -39,11 +54,11 @@ const csEq: BinaryOperator = (left: SQLWrapper, right: unknown): SQL => {
3954
// return sql`cs_ore_64_8_v1(${left}) < cs_ore_64_8_v1(${bindIfParam(right, left)})`;
4055
// };
4156

42-
// const csMatch: BinaryOperator = (left: SQLWrapper, right: unknown): SQL => {
43-
// return sql`cs_match_v1(${left}) @> cs_match_v1(${bindIfParam(right, left)})`;
44-
// };
57+
const csMatch: BinaryOperator = (left: SQLWrapper, right: unknown): SQL => {
58+
return sql`cs_match_v1(${left}) @> cs_match_v1(${bindIfParam(right, left)})`;
59+
};
4560

46-
const filterInput = await protectClient.encrypt(email, {
61+
const filterInput = await protectClient.encrypt(filter, {
4762
column: 'email_encrypted',
4863
table: 'users',
4964
})
@@ -52,12 +67,15 @@ if (filterInput.failure) {
5267
throw new Error(`[protect]: ${filterInput.failure.message}`)
5368
}
5469

70+
const filterFn = fnForOp(op);
71+
5572
const query = db
5673
.select({
5774
email: users.email_encrypted,
5875
})
5976
.from(users)
60-
.where(csEq(users.email_encrypted, filterInput.data))
77+
.where(filterFn(users.email_encrypted, filterInput.data))
78+
.orderBy(sql`cs_ore_64_8_v1(users.email_encrypted)`)
6179

6280
const sqlResult = query.toSQL()
6381
console.log('[INFO] SQL statement:', sqlResult)

0 commit comments

Comments
 (0)