Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// @flow
import { mergeSchemas } from '@tanker/datastore-base';
import { expect } from '@tanker/test-utils';
import dataStoreConfig, { makePrefix, openDataStore } from './TestDataStore';
import KeyStore from '../Session/LocalUser/KeyStore';
import UserStore from '../Users/UserStore';
import TrustchainStore from '../Trustchain/TrustchainStore';
const schemas = mergeSchemas(
KeyStore.schemas,
UserStore.schemas,
TrustchainStore.schemas,
);
const baseConfig = { ...dataStoreConfig, schemas };
async function makeMemoryDataStore() {
const config = { ...baseConfig, dbName: `trustchain-${makePrefix()}` };
const dataStore = await openDataStore(config);
return { dataStore, config };
}
describe('TrustchainStore', () => {
const TABLE_METADATA = 'trustchain_metadata';
const LAST_BLOCK_INDEX_KEY = 'lastBlockIndex';
async open(userId: Uint8Array, userSecret: Uint8Array): Promise {
const { adapter, prefix, dbPath, url } = this._options;
const schemas = mergeSchemas(
GlobalSchema,
KeyStore.schemas,
ResourceStore.schemas,
GroupStore.schemas,
);
const dbName = `tanker_${prefix ? `${prefix}_` : ''}${utils.toSafeBase64(userId)}`;
// $FlowIKnow DataStore is a flow interface, which does not support static methods
this._datastore = await adapter().open({ dbName, dbPath, schemas, url });
this._schemas = schemas;
this._keyStore = await KeyStore.open(this._datastore, userSecret);
this._resourceStore = await ResourceStore.open(this._datastore, userSecret);
this._groupStore = await GroupStore.open(this._datastore, userSecret);
await this._checkVersion(userSecret);
const previousRecords = await this.find(table, { selector: { _id: { $in: ids } } });
previousRecords.forEach(rec => {
idToRev[rec._id] = rec._rev;
});
// add missing _rev
all = all.map(rec => {
const rev = idToRev[rec._id];
return rev ? { ...rec, _rev: rev } : rec;
});
}
await this._dbs[table].bulkDocs(toDB(all));
} catch (e) {
if (e.status === 409) {
throw new dbErrors.RecordNotUnique(e);
}
throw new dbErrors.UnknownError(e);
}
}
get = async (table: string, id: string) => {
let record;
try {
record = fromDB(await this._db.table(table).get(id));
} catch (e) {
throw new dbErrors.UnknownError(e);
}
// undefined is return when record not found
if (!record) {
throw new dbErrors.RecordNotFound();
}
return record;
}
import { errors as dbErrors, transform, type DataStore, type SortParams, type Schema, type BaseConfig } from '@tanker/datastore-base';
export type Config = BaseConfig;
export type { Schema };
class UnsupportedTypeError extends Error {
name: string;
constructor(type: string) {
super(`Dexie can't support search for ${type} values on an index, as they are invalid IndexedDB keys.`);
this.name = this.constructor.name;
}
}
const iframe = (typeof window !== 'undefined') && window.parent && window.parent !== window;
const fromDB = iframe ? transform.fixObjects : transform.identity;
Dexie.dataStoreName = 'DexieBrowser';
export default () => class DexieBrowserStore implements DataStore {
/*:: _db: Dexie; */
/*:: _indexes: { [table: string]: { [field: string]: bool } }; */
constructor(db: Dexie) {
// _ properties won't be enumerable, nor reconfigurable
Object.defineProperty(this, '_db', { value: db, writable: true });
Object.defineProperty(this, '_indexes', { value: {}, writable: true });
return this;
}
get className(): string {
return this.constructor.name;
async init(skipRootBlock?: bool) {
const schemas = mergeSchemas(
KeyStore.schemas,
UserStore.schemas,
TrustchainStore.schemas,
GroupStore.schemas,
UnverifiedStore.schemas,
);
this.trustchainKeyPair = tcrypto.makeSignKeyPair();
this.generator = await Generator.open(this.trustchainKeyPair);
this.dataStoreConfig = { ...dataStoreConfig, schemas, dbName: `trustchain-${makePrefix()}` };
const { trustchainId } = this.generator;
const userIdString = 'let try this for now';
export async function makeMemoryDataStore(): Promise> {
const schemas = mergeSchemas(UserStore.schemas);
const baseConfig = { ...dataStoreConfig, schemas };
const config = { ...baseConfig, dbName: `user-store-test-${makePrefix()}` };
return openDataStore(config);
}
export function makeMemoryDataStore(schemas: Array, dbName: string): Promise> {
const config = { adapter: PouchDBMemory, schemas: mergeSchemas(schemas), dbName: makePrefix() + dbName };
return openDataStore(config);
}
export async function makeMemoryDataStore(): Promise> {
const schemas = mergeSchemas(GroupStore.schemas);
const baseConfig = { ...dataStoreConfig, schemas };
const config = { ...baseConfig, dbName: `group-store-test-${makePrefix()}` };
return openDataStore(config);
}
bulkAdd = async (table: string, records: Array | Object, ...otherRecords: Array) => {
const all = (records instanceof Array) ? records : [records, ...otherRecords];
try {
const allWithoutRevs = all.map(record => {
const recordWithoutRev = { ...record };
delete recordWithoutRev._rev;
return recordWithoutRev;
});
await this._dbs[table].bulkDocs(toDB(allWithoutRevs));
} catch (e) {
if (e.status === 409) {
throw new dbErrors.RecordNotUnique(e);
}
throw new dbErrors.UnknownError(e);
}
}