Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
* this is the test for the typescript-tutorial
* IMPORTANT: whenever you change something here,
* ensure it is also changed in /docs-src/tutorials/typescript.md
*/
// import types
import RxDB, {
RxDatabase,
RxCollection,
RxJsonSchema,
RxDocument
} from 'rxdb';
import * as MemoryAdapter from 'pouchdb-adapter-memory';
RxDB.plugin(MemoryAdapter);
/**
* declare types
*/
type HeroDocType = {
passportId: string;
firstName: string;
lastName: string;
age?: number; // optional
};
type HeroDocMethods = {
scream: (v: string) => string;
};
async function run() {
// create database
const db = await RxDB.create({
name: 'mydb',
adapter: 'memory'
});
// create a collection
const mySchema = {
version: 0,
type: 'object',
properties: {
key: {
type: 'string',
primary: true
},
value: {
type: 'string'
}
import * as RxDB from 'rxdb';
RxDB.plugin(require('pouchdb-adapter-idb'));
RxDB.plugin(require('pouchdb-replication')); //enable syncing
RxDB.plugin(require('pouchdb-adapter-http')); //enable syncing over http
const syncURL = 'http://' + window.location.hostname + ':10102/';
console.log('host: ' + syncURL);
// const syncURL = host;
let dbPromise = null;
const _create = async function() {
console.log('DatabaseService: creating database..');
const db = await RxDB.create({
name: 'niraniadb',
adapter: 'idb',
password: 'myLongAndStupidPassword'
});
async _init() {
// Only create db if it doesnt exist already
this.db = await RxDB.create({
name: this.name,
adapter: this.adapter,
multiInstance: false,
});
this.collection = await this._createCollection();
}
async createDatabase() {
// password must have at least 8 characters
const db = await RxDB.create(
{name: dbName, adapter: 'idb', password: '12345678'}
);
console.dir(db);
// show who's the leader in page's title
db.waitForLeadership().then(() => {
document.title = '♛ ' + document.title;
});
// create collection
const messagesCollection = await db.collection({
name: 'messages',
schema: schema
});
// set up replication
async function _create(): Promise {
console.log('DatabaseService: creating database..');
const db = await RxDB.create({
name: 'heroes',
adapter: useAdapter,
queryChangeDetection: true
// password: 'myLongAndStupidPassword' // no password needed
});
console.log('DatabaseService: created database');
(window as any).db = db; // write to window for debugging
// show leadership in title
db.waitForLeadership()
.then(() => {
console.log('isLeader now');
document.title = '♛ ' + document.title;
});
// create collections
async initDb(config: NgxRxdbConfig) {
console.log('INIT DB');
try {
const db: RxDatabase = await RxDB.create({
...DEFAULT_CONFIG,
...config
});
this._dbInstance = db;
console.log(this.db);
console.log('RxdbService: created database');
// also can create collections from root config
if (config && config.options && config.options.schemas) {
await this.initCollections(config.options.schemas);
console.log('RxdbService: created collections bulk');
}
console.log(config);
if (config && config.options && config.options.dumpPath) {
// fetch dump json
const dump = await (await fetch(config.options.dumpPath)).json();
async function _create(): Promise {
console.log('DatabaseService: creating database..');
const db = await RxDB.create({
name: 'heroes',
adapter: useAdapter,
queryChangeDetection: true
// password: 'myLongAndStupidPassword' // no password needed
});
console.log('DatabaseService: created database');
(window as any)['db'] = db; // write to window for debugging
// show leadership in title
db.waitForLeadership()
.then(() => {
console.log('isLeader now');
document.title = '♛ ' + document.title;
});
// create collections
async function _create(): Promise {
await loadRxDBPlugins();
console.log('DatabaseService: creating database..');
const db = await RxDB.create({
name: 'angularheroes',
adapter: 'idb',
queryChangeDetection: true
// password: 'myLongAndStupidPassword' // no password needed
});
console.log('DatabaseService: created database');
(window as any)['db'] = db; // write to window for debugging
// show leadership in title
db.waitForLeadership()
.then(() => {
console.log('isLeader now');
document.title = '♛ ' + document.title;
});
// create collections
async createCollection(schemaConfig: NgxRxdbCollectionConfig) {
console.log('CREATE COLLECTION');
// TODO this needs to be fixed to be available initially
await promiseTimeout(2500);
if (!schemaConfig || !schemaConfig.schema) {
throw new Error('RxdbService: missing schema object');
}
let collection: RxCollection = this.db[name];
// delete collection if exists
if (RxDB.isRxCollection(collection)) {
console.log('RxdbService: collection', name, 'exists, skip create');
await collection.remove();
}
collection = await this.db.collection(new NgxRxdbCollectionCreator(schemaConfig));
console.log(`RxdbService: created collection "${name}"`);
// preload data into collection
const docsCount = await collection.countAllDocuments();
console.log(`RxdbService: collection "${name}" has "${parseInt(docsCount, 0)}" docs`);
if (schemaConfig.options && schemaConfig.options.initialDocs && !!!docsCount) {
const dumpObj = {
name,
schemaHash: collection.schema.hash,
encrypted: false,
docs: [...schemaConfig.options.initialDocs],
};
await collection.importDump(dumpObj);