
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
@cipherstash/schema
Advanced tools
A TypeScript schema builder for CipherStash Protect.js that enables you to define encryption schemas with searchable encryption capabilities.
@cipherstash/schema
is a standalone package that provides the schema building functionality used by @cipherstash/protect
. While not required for basic Protect.js usage, this package is available if you need to build encryption configuration schemas directly or want to understand the underlying schema structure.
npm install @cipherstash/schema
# or
yarn add @cipherstash/schema
# or
pnpm add @cipherstash/schema
import { csTable, csColumn, buildEncryptConfig } from '@cipherstash/schema'
// Define your schema
const users = csTable('users', {
email: csColumn('email').freeTextSearch().equality().orderAndRange(),
name: csColumn('name').freeTextSearch(),
age: csColumn('age').orderAndRange(),
})
// Build the encryption configuration
const config = buildEncryptConfig(users)
console.log(config)
csTable(tableName, columns)
Creates a table definition with encrypted columns.
import { csTable, csColumn } from '@cipherstash/schema'
const users = csTable('users', {
email: csColumn('email'),
name: csColumn('name'),
})
csColumn(columnName)
Creates a column definition with configurable indexes and data types.
import { csColumn } from '@cipherstash/schema'
const emailColumn = csColumn('email')
.freeTextSearch() // Enable text search
.equality() // Enable exact matching
.orderAndRange() // Enable sorting and range queries
.dataType('text') // Set data type
csValue(valueName)
Creates a value definition for nested objects (up to 3 levels deep).
import { csTable, csColumn, csValue } from '@cipherstash/schema'
const users = csTable('users', {
email: csColumn('email').equality(),
profile: {
name: csValue('profile.name'),
address: {
street: csValue('profile.address.street'),
location: {
coordinates: csValue('profile.address.location.coordinates'),
},
},
},
})
buildEncryptConfig(...tables)
Builds the final encryption configuration from table definitions.
import { buildEncryptConfig } from '@cipherstash/schema'
const config = buildEncryptConfig(users, orders, products)
.equality()
)Enables exact matching queries.
const emailColumn = csColumn('email').equality()
// SQL equivalent: WHERE email = 'example@example.com'
.freeTextSearch()
)Enables text search with configurable options.
const descriptionColumn = csColumn('description').freeTextSearch({
tokenizer: { kind: 'ngram', token_length: 3 },
token_filters: [{ kind: 'downcase' }],
k: 6,
m: 2048,
include_original: true,
})
// SQL equivalent: WHERE description LIKE '%example%'
.orderAndRange()
)Enables sorting and range queries.
const priceColumn = csColumn('price').orderAndRange()
// SQL equivalent: ORDER BY price ASC, WHERE price > 100
Set the data type for a column using .dataType()
:
const column = csColumn('field')
.dataType('text') // text (default)
.dataType('int') // integer
.dataType('big_int') // big integer
.dataType('small_int') // small integer
.dataType('real') // real number
.dataType('double') // double precision
.dataType('boolean') // boolean
.dataType('date') // date
.dataType('jsonb') // JSON binary
Support for nested object encryption (up to 3 levels deep):
const users = csTable('users', {
email: csColumn('email').equality(),
profile: {
name: csValue('profile.name'),
address: {
street: csValue('profile.address.street'),
city: csValue('profile.address.city'),
location: {
coordinates: csValue('profile.address.location.coordinates'),
},
},
},
})
Note: Nested objects are not searchable and are not recommended for SQL databases. Use separate columns for searchable fields.
const column = csColumn('field').equality([
{ kind: 'downcase' }
])
const column = csColumn('field').freeTextSearch({
tokenizer: { kind: 'standard' },
token_filters: [{ kind: 'downcase' }],
k: 8,
m: 4096,
include_original: false,
})
const column = csColumn('metadata').josn('meta')
The schema builder provides full TypeScript support:
import { csTable, csColumn, type ProtectTableColumn } from '@cipherstash/schema'
const users = csTable('users', {
email: csColumn('email').equality(),
name: csColumn('name').freeTextSearch(),
} as const)
// TypeScript will infer the correct types
type UsersTable = typeof users
While this package can be used standalone, it's typically used through @cipherstash/protect
:
import { csTable, csColumn } from '@cipherstash/protect'
const users = csTable('users', {
email: csColumn('email').equality().freeTextSearch(),
})
const protectClient = await protect({
schemas: [users],
})
The buildEncryptConfig
function generates a configuration object like this:
{
v: 2,
tables: {
users: {
email: {
cast_as: 'text',
indexes: {
unique: { token_filters: [] },
match: {
tokenizer: { kind: 'ngram', token_length: 3 },
token_filters: [{ kind: 'downcase' }],
k: 6,
m: 2048,
include_original: true,
},
ore: {},
},
},
},
},
}
csTable(tableName: string, columns: ProtectTableColumn)
Creates a table definition.
Parameters:
tableName
: The name of the table in the databasecolumns
: Object defining the columns and their configurationsReturns: ProtectTable<T> & T
csColumn(columnName: string)
Creates a column definition.
Parameters:
columnName
: The name of the column in the databaseReturns: ProtectColumn
Methods:
.dataType(castAs: CastAs)
: Set the data type.equality(tokenFilters?: TokenFilter[])
: Enable equality index.freeTextSearch(opts?: MatchIndexOpts)
: Enable text search.orderAndRange()
: Enable order and range index.josn(prefix: string)
: Enable JSON field with prefixcsValue(valueName: string)
Creates a value definition for nested objects.
Parameters:
valueName
: Dot-separated path to the value (e.g., 'profile.name')Returns: ProtectValue
Methods:
.dataType(castAs: CastAs)
: Set the data typebuildEncryptConfig(...tables: ProtectTable[])
Builds the encryption configuration.
Parameters:
...tables
: Variable number of table definitionsReturns: EncryptConfig
MIT License - see LICENSE.md for details.
FAQs
CipherStash schema builder for TypeScript
The npm package @cipherstash/schema receives a total of 3,876 weekly downloads. As such, @cipherstash/schema popularity was classified as popular.
We found that @cipherstash/schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.