Skip to content

Commit

Permalink
connect to local postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
cindywu committed Oct 18, 2022
1 parent e2eba2e commit e783cec
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 57 deletions.
14 changes: 14 additions & 0 deletions datamodel/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import Database from 'tauri-plugin-sql-api'

// async function db(){
// const db = await Database.load('postgres://postgres:jammy-one@localhost:1234/postgres')
// console.log('response', response)
// return db
// }

// export default db

import pgInit from 'pg-promise'

const pgp = pgInit()
export const db = pgp('postgres://postgres:jammy-one@localhost:1234/postgres')
121 changes: 65 additions & 56 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tauri-build = { version = "1.1.1", features = [] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.1.1", features = ["api-all"] }
tauri-plugin-sql = { git = "https://github.com/tauri-apps/tauri-plugin-sql", branch = "dev", features = ["sqlite"] }
tauri-plugin-sql = { git = "https://github.com/tauri-apps/tauri-plugin-sql", branch = "dev", features = ["postgres"] }

[features]
# by default Tauri runs in production mode
Expand Down
24 changes: 24 additions & 0 deletions src/pages/api/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {db} from '../../../datamodel/db.js';

// eslint-disable-next-line import/no-anonymous-default-export
export default async (_, res) => {
await db.task(async t => {
await t.none('DROP TABLE IF EXISTS message');
await t.none('DROP TABLE IF EXISTS replicache_client');
await t.none('DROP SEQUENCE IF EXISTS version');
// Stores chat messages
await t.none(`CREATE TABLE message (
id VARCHAR(21) PRIMARY KEY NOT NULL,
sender VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
ord BIGINT NOT NULL,
version BIGINT NOT NULL)`);
// Stores last mutation ID for each Replicache client
await t.none(`CREATE TABLE replicache_client (
id VARCHAR(36) PRIMARY KEY NOT NULL,
last_mutation_id BIGINT NOT NULL)`);
// Will be used for computing diffs for pull response
await t.none('CREATE SEQUENCE version');
});
res.send('ok');
};

0 comments on commit e783cec

Please sign in to comment.