Open ArtemFrontendACTUM opened 2 years ago
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
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];
}
}
Sounds like redundant complication of ES standard. Could you provide some examples or repo for the problematics? Because the problem seems farfetched.