Skip to content

Commit 838594e

Browse files
committed
feat: clean up the javascript example with jseql
1 parent fee713f commit 838594e

File tree

14 files changed

+181
-156
lines changed

14 files changed

+181
-156
lines changed
File renamed without changes.

examples/javascript/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
# EQL JavaScript Examples
22

3+
All of the JavaScript examples use the [CipherStash JS/TypeScript EQL](https://github.com/cipherstash/jseql) package.
34

5+
These examples are available:
46

7+
- [Drizzle ORM](./apps/drizzle)
8+
- [Prisma](./apps/prisma)
9+
10+
## Setup
11+
12+
Build the shared packages:
13+
14+
```bash
15+
bun run build
16+
```
17+
18+
Install the dependencies:
19+
20+
```bash
21+
bun install
22+
```
23+
24+
## Running the examples
25+
26+
To run the examples, you will need to start CipherStash Proxy and PostgreSQL.
27+
28+
Please set up the [playground environment](../../playground/README.md) to run the the following examples.
529

examples/javascript/apps/drizzle/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
"type": "module",
55
"devDependencies": {
66
"@types/bun": "latest",
7-
"drizzle-kit": "^0.24.2"
7+
"drizzle-kit": "^0.24.2",
8+
"typescript": "^5.0.0"
89
},
910
"scripts": {
1011
"insert": "bun run src/insert.ts",
1112
"select": "bun run src/select.ts"
1213
},
1314
"peerDependencies": {
14-
"typescript": "^5.0.0",
15-
"@cipherstash/eql": "workspace:*",
16-
"@cipherstash/utils": "workspace:*"
15+
"@cipherstash-jseql-examples/utils": "workspace:*"
1716
},
1817
"dependencies": {
18+
"@cipherstash/jseql": "^2.1.0",
1919
"drizzle-orm": "^0.33.0",
2020
"postgres": "^3.4.4"
2121
}
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import { getEmailArg } from "@cipherstash/utils";
2-
import { eqlPayload } from "@cipherstash/eql";
3-
import { db } from "./db";
4-
import { users } from "./schema";
1+
import { getEmailArg } from '@cipherstash-jseql-examples/utils'
2+
import { createEqlPayload } from '@cipherstash/jseql'
3+
import { db } from './db'
4+
import { users } from './schema'
55

66
const email = getEmailArg({
7-
required: true,
8-
});
7+
required: true,
8+
})
99

1010
const sql = db.insert(users).values({
11-
email: email,
12-
email_encrypted: eqlPayload({
13-
plaintext: email,
14-
table: "users",
15-
column: "email_encrypted",
16-
}),
17-
});
11+
email: email,
12+
email_encrypted: createEqlPayload({
13+
plaintext: email || '',
14+
table: 'users',
15+
column: 'email_encrypted',
16+
}),
17+
})
1818

19-
const sqlResult = sql.toSQL();
20-
console.log("[INFO] SQL statement:", sqlResult);
19+
const sqlResult = sql.toSQL()
20+
console.log('[INFO] SQL statement:', sqlResult)
2121

22-
await sql.execute();
22+
await sql.execute()
2323
console.log(
24-
"[INFO] You've inserted a new user with an encrypted email from the plaintext",
25-
email,
26-
);
24+
"[INFO] You've inserted a new user with an encrypted email from the plaintext",
25+
email,
26+
)
2727

28-
process.exit(0);
28+
process.exit(0)
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import type { CsEncryptedV1Schema } from "@cipherstash/eql";
2-
import { customType, pgTable, serial, varchar } from "drizzle-orm/pg-core";
1+
import type { CsPlaintextV1Schema } from '@cipherstash/jseql'
2+
import { customType, pgTable, serial, varchar } from 'drizzle-orm/pg-core'
33

44
const cs_encrypted_v1 = <TData>(name: string) =>
5-
customType<{ data: TData; driverData: string }>({
6-
dataType() {
7-
return "cs_encrypted_v1";
8-
},
9-
toDriver(value: TData): string {
10-
return JSON.stringify(value);
11-
},
12-
})(name);
5+
customType<{ data: TData; driverData: string }>({
6+
dataType() {
7+
return 'cs_encrypted_v1'
8+
},
9+
toDriver(value: TData): string {
10+
return JSON.stringify(value)
11+
},
12+
})(name)
1313

