Skip to content

Commit c5517f7

Browse files
Typeorm using sqlite with electron hello world
1 parent 6c13c9b commit c5517f7

File tree

7 files changed

+71
-14
lines changed

7 files changed

+71
-14
lines changed

babel.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module.exports = {
99
plugins: [
1010
['@babel/plugin-transform-runtime', {
1111
regenerator: true
12-
}]
12+
}],
13+
"babel-plugin-transform-typescript-metadata",
14+
["@babel/plugin-proposal-decorators", { "legacy": true }],
1315
]
1416
}

electron/main.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
import { app, BrowserWindow, ipcMain } from 'electron'
2-
import sqlite3 from 'sqlite3'
1+
import "reflect-metadata";
32

4-
const database = new sqlite3.Database('./public/db.sqlite3', (err) => {
5-
if (err) console.error('Database opening error: ', err);
3+
import { app, BrowserWindow, ipcMain } from 'electron';
4+
import { DataSource } from "typeorm";
5+
6+
import { User } from '../entities/User';
7+
8+
const dataSource = new DataSource({
9+
type: 'sqlite',
10+
database: './public/db.sqlite3',
11+
entities: [User],
12+
synchronize: true,
13+
dropSchema: true,
14+
});
15+
16+
dataSource.initialize().then(() => {
17+
console.log("Data Source has been initialized!")
18+
}).catch((err) => {
19+
console.error("Error during Data Source initialization", err)
620
});
721

822
let mainWindow: BrowserWindow | null
@@ -42,13 +56,12 @@ async function registerListeners () {
4256
// ipcMain.on('message', (_, message) => {
4357
// console.log(message)
4458
// })
45-
46-
ipcMain.on('sqlite-message', (event, message) => {
47-
const sql = 'SELECT * FROM repositories';
48-
database.all(sql, (err, rows: any) => {
49-
console.log(rows);
50-
event.reply('sqlite-reply', (err && err.message) || rows);
51-
});
59+
60+
ipcMain.on('sqlite-message', async (event, message) => {
61+
const user = await dataSource.getRepository(User).save({ firstName: 'Bernardo', lastName: 'Duarte', isActive: true });
62+
console.log(user);
63+
const users = await dataSource.getRepository(User).find();
64+
event.reply('sqlite-reply', users);
5265
});
5366
}
5467

entities/User.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm"
2+
3+
@Entity()
4+
export class User {
5+
@PrimaryGeneratedColumn()
6+
id: number
7+
8+
@Column()
9+
firstName: string
10+
11+
@Column()
12+
lastName: string
13+
14+
@Column()
15+
isActive: boolean
16+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"devDependencies": {
3030
"@babel/core": "7.14.6",
31+
"@babel/plugin-proposal-decorators": "^7.20.13",
3132
"@babel/plugin-transform-runtime": "7.14.5",
3233
"@babel/preset-env": "7.14.5",
3334
"@babel/preset-react": "7.14.5",
@@ -51,6 +52,7 @@
5152
"@typescript-eslint/eslint-plugin": "4.27.0",
5253
"@typescript-eslint/parser": "4.27.0",
5354
"babel-loader": "8.2.2",
55+
"babel-plugin-transform-typescript-metadata": "^0.3.2",
5456
"cross-env": "7.0.3",
5557
"electron": "13.1.2",
5658
"electron-rebuild": "^3.2.9",

src/App.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import "reflect-metadata";
2-
31
import { GlobalStyle } from './styles/GlobalStyle'
42

53
import { Admin, Resource, ListGuesser, EditGuesser } from "react-admin";

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
"esModuleInterop": true,
1717
"emitDecoratorMetadata": true,
1818
"experimentalDecorators": true,
19+
"strictPropertyInitialization": false,
1920
}
2021
}

yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,17 @@
336336
"@babel/helper-plugin-utils" "^7.20.2"
337337
"@babel/plugin-syntax-class-static-block" "^7.14.5"
338338

339+
"@babel/plugin-proposal-decorators@^7.20.13":
340+
version "7.20.13"
341+
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-decorators/-/plugin-proposal-decorators-7.20.13.tgz#b6bea3b18e88443688fa7ed2cc06d2c60da9f4a7"
342+
integrity sha512-7T6BKHa9Cpd7lCueHBBzP0nkXNina+h5giOZw+a8ZpMfPFY19VjJAjIxyFHuWkhCWgL6QMqRiY/wB1fLXzm6Mw==
343+
dependencies:
344+
"@babel/helper-create-class-features-plugin" "^7.20.12"
345+
"@babel/helper-plugin-utils" "^7.20.2"
346+
"@babel/helper-replace-supers" "^7.20.7"
347+
"@babel/helper-split-export-declaration" "^7.18.6"
348+
"@babel/plugin-syntax-decorators" "^7.19.0"
349+
339350
"@babel/plugin-proposal-dynamic-import@^7.14.5":
340351
version "7.18.6"
341352
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-dynamic-import/-/plugin-proposal-dynamic-import-7.18.6.tgz#72bcf8d408799f547d759298c3c27c7e7faa4d94"
@@ -466,6 +477,13 @@
466477
dependencies:
467478
"@babel/helper-plugin-utils" "^7.14.5"
468479

480+
"@babel/plugin-syntax-decorators@^7.19.0":
481+
version "7.19.0"
482+
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-decorators/-/plugin-syntax-decorators-7.19.0.tgz#5f13d1d8fce96951bea01a10424463c9a5b3a599"
483+
integrity sha512-xaBZUEDntt4faL1yN8oIFlhfXeQAWJW7CLKYsHTUqriCUbj8xOra8bfxxKGi/UwExPFBuPdH4XfHc9rGQhrVkQ==
484+
dependencies:
485+
"@babel/helper-plugin-utils" "^7.19.0"
486+
469487
"@babel/plugin-syntax-dynamic-import@^7.8.3":
470488
version "7.8.3"
471489
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-dynamic-import/-/plugin-syntax-dynamic-import-7.8.3.tgz#62bf98b2da3cd21d626154fc96ee5b3cb68eacb3"
@@ -3297,6 +3315,13 @@ babel-plugin-syntax-jsx@^6.18.0:
32973315
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
32983316
integrity sha512-qrPaCSo9c8RHNRHIotaufGbuOBN8rtdC4QrrFFc43vyWCCz7Kl7GL1PGaXtMGQZUXrkCjNEgxDfmAuAabr/rlw==
32993317

3318+
babel-plugin-transform-typescript-metadata@^0.3.2:
3319+
version "0.3.2"
3320+
resolved "https://registry.yarnpkg.com/babel-plugin-transform-typescript-metadata/-/babel-plugin-transform-typescript-metadata-0.3.2.tgz#7a327842d8c36ffe07ee1b5276434e56c297c9b7"
3321+
integrity sha512-mWEvCQTgXQf48yDqgN7CH50waTyYBeP2Lpqx4nNWab9sxEpdXVeKgfj1qYI2/TgUPQtNFZ85i3PemRtnXVYYJg==
3322+
dependencies:
3323+
"@babel/helper-plugin-utils" "^7.0.0"
3324+
33003325
babel-preset-current-node-syntax@^1.0.0:
33013326
version "1.0.1"
33023327
resolved "https://registry.yarnpkg.com/babel-preset-current-node-syntax/-/babel-preset-current-node-syntax-1.0.1.tgz#b4399239b89b2a011f9ddbe3e4f401fc40cff73b"

0 commit comments

Comments
 (0)