I'm new in Loopback 3.
And I need to define the model with the unique field.
Email field should be unique. I'm using Postgresql as DB.
I have tried adding "unique": true option. Also, I have tried to follow these pieces of advice: Ensure unique field value in loopback model. But it didn't give the desired result.
"properties": {
"id": {
"type": "number",
"id": true,
"generated": true,
"postgresql": {
"dataType": "bigint"
}
},
"name": {
"type": "string",
"postgresql": {
"dataType": "character varying"
}
},
"email": {
"type": "varchar",
"postgresql": {
"dataType": "character varying"
}
},
"added_date": {
"type": "date",
"postgresql": {
"dataType": "date"
}
}
}
In the end result, I want to have a unique field in Postgres scheme.
It should look like this in Postgres :
-- Table: public."user"
-- DROP TABLE public."user";
CREATE TABLE public."user"
(
id bigint NOT NULL DEFAULT nextval('user_id_seq'::regclass),
name character varying COLLATE pg_catalog."default",
email character varying COLLATE pg_catalog."default",
added_date date,
CONSTRAINT user_pkey PRIMARY KEY (id),
CONSTRAINT user_email_key UNIQUE (email)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public."user"
OWNER to postgres;
I think these will work. Either add the index property to the column def, or add it to the indexes portion of the model. It's weird there isn't more documentation on this.
"properties": {
"id": {
"type": "number",
"id": true,
"generated": true,
"postgresql": {
"dataType": "bigint"
}
},
"name": {
"type": "string",
"postgresql": {
"dataType": "character varying"
}
},
"email": {
"type": "varchar",
"postgresql": {
"dataType": "character varying"
}
},
"added_date": {
"type": "date",
"postgresql": {
"dataType": "date"
}
}
},
"indexes": {
"EMAIL_INDEX": {
"columns": "email",
"kind": "unique"
}
}
Or alternatively
"properties": {
"id": {
"type": "number",
"id": true,
"generated": true,
"postgresql": {
"dataType": "bigint"
}
},
"name": {
"type": "string",
"postgresql": {
"dataType": "character varying"
}
},
"email": {
"type": "varchar",
"postgresql": {
"dataType": "character varying"
},
"index": {"kind": "UNIQUE"}
},
"added_date": {
"type": "date",
"postgresql": {
"dataType": "date"
}
}
}
Related
I want to convert json schema to avro schema using python because I'm building my microservice in Python Fastapi.
json-schema
{
"type":"object",
"properties":{
"IsNonPO":{
"title":"IsNonPO",
"type":[
"boolean",
"null"
],
"precision":null,
"scale":null,
"size":null,
"allowedValues":null
},
"ApprovedState":{
"title":"ApprovedState",
"type":[
"number",
"null"
],
"precision":null,
"scale":null,
"size":null,
"allowedValues":[
{
"key":"8",
"value":"Invalid"
},
{
"key":"1",
"value":"Composing"
},
{
"key":"2",
"value":"Submitted"
},
{
"key":"4",
"value":"Approved"
},
{
"key":"16",
"value":"Denied"
}
]
},
"CreateDate":{
"title":"CreateDate",
"type":[
"string",
"null"
],
"precision":null,
"scale":null,
"size":null,
"allowedValues":null,
"format":"date-time"
},
"RemitToAddress": {
"type": ["object", "null"],
"properties": {
"State": {
"title": "RemitToAddress.State",
"type": ["string", "null"],
"precision": null,
"scale": null,
"size": 50,
"allowedValues": null
},
"Phone": {
"title": "RemitToAddress.Phone",
"type": ["string", "null"],
"precision": null,
"scale": null,
"size": 70,
"allowedValues": null
},
"Country": {
"type": ["object", "null"],
"properties": {
"UniqueName": {
"title": "RemitToAddress.Country.UniqueName",
"type": ["string", "null"],
"precision": null,
"scale": null,
"size": 50,
"allowedValues": null
}
}
},
"PostalCode": {
"title": "RemitToAddress.PostalCode",
"type": ["string", "null"],
"precision": null,
"scale": null,
"size": 50,
"allowedValues": null
},
"City": {
"title": "RemitToAddress.City",
"type": ["string", "null"],
"precision": null,
"scale": null,
"size": 50,
"allowedValues": null
},
"Fax": {
"title": "RemitToAddress.Fax",
"type": ["string", "null"],
"precision": null,
"scale": null,
"size": 70,
"allowedValues": null
},
"UniqueName": {
"title": "RemitToAddress.UniqueName",
"type": ["string", "null"],
"precision": null,
"scale": null,
"size": 50,
"allowedValues": null
},
"Lines": {
"title": "RemitToAddress.Lines",
"type": ["string", "null"],
"precision": null,
"scale": null,
"size": 1024,
"allowedValues": null
},
"Name": {
"title": "RemitToAddress.Name",
"type": ["string", "null"],
"precision": null,
"scale": null,
"size": 128,
"allowedValues": null
}
}
}
}
}
Avro schema
{
"type":"record",
"name":"invoice",
"namespace":"com.xyz.com",
"fields":[
{
"name":"IsNonPO",
"type":[
"null",
"boolean"
]
},
{
"name":"ApprovedState",
"type":[
"null",
"long"
]
},
{
"name":"CreateDate",
"type":[
"null",
{
"type":"string",
"logicalType":"timestamp-micros"
}
]
},
{
"name":"RemitToAddress",
"type":[
{
"type":"record",
"name":"RemitToAddress",
"namespace":"com.xyz.com.invoice",
"fields":[
{
"name":"City",
"type":[
"null",
"string"
]
},
{
"name":"Country",
"type":[
{
"type":"record",
"name":"Country",
"namespace":"com.xyz.com.invoice.RemitToAddress",
"fields":[
{
"name":"UniqueName",
"type":[
"null",
"string"
]
}
]
},
"null"
]
},
{
"name":"Fax",
"type":[
"null",
"string"
]
},
{
"name":"Lines",
"type":[
"null",
"string"
]
},
{
"name":"Name",
"type":[
"null",
"string"
]
},
{
"name":"Phone",
"type":[
"null",
"string"
]
},
{
"name":"PostalCode",
"type":[
"null",
"string"
]
},
{
"name":"State",
"type":[
"null",
"string"
]
},
{
"name":"UniqueName",
"type":[
"null",
"string"
]
}
]
},
"null"
]
}
]
}
I tried to find the converter in python but I can't. I found a converter but does it in java. Please let me know if there are any Python converter exists or do I write of my own library?
I have A log message in Kibana that contains this:
org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:597)
Actual search that isn't returning results: log_message: "hibernate3"
If I search for "hibernate3" this message will not appear. I am using an Elasticsearch template and have indexed the field, but also want to be able to do case-insensitive full-text searching. Is this possible?
Template that is in use:
{
"template": "filebeat-*",
"mappings": {
"mainProgram": {
"properties": {
"#timestamp": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"#version": {
"type": "text"
},
"beat": {
"properties": {
"hostname": {
"type": "text"
},
"name": {
"type": "text"
}
}
},
"class_method": {
"type": "text",
"fielddata": "true",
"index": "true"
},
"class_name": {
"type": "text",
"fielddata": "true"
},
"clientip": {
"type": "ip",
"index": "not_analyzed"
},
"count": {
"type": "long"
},
"host": {
"type": "text",
"index": "not_analyzed"
},
"input_type": {
"type": "text",
"index": "not_analyzed"
},
"log_level": {
"type": "text",
"fielddata": "true",
"index": "true"
},
"log_message": {
"type": "text",
"index": "true"
},
"log_timestamp": {
"type": "text"
},
"log_ts": {
"type": "long",
"index": "not_analyzed"
},
"message": {
"type": "text"
},
"offset": {
"type": "long",
"index": "not_analyzed"
},
"query_params": {
"type": "text",
"index": "true"
},
"sessionid": {
"type": "text",
"index": "true"
},
"source": {
"type": "text",
"index": "not_analyzed"
},
"tags": {
"type": "text"
},
"thread": {
"type": "text",
"index": "true"
},
"type": {
"type": "text"
},
"user_account_combo": {
"type": "text",
"index": "true"
},
"version": {
"type": "text"
}
}
},
"access": {
"properties": {
"#timestamp": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"#version": {
"type": "text"
},
"beat": {
"properties": {
"hostname": {
"type": "text"
},
"name": {
"type": "text"
}
}
},
"clientip": {
"type": "ip",
"index": "not_analyzed"
},
"count": {
"type": "long",
"index": "not_analyzed"
},
"host": {
"type": "text",
"index": "true"
},
"input_type": {
"type": "text",
"index": "not_analyzed"
},
"log_timestamp": {
"type": "text"
},
"log_ts": {
"type": "long",
"index": "not_analyzed"
},
"message": {
"type": "text"
},
"offset": {
"type": "long",
"index": "not_analyzed"
},
"query_params": {
"type": "text",
"index": "true"
},
"response_time": {
"type": "long"
},
"sessionid": {
"type": "text",
"index": "true"
},
"source": {
"type": "text",
"index": "not_analyzed"
},
"statuscode": {
"type": "long"
},
"tags": {
"type": "text"
},
"thread": {
"type": "text",
"index": "true"
},
"type": {
"type": "text",
"index": "true"
},
"uripath": {
"type": "text",
"index": "true"
},
"user_account_combo": {
"type": "text",
"index": "true"
},
"verb": {
"type": "text",
"index": "true"
}
}
}
}
}
message: *.hibernate3.*
also works (please note, that no quotes are needed for that)
According to your scenario, what you're looking for is an analyzed type string which would first analyze the string and then index it. A quote from the doc.
In other words, index this field as full text.
Thus make sure that, you have your mapping of the necessary fields properly so that you'll be able to do a full-text search on the docs.
Assuming that, in Kibana if the log line is under the field message, you could simply search for the word by:
message:"hibernate3"
You might also want to refer this, to identify the variance between Term Based and Full-Text.
EDIT
Have the mapping of the field log_message as such:
"log_message": {
"type": "string", <- to make it analyzed
"index": "true"
}
Also try doing a wildcard search as such:
{"wildcard":{"log_message":"*.hibernate3.*"}}
With Kibana 6.4.1 I used the "%" as wildcard.
message: %hibernate3%
For me it was because I was using the ".keyword".
My key was called "message" and I had "message" and "message.keyword" available.
Full text search isn't working on ".keyword".
Not working :
message.keyword : hello
Working :
message : hello
Hi i am very new to loopback postgresql connector. I need date datatype without timezone.
Now my model.json is as follows:
"properties": {
"start_date": {
"type": "date",
"required": true
},
"end_date": {
"type": "date",
"required": true
},
"amount": {
"type": "number",
"required": true
},
"description": {
"type": "string",
"required": true
},
"amount_saved": {
"type": "number",
"required": true
}
}
Now the date is storing as 2015-02-01T00:00:00.000Z but i need 2015-02-01 to be stored.. Please help me to solve this. Thanks in advance.
Use the database mapping to achieve this. You can then map to a native data type - for instance here are the PostgreSQL date/time datatypes.
In your example, you just need to map the field to the PostgreSQL date data type:
"start_date": {
"type": "date",
"required": true,
"postgresql": {
"columnName": "StartDate",
"dataType": "date",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "YES"
}
}
I want to establish a relation between two models, following the json files:
{
"name": "Surveyor",
"plural": "surveyors",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mongodb": {
"collection": "surveyors"
},
"settings": {
"mongodb": {
"allowExtendedOperators": true
}
},
"properties": {
"name": {
"type": "string",
"required": true
},
"surname": {
"type": "string",
"required": true
},
"dateOfBirth": {
"type": "date"
},
"birthPlace": {
"type": "string"
},
"provinceOfBirth": {
"type": "string"
},
"gender": {
"type": "string"
},
"registrationNumber": {
"type": "string",
"required": true,
"unique": true
}
},
"validations": [],
"relations": {}
},
"acls": [],
"methods": {}
}
{
"name": "Dossier",
"plural": "dossiers",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mongodb": {
"collection": "dossiers"
},
"settings": {
"mongodb": {
"allowExtendedOperators": true
}
},
"properties": {
"delCode": {
"type": "string",
"required": true
},
"client": {
"name": {
"type": "string",
"required": true
},
"surname": {
"type": "string",
"required": true
},
"taxCode": {
"type": "string"
}
},
"surveyor": {
"type": "string"
},
"examination": {
"type": "object"
},
"appointment": {
"type": "date"
},
"status": {
"type": "string",
"required": true
},
"notes": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
One dossier can have one surveyor, as key I want to use the property surveyor in the Dossier Model, corresponding to the property registrationNumber in the Surveyor Model.
I want to query the dossier Model and obtain the surveyor data in the dossier doc:
[GET] /api/dossiers
or with a filter:
[GET] /api/dossiers?filter={"include":"surveyor"}
if it is possible I want to configure the relation in the json file, not in the code.
You can use the relation generator to create that for you. In your case you're going to need to create a belongsTO relation from Dossier to Surveyor. If you want to query the other way around (dossiers from a surveyor) you're going to need to create a hasMany relation from Surveyor to Dossier.
In short, for what you've asked, you're going to end up with something like this:
In Dossier model:
...
"relations": {
"surveyor": {
"type": "belongsTo",
"model": "Surveyor",
"foreignKey": "surveyor",
"primaryKey": "registrationNumber"
}
}
...
My models.json:
My models.json
"masterTrip": {
"options": {
"relations": {
"trips": {
"type": "hasMany",
"model": "trip",
"foreignKey": "masterTripId"
}
}
}
},
"trip": {
"options": {
"relations": {
"masterTrips": {
"type": "belongsTo",
"model": "masterTrip",
"foreignKey": "masterTripId"
}
}
}
},
But I do not get the relation between the trip and mastertrip. can anybody explain?
There are a couple things that might be the problem. Here is what comes to mind:
You should only need the foreign key on the model that belongsTo the other one. (In this case, that would be trip.)
Have you actually created trips underneath masterTrip? (Either in the code itself or via the API?) I know this sounds silly, but the context wasn't clear enough for me to tell if you had created the sample data or not.
It sounds like you might actually be getting data when you do a GET on /masterTrip/1/trips Is that right? If so , then that sounds like the correct behavior.
I'm still relatively new to LoopBack myself, but I'm not sure that filter[include]=belongsToRelationName is the correct way to get the data you want. Technically, you are just looking for the associated array of hasMany data, right? In this case, trips that belongTo masterTrip. The RESTful way to get that would be masterTrip/{id}/trips
Hope one of those helps.
Your "belongs to" relation name is not singular. It should be singular.
When you making " belongs to " relation name is singular and for hasMany your relation name in plural. Please see the official documentation for more details -
See this working example below -
{
"name": "Booking",
"base": "PersistedModel",
"strict": false,
"idInjection": true,
"properties": {
"myuserId": {
"type": "number"
},
"orgin": {
"type": "string"
},
"orgin_lat": {
"type": "string"
},
"orgin_lon": {
"type": "string"
},
"destination": {
"type": "string"
},
"dest_lat": {
"type": "string"
},
"dest_lon": {
"type": "string"
},
"parcel_type": {
"type": "string"
},
"volume": {
"type": "string"
},
"weight": {
"type": "string"
},
"price": {
"type": "string"
},
"receiver_phn": {
"type": "string"
},
"payment_mode": {
"type": "string"
},
"booking_status": {
"type": "string"
},
"lang": {
"type": "string"
},
"booking_no": {
"type": "string"
},
"cancel_reason": {
"type": "string"
},
"cancel_by": {
"type": "string"
},
"booking_date": {
"type": "string"
},
"plan_later": {
"type": "string"
},
"plan_date": {
"type": "string"
},
"created": {
"type": "string"
},
"modified": {
"type": "string"
}
},
"validations": [],
"relations": {
"biddings": {
"type": "hasMany",
"model": "Bidding",
"foreignKey": "bookingId"
}
},
"acls": [],
"methods": {}
}
{
"name": "Bidding",
"base": "PersistedModel",
"strict": false,
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"myuserId": {
"type": "number"
},
"bookingId": {
"type": "number"
},
"booking_no": {
"type": "string"
},
"price": {
"type": "string"
},
"message": {
"type": "string"
},
"bid_date": {
"type": "string"
},
"bid_time": {
"type": "string"
},
"bid_status": {
"type": "string"
},
"lang": {
"type": "string"
},
"rate_driver": {
"type": "number"
},
"created": {
"type": "string"
},
"modified": {
"type": "string"
}
},
"validations": [],
"relations": {
"booking": {
"type": "belongsTo",
"model": "Booking",
"foreignKey": "bookingId"
},
"myuser": {
"type": "belongsTo",
"model": "Myuser",
"foreignKey": "myuserId"
}
},
"acls": [],
"methods": {}
}