本文整理匯總了TypeScript中rev-models.ModelManager.create方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript ModelManager.create方法的具體用法?TypeScript ModelManager.create怎麽用?TypeScript ModelManager.create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rev-models.ModelManager
的用法示例。
在下文中一共展示了ModelManager.create方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: 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);
})();
示例2: 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);
});
示例3: 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();
})();
示例4: beforeEach
beforeEach(async () => {
modelManager = models.getModelManager();
apiManager = new ModelApiManager(modelManager);
apiManager.register(models.ModelWithAllScalarFields, { operations: ['read'] });
api = new GraphQLApi(apiManager);
api.fieldMappings.forEach((converter) => {
if (converter[0] == fields.TextFieldBase) {
converter[1].converter = (model, fieldName) => {
return 'I am a custom converter!';
};
}
});
data = new models.ModelWithAllScalarFields({
integerField: 2,
numberField: 3.456,
textField: 'I should be overridden...',
emailField: 'test@test.com',
urlField: 'http://www.test.com',
passwordField: 'password123',
booleanField: true,
selectField: 'Y',
multiSelectField: ['A', 'B'],
dateField: '2017-12-25',
timeField: '12:13:14',
dateTimeField: '2017-12-25T12:13:14'
});
await modelManager.create(data);
schema = api.getSchema();
});
示例5: createData
export async function createData() {
await modelManager.create(new Post({
category: 'agriculture',
title: 'My First Post',
body: 'This is a really cool post made in RevJS',
rating: 3,
published: true
}));
await modelManager.create(new Post({
category: 'technology',
title: 'RevJS is awesome!',
body: 'I should use it for ALL TEH THINGZZZ!',
rating: 5,
published: true
}));
await modelManager.create(new Post({
category: 'technology',
title: 'Draft Post',
body: 'Not ready to publish this just yet...',
published: false
}));
await modelManager.create(new Post({
category: 'science',
title: 'Another Draft Post',
body: 'This post sucks, so will not be published yet!',
published: false
}));
await modelManager.create(new Post({
category: 'science',
title: 'Cool as post',
body: 'Read this post, its great!...',
rating: 5,
published: true
}));
await modelManager.create(new Post({
category: 'music',
title: 'New release from Fiddy Cent',
body: 'Its called "Ok, I admit it, I suck" :)',
rating: 2,
published: true
}));
await modelManager.create(new Post({
category: 'music',
title: 'New release from Bruno Mars',
body: 'Its called "Daaaamn, you ugly"',
rating: 2,
published: true
}));
}
示例6: createData
export async function createData(manager: ModelManager): Promise<IModelTestData> {
for (const user of users) {
try {
await manager.create(user);
}
catch (e) {
console.log(user);
console.log(e.result.validation.fieldErrors);
}
}
for (const post of posts) {
try {
await manager.create(post);
}
catch (e) {
console.log(post);
console.log(e.result.validation.fieldErrors);
}
}
for (const comment of comments) {
try {
await manager.create(comment);
}
catch (e) {
console.log(comment);
console.log(e.result.validation.fieldErrors);
}
}
return {
users,
posts,
comments
};
}
示例7: Post
(async () => {
try {
// Create a valid post
await modelManager.create(new Post({
title: 'This is a valid post',
body: 'No alternative facts here!',
is_published: true
}));
// Try to create a duplicate post...
await modelManager.create(new Post({
title: 'This is a valid post',
body: 'Not really! Its a duplicate, so should cause an error...',
is_published: true
}));
}
catch (e) {
console.error(e.message);
}
})();
示例8: Comment
(async () => {
try {
await modelManager.create(new Comment({
comment: 'This comment has an invalid status',
status: 'Awesome!'
}));
}
catch (e) {
if (e instanceof ValidationError) {
console.log('Model Failed Validation :(');
console.log('Field Errors:', e.validation.fieldErrors);
console.log('Model Errors:', e.validation.modelErrors);
}
else {
throw e;
}
}
})();
示例9: getData
export async function getData(manager: ModelManager): Promise<IModelTestData> {
const posts = [
new Post({
id: 1,
title: 'RevJS v1.0.0 Released!',
body: `Great news! RevJS has finally hit version 1!
Go download it now and use it for ALL TEH THINGS!`,
published: false,
post_date: '2018-01-31T12:11:10',
}),
new Post({
id: 2,
title: 'JavaScript is Awesome',
body: `One language to rule them all...
My precious...`,
keywords: 'js, javascript, awesome',
published: true,
post_date: '2017-04-15T13:14:15',
}),
new Post({
id: 3,
title: 'Ruby Sucks',
body: `Why do people use it?
... who knows!`,
keywords: 'ruby, sucks',
published: true,
post_date: '2017-07-02T01:02:03',
}),
new Post({
id: 4,
title: 'Post 4',
body: `Another post...`,
published: true,
post_date: '2017-04-15T13:14:15',
}),
new Post({
id: 5,
title: 'Post 5',
body: `Another post...`,
published: true,
post_date: '2017-04-15T13:14:15',
}),
new Post({
id: 6,
title: 'Post 6',
body: `Another post...`,
published: true,
post_date: '2017-04-15T13:14:15',
}),
new Post({
id: 7,
title: 'Post 7',
body: `Another post...`,
published: true,
post_date: '2017-04-15T13:14:15',
})
];
for (const post of posts) {
try {
await manager.create(post);
}
catch (e) {
console.log(post);
console.log(e.result.validation.fieldErrors);
}
}
return {
posts
};
}