Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'rxdb' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
/**
* 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);