So I have three entity types:
addType
name: 'DaySchedule'
apiVersion: 3
dataProperties:
uid: "uid"
employee_id: "int"
day: "string"
shifts:
complexType: "Shift"
hasMany: true
navigationProperties:
employee:
type: "Employee"
assoc: "DayScheduleEmployee"
key: ["employee_id"]
addType
name: 'Employee'
dataProperties:
id: "id"
details:
complexType: "EmployeeDetails"
navigationProperties:
schedules:
entityTypeName: "DaySchedule"
hasMany: true
associationName: "DayScheduleEmployee"
attendanceDays:
entityTypeName: "AttendanceDay"
hasMany: true
associationName: "AttendanceDayEmployee"
apiSchemaHelper.addType
name: "AttendanceDay"
apiVersion: 3
dataProperties:
uid: "uid"
employee_id: "int"
date: "string"
shifts:
complexType: "AttendanceShift"
hasMany: true
navigationProperties:
employee:
type: "Employee"
assoc: "AttendanceDayEmployee"
key: ["employee_id"]
So as you see, employee has many DaySchedule and AttendanceDay objects.
If I create three objects of this type in the following order:
employee = createEntity "Employee", id: 20
daySchedule = createEntity "DaySchedule", employee_id: 20
attendanceDay = createEntity "AttendanceDay", employee_id: 20
Then all the navigationProperties are correctly made.
However if I put employee creation last:
daySchedule = createEntity "DaySchedule", employee_id: 20
attendanceDay = createEntity "AttendanceDay", employee_id: 20
employee = createEntity "Employee", id: 20
Then employee.schedules is as expected, but employee.attendanceDays is an empty array.
Ok, this was a bug and is now fixed in the breeze.js GitHub repo. This fix will also be included in the next full breeze.js release (1.4.14) out sometime next week.
Related
this is my sample data model
I have declared the following classes:
#Entity({
name: 'user'
})
export class User {
#Column({ type: 'int4' })
#PrimaryColumn()
userid: number
#Column({name: 'name', type: 'varchar', length: 30})
name: string
#Column({name: 'age', type: 'int2'})
age: number
#ManyToMany(() => Department, (department)=> department.users)
#JoinTable({
name: 'department_user'
})
departments: Department[]
}
#Entity({ name: 'department' })
export class Department {
#Column({ type: 'int2' })
#PrimaryColumn()
departmentid: number
#Column({type: 'varchar', length: 50})
title: string
#Column({type:'text'})
notes: string
#ManyToMany(() => User, (user)=> user.departments)
#JoinTable({ name: 'department_user' })
users: User[]
}
whenever I run the app, it creates the departmentDepartmentId & userUserId columns and not utilize the columns in the corresponding join table. How can I tell typeorm to only use the predefined join column in join table?
Update 2 (as mentioned by #suvantorw)
I recreated the join table with the statement below:
create table department_user(
departmentid integer not null,
userid integer not null);
alter table department_user add constraint fk_dept_dept foreign key (departmentid) references department(departmentid);
alter table department_user add constraint fk_dept_user foreign key (userid) references "user"(userid);
alter table department_user add constraint pk_dept_user primary key (departmentid, userid);
and modified the entities like this:
user
#ManyToMany(() => Department, (department)=> department.users)
#JoinTable({
name: 'department_user',
joinColumn: { name: 'userid' },
inverseJoinColumn: { name: 'departmentid' }
})
departments: Department[]
}
department
#ManyToMany(() => User, (user)=> user.departments)
#JoinTable({
name: 'department_user',
joinColumn: { name: 'departmentid' },
inverseJoinColumn: { referencedColumnName: 'userid' }
})
users: User[]
}
it does run without errors but when it runs the table structure is modified to this
As you can see, ,y foreign key constraints are gone and new ones are created. Any clue what I'm doing wrong here?
Update 3
Finally I modified the classes as below and now the TypeORM accepts the relationships and does not create its own. it was a very painful experience to solve this and documentation about this decorator doesn't say much either.
user
#ManyToMany(() => Department, (department)=> department.users)
#JoinTable({
name: 'department_user',
joinColumn: {
name: 'userid',
foreignKeyConstraintName: 'fk_dept_user'
},
inverseJoinColumn: {
referencedColumnName: 'departmentid',
name: 'departmentid',
foreignKeyConstraintName: 'fk_dept_dept'
}
})
departments: Department[]
}
department
#ManyToMany(() => User, (user)=> user.departments)
#JoinTable({
name: 'department_user',
joinColumn: {
name: 'departmentid',
foreignKeyConstraintName: 'fk_dept_dept'
},
inverseJoinColumn: {
referencedColumnName: 'userid',
name: 'userid',
foreignKeyConstraintName: 'fk_dept_user'
}
})
users: User[]
}
You can specify the join column name and inverse join column name. Something like this should work for you:
#ManyToMany(() => Department, (department)=> department.users)
#JoinTable({
name: 'department_user',
joinColumn: { name: 'userid' },
inverseJoinColumn: { name: 'departmentid' }
})
departments: Department[]
Stripe news API update for Connect accounts doesn't allow the "legal_entity" param for new stripe accounts. The new updated way is for "business_type".. But the issue i have is that I need to pass data from either of the 2 choices for business_type of "individual" or "company.
This is the old way that worked:
acct = Stripe::Account.create({
:country => stripe_account_params[:country],
:type => "custom",
legal_entity: {
first_name: stripe_account_params[:first_name].capitalize,
last_name: stripe_account_params[:last_name].capitalize,
type: stripe_account_params[:account_type],
dob: {
day: stripe_account_params[:dob_day],
month: stripe_account_params[:dob_month],
year: stripe_account_params[:dob_year]
},
address: {
line1: stripe_account_params[:address_line1],
city: stripe_account_params[:address_city],
state: stripe_account_params[:address_state],
postal_code: stripe_account_params[:address_postal]
},
ssn_last_4: stripe_account_params[:ssn_last_4]
},
tos_acceptance: {
date: Time.now.to_i,
ip: request.remote_ip
}
})
The new way (my attempt):
acct = Stripe::Account.create({
:country => stripe_account_params[:country],
:type => "custom",
:business_type => stripe_account_params[:account_type],
requested_capabilities: ['card_payments'],
# company: {
# name: stripe_account_params[:business_name],
# phone: stripe_account_params[:business_phone],
# phone: stripe_account_params[:business_tax_id],
# address: {
# line1: stripe_account_params[:business_address_line1],
# city: stripe_account_params[:business_address_city],
# state: stripe_account_params[:business_address_state],
# postal_code: stripe_account_params[:business_address_postal]
# },
# },
individual: {
address: stripe_account_params[:address_line1],
first_name: stripe_account_params[:first_name],
last_name: stripe_account_params[:last_name],
ssn_last_4: stripe_account_params[:ssn_last_4],
# phone: stripe_account_params[:business_tax_id],
dob: {
day: stripe_account_params[:dob_day],
month: stripe_account_params[:dob_month],
year: stripe_account_params[:dob_year]
},
address: {
line1: stripe_account_params[:address_line1],
city: stripe_account_params[:address_city],
state: stripe_account_params[:address_state],
postal_code: stripe_account_params[:address_postal]
},
},
tos_acceptance: {
date: Time.now.to_i,
ip: request.remote_ip
}
})
With the section i have commented out, not commented out, I get this error:
(If i choose individual with the commented out area, it will work)
I tried simply not defining the address, etc. and loosely having the params and see if Stripe will decide where they go and that didn't work so it seems as if they need to be defined like above but i don't know how to distinguish them.
You cannot provide both company and individual parameters. Only
provide them accordingly with the business_type on the account.
Now, the fields are named the same within stripe:
https://stripe.com/docs/api/accounts/create
So i am unsure how i can pass this through. Any suggestions on how to do this?
I am using Swagger Hub to generate API and wanting to get multiple responses for the get request: https://virtserver.swaggerhub.com/factly/test/1.0.0/categories
Following is how I defined my API. When I execute the API I only get one category in response. How do I get all the three categories defined as a response? Any help is greatly appreciated.
openapi: 3.0.0
info:
description: This is the sample API for Core
version: "1.0.0"
title: Dega Core API
tags:
- name: Core
description: Operations related to Core
paths:
/categories:
get:
tags:
- Core
summary: gets all the categories
description: this is to get all the available categories
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/CategoryItem'
examples:
category1:
$ref: '#/components/examples/category1'
category2:
$ref: '#/components/examples/category2'
category3:
$ref: '#/components/examples/category3'
components:
schemas:
CategoryItem:
type: object
required:
- id
- name
- slug
- createdDate
properties:
id:
type: string
format: uuid
example: d290f1ee-6c54-4b01-90e6-d701748f0851
name:
type: string
example: Category 1
slug:
type: string
example: category-1
description:
type: string
example: This is a sample category
parent:
type: string
example: null
createdDate:
type: string
format: date-time
example: '2016-08-29T09:12:33.001Z'
examples:
category1:
value:
id: d290f1ee-6c54-4b01-90e6-d701748f0851
name: Category 1
slug: category-1
description: This is the sample description for Category 1
parent: null
createdDate: '2016-08-29T09:12:33.001Z'
category2:
value:
id: d290f1ee-6c54-4b01-90e6-d701748f0851
name: Category 2
slug: category-2
description: This is the sample description for Category 2
parent: null
createdDate: '2016-08-29T09:12:33.001Z'
category3:
value:
id: d290f1ee-6c54-4b01-90e6-d701748f0851
name: Category 3
slug: category-3
description: This is the sample description for Category 3
parent: d290f1ee-6c54-4b01-90e6-d701748f0851
createdDate: '2016-08-29T09:12:33.001Z'
servers:
- url: 'https://virtserver.swaggerhub.com/factly/test/1.0.0'
The response I am expecting is the following:
[{
"id": "d290f1ee-6c54-4b01-90e6-d701748f0851",
"name": "Category 1",
"slug": "category-1",
"description": "This is the sample description for Category 1",
"createdDate": "2016-08-29T09:12:33.001Z"
},
{
"id": "d290f1ee-6c54-4b01-90e6-d701748f0851",
"name": "Category 2",
"slug": "category-2",
"description": "This is the sample description for Category 2",
"createdDate": "2016-08-29T09:12:33.001Z"
},
{
"id": "d290f1ee-6c54-4b01-90e6-d701748f0851",
"name": "Category 3",
"slug": "category-3",
"description": "This is the sample description for Category 3",
"parent": "d290f1ee-6c54-4b01-90e6-d701748f0851",
"createdDate": "2016-08-29T09:12:33.001Z"
}]
Thanks,
Shashi
So your response is an array of objects, and you want to specify the response example containing an array with multiple items.
There are several ways to specify examples for array responses, but in any case the example must be a complete example, that is you cannot $ref'erence parts of an example (such as the values of individual array items). In other words, the example value cannot be built from partial $refs.
You can put the example inside your array schema, alongside type: array:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/CategoryItem'
example:
- id: d290f1ee-6c54-4b01-90e6-d701748f0851
name: Category 1
slug: category-1
description: This is the sample description for Category 1
createdDate: '2016-08-29T09:12:33.001Z'
- id: d290f1ee-6c54-4b01-90e6-d701748f0851
name: Category 2
slug: category-2
description: This is the sample description for Category 2
createdDate: '2016-08-29T09:12:33.001Z'
- id: d290f1ee-6c54-4b01-90e6-d701748f0851
name: Category 3
slug: category-3
description: This is the sample description for Category 3
parent: d290f1ee-6c54-4b01-90e6-d701748f0851
createdDate: '2016-08-29T09:12:33.001Z'
Or add the example alongside the response schema:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/CategoryItem'
example:
- id: d290f1ee-6c54-4b01-90e6-d701748f0851
name: Category 1
slug: category-1
description: This is the sample description for Category 1
createdDate: '2016-08-29T09:12:33.001Z'
- id: d290f1ee-6c54-4b01-90e6-d701748f0851
...
Or if you want to specify a description for the example, use the examples keyword (plural) as shown below. (But examples are currently not displayed in Swagger UI.)
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/CategoryItem'
examples:
categoryWithThreeItems:
summary: Example of a category with three items
value:
- id: d290f1ee-6c54-4b01-90e6-d701748f0851
name: Category 1
slug: category-1
description: This is the sample description for Category 1
createdDate: '2016-08-29T09:12:33.001Z'
- id: d290f1ee-6c54-4b01-90e6-d701748f0851
...
or put the whole example to the components/example section and $ref it. Note again, we can $ref whole examples only but not parts of an example.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/CategoryItem'
examples:
categoryWithThreeItems:
$ref: '#/components/examples/categoryWithThreeItems'
components:
examples:
categoryWithThreeItems:
summary: Example of a category with three items
value:
- id: d290f1ee-6c54-4b01-90e6-d701748f0851
name: Category 1
slug: category-1
description: This is the sample description for Category 1
createdDate: '2016-08-29T09:12:33.001Z'
- id: d290f1ee-6c54-4b01-90e6-d701748f0851
...
Basically, I want to set up swagger to accept something like this in the request
[
{
"input_id": "",
"city": "",
"state": "",
"zipcode": "20500"
},
{
"input_id": "",
"city": "Cupertino",
"state": "CA",
"zipcode": ""
},
{
"input_id": "",
"city": "",
"state": "",
"zipcode": "95014"
},
{
"input_id": "Apple",
"city": "Cupertino",
"state": "ca",
"zipcode": "90210"
}
]
I am having some issues with my current setup
- name: testing
in: query
description: 'zip request info'
type: array
items:
type: object
properties:
input_id:
type: string
city:
type: string
state:
type: string
zipcode:
type: string
This sets the POST url to access these items like this
...testing[0][city]=burbank&testing[0][state]=ca...
When I want to access them like this
...city=burbank&state=ca... for each city/state/zipcode/input_id pair.
UPDATE:
my new setup is working, besides errors thrown by swagger. It works though!
- name: City/State pair or ZIP Code
in: body
description: 1 or more
type: array
items:
minimum: 1
type: object
properties:
input_id:
type: string
description: A unique identifier for this address used in your application; this field will be copied into the output
city:
type: string
description: The city name
state:
type: string
description: State name or abbreviation
zipcode:
type: string
description: The 5-digit ZIP Code
If anyone can find a better solution than this (without errors) let me know
I was almost correct in my edit. I just needed to add in schema: on the line before adding type: array
name: 'City/State pair or ZIP Code'
in: body
description: 1 or more
schema:
type: array
items:
type: object
properties:
input_id:
type: string
description: A unique identifier for this address used in your application; this field will be copied into the output
city:
type: string
description: The city name
state:
type: string
description: State name or abbreviation
zipcode:
type: string
description: 'The 5-digit ZIP Code'
This works and has no errors. I can add multiple objects to my array so it will post in the format I want
I have a has_many_and_belongs_to relationship between central and coordinators.
So my mongo documents are represented as below:
central = {
_id: 1,
title: 'First Name',
coordinators: [
BSON[1],
BSON[2],
BSON[3]
]
}
coordinators = [
{
_id: 1,
name: 'Me',
centrals: [BSON[1], BSON[2]]
},
{
_id: 1,
name: 'Mateus'
centrals: [BSON[1]]
},
{
_id: 1,
name: 'Gomes'
centrals: [BSON[1]]
},
]
If I do this:
#central = Central.find(1)
#coordinator = #central.coordinators.find(1)
#coordinator.can_edit = false
It will apply to the coordinators document resulting in this:
coordinator = {
_id: 1,
name: 'Me',
centrals: [BSON[1], BSON[2]],
can_edit: false
}
But what I really want to do is apply this can_edit attribute in the relationship, like in pivot table in RDBMS:
central = {
_id: 1,
titulo: 'First Name',
coordinators: [
{
_id: 1,
name: 'Me',
can_edit: false
},
BSON[2],
BSON[3]
]
}
Only for the central with id 1 I want to aply the can_edit to false.
I have to keep the relation between a Central and Coordinator, but in some situation, I want to have an additional information about that relation, like if I would not allow a coordinator to edit some data only in central with id 1.
How can I do this using mongoid?
The solution for this was create another relation N-N:
Add on central.rb
has_and_belongs_to_many :blocked_coordenadors,
class_name: "Central",
inverse_of: :blocked_centrals
And in coordinator.rb:
has_and_belongs_to_many :blocked_centrals,
class_name: "Central",
inverse_of: :blocked_coordenadors
And to check I do this:
central.blocked_coordenadors.include? coordinator