Giter VIP home page Giter VIP logo

Comments (3)

kearfy avatar kearfy commented on May 21, 2024 1

Did an implementation of the above concept in #80

from surrealdb.js.

btmnk avatar btmnk commented on May 21, 2024

I had an idea of inferring the return type from the passed id, since it would only return an array if the first param is a table name, right?
If we can infer it by checking if the first param matches ":" we could predict that the return type is a single object and an array otherwise.
(We can safely do this since we already decide for the array with typeof id === "string" && id.includes(":"))

It could look something like this:

	change<Thing extends string, T extends object, U extends object = T>(
		thing: Thing,
		data?: Partial<T> & U
	): Thing extends `${infer Table}:${infer ID}`
		? Promise<T & U & { id: string }>
		: Promise<Array<T & U & { id: string }>> {
		const id = guid();
		return this.wait().then(() => {
			return new Promise((resolve, reject) => {
				this.once(id, (res) =>				
					this.#output(res, "change", thing, resolve, reject)
				);
				this.#send(id, "change", [thing, data]);
			});
		});
	}

I wasn't able to get the types to become properly compatible but I think the idea gets through.
With that the consumer could work comfortably with the change method with the correct return type for the use case.

from surrealdb.js.

mathe42 avatar mathe42 commented on May 21, 2024

@btmnk Looks good! woll give it a try. I currently don't know what happens if the Table only has 1 row...

from surrealdb.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.