ansa
ansa3y ago

db.insert return type

So I'm encountering the following error in my vscode:
Type 'GenericId<"submissions">' is missing the following properties from type '{ _id: GenericId<"submissions">; _creationTime: number; user: GenericId<"users">; application: GenericId<"applications">; submitted: boolean; fields: Map<...>; }': _id, _creationTime, user, application, and 2 more.
Type 'GenericId<"submissions">' is missing the following properties from type '{ _id: GenericId<"submissions">; _creationTime: number; user: GenericId<"users">; application: GenericId<"applications">; submitted: boolean; fields: Map<...>; }': _id, _creationTime, user, application, and 2 more.
Here's what I have:
return db.insert("submissions", {
user: user._id,
application: applicationId,
submitted: false,
fields: new Map<string, string | boolean | string[]>(),
});
return db.insert("submissions", {
user: user._id,
application: applicationId,
submitted: false,
fields: new Map<string, string | boolean | string[]>(),
});
here's my schema:
submissions: defineTable({
user: s.id("users"),
application: s.id("applications"),
submitted: s.boolean(),
fields: s.map(
s.string(),
s.union(s.string(), s.boolean(), s.array(s.string()))
),
}),
submissions: defineTable({
user: s.id("users"),
application: s.id("applications"),
submitted: s.boolean(),
fields: s.map(
s.string(),
s.union(s.string(), s.boolean(), s.array(s.string()))
),
}),
11 Replies
ansa
ansaOP3y ago
user is of type Document<"users"> and applicationId is of type GenericId<"applications"> where users, applications, and submissions are all database tables
ian
ian3y ago
what is the return type? maybe it's failing b/c you're returning GenericId<"submissions"> and you're trying to return the submission itself? user and application are both GenericId types. Btw, if you have a schema, then you can use Id<"users"> and Id<"applications"> : GenericId is just for when you don't have a schema the return type of db.insert should be something like Promise<GenericId<"submissions">> which is making me think your error is from returning the id when you expect to be returning the document itself
ansa
ansaOP3y ago
oh i see! is there an easy way to get this query to return the submission document? or should i make a document using the parameters I passed into the db.insert? also, just wondering, how did you know that was the issue? oh nevermind, I see it-- I was reading the msg incorrectly! thanks if i create it using the parameters, I still won't have the creationtime..
alexcole
alexcole3y ago
is there an easy way to get this query to return the submission document?
You can always use db.get to turn the ID into a document:
const submissionId = await db.insert...
return db.get(submissionId);
const submissionId = await db.insert...
return db.get(submissionId);
ansa
ansaOP3y ago
ah so, then I get this...
Type 'null' is not assignable to type '{ _id: GenericId<"submissions">; _creationTime: number; user: GenericId<"users">; application: GenericId<"applications">; submitted: boolean; fields: Map<...>; }'.
Type 'null' is not assignable to type '{ _id: GenericId<"submissions">; _creationTime: number; user: GenericId<"users">; application: GenericId<"applications">; submitted: boolean; fields: Map<...>; }'.
though this should never return null, because I just put it in the db; I'm a bit new to typescript so I'm not super sure how to resolve this did this
const newSubmission = await db.get(subId);
if (newSubmission === null)
throw new Error("new submission should not be null!");
return newSubmission;
const newSubmission = await db.get(subId);
if (newSubmission === null)
throw new Error("new submission should not be null!");
return newSubmission;
let me know if there's a better fix! thanks!
ian
ian3y ago
Hmm. That shouldn't ever happen - can you reproduce it? If you could print the _id after the insert, I'd be curious if id is well formed & points to actual data
jamwt
jamwt3y ago
@ian it's probably because .get() will return obj | null b/c it can't guarantee the object is present -- the typescript type system can't @ansa since you just inserted the document, it's safe to do db.get(...)!; there. the ! means I know it's not null, compiler trust me
ian
ian3y ago
ah, so it won't happen at runtime, just the type error. that makes sense. I got confused and thought this was a runtime exception
ansa
ansaOP3y ago
gotcha! thanks!
jamwt
jamwt3y ago
right, not a runtime error, ts error yep @ansa no problem!
alexcole
alexcole3y ago
Oh duh. Sorry for not checking the types on my snippet! The nulls returned from db.get are kinda obnoxious, but perhaps unavoidable given that we allow document deletion. This conversation also makes me wonder if we should flip back to db.insert returning the entire document, not just the ID (this is an option now that db.insert is async). Although in many cases using the ID is better so that you can get a reactive version of the document from useQuery.

Did you find this page helpful?