How to use interface and default parameters together? - interface

Code below:
enum Type { digit=1, alpha=2, alnum=3 }
enum Transform{uppercase=1,lowercase}
interface Options {
type: Type;
length: number;
min: number;
max: number;
uppercase: boolean;
lowercase: boolean;
transform: Transform;
valueChange: (element:Object,value:string) => string;
}
class InputFilter {
constructor(private options: Options, private element: Object) {
}
}
I wanna to make options not only have interface,also have defaulte value,just like this:
options = {
"type": "alnum",
"length": null,
"min": 0,
"max": Infinity,
"uppercase": true,
"lowercase": true,
"transform": null,
"valueChange": function(element, value) {}
};
How can I do that?

You will need to create a factory method or function somewhere that will create an object with the default values that conforms to your interface.
Here's an example:
function createOptionsWithDefaultValues(): Options {
return {
type: Type.alnum,
length: null,
min: 0,
max: Infinity,
uppercase: true,
lowercase: true,
transform: null,
valueChange: function(element, value) {
return null;
}
};
}
let options = createOptionsWithDefaultValues();
// use options...

Related

Prisma Client Select query on existence of value in joined table via Schema

In my instance i have a schema joining bonuses to a casino. query works great for data but I am unable to filter via the query itself. The where clause I used appears to be correct but I get an error the stating Object literal may only specify known properties, and 'nodeposit' does not exist in type. But I can query that data.
const data = await prisma.casino_p_casinos.findMany({
where: {
approved: 1,
rogue: 0,
bonuses: {
nodeposit: { gt : 0 },
}
},
select: {
id: true,
clean_name: true,
casino: true,
button: true,
bonuses: {
where: {
nodeposit: { gt: 0 },
},
},
},
take: 14,
});
If I remove the bonus pard in the WHERE clause the query works as expected but I want to grab all bonuses for each casino, but only if the bonuses contains a nodeposit value.
This nis what I want to use.
const data = await prisma.casino_p_casinos.findMany({
where: {
approved: 1,
rogue: 0,
bonuses: {
nodeposit: { gt : 0 },
},
},
select: {
id: true,
clean_name: true,
casino: true,
button: true,
bonuses: true,
},
take: 14,
});
SCHEMA :
model casino_p_casinos {
id Int #id #default(autoincrement())
casino String?
type String?
url String?
bonuses casino_p_bonus[]
model casino_p_bonus {
id Int #id #default(autoincrement())
parent Int
game String?
freespins Int?
freeplay String?
nodeposit Int?
deposit Int?
casino_p_casinos casino_p_casinos #relation(fields: [parent], references: [id])
}
You have a one to many relation, so when you add a where clause, you have one more layer with some, every or none like
const data = await prisma.casino_p_casinos.findMany({
where: {
approved: 1,
rogue: 0,
bonuses: {
// 'some' can be replaced by 'every' or 'none' here
some: {
nodeposit: { gt: 0 }
}
}
},
select: {
id: true,
clean_name: true,
casino: true,
button: true,
bonuses: true
},
take: 14
})
This query will filter casinos where some nodeposit are greater than 0 and return all bonuses, even those who are equals to 0.
And then, if you only want bonuses with nodeposit greater than 0 in casinos that have some, you should do:
const data = await prisma.casino_p_casinos.findMany({
where: {
approved: 1,
rogue: 0,
bonuses: {
// 'some' can be replaced by 'every' or 'none' here
some: {
nodeposit: { gt: 0 }
}
}
},
select: {
id: true,
clean_name: true,
casino: true,
button: true,
bonuses: {
where: {
nodeposit: { gt: 0 }
}
}
},
take: 14
})

Mongoose specify optional array of objects

One of the keys in my mongo collection is
options: [
new mongoose.Schema(
{
answer: {
type: String,
required: true,
},
value: {
type: Number,
min: -10,
max: 10,
required: true,
},
},
{ _id: false }
),
],
The issue I'm having here is that options is optional but when there's no options field filled out, the inserted document has options: []
I believe I'm able to solve this normally by putting a default: undefined but I'm not sure how to go about doing this for this array of objects.
Thanks!
In mongoose an empty array is a default value for an array type. You can override it by using default field this way:
let optionsSchema = new mongoose.Schema(
{
answer: {
type: String,
required: true,
},
value: {
type: Number,
min: -10,
max: 10,
required: true,
},
},
{ _id: false });
const RootSchema = new Schema({
options : {
type: [optionsSchema],
default: undefined
}
})

mongoose - how to get a schema's final document without an insertion

Say I have a schema like this.
{
__content: {
default: "",
index: true,
type: Mixed,
validate: {
validator(v)
{
return !!(
typeof v === "string" ||
(
typeof v === "object" &&
!Array.isArray(v)
)
)
}
}
},
__hidden: {
default: false,
index: true,
type: Boolean
},
__title: {
required: true,
index: true,
type: String,
},
__type: {
default: "text",
enum: ["text", "table"],
index: true,
type: String
},
}
Is it possible to return what the schema would be like if I made a blank insert e.g. Model.create({}) without an actual insertion? Right now, my idea is to insert it into a throwaway collection and just get the return

Save array of objects in Postgresql using Sequalize.js

I have comething like this
[{
"id":"3",
"clientName":"John Doe",
"address":"street, 15",
"latitude":"50.1212",
"longitude":"30.1111",
"timeFrom":"2017-04-05T14:48:00.000Z",
"timeTo":"2017-04-05T15:48:00.000Z",
"comments":"call before delivery"
}]
This is my part of my model:
Order = sequelize.define('Order', {
latitude: {
type: DataTypes.FLOAT,
allowNull: false,
defaultValue: null,
validate: {
min: -90,
max: 90
}
},
longitude: {
type: DataTypes.FLOAT,
allowNull: false,
defaultValue: null,
validate: {
min: -180,
max: 180
}
},
{
classMethods: {
associate: function (models) {
Order.belongsTo(models.User);
}
}
});
return Order;
How to write array of object via Sequalize.js? If I'm trying to write object, it's pass fine, but with [{},{}] got some troubles
You'll want to start here with .bulkCreate() - http://docs.sequelizejs.com/en/v3/docs/instances/#working-in-bulk-creating-updating-and-destroying-multiple-rows-at-once
It can take an array of objects and write them all as long as the keys in the object match the column names.

Kendo UI Grid create data not making it to controller

I am having difficulty getting data to my controller using the MVVM method as shown in this Kendo Dojo example
I can see in my parameterMap function that the data is in the options.models but when I look for data at the controller, FAC_FuelReceipts is null. I can manually us an ajax call but I want this to work "Out of the Box" first. What am I doing wrong?
Grid:
$("#grid").kendoGrid({
height: 430,
columns: [
{ field: "FuelReceiptID" },
{ field: "ReceiptDate", title: "Receipt Date", width: 110, format: "{0:MM/dd/yyyy}" },
{ field: "FuelType", title: "Fuel Type", width: 110, editor: fuelTypeDropDownEditor },
{ field: "Qty", width: 110 },
{ field: "ReceivedBy", width: 110 }
],
editable: true,
pageable: true,
sortable: true,
filterable: true,
navigatable: true,
toolbar: ["create", "save", "cancel"],
dataSource: viewModel.receipts
});
ViewModel Code:
var viewModel;
$(function () { //On Ready
viewModel = kendo.observable({
receipts: new kendo.data.DataSource({
schema: {
model: {
id: "FuelReceiptID",
fields: {
FuelReceiptID: { editable: false, nullable: true },
ReceiptDate: { type: "date", validation: { required: true } },
FuelType: { type: "string", defaultValue:"Diesel" },
Qty: { type: "number", validation: { required: true } },
ReceivedBy: { type: "string" }
}
}
},
batch:true,
transport: {
read: {
cache:false,
url: "/Fuels/GetFuelReceipts",
dataType: "json"
},
create: {
url: "/Fuels/Create",
dataType: "json",
type: "POST"
},
parameterMap:function(options,operation){
if (operation == "read") {
return{
SiteID: SiteID,
ReceiptMonth: ReceiptMonth,
ReceiptYear: ReceiptYear
}
}
if (operation !== "read" && options.models) {
return { FAC_FuelReceipts: kendo.stringify(options.models) };
}
} //parameterMap fuction
} //transport
})
});
Controller Code:
[HttpPost]
public JsonResult Create(IEnumerable<FAC_FuelReceipts> FAC_FuelReceipts) //**empty here**
{
//Do something with data here
return Json(FAC_FuelReceipts, JsonRequestBehavior.AllowGet);
}
Use String instead of IEnumerable, As your parameter data is in string format.
Once you get data in string format deserialize into your object
[HttpPost]
public JsonResult Create(string FAC_FuelReceipts)
{
IList<FAC_FuelReceipts> Items= new JavaScriptSerializer().Deserialize<IList<FAC_FuelReceipts>>(FAC_FuelReceipts);
/**your code*/
return Json(FAC_FuelReceipts);
}