I am using #google-cloud/datastore to save data in my entity.I have created the entity with custom key name = id.
How can I have a custom UUID as the key of the entity?
ds.save({
id: <uuid>,
data: Log
}).then(function () {
console.log(entities.map(fromDatastore));
});
const dsKey = ds.key({
namespace : namespace,// optional
path : ([kindName, id])
})
The key can be generated like this. The namespace is optional, if not provided the default namespace will be used. In the path kindname should be provided(if there is a parent kind for the provided kind, the path array value should begin from the root parent name and id) and the id can be any id you assign. If the id is not provided, datastore will generate a random id and assign it to the entity you inserted.
If you meant that you wanted the UUId as the complete key, that is not possible.
Related
I am learning prisma and I can't figure out how to use the prisma types correctly if the returned data includes a sub model.
For example, I have the following two tables
model Services {
id Int #id #default(autoincrement())
service_name String #db.VarChar(255)
description String #db.MediumText
overall_status ServiceStatus #default(OPERATIONAL)
deleted Boolean #default(false)
sub_services SubServices[]
}
model SubServices {
id Int #id #default(autoincrement())
name String #db.VarChar(255)
description String #db.MediumText
current_status ServiceStatus #default(OPERATIONAL)
service Services? #relation(fields: [service_id], references: [id], onDelete: Cascade)
service_id Int?
}
I am then pulling data from the Services model using the following:
const services = await prisma.services.findMany({
where: {
deleted: false
},
include: {
sub_services: true
}
});
I am then in the client side referencing the Services model, but the IDE isn't detecting that Services can include sub_services. I can use it and it works but the IDE is always showing a squiggly line as if the code is wrong, example is below:
import {Services} from "#prisma/client";
const MyComponent : React.FC<{service: Services}> = ({services}) => {
return (
<>
service.sub_services.map(service => {
})
</>
)
}
but in the above example sub_services is underlined with the error TS2339: Property 'sub_services' does not exist on type 'Services'.
So how would I type it in a way that IDE can see that I can access sub_services from within services model.
UPDATE
I found a way to do it, but I'm not sure if this is the correct way or not as I am creating a new type as below:
type ServiceWithSubServices <Services> = Partial<Services> & {
sub_services: SubServices[]
}
and then change the const definition to the below
const ServiceParent : React.FC<{service: ServiceWithSubServices<Services>}> = ({service}) => {
Although this does seem to work, is this the right way to do it, or is there some more prisma specific that can do it without creating a new type.
In Prisma, by default only the scalar fields are included in the generated type. So, in your case for the Services type, all the scalar fields except sub_services would be included in the type. sub_services is not included because it's a relation field.
To include the relation fields, you would need to use Prisma.validator, here's a guide on generating types that include the relation field.
For some inexplicable reason, my project id is attached to the Key of my User entity:
<Key('User', 5703358593630208), project=my-project-id>
This is giving me issues, such as when I am trying to use this same key as an ancestor of another entity — I would get this error:
google.cloud.ndb.exceptions.BadValueError: Expected Key instance, got <Key('User', 5703358593630208), project=my-project-id>
I created the User entity like this:
from google.cloud import datastore
datastore_client = datastore.Client()
def save_user(name):
key = datastore_client.key('User')
user = datastore.Entity(key=key)
user.update({
'name': name,
'created': datetime.datetime.utcnow()
})
datastore_client.put(user)
Additional Example: Making an ancestral query
query = MyEntity.query(ancestor=user_key)
TypeError: ancestor must be a Key; received <Key('User', 5752652897976320), project=my-project-id>
What could be the explanation for this?
I believe the issue is that you are using both google.cloud.datastore and the NDB library and the key objects are not compatible. Here's an example of converting a datastore client key to an NDB key:
from google.cloud import datastore
from google.cloud import ndb
# Start with a google.cloud.datastore key
datastore_client = datastore.Client()
datastore_key = datastore_client.key('Parent', 'foo', 'User', 1234)
def key_to_ndb_key(key):
# Use flat_path property to create an ndb_key
key_path = key.flat_path
ndb_key = ndb.Key(*key_path)
return ndb_key
# Convert to a ndb key
ndb_client = ndb.Client()
with ndb_client.context() as context:
ndb_key = key_to_ndb_key(datastore_key)
print(ndb_key)
Entities are partitioned into subsets, currently identified by a project ID and namespace ID.
for more reference please check google doc and this.
I have a db in which I want to store data like this:
Books:
0:{pushed Object}
1:{pushed object} ...
As if it is an arrays so I can iterate over it and do the stuff I need to do. The problem is that I am storing data as pic related shows:
pic related
It is storing the object with the push_ID firebase assigns to an object but not with the index of an array. This is how I am pushing the object to my database:
let book_data = {
id: bookId,
name: bookData.name,
author: bookData.author,
genre: bookData.genre,
publishDate: bookData.publishDate
};
this.fb.list(`my-lists/${bookList.name}/books/`).push(bookData);
What am I doing wrong?
If needed, I'm coding this for an ionic project.
If you use push() method, Firebase assign it a unique ID without you doing anything else. so if you want a custom key that you defined, you can use set() to save data to a specified reference, also it can be replacing any existing data at that path.
let book_data = {
id: bookId,
name: bookData.name,
author: bookData.author,
genre: bookData.genre,
publishDate: bookData.publishDate
};
let yourkey = 0;
this.fb.list(`my-lists/${bookList.name}/books/${yourkey}`).set(bookData);
When you set new object next time, you have to change the key again and save data
I am writing an app using the Realm.io database that will pull data from another, server database. The server database has some tables whose primary keys are composed of more than one field. Right now I can't find a way to specify a multiple column key in realm, since the primaryKey() function only returns a String optional.
This one works:
//index
override static func primaryKey() ->String?
{
return "login"
}
But what I would need looks like this:
//index
override static func primaryKey() ->[String]?
{
return ["key_column1","key_column2"]
}
I can't find anything on the docs on how to do this.
Supplying multiple properties as the primary key isn't possible in Realm. At the moment, you can only specify one.
Could you potentially use the information in those two columns to create a single unique value that you could use instead?
It's not natively supported but there is a decent workaround. You can add another property that holds the compound key and make that property the primary key.
Check out this conversation on github for more details https://github.com/realm/realm-cocoa/issues/1192
You can do this, conceptually, by using hash method drived from two or more fields.
Let's assume that these two fields 'name' and 'lastname' are used as multiple primary keys. Here is a sample pseudo code:
StudentSchema = {
name: 'student',
primaryKey: 'pk',
properties: {
pk: 'string',
name: 'string',
lastname: 'string',
schoolno: 'int'
}
};
...
...
// Create a hash string drived from related fields. Before creating hash combine the fields in order.
myname="Uranus";
mylastname="SUN";
myschoolno=345;
hash_pk = Hash( Concat(myname, mylastname ) ); /* Hash(myname + mylastname) */
// Create a student object
realm.create('student',{pk:hash_pk,name:myname,lastname:mylastname,schoolno: myschoolno});
If ObjectId is necessary then goto Convert string to ObjectID in MongoDB
What JSON Structure is used to insert a null value for a property into Google Cloud Datastore using the REST API?
Usually you define a property name, valuetype, and value, like so:
var property = {
propertyname: { integerValue: 4 }
};
With integerValue denoting the valuetype. All the valuetypes can be seen here but I can not work out the structure for null.
This is a bug in the JSON API.
I've filed https://github.com/GoogleCloudPlatform/google-cloud-datastore/issues/41 to track it.
In the proto API, you would do this:
Property property = Property.newBuilder()
.setName("propertyName")
.setValue(Value.getDefaultInstance())
.build();