Can I have camel case as default when using postgres in loopback? - loopbackjs

If you don't specify anything all the fields are created as lower case in the database when using postgres. Is it possible to change the default behavior to use the exact name of the fields in the model? That makes it easier to write custom queries.
As it is now I have to configure the property on each field to say that they should be camel case, and that is quite error prone since that is something that is easy to forget.
If that isn't possible, is it possible to use the functionality in the repository that does the mapping from all lowercase to the fields in the models in an easy manner somehow?

Not sure if this helps, but you can use name property
example:
export class User extends .... { #property({
type: 'number',
id: true, }) id?: number;
#property({
type: 'string',
required: true,
name: 'first_name',
})
firstName: string;
#property({
type: 'string',
name: 'last_name',
})
lastName: string;

Related

How to add a unique constraints through Loopback 4 Model?

I have a Department model in my project. The properties are id, name. name of course should be unique. However I can't seem to look where the setting is so I can put something like
#model()
export class Department extends Entity {
#property({
type: 'number',
id: true,
generated: true,
})
id?: number;
#property({
type: 'string',
required: true,
unique: true // ---> This is what I want. But how?
})
name: string;
constructor(data?: Partial<Department>) {
super(data);
}
}
I tried digging to Model documentation, it seems that there is something I can do with the #model decorator. However I found no documentation about it.
Also, I want to do this in PostgreSQL as a datasource. Any clue? Any advice would be appreciated.
#property({
type: 'string',
required: true,
index: {
unique: true,
}
})
name: string;
Using index object.
Migrate Database - Modify or alter table
run command : npm run build
migrate database : npm run migrate

Loopback 4: non-primitive properties instead of relations

Using Loopback 4, I want to use the type-safety of Typescript in my application code while getting the schema validation from the repository decorators, but store a property as serialized json (in this case, Postgres bson) instead of as a separate entity with a FK relation.
Consider a address book Contact model that could have a list of phone numbers:
#model()
export class PhoneNumber {
#property({ required: true })
number: string;
#property()
type: string;
}
#model()
export class Contact extends Entity {
#property({ id: true, required: true })
id: string;
#property({ required: true })
email: string;
#property({ required: true })
name: string;
#property.array(PhoneNumber)
phoneNumbers: PhoneNumber[];
constructor(data?: Partial<Contact>) {
super(data);
}
}
In the above example, I get full schema validation, but if I try to save an instance of Contact using a generated Repository that extends DefaultCrudRespository, it just drops whatever was provided in the phoneNumbers field and saves an empty column in the db.
If I change the property annotation to:
#property.array(Object)
phoneNumbers: PhoneNumber[];
It will save the field properly, serialized as json, but it won't attempt to validate the field, and also won't specify the type as an array PhoneNumber in the generated openapi.json spec
It seems that Loopback 3 had support for embedded models: https://loopback.io/doc/en/lb3/Embedded-models-and-relations.html
No mention of it in the Loopback 4 docs, though.
Try use the strict mode filter in the model PhoneNumber
#model({settings: {strict: "filter"}})
This make any unknow field be ignored when add in the database

DynamoDB AppSync field resolvers timing out

So I have the schema below. If I try to query data off this schema AppSync will time out saying 'NetworkError when attempting to fetch resource.'
type Model {
PartitionKey: ID!
SortKey: ID!
Name: String
Version: Int
FBX: String
# ms since epoch
CreatedAt: AWSTimestamp
Description: String
Tags: [Tag]
}
type ImageSet {
PartitionKey: ID!
SortKey: ID!
Name: String
CreatedAt: AWSTimestamp
Description: String
Tags: [String]
}
Now, if I change 'Name' in the model to 'ModelName' then queries on that will work. If I change 'Name' in ImageSet to 'SetName' then queries on that will work.
What is going on with this? What is wrong with the 'Name' field name? 'Description' and 'CreatedAt' do not have this issue.
Edit
Actually I am encountering this happening with other fields in the
schema as well. Please help.
I do have resolvers attached to specific fields. Removing them does
solve the problem. Am I not supposed to attach revolvers to specific
fields or is something else wrong?
-
Edit 2
This really does seem to only occur if the name of a field is shared
between different schema objects, is that not allowed!?

Ember Data: How do I know if a relationship was changed?

I'm writing a custom data adapter (doesn't talk to webserver) and I need to know whether hasMany relationship was changed or not to persist relationship data.
App.Note = DS.Model.extend({
content: attr('string'),
createdAt: attr('date'),
tags: DS.hasMany('tag', { async: true })
});
App.Tag = DS.Model.extend({
name: DS.attr('string'),
notes: DS.hasMany('note')
});
I need to implement adapter's updateRecord:
updateRecord: function(store, type, record) {
// record.changedAttributes() returns changed attributes but not relationships
}
In my case list of tags attached to note may change (via note.get('tags').addObject(..) / removeObject(..)) so I need to get a diff. What's the best way to do that? I can remove all tags in a database and insert new tags but that's not efficient at all

Load but don't send embedded objects

Say you have the following model:
App.Item = DS.Model.extend({
owner: DS.belongsTo('App.Person', {embedded: true})
})
This means you can load this embedded association, but it also means that if you want to create a new Item for a person that already exists ember-data will also embed the Person object for every new item.
Is it possible to make it load embedded objects but when creating associations only send the ids? i.e send this instead:
{"item": {"owner_id": 5}}
Edit:
To clarify, I want ember-data to load embedded relations, but if I set {embedded: true}
this code:
App.Item.createRecord({name: 'Something', owner: App.Person.find(1)});
// And a few moments later when App.Person.find(1) has loaded
App.store.commit()
It will send the following json:
{ "item": {"name": "Something", owner: { id: 1, name: "whatever" }}
But what I want is:
{ "item": {"name": "Something", owner_id: 1 }}
Basically if I set embedded = true ember-data will also embed the assocations when you create an object.
If I correctly understand your aim, you shouldn't have to specify { embedded: true }. The default ember-data behavior is to be lazy.
It you are using active_model_serializers (which I strongly recommend to you), you should declare your server-side serializer as follow:
class ItemSerializer < ActiveModel::Serializer
embed :ids, include: false
#...
end