-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
157 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NotFoundError } from '../errors.js' | ||
/** @import { DataStore } from '../datastore/index.js' */ | ||
/** @import { MapeoDocMap, MapeoValueMap } from '../types.js' */ | ||
/** @import { DataType, MapeoDocTables } from './index.js' */ | ||
|
||
/** | ||
* @template {MapeoDocTables} TTable | ||
* @template {TTable['_']['name']} TSchemaName | ||
* @template {MapeoDocMap[TSchemaName]} TDoc | ||
* @template {MapeoValueMap[TSchemaName]} TValue | ||
* @param {DataType<DataStore, TTable, TSchemaName, TDoc, TValue>} dataType | ||
* @param {string} docId | ||
* @returns {Promise<null | TDoc & { forks: string[] }>} | ||
*/ | ||
export async function getByDocIdIfExists(dataType, docId) { | ||
try { | ||
return await dataType.getByDocId(docId) | ||
} catch (err) { | ||
if (err instanceof NotFoundError) return null | ||
throw err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { testenv } from './test-helpers.js' | ||
import test, { describe } from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import { getByDocIdIfExists } from '../../src/datatype/get-if-exists.js' | ||
import { valueOf } from '@comapeo/schema' | ||
import { generate } from '@mapeo/mock-data' | ||
|
||
describe('getByDocIdIfExists', () => { | ||
test('resolves with null if no document exists with that ID', async () => { | ||
const { dataType } = await testenv() | ||
assert.equal(await getByDocIdIfExists(dataType, 'foo bar'), null) | ||
}) | ||
|
||
test('resolves with the document if it exists', async () => { | ||
const { dataType } = await testenv() | ||
const fixture = valueOf(generate('observation')[0]) | ||
const observation = await dataType.create(fixture) | ||
assert(await getByDocIdIfExists(dataType, observation.docId)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import assert from 'node:assert/strict' | ||
import RAM from 'random-access-memory' | ||
import { DataStore } from '../../src/datastore/index.js' | ||
import { DataType } from '../../src/datatype/index.js' | ||
import { IndexWriter } from '../../src/index-writer/index.js' | ||
import { observationTable, translationTable } from '../../src/schema/project.js' | ||
import { createCoreManager } from '../helpers/core-manager.js' | ||
|
||
import { decode, decodeBlockPrefix } from '@comapeo/schema' | ||
import Database from 'better-sqlite3' | ||
import { drizzle } from 'drizzle-orm/better-sqlite3' | ||
import { migrate } from 'drizzle-orm/better-sqlite3/migrator' | ||
import TranslationApi from '../../src/translation-api.js' | ||
|
||
/** | ||
* @param {object} [opts={}] | ||
* @param {Buffer} [opts.projectKey] | ||
*/ | ||
export async function testenv(opts = {}) { | ||
const sqlite = new Database(':memory:') | ||
const db = drizzle(sqlite) | ||
migrate(db, { | ||
migrationsFolder: new URL('../../drizzle/project', import.meta.url) | ||
.pathname, | ||
}) | ||
|
||
const coreManager = createCoreManager({ ...opts, db }) | ||
|
||
const indexWriter = new IndexWriter({ | ||
tables: [observationTable, translationTable], | ||
sqlite, | ||
}) | ||
|
||
const dataStore = new DataStore({ | ||
coreManager, | ||
namespace: 'data', | ||
batch: async (entries) => indexWriter.batch(entries), | ||
storage: () => new RAM(), | ||
reindex: false, | ||
}) | ||
|
||
const configDataStore = new DataStore({ | ||
coreManager, | ||
namespace: 'config', | ||
batch: async (entries) => { | ||
/** @type {import('multi-core-indexer').Entry[]} */ | ||
const entriesToIndex = [] | ||
for (const entry of entries) { | ||
const { schemaName } = decodeBlockPrefix(entry.block) | ||
try { | ||
if (schemaName === 'translation') { | ||
const doc = decode(entry.block, { | ||
coreDiscoveryKey: entry.key, | ||
index: entry.index, | ||
}) | ||
assert( | ||
doc.schemaName === 'translation', | ||
'expected a translation doc' | ||
) | ||
translationApi.index(doc) | ||
} | ||
entriesToIndex.push(entry) | ||
} catch { | ||
// Ignore errors thrown by values that can't be decoded for now | ||
} | ||
} | ||
const indexed = await indexWriter.batch(entriesToIndex) | ||
return indexed | ||
}, | ||
storage: () => new RAM(), | ||
reindex: false, | ||
}) | ||
|
||
const translationDataType = new DataType({ | ||
dataStore: configDataStore, | ||
table: translationTable, | ||
db, | ||
getTranslations: () => { | ||
throw new Error('Cannot get translations for translations') | ||
}, | ||
}) | ||
|
||
const translationApi = new TranslationApi({ dataType: translationDataType }) | ||
|
||
const dataType = new DataType({ | ||
dataStore, | ||
table: observationTable, | ||
db, | ||
getTranslations: translationApi.get.bind(translationApi), | ||
}) | ||
|
||
return { coreManager, dataType, dataStore, translationApi } | ||
} |