Skip to content

Commit 02249de

Browse files
committed
fix: default to text for encrypt config
1 parent 070c5ea commit 02249de

File tree

5 files changed

+104
-27
lines changed

5 files changed

+104
-27
lines changed

packages/stack/__tests__/schema-builders.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ describe('schema builders', () => {
2323
it('defaults castAs to string', () => {
2424
const col = encryptedColumn('name')
2525
const built = col.build()
26-
expect(built.cast_as).toBe('string')
26+
expect(built.cast_as).toBe('text')
2727
})
2828

2929
it('.dataType("string") sets castAs to string', () => {
3030
const col = encryptedColumn('name').dataType('string')
31-
expect(col.build().cast_as).toBe('string')
31+
expect(col.build().cast_as).toBe('text')
3232
})
3333

3434
it('.dataType("number") sets castAs to number', () => {
@@ -140,7 +140,7 @@ describe('schema builders', () => {
140140
const built = col.build()
141141

142142
expect(built).toEqual({
143-
cast_as: 'string',
143+
cast_as: 'text',
144144
indexes: {
145145
unique: { token_filters: [] },
146146
ore: {},
@@ -152,7 +152,7 @@ describe('schema builders', () => {
152152
const col = encryptedColumn('raw')
153153
const built = col.build()
154154
expect(built).toEqual({
155-
cast_as: 'string',
155+
cast_as: 'text',
156156
indexes: {},
157157
})
158158
})
@@ -199,7 +199,7 @@ describe('schema builders', () => {
199199
expect(built.tableName).toBe('users')
200200
expect(built.columns).toEqual({
201201
email: {
202-
cast_as: 'string',
202+
cast_as: 'text',
203203
indexes: {
204204
unique: { token_filters: [] },
205205
},
@@ -253,7 +253,7 @@ describe('schema builders', () => {
253253
tables: {
254254
users: {
255255
email: {
256-
cast_as: 'string',
256+
cast_as: 'text',
257257
indexes: {
258258
unique: { token_filters: [] },
259259
},
@@ -317,7 +317,7 @@ describe('schema builders', () => {
317317
it('defaults castAs to string', () => {
318318
const value = encryptedField('field')
319319
const built = value.build()
320-
expect(built.cast_as).toBe('string')
320+
expect(built.cast_as).toBe('text')
321321
})
322322

323323
it('.dataType("json").build() produces correct shape', () => {
@@ -362,7 +362,7 @@ describe('schema builders', () => {
362362
expect(built.columns).toHaveProperty('firstName')
363363
expect(built.columns).toHaveProperty('lastName')
364364
expect(built.columns.firstName).toEqual({
365-
cast_as: 'string',
365+
cast_as: 'text',
366366
indexes: {},
367367
})
368368
})

packages/stack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
},
181181
"dependencies": {
182182
"@byteslice/result": "^0.2.0",
183-
"@cipherstash/protect-ffi": "0.20.1",
183+
"@cipherstash/protect-ffi": "0.20.2",
184184
"evlog": "^1.9.0",
185185
"zod": "3.24.2"
186186
},

packages/stack/src/encryption/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,7 @@ export class EncryptionClient {
178178
* @see {@link LockContext}
179179
* @see {@link EncryptOperation}
180180
*/
181-
encrypt(
182-
plaintext: JsPlaintext,
183-
opts: EncryptOptions,
184-
): EncryptOperation {
181+
encrypt(plaintext: JsPlaintext, opts: EncryptOptions): EncryptOperation {
185182
return new EncryptOperation(this.client, plaintext, opts)
186183
}
187184

packages/stack/src/schema/index.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { z } from 'zod'
1515
* - `"number"`
1616
* - `"string"`
1717
* - `"json"`
18+
* - `"text"`
1819
*
1920
* @remarks
2021
* This is a Zod enum used at runtime to validate schema definitions.
@@ -23,8 +24,8 @@ import { z } from 'zod'
2324
* @internal
2425
*/
2526
export const castAsEnum = z
26-
.enum(['bigint', 'boolean', 'date', 'number', 'string', 'json'])
27-
.default('string')
27+
.enum(['bigint', 'boolean', 'date', 'number', 'string', 'json', 'text'])
28+
.default('text')
2829

2930
const tokenFilterSchema = z.object({
3031
kind: z.literal('downcase'),
@@ -141,7 +142,7 @@ export class EncryptedField {
141142

142143
constructor(valueName: string) {
143144
this.valueName = valueName
144-
this.castAsValue = 'string'
145+
this.castAsValue = 'text'
145146
}
146147

147148
/**
@@ -162,13 +163,13 @@ export class EncryptedField {
162163
* ```
163164
*/
164165
dataType(castAs: CastAs) {
165-
this.castAsValue = castAs
166+
this.castAsValue = castAs === 'string' ? 'text' : castAs
166167
return this
167168
}
168169

169170
build() {
170171
return {
171-
cast_as: this.castAsValue,
172+
cast_as: this.castAsValue === 'string' ? 'text' : this.castAsValue,
172173
indexes: {},
173174
}
174175
}
@@ -211,7 +212,7 @@ export class EncryptedColumn {
211212
* ```
212213
*/
213214
dataType(castAs: CastAs) {
214-
this.castAsValue = castAs
215+
this.castAsValue = castAs === 'string' ? 'text' : castAs
215216
return this
216217
}
217218

@@ -337,7 +338,7 @@ export class EncryptedColumn {
337338

338339
build() {
339340
return {
340-
cast_as: this.castAsValue,
341+
cast_as: this.castAsValue === 'string' ? 'text' : this.castAsValue,
341342
indexes: this.indexesValue,
342343
}
343344
}
@@ -595,11 +596,28 @@ export function encryptedField(valueName: string) {
595596
return new EncryptedField(valueName)
596597
}
597598

598-
// ------------------------
599-
// Internal functions
600-
// ------------------------
601-
602-
/** @internal */
599+
/**
600+
* Build an encrypt config from a list of encrypted tables.
601+
*
602+
* @param ...tables - The list of encrypted tables to build the config from.
603+
* @returns An encrypt config object.
604+
*
605+
* @example
606+
* ```typescript
607+
* import { buildEncryptConfig } from "@cipherstash/stack/schema"
608+
*
609+
* const users = encryptedTable("users", {
610+
* email: encryptedColumn("email").equality(),
611+
* })
612+
*
613+
* const orders = encryptedTable("orders", {
614+
* amount: encryptedColumn("amount").dataType("number"),
615+
* })
616+
*
617+
* const config = buildEncryptConfig(users, orders)
618+
* console.log(config)
619+
* ```
620+
*/
603621
export function buildEncryptConfig(
604622
...protectTables: Array<EncryptedTable<EncryptedTableColumn>>
605623
): EncryptConfig {

pnpm-lock.yaml

Lines changed: 64 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)