I have created basic schema with timestamps, but when I insert into table in seeder, column created_at and updated_at are null, but based on the Knex.js documentation I thought it should be current datetime if not specified.
Latest adonis libraries, database: mysql 5.7.
My Schema
'use strict'
const Schema = use('Schema')
class UserSchema extends Schema {
up () {
this.create('users', (table) => {
table.increments()
table.string('name')
table.timestamps()
})
}
down () {
this.drop('users')
}
}
module.exports = UserSchema
My Seeder
'use strict'
const Factory = use('Factory')
const Database = use('Database')
class UserSeeder {
async run () {
await Database.table('users').insert({
name: 'JP',
})
}
}
module.exports = UserSeeder
Timestamps only works on LUCID ORM. You are using direct database.
Set your json in the fields, it will work:
await Database.table('users').insert({
name: 'JP',
created_at,: Database.fn.now(),
updated_at : Database.fn.now()
})
I have found that timestamps are default only when using models, but not when using database directly. This one is now covered in the documentation. Would be great if it was default on database level instead.
You should add the timestamps arguments like useTimestampType and makeDefaultNow:
table.timestamps(true, true)
Related
Intention:
trying to query from apollo client based on dynamic id. Have successfully checked in server provided interface which is working... and trying to do same from the client.
From the doc it looks like i need to use variables which i did.
Problem:
query using variables looks good but i am getting undefined in client.
Query which is working in graphql API:
query abc {
getCategoryProduct(id:"NzI1NDc1MTM1") {
id
title
description
favorited
published
price_per_day
price_per_week
price_per_month
price_per_weekend
picture
pictures {
id
url
}
createdAt
updatedAt
}
}
Problematic code in client
const GETDETAILS = gql`
query abc($id: String!) {
getCategoryProduct(id: $id) {
id
title
description
favorited
published
price_per_day
price_per_week
price_per_month
price_per_weekend
picture
pictures {
id
url
}
createdAt
updatedAt
}
}
`;
const DetailScreen = () => {
const { loading, error, data } = useQuery(GETDETAILS, {
variables: { id: "NzI1NDc1MTM1" },
});
useEffect(() => {
if (loading == false) {
console.log("=====data=====", data); // DATA IS EMPTY DO NOT NOT WHY??
}
}, [data]);
}
I was getting the same bug, and it looked like I had tried everything to solve it, including following the instruction in useQuery returns undefined, But returns data on gql playground, but it still didn't work.
Later, I change the variable name—in your case $id—to something else, so it's different from the name in typeDefs (getCategoryProduct(id:ID)), and it now works for me 🤨🙏.
Am writing some unit testing, I have a component with meta info set using Vue-meta
My Component looks like this.
export default {
...
metaInfo () {
const expertName = this.getBlogInfo.blog.author.trim()
const fullName = expertName ? `${expertName.first_name} ${expertName.last_name}` : 'Cowsoko'
return {
title: `Dairynomics - Blog post from ${fullName}`,
meta: [
{
vmid: 'og:description',
name: 'og:description',
content: this.description
},
{
vmid: 'og:image',
name: 'og:image',
content: this.getBlogInfo.blog.photo
}
]
}
}
...
There's an issue on their github repo which says you need to create a local Vue instance.
You can read about local Vue instances in the vue-test-utils docs. It allows you to add components, mixins and install plugins without polluting the global Vue class, i.e. add in the vue-meta properties for this test only.
import { shallowMount, createLocalVue } from '#vue/test-utils'
import Component from './Component.vue'
import VueMeta from 'vue-meta'
let localVue = createLocalVue();
localVue.use(VueMeta);
describe('Component.vue', function() {
// Set up the wrapper
const wrapper = shallowMount(Component)
it('has a getTitle() method that returns the page title', () => {
expect(wrapper.vm.getTitle()).toBe(title)
})
it('has its meta title correctly set', () => {
expect(wrapper.vm.$meta().refresh().metaInfo.title).toBe('some title')
})
})
You can insert your meta data normally in each component.
If your pages are dynamic and if you want any dynamic SEO or meta tags you can use vue-headful.
Like this
<vue-headful
title="Title from vue-headful"
description="Description from vue-headful"
/>
In vue-headful you can write all the meta tags.
I'm trying to get state, city and slaughter from this query. I've tried this and it only returns 1 of them as 'name' which is name of each place in 'places' table. how can I get alias for them? I didn't find anything on documents.
const bargiris = await Database
.table('bargiris')
.innerJoin('users', 'bargiris.nazer_id', 'users.id')
.innerJoin('companies', 'bargiris.sherkat_id', 'companies.id')
.innerJoin('sifs', 'bargiris.sif_code', 'sifs.sif_id')
.innerJoin('places', function() {
this.on('sifs.state_id', 'places.id')
.orOn('sifs.city_id', 'places.id')
.orOn('sifs.slaughter_id', 'places.id')
})
You can add the select() method and pass the field names with an alias
const bargiris = await Database
.table('bargiris')
.innerJoin('users', 'bargiris.nazer_id', 'users.id')
.innerJoin('companies', 'bargiris.sherkat_id', 'companies.id')
.innerJoin('sifs', 'bargiris.sif_code', 'sifs.sif_id')
.innerJoin('places', function() {
this.on('sifs.state_id', 'places.id')
.orOn('sifs.city_id', 'places.id')
.orOn('sifs.slaughter_id', 'places.id')
})
.select('state.name as state', 'city.name as city')
I have been looking around option to create data seeders to add dummy data in my loopback 4 application. However I am not able to find any option in official documentation.
I have found couple of post but those refer to loopback 3, like:
Loopback: Creating a Seed Script
loopback-seed
Please point me out to documentation to do so.
EDIT:
As per suggestion I have created start.js file in scripts folder:
require('babel-register')({
presets: ['es2015']
})
module.exports = require('./seed.js')
And I have copied the script converting it to JavaScript mentioned in seed.js file. When I am running the script, I am getting error:
Cannot find module Models and Repositories
though I have typed correct path.
Actually, I'm doing it with Loopback directly like this (this is typescript):
import * as users from './users.json';
import * as Promise from 'bluebird';
import {Entity, DefaultCrudRepository} from '#loopback/repository';
import {MyApplication} from '../src/application';
import {User} from '../src/models';
import {UserRepository} from '../src/repositories';
const app = new MyApplication();
async function loadByModel<T extends Entity, ID>(items: T[], repository$: DefaultCrudRepository<T,ID>, type: { new(it: Partial<T>): T ;}){
console.log(type.toString());
let repository = await repository$;
await repository.deleteAll();
await Promise.map(items, async (item: T) => {
try{
return await repository.create((new type(item)));
} catch(e){
console.log(item);
}
}, {concurrency: 50});
}
async function load(){
await loadByModel(users, await app.getRepository(UserRepository), User);
}
app.boot().then(async () => {
await load();
console.log('done');
});
We used a separate library db-migrate to keep our migration and seed scripts out of our loopback codebase. Moreso, because db.migrate and db.update methods of juggler are not 100% accurate as mentioned in docs as well. LB4 Database Migrations
Does storongloop loopback has pluck function for a given model ?
For a Product model,
in Rails I can write
Product.where(some_condition).pluck(:name)
It will return an array of names of products matching the condition.
Is there anything similar in loopback?
PS: I know I can use fields filter and then use underscore( or lodash)'s pluck but that's a two step process.
Select specific columns:
{"fields":{"name":true, "email":true}}
Where condition:
{"where":{"id":2}}
Combining:
{"fields":{"id":true},"where":{"id":{"inq":[10,20,30]}}}
The above code works in swagger. Node.js code would be as follows:
var m = server.models.customer;
m.findOne({
fields: ['name', 'email'],
where: {
id:{inq:[10,20,30]}}
}
}, function (err, data) {
console.log(data);
})