Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add schema definition for project keys table #169

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/schema/client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
// These schemas are all in a "client" database. There is only one client
// database and it contains information that is shared across all projects on a
// device
import { sqliteTable } from 'drizzle-orm/sqlite-core'
import { blob, sqliteTable, text } from 'drizzle-orm/sqlite-core'
import { dereferencedDocSchemas as schemas } from '@mapeo/schema'
import { NAMESPACES } from '../core-manager/index.js'
import { jsonSchemaToDrizzleColumns as toColumns } from './schema-to-drizzle.js'
import { backlinkTable } from './utils.js'

export const projectTable = sqliteTable('project', toColumns(schemas.project))
export const projectBacklinkTable = backlinkTable(projectTable)
export const projectKeysTable = generateProjectKeysTable()

function generateProjectKeysTable() {
/** @type {Record<string, import('drizzle-orm/sqlite-core').SQLiteColumnBuilder>}*/
const columns = {
projectId: text('projectId').notNull().primaryKey(),
projectSecretKey: blob('projectSecretKey'),
}

for (const namespace in NAMESPACES) {
const columnName = namespace + 'EncryptionKey'
columns[columnName] = blob(columnName)
}

return sqliteTable('projectKeys', columns)
}
15 changes: 9 additions & 6 deletions tests/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
} from '../src/schema/utils.js'
import { deNullify } from '../src/datatype/index.js'

const MAPEO_DATATYPE_NAMES = Object.keys(jsonSchemas)

test('Expected table config', (t) => {
const allTableSchemas = [
...Object.values(clientTableSchemas),
Expand All @@ -20,8 +22,10 @@ test('Expected table config', (t) => {

for (const tableSchema of allTableSchemas) {
const config = getTableConfig(tableSchema)
// Ignore backlink tables for this test
if (config.name.endsWith(BACKLINK_TABLE_POSTFIX)) continue

// Only test Mapeo Schema data types in this test
if (!MAPEO_DATATYPE_NAMES.includes(config.name)) continue

const schemaName = config.name
if (!(schemaName in jsonSchemas)) {
t.fail()
Expand Down Expand Up @@ -84,10 +88,9 @@ test('Types match', { skip: true }, (t) => {
t.pass()
})

test('backlink table exists for every endexed data type', (t) => {
test('backlink table exists for every indexed data type', (t) => {
// Every indexed datatype needs a backlink table, which is used by
// sqlite-indexer to track backlinks

const allTableNames = [
...Object.values(clientTableSchemas),
...Object.values(projectTableSchemas),
Expand All @@ -98,8 +101,8 @@ test('backlink table exists for every endexed data type', (t) => {
const backlinkTableNames = allTableNames.filter((name) =>
name.endsWith(BACKLINK_TABLE_POSTFIX)
)
const dataTypeTableNames = allTableNames.filter(
(name) => !name.endsWith(BACKLINK_TABLE_POSTFIX)
const dataTypeTableNames = allTableNames.filter((name) =>
MAPEO_DATATYPE_NAMES.includes(name)
)

for (const name of dataTypeTableNames) {
Expand Down