14-
export const users = pgTable("users", {
15-
id: serial("id").primaryKey(),
16-
email: varchar("email").unique(),
17-
email_encrypted: cs_encrypted_v1<CsEncryptedV1Schema>("email_encrypted"),
18-
});
14+
export const users = pgTable('users', {
15+
id: serial('id').primaryKey(),
16+
email: varchar('email').unique(),
17+
email_encrypted: cs_encrypted_v1<CsPlaintextV1Schema>('email_encrypted'),
18+
})
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
import { getEmailArg } from "@cipherstash/utils";
2-
import { cs_match_v1 } from "@cipherstash/eql/drizzle";
3-
import { getPlaintext } from "@cipherstash/eql";
4-
import { db } from "./db";
5-
import { users } from "./schema";
1+
import { getEmailArg } from '@cipherstash-jseql-examples/utils'
2+
import { cs_match_v1 } from '@cipherstash/jseql/drizzle'
3+
import { getPlaintext } from '@cipherstash/jseql'
4+
import { db } from './db'
5+
import { users } from './schema'
66

77
const email = getEmailArg({
8-
required: false,
9-
});
8+
required: false,
9+
})
1010

1111
const sql = db
12-
.select({
13-
email: users.email_encrypted,
14-
})
15-
.from(users);
12+
.select({
13+
email: users.email_encrypted,
14+
})
15+
.from(users)
1616

1717
if (email) {
18-
sql.where(cs_match_v1(users, users.email_encrypted, email));
18+
sql.where(cs_match_v1(users, users.email_encrypted, email))
1919
}
2020

21-
const sqlResult = sql.toSQL();
22-
console.log("[INFO] SQL statement:", sqlResult);
21+
const sqlResult = sql.toSQL()
22+
console.log('[INFO] SQL statement:', sqlResult)
2323

24-
const data = await sql.execute();
25-
console.log("[INFO] All emails have been decrypted by CipherStash Proxy");
24+
const data = await sql.execute()
25+
console.log('[INFO] All emails have been decrypted by CipherStash Proxy')
2626
console.log(
27-
"Emails:",
28-
JSON.stringify(
29-
data.map((row) => getPlaintext(row.email)),
30-
null,
31-
2,
32-
),
33-
);
27+
'Emails:',
28+
JSON.stringify(
29+
data.map((row) => row.email && getPlaintext(row.email)),
30+
null,
31+
2,
32+
),
33+
)
3434

35-
process.exit(0);
35+
process.exit(0)

examples/javascript/apps/prisma/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
"devDependencies": {
66
"@types/bun": "latest",
77
"prisma": "^5.20.0",
8-
"prisma-json-types-generator": "^3.1.1"
8+
"prisma-json-types-generator": "^3.1.1",
9+
"typescript": "^5.0.0"
910
},
1011
"scripts": {
1112
"insert": "bun run src/insert.ts",
1213
"select": "bun run src/select.ts"
1314
},
1415
"peerDependencies": {
15-
"typescript": "^5.0.0",
16-
"@cipherstash/eql": "workspace:*"
16+
"@cipherstash-jseql-examples/utils": "workspace:*"
1717
},
1818
"dependencies": {
19+
"@cipherstash/jseql": "^2.1.0",
1920
"@prisma/client": "^5.20.0",
2021
"postgres": "^3.4.4"
2122
}
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
import { eqlPayload } from "@cipherstash/eql";
2-
import { PrismaClient, Prisma } from "@prisma/client";
1+
import { createEqlPayload } from '@cipherstash/jseql'
2+
import { PrismaClient, Prisma } from '@prisma/client'
33

44
// TODO: Fix dynamic type of the whereEncrypted method
55
export const prisma = new PrismaClient().$extends({
6-
model: {
7-
$allModels: {
8-
async whereEncrypted<T>(
9-
this: T,
10-
column: string,
11-
plaintext: string,
12-
): Promise<T[]> {
13-
const context = Prisma.getExtensionContext(this);
14-
const tableName = context.$name ?? "";
6+
model: {
7+
$allModels: {
8+
async whereEncrypted<T>(
9+
this: T,
10+
column: string,
11+
plaintext: string,
12+
): Promise<T[]> {
13+
const context = Prisma.getExtensionContext(this)
14+
const tableName = context.$name ?? ''
1515

16-
const result = (await prisma.$queryRaw`SELECT current_schema()`) as [
17-
{ current_schema: string },
18-
];
19-
const schema = result[0].current_schema;
16+
const result = (await prisma.$queryRaw`SELECT current_schema()`) as [
17+
{ current_schema: string },
18+
]
19+
const schema = result[0].current_schema
2020

21-
const payload = JSON.stringify(
22-
eqlPayload({
23-
plaintext,
24-
table: tableName,
25-
column,
26-
}),
27-
);
21+
const payload = JSON.stringify(
22+
createEqlPayload({
23+
plaintext,
24+
table: tableName,
25+
column,
26+
}),
27+
)
2828

29-
// TODO: Fix Prisma.raw to prevent SQL injection
30-
return prisma.$queryRaw<
31-
T[]
32-
>`SELECT * FROM "${Prisma.raw(schema)}"."${Prisma.raw(tableName)}" WHERE cs_match_v1(${Prisma.raw(column)}) @> cs_match_v1('${Prisma.raw(payload)}')`;
33-
},
34-
},
35-
},
36-
});
29+
// TODO: Fix Prisma.raw to prevent SQL injection
30+
return prisma.$queryRaw<
31+
T[]
32+
>`SELECT * FROM "${Prisma.raw(schema)}"."${Prisma.raw(tableName)}" WHERE cs_match_v1(${Prisma.raw(column)}) @> cs_match_v1('${Prisma.raw(payload)}')`
33+
},
34+
},
35+
},
36+
})
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import { getEmailArg } from "@cipherstash/utils";
2-
import { eqlPayload } from "@cipherstash/eql";
3-
import { prisma } from "./db";
1+
import { getEmailArg } from '@cipherstash-jseql-examples/utils'
2+
import { createEqlPayload } from '@cipherstash/jseql'
3+
import { prisma } from './db'
44

55
const email = getEmailArg({
6-
required: true,
7-
});
6+
required: true,
7+
})
88

99
await prisma.user.create({
10-
data: {
11-
email: email ?? "test@test.com",
12-
email_encrypted: eqlPayload({
13-
plaintext: email,
14-
table: "users",
15-
column: "email_encrypted",
16-
}),
17-
},
18-
});
10+
data: {
11+
email: email ?? 'test@test.com',
12+
email_encrypted: createEqlPayload({
13+
plaintext: email,
14+
table: 'users',
15+
column: 'email_encrypted',
16+
}),
17+
},
18+
})
1919

2020
console.log(
21-
"[INFO] You've inserted a new user with an encrypted email from the plaintext",
22-
email,
23-
);
21+
"[INFO] You've inserted a new user with an encrypted email from the plaintext",
22+
email,
23+
)
2424

25-
await prisma.$disconnect();
26-
process.exit(0);
25+
await prisma.$disconnect()
26+
process.exit(0)
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import { getPlaintext } from "@cipherstash/eql";
2-
import { prisma } from "./db";
3-
import { getEmailArg } from "@cipherstash/utils";
4-
import type { User } from "@prisma/client";
1+
import { getPlaintext } from '@cipherstash/jseql'
2+
import { prisma } from './db'
3+
import { getEmailArg } from '@cipherstash-jseql-examples/utils'
4+
import type { User } from '@prisma/client'
55

66
const email = getEmailArg({
7-
required: false,
8-
});
7+
required: false,
8+
})
99

10-
let users: User[];
10+
let users: User[]
1111

1212
if (email) {
13-
// TODO: Fix dynamic type of the whereEncrypted method
14-
users = (await prisma.user.whereEncrypted(
15-
"email_encrypted",
16-
email,
17-
)) as unknown as User[];
13+
// TODO: Fix dynamic type of the whereEncrypted method
14+
users = (await prisma.user.whereEncrypted(
15+
'email_encrypted',
16+
email,
17+
)) as unknown as User[]
1818
} else {
19-
users = await prisma.user.findMany();
19+
users = await prisma.user.findMany()
2020
}
2121

22-
console.log("[INFO] All emails have been decrypted by CipherStash Proxy");
22+
console.log('[INFO] All emails have been decrypted by CipherStash Proxy')
2323
console.log(
24-
"Emails:",
25-
JSON.stringify(
26-
users.map((row) => getPlaintext(row.email_encrypted)),
27-
null,
28-
2,
29-
),
30-
);
24+
'Emails:',
25+
JSON.stringify(
26+
users.map((row) => getPlaintext(row.email_encrypted)),
27+
null,
28+
2,
29+
),
30+
)
3131

32-
await prisma.$disconnect();
33-
process.exit(0);
32+
await prisma.$disconnect()
33+
process.exit(0)

0 commit comments

Comments
 (0)