- Sep 2024
-
-
And if I need for simple service object without validation? You can use Formalism::Action, a parent of Formalism::Form.
-
- May 2024
-
suu.instructure.com suu.instructure.com
-
Failure to assemble an appropriate IEP team:
Tags
Annotators
URL
-
- Jan 2024
-
mongoosejs.com mongoosejs.com
-
Instance methods Instances of Models are documents. Documents have many of their own built-in instance methods. We may also define our own custom document instance methods. // define a schema const animalSchema = new Schema({ name: String, type: String }, { // Assign a function to the "methods" object of our animalSchema through schema options. // By following this approach, there is no need to create a separate TS type to define the type of the instance functions. methods: { findSimilarTypes(cb) { return mongoose.model('Animal').find({ type: this.type }, cb); } } }); // Or, assign a function to the "methods" object of our animalSchema animalSchema.methods.findSimilarTypes = function(cb) { return mongoose.model('Animal').find({ type: this.type }, cb); }; Now all of our animal instances have a findSimilarTypes method available to them. const Animal = mongoose.model('Animal', animalSchema); const dog = new Animal({ type: 'dog' }); dog.findSimilarTypes((err, dogs) => { console.log(dogs); // woof }); Overwriting a default mongoose document method may lead to unpredictable results. See this for more details. The example above uses the Schema.methods object directly to save an instance method. You can also use the Schema.method() helper as described here. Do not declare methods using ES6 arrow functions (=>). Arrow functions explicitly prevent binding this, so your method will not have access to the document and the above examples will not work.
Certainly! Let's break down the provided code snippets:
1. What is it and why is it used?
In Mongoose, a schema is a blueprint for defining the structure of documents within a collection. When you define a schema, you can also attach methods to it. These methods become instance methods, meaning they are available on the individual documents (instances) created from that schema.
Instance methods are useful for encapsulating functionality related to a specific document or model instance. They allow you to define custom behavior that can be executed on a specific document. In the given example, the
findSimilarTypes
method is added to instances of theAnimal
model, making it easy to find other animals of the same type.2. Syntax:
Using
methods
object directly in the schema options:javascript const animalSchema = new Schema( { name: String, type: String }, { methods: { findSimilarTypes(cb) { return mongoose.model('Animal').find({ type: this.type }, cb); } } } );
Using
methods
object directly in the schema:javascript animalSchema.methods.findSimilarTypes = function(cb) { return mongoose.model('Animal').find({ type: this.type }, cb); };
Using
Schema.method()
helper:javascript animalSchema.method('findSimilarTypes', function(cb) { return mongoose.model('Animal').find({ type: this.type }, cb); });
3. Explanation in Simple Words with Examples:
Why it's Used:
Imagine you have a collection of animals in your database, and you want to find other animals of the same type. Instead of writing the same logic repeatedly, you can define a method that can be called on each animal instance to find similar types. This helps in keeping your code DRY (Don't Repeat Yourself) and makes it easier to maintain.
Example:
```javascript const mongoose = require('mongoose'); const { Schema } = mongoose;
// Define a schema with a custom instance method const animalSchema = new Schema({ name: String, type: String });
// Add a custom instance method to find similar types animalSchema.methods.findSimilarTypes = function(cb) { return mongoose.model('Animal').find({ type: this.type }, cb); };
// Create the Animal model using the schema const Animal = mongoose.model('Animal', animalSchema);
// Create an instance of Animal const dog = new Animal({ type: 'dog', name: 'Buddy' });
// Use the custom method to find similar types dog.findSimilarTypes((err, similarAnimals) => { console.log(similarAnimals); }); ```
In this example,
findSimilarTypes
is a custom instance method added to theAnimal
schema. When you create an instance of theAnimal
model (e.g., a dog), you can then callfindSimilarTypes
on that instance to find other animals with the same type. The method uses thethis.type
property, which refers to the type of the current animal instance. This allows you to easily reuse the logic for finding similar types across different instances of theAnimal
model.
Tags
Annotators
URL
-
- Dec 2023
-
developers.google.com developers.google.com
-
A personalized button gives users a quick indication of the session status, both on Google's side and on your website, before they click the button. This is especially helpful to end users who visit your website only occasionally. They may forget whether an account has been created or not, and in which way. A personalized button reminds them that Sign In With Google has been used before. Thus, it helps to prevent unnecessary duplicate account creation on your website.
first sighting: sign-in: problem: forgetting whether an account has been created or not, and in which way
-
- Oct 2023
-
docdrop.org docdrop.org
-
-
for: Yuval Noah Harari, Hamas Israel war 2023, global liberal order, abused-abuser cycle, And not Or
-
summary
- In this interview, Yuval Noah Harari offers insights on the trap that Hamas's brutality has set for the Israeli army and reflections on the abused-abuser cycle.
- If Israeli government falls into that trap and inflicts unprecedented collateral damage, it will scupper the chance for peace for generations to come, and Hamas will have succeeded in its goal.
- Harari identifies a key insight that could help create better empathy between warring parties, to recognize the abused-abuser cycle and how we can be both abused AND abuser at the same time
- Harari reflects on the breakdown of the global liberal order, offering a simple yet insightful anthropological definition of the global liberal order showing how in spite of its many flaws, fundamentally is biologically humanistic at the core. What is needed is a way to decouple the harmful features the current form of it possesses
-
-
it's hard to people to understand that you can be victim and perpetrator at the 00:35:03 same time it's a very simple fact impossible to accept for most people either you're a victim or you're perpetrator there is no other but no usually we are both you know from the level of individuals how we behave in 00:35:17 our family to the level of entire nations we are usually both and and and of course perhaps one issue is that we don't feel like that as individuals we don't feel that we have the full responsibility for our state so there's 00:35:28 a sort of strange problem here too which is that you feel as an individual that you're a victim and you feel distance from your state
-
for: victim AND perpetrator, situatedness, perspectival knowing, AND, not OR, abused-abuser cycle, individual /collective gestalt, Hamas Israel war 2023
-
quote
- It's hard for people to understand that you can be victim and perpetrator at the same time
- It's a very simple fact impossible to accept for most people
- author: Yuval Noah Harari
- date: Sept 2023
-
-
-
peacemakers.beehiiv.com peacemakers.beehiiv.com
-
It’s not one or the other. It’s both.
-
for: conflict, Israel-Palestine conflict
-
comment
- it's "and", not "or"
-
Tags
Annotators
URL
-
- May 2022
-
wordpress.com wordpress.com
-
"I didn't fully understand it at the time, but throughout my time as a freshman at Boston College I've realized that I have the power to alter myself for the better and broaden my perspective on life. For most of my high school experience, I was holding to antiquated thoughts that had an impact on the majority of my daily interactions. Throughout my life, growing up as a single child has affected the way am in social interactions. This was evident in high school class discussions, as I did not yet have the confidence to be talkative and participate even up until the spring term of my senior year."
Tags
- Introduction p.1
- In this annotation, I choose to expand on my introduction. Before I explain why I chose the words I did, I should mention that my first draft failed to meet one of the assignment's primary requirements: a "Story like" structure. Finally, I decided to rework my introduction because my first draft did not begin with a clear beginning. Instead, I started by describing the fundamental context of the encounter before detailing my previous experiences. To improve my final edit, I made sure I described my experiences and/or how I felt before they occurred.
- (Major Essay) Introduction paragraph
Annotators
URL
-
- Apr 2021
-
github.com github.com
-
I don't believe the sprockets and sprockets-rails maintainers (actually it's up to the Rails maintainers, see rails/rails#28430) currently consider it broken. (I am not a committer/maintainer on any of those projects). So there is no point in "waiting for someone else to fix" it; that is not going to happen (unless you can change their minds). You just need to figure out the right way to use sprockets 4 with rails as it is.
Tags
- frustrating when maintainers stubbornly stick to opinions/principles/decisions and won't change despite popular user support
- at the mercy of maintainer
- waiting for someone else to fix it: that is not going to happen
- whether maintainer or contributor should/will implement something
- whose responsibility is it?
Annotators
URL
-
- Mar 2021
-
www.chevtek.io www.chevtek.io
-
I would much rather have a "cosine" module than a "trigonometry" module because chances are good I only need a small fraction of the utilities provided by the larger trig module.
-
Second, I don't agree that there are too many small modules. In fact, I wish every common function existed as its own module. Even the maintainers of utility libraries like Underscore and Lodash have realized the benefits of modularity and allowed you to install individual utilities from their library as separate modules. From where I sit that seems like a smart move. Why should I import the entirety of Underscore just to use one function? Instead I'd rather see more "function suites" where a bunch of utilities are all published separately but under a namespace or some kind of common name prefix to make them easier to find. The way Underscore and Lodash have approached this issue is perfect. It gives consumers of their packages options and flexibility while still letting people like Dave import the whole entire library if that's what they really want to do.
-