Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples? #122

Open
ArtemFrontendACTUM opened this issue May 5, 2022 · 2 comments
Open

Examples? #122

ArtemFrontendACTUM opened this issue May 5, 2022 · 2 comments

Comments

@ArtemFrontendACTUM
Copy link

Sounds like redundant complication of ES standard. Could you provide some examples or repo for the problematics?
Because the problem seems farfetched.

@Delapouite
Copy link

In their latest release, Bun decided to use an import attribute to interact with sqlite database:

import db from './my.db' with {type: "sqlite"};

const {id} = db
  .query("SELECT id FROM users LIMIT 1")
  .get();

console.log(id); // 1

https://bun.sh/blog/bun-v1.0.23

@juner
Copy link

juner commented Mar 29, 2024

check support sample.

  async function enableWithTypeSyntax([type, dataUrl, ok]) {
    let enableWithTypeSyntax = false;
    const option = (() => {
      const option = {};
      const w = {type};
      Reflect.defineProperty(option, "with", {
        get: () => (enableWithTypeSyntax=true,w),
      });
      return option;
    })();
    let result;
    try {
      const importMethod = new Function("dataUrl", "option", "return import(dataUrl, option);");
      result = await importMethod(dataUrl, option);
      if (!enableWithTypeSyntax) return [false, new Error("not with read.")];
      const isValid = typeof ok === "function" ? ok(result?.["default"], result) : !!ok;
      if (!isValid) return [false, new Error("type is invalid", {cause: {default: result?.default, module: result}})];
      return [true,"ok"];
    }catch(e){
      if (e instanceof SyntaxError) return [false, new Error("not supported. import attribute syntax", {cause:e})]
      return [false, e];
    }
  }

playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants