本文整理汇总了TypeScript中rev-models.ModelManager类的典型用法代码示例。如果您正苦于以下问题:TypeScript ModelManager类的具体用法?TypeScript ModelManager怎么用?TypeScript ModelManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ModelManager类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: beforeEach
beforeEach(() => {
models = new rev.ModelManager();
models.registerBackend('default', new rev.InMemoryBackend());
models.register(TestDecoratedModel);
models.register(TestModel);
apiManager = new ModelApiManager(models);
});
示例2: MongoDBBackend
(async () => {
// Create a MongoDBBackend and connect to MongoDB
const mongo = new MongoDBBackend({
url: 'mongodb://localhost:27017',
dbName: 'example_db'
});
await mongo.connect();
// Create a ModelManager, and assign MongoDB as the default backend
const modelManager = new ModelManager();
modelManager.registerBackend('default', mongo);
modelManager.register(TestModel);
// Create some data, then disconnect afterwards
await modelManager.create(new TestModel({
name: 'data from RevJS!',
description: 'This beautiful record was created by RevJS',
score: 110
}));
console.log('Data successfully created in MongoDB!');
await mongo.disconnect();
})();
示例3:
/**
* @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;
}
示例4: 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;
}
示例5: beforeEach
beforeEach(async () => {
modelManager = models.getModelManager();
apiManager = new ModelApiManager(modelManager);
apiManager.register(models.Post, { operations: ['read', 'update'] });
apiManager.register(models.User, { operations: ['read'] });
apiManager.register(models.Comment, { operations: ['read'] });
api = new GraphQLApi(apiManager);
schema = api.getSchema();
existingPost = new models.Post({
id: 10,
title: 'Existing Post',
body: 'This post has already been created',
published: true,
post_date: '2017-12-23T09:30:21'
});
existingPost2 = new models.Post({
id: 11,
title: 'Another Post',
body: 'This is another post',
published: true,
post_date: '2018-01-05T12:23:34'
});
await modelManager.create(existingPost);
await modelManager.create(existingPost2);
});
示例6: getModelManager
export function getModelManager() {
const modelManager = new ModelManager();
modelManager.registerBackend('default', new InMemoryBackend());
modelManager.register(User);
modelManager.register(Post);
modelManager.register(Comment);
return modelManager;
}
示例7: before
before(async () => {
// Create an in-memory model manager for API data
const apiModelManager = new ModelManager();
apiModelManager.registerBackend('default', new InMemoryBackend());
registerModels(apiModelManager);
// Create a mock Axios client for querying the API
const mockHttpClient = getMockApiHttpClient(apiModelManager);
// Create the backend ready for testing
backend = new ModelApiBackend('/api', mockHttpClient);
config.backend = backend;
});
示例8: _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;
}
示例9: Error
/**
* @private
*/
async update<T extends IModel>(manager: ModelManager, model: T, options: IUpdateOptions, result: ModelOperationResult<T, IUpdateMeta>): Promise<ModelOperationResult<T, IUpdateMeta>> {
if (!options.where) {
throw new Error(`update() requires the 'where' option to be set.`);
}
const meta = manager.getModelMeta(model);
const data = this._buildGraphQLModelData(manager, meta, model, options.fields);
const mutationName = meta.name + '_update';
let args: any = {
model: data
};
if (options.where) {
args.where = options.where;
}
const query = {
mutation: {
[mutationName]: {
__args: args
}
}
};
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 updateResult: ModelOperationResult<any, IUpdateMeta> = httpResult.data.data[mutationName];
result.success = updateResult.success;
result.validation = updateResult.validation;
result.meta = updateResult.meta;
return result;
}
示例10: Post
(async () => {
// Create some data
await modelManager.create(new Post({
title: 'My First Post',
body: 'This is a really cool post made in RevJS',
status: 'draft'
}));
await modelManager.create(new Post({
title: 'RevJS is awesome!',
body: 'I should use it for ALL TEH THINGZZZ!',
status: 'published'
}));
// Read it back
const res = await modelManager.read(Post, {
where: {
_or: [
{ title: { _like: '%RevJS%' }},
{ body: { _like: '%RevJS%' }}
]
}
});
console.log(res.results);
})();