Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
getCollection(name: string): RxCollection {
const collection: RxCollection = this.db[name];
if (RxDB.isRxCollection(collection)) {
return collection;
} else {
console.warn(`RxdbService: returned false for RxDB.isRxCollection(${name})`);
return null;
}
}
public get collection() {
if (RxDB.isRxCollection(this._collection)) {
return this._collection;
} else {
console.warn(`RxdbService: returned false for RxDB.isRxCollection(${this._config.name})`);
return null;
}
}
getAllDocs(name: string): Observable[]> {
const collection: RxCollection = this.db[name];
return RxDB.isRxCollection(collection) ? collection.find().$ : of([]);
}