本文整理汇总了TypeScript中arangojs.Database.collection方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Database.collection方法的具体用法?TypeScript Database.collection怎么用?TypeScript Database.collection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类arangojs.Database
的用法示例。
在下文中一共展示了Database.collection方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: truncate
/**
* When calling without a collection name,
* delete all documents in all collections in the database.
* When providing a collection name,
* delete all documents in specified collection in the database.
* @param [string] collection Collection name.
*/
async truncate(collection: string): Promise<any> {
if (_.isNil(collection)) {
const collections = await this.db.collections();
for (let i = 0; i < collections.length; i += 1) {
const c = this.db.collection(collections[i].name);
await c.truncate();
}
} else {
const c = this.db.collection(collection);
await c.truncate();
}
}
示例2: update
/**
* Find documents by filter and updates them with document.
*
* @param {String} collection Collection name
* @param {Object} filter Key, value Object
* @param {Object} document A document patch.
*/
async update(collectionName: string, filter: any, document: any): Promise<any> {
if (_.isNil(collectionName) ||
!_.isString(collectionName) || _.isEmpty(collectionName)) {
throw new Error('invalid or missing collection argument');
}
if (_.isNil(document)) {
throw new Error('invalid or missing document argument');
}
const doc = sanitizeInputFields(_.clone(document));
const collection = this.db.collection(collectionName);
let queryString = aql`FOR node in ${collection}
FILTER node.id == ${doc.id}
UPDATE node WITH ${doc} in ${collection} return NEW`;
const res = await query(this.db, collectionName, queryString);
const upDocs = await res.all();
return _.map(upDocs, sanitizeOutputFields);
}
示例3: insert
/**
* Insert documents into database.
*
* @param {String} collection Collection name
* @param {Object|array.Object} documents A single or multiple documents.
*/
async insert(collectionName: string, documents: any): Promise<any> {
if (_.isNil(collectionName) || !_.isString(collectionName) || _.isEmpty(collectionName)) {
throw new Error('invalid or missing collection argument');
}
if (_.isNil(documents)) {
throw new Error('invalid or missing documents argument');
}
let docs = _.cloneDeep(documents);
if (!_.isArray(documents)) {
docs = [documents];
}
_.forEach(docs, (document, i) => {
docs[i] = sanitizeInputFields(document);
});
const collection = this.db.collection(collectionName);
const queryTemplate = aql`FOR document in ${docs} INSERT document INTO ${collection}`;
await query(this.db, collectionName, queryTemplate);
}
示例4: upsert
/**
* Find each document based on it's key and update it.
* If the document does not exist it will be created.
*
* @param {String} collection Collection name
* @param {Object|Array.Object} documents
*/
async upsert(collectionName: string, documents: any): Promise<any> {
if (_.isNil(collectionName) ||
!_.isString(collectionName) || _.isEmpty(collectionName)) {
throw new Error('invalid or missing collection argument');
}
if (_.isNil(documents)) {
throw new Error('invalid or missing documents argument');
}
let docs = _.cloneDeep(documents);
if (!_.isArray(documents)) {
docs = [documents];
}
_.forEach(docs, (document, i) => {
docs[i] = sanitizeInputFields(document);
});
const collection = this.db.collection(collectionName);
const queryTemplate = aql`FOR document in ${docs} UPSERT { _key: document._key }
INSERT document UPDATE document IN ${collection} return NEW`;
const res = await query(this.db, collectionName, queryTemplate);
const newDocs = await res.all();
return _.map(newDocs, sanitizeOutputFields);
}