本文整理匯總了TypeScript中rev-models.ModelManager.hydrate方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript ModelManager.hydrate方法的具體用法?TypeScript ModelManager.hydrate怎麽用?TypeScript ModelManager.hydrate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rev-models.ModelManager
的用法示例。
在下文中一共展示了ModelManager.hydrate方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1:
/**
* @private
*/
async create<T extends IModel>(manager: ModelManager, model: T, options: ICreateOptions, result: ModelOperationResult<T, ICreateMeta>): Promise<ModelOperationResult<T, ICreateMeta>> {
const meta = manager.getModelMeta(model);
const data = this._buildGraphQLModelData(manager, meta, model);
const mutationName = meta.name + '_create';
const query = {
mutation: {
[mutationName]: {
__args: {
model: data
}
}
}
};
const httpResult = await this._getGraphQLQueryResult(query);
if (!httpResult.data.data
|| !httpResult.data.data[mutationName]) {
throw this._createHttpError('GraphQL response did not contain the expected operation results', httpResult);
}
const createResult: ModelOperationResult<any, ICreateMeta> = httpResult.data.data[mutationName];
result.success = createResult.success;
result.validation = createResult.validation;
if (createResult.result) {
result.result = manager.hydrate(meta.ctor, createResult.result);
}
return result;
}
示例2: if
/**
* @private
*/
async create<T extends IModel>(manager: ModelManager, model: T, params: ICreateParams, result: ModelOperationResult<T, ICreateMeta>): Promise<ModelOperationResult<T, ICreateMeta>> {
const meta = manager.getModelMeta(model);
const document: IKeyMap<number> = {};
let fieldList = Object.keys(meta.fieldsByName);
for (let fieldName of fieldList) {
let field = meta.fieldsByName[fieldName];
if (field.options.stored) {
if (field instanceof fields.AutoNumberField
&& typeof model[fieldName] == 'undefined') {
document[fieldName] = await this._getNextAutoNumberValue(meta.name, fieldName);
}
else if (typeof model[fieldName] != 'undefined') {
let value = field.toBackendValue(manager, model[fieldName]);
if (typeof value != 'undefined') {
document[fieldName] = value;
}
}
}
}
const colName = this._getCollectionName(meta);
const createResult = await this.db.collection(colName).insertOne(document);
if (createResult.insertedCount != 1) {
throw new Error('mongodb insert failed'); // TODO: Something nicer
}
result.result = manager.hydrate(meta.ctor, document);
return result;
}
示例3: _hydrateRecordWithRelated
private _hydrateRecordWithRelated(manager: ModelManager, meta: IModelMeta<any>, recordData: any, related?: string[]) {
const model = manager.hydrate(meta.ctor, recordData);
if (related) {
const relFieldNames = getOwnRelatedFieldNames(related);
meta.fields.forEach((field) => {
if (relFieldNames.indexOf(field.name) > -1 && typeof recordData[field.name] != 'undefined') {
const relMeta = manager.getModelMeta(field.options.model);
const childRelFieldNames = getChildRelatedFieldNames(field.name, related);
if (field instanceof fields.RelatedModelField) {
if (recordData[field.name] == null) {
model[field.name] = null;
}
else {
model[field.name] = this._hydrateRecordWithRelated(manager, relMeta, recordData[field.name], childRelFieldNames);
}
}
else if (field instanceof fields.RelatedModelListField && recordData[field.name] instanceof Array) {
model[field.name] = [];
recordData[field.name].forEach((record: any) => {
model[field.name].push(this._hydrateRecordWithRelated(manager, relMeta, record, childRelFieldNames));
});
}
}
});
}
return model;
}
示例4:
records.forEach((record) => {
const modelInstance = manager.hydrate(meta.ctor, record);
result.results!.push(modelInstance);
if (meta.primaryKey) {
primaryKeyValues.push(modelInstance[meta.primaryKey]);
}
// TODO: Refactor this into a method shared with InMemoryBackend (and future backends)
if (relatedFieldNames) {
for (let fieldName of relatedFieldNames) {
let field = meta.fieldsByName[fieldName];
let keyValue = record[fieldName];
if (field instanceof fields.RelatedModelField) {
if (!(fieldName in foreignKeyValues)) {
foreignKeyValues[fieldName] = [];
}
if (typeof keyValue != 'undefined' && keyValue !== null) {
modelInstance[fieldName] = keyValue;
if (foreignKeyValues[fieldName].indexOf(keyValue) == -1) {
foreignKeyValues[fieldName].push(keyValue);
}
}
else {
modelInstance[fieldName] = null;
}
}
}
}
if (params.rawValues) {
let rawValueObj: IRawValueRow = {};
for (let fieldName of params.rawValues) {
rawValueObj[fieldName] = record[fieldName];
}
rawValues.push(rawValueObj);
}
});