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

ask the way to inject sql.js when creating new connections on Express.js #522

Closed
sigridjineth opened this issue Jul 21, 2022 · 1 comment

Comments

@sigridjineth
Copy link

sigridjineth commented Jul 21, 2022

I tried this to create new connection to sql.js by using the code below on index.ts

createConnection({
  type: "sqljs",
  location: "../node_modules/sql.js/dist/sql-wasm.wasm",
  autoSave: true,
  entities: [
      "../entity/**/*.ts"
  ],
  logging: ['query', 'schema'],
  synchronize: true
}).then(async connection => {
  // await insertScriptData(connection);
  // const user = await MockUserToFavorite(connection);
  // await insertNewsData(connection);
  log.debug(connection)

  app.listen(8080, () => {
    log.info('Server is running on port 8080');
  });
});

However, the error was thrown like the following:

(node:85901) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
TypeError: Failed to parse URL from /Users/sigridjin.eth/Documents/deliverble-backend/node_modules/sql.js/dist/sql-wasm.wasm

I already tried the following options but it was failed; the method of readFile does not expect two arguments.

 const wasmBinary = await readFile(new URL("../node_modules/sql.js/dist/sql-wasm.wasm", import.meta.url));
  const SQLJS = await initSql({
    wasmBinary,
  });

#517

What should I do? Thanks for in advance.

@sigridjineth
Copy link
Author

I just solved the problem by directly copying the sql-wasm.wasm file on the index.ts directory, while autoSave should be true. When autoSave option is not declared, I do not see the clarification by userId.

import express from 'express';
import {createConnection} from 'typeorm';
import routes from './routes';
import {errorHandler} from './error/ErrorHandler';
import redis from "redis";
import {Logger} from "tslog";

const cors = require('cors');

// redis setting
const port = 6379;
const host = "localhost";
const password = "changeme";
const redisClient = redis.createClient(port, host);
redisClient.auth(password);

const app = express();

const initSqlJs = require('sql.js');

const getSQL = async() => {
  const SQL = await initSqlJs({
    // Required to load the wasm binary asynchronously. Of course, you can host it wherever you want
    // You can omit locateFile completely when running in node
    locateFile: file => `https://sql.js.org/dist/${file}`
  });
  return new SQL.Database();
}

console.log(getSQL())



// CORS 미들웨어 사용
app.use(cors());

// Request body를 parsing 하기 위한 미들웨어 사용
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// routes 폴더로 분기
app.use(routes);
app.use(errorHandler);



const log: Logger = new Logger({ name: "딜리버블 백엔드 짱짱" });

createConnection({
  type: "sqljs",
  location: "./sql-wasm.wasm",
  autoSave: true,
  entities: [
    "../entity/**/*.ts"
  ],
  logging: ['query', 'schema'],
  synchronize: true
}).then(async (connection) => {
  // await insertScriptData(connection);
  // const user = await MockUserToFavorite(connection);
  // await insertNewsData(connection);

  app.listen(8080, () => {
    log.info('Server is running on port 8080');
  });
});
(Use `node --trace-warnings ...` to show where the warning was created)
2022-07-21 19:02:47.115  INFO  [딜리버블 백엔드 짱짱 src/util/redis.ts:16 RedisClient.<anonymous>] Redis plugged in. 
2022-07-21 19:02:47.117  INFO  [딜리버블 백엔드 짱짱 src/controllers/UserController.ts:19 RedisClient.<anonymous>] Redis plugged in. 
Redis plugged in.
query: PRAGMA foreign_keys = OFF
query: BEGIN TRANSACTION
query: SELECT * FROM "sqlite_master" WHERE "type" = 'table' AND "name" = 'typeorm_metadata'
query: COMMIT
query: PRAGMA foreign_keys = ON
2022-07-21 19:02:47.342  INFO  [딜리버블 백엔드 짱짱 src/index.ts:64 Server.<anonymous>] Server is running on port 8080 
^C

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

2 participants