本文整理汇总了TypeScript中arangojs.Database类的典型用法代码示例。如果您正苦于以下问题:TypeScript Database类的具体用法?TypeScript Database怎么用?TypeScript Database使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Database类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: drop
drop: async function drop(): Promise<any> {
await config.load(process.cwd() + '/test');
const cfg = await config.get();
const dbHost: string = cfg.get('database:arango:host');
const dbPort: string = cfg.get('database:arango:port');
const dbName: string = cfg.get('database:arango:database');
const db = new Database('http://' + dbHost + ':' + dbPort);
await db.dropDatabase(dbName);
},
示例2: retry
return await retry(async () => {
logger.info('Attempt to connect database', {
dbHost, dbPort, dbName,
attempt: i
});
i += 1;
const db = new Database({
url,
arangoVersion,
});
try {
db.useDatabase(dbName);
if (username && password) {
db.useBasicAuth(username, password);
}
await db.get();
} catch (err) {
if (err.name === 'ArangoError' && err.errorNum === 1228) {
if (autoCreate) {
logger.verbose(`auto creating arango database ${dbName}`);
// Database does not exist, create a new one
db.useDatabase(DB_SYSTEM);
await db.createDatabase(dbName);
db.useDatabase(dbName);
return db;
}
}
throw err;
}
return db;
}, { retries: attempts, minTimeout: delay });
示例3: 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();
}
}
示例4: 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);
}
示例5: 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);
}
示例6: 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);
}