170 lines
3.9 KiB
Markdown
170 lines
3.9 KiB
Markdown
|
# db for GoJS
|
||
|
|
||
|
## set db config in env.yml
|
||
|
|
||
|
```yaml
|
||
|
db:
|
||
|
default: sqlite3://test.db
|
||
|
db2: mysql://root:zKeL8Qhs09tDvg4pYab0zg==@localhost:3306/test
|
||
|
```
|
||
|
|
||
|
### more config see [ssgo/db](https://github.com/ssgo/db)
|
||
|
|
||
|
set default connection can use db.query, db.xxxx directly
|
||
|
|
||
|
or set default connection by db.setDefault('sqlite3://test.db')
|
||
|
|
||
|
## encrypt password
|
||
|
|
||
|
```shell
|
||
|
go install github.com/ssgo/tool/sskey@latest
|
||
|
sskey -e 'your password' | grep 'url base64'
|
||
|
```
|
||
|
|
||
|
use sskey to encrypt password, then use url base64 to encode it.
|
||
|
|
||
|
if you want use custom sskey secret
|
||
|
|
||
|
add code to your project
|
||
|
|
||
|
```go
|
||
|
import "github.com/ssgo/db"
|
||
|
func setSSKey(key, iv []byte){
|
||
|
db.SetEncryptKeys(key, iv)
|
||
|
}
|
||
|
```
|
||
|
|
||
|
generate setSSKey.go into your project
|
||
|
|
||
|
```shell
|
||
|
sskey -c your_secret_name
|
||
|
sskey -go your_secret_name > setSSKey.go
|
||
|
```
|
||
|
|
||
|
## set db config in env for docker
|
||
|
|
||
|
```shell
|
||
|
docker run -e DB_DEFAULT=sqlite3://test.db -e DB2="mysql://root:zKeL8Qhs09tDvg4pYab0zg==@localhost:3306/test"
|
||
|
```
|
||
|
|
||
|
## import db driver in use
|
||
|
|
||
|
```go
|
||
|
_ "modernc.org/sqlite"
|
||
|
_ "github.com/go-sql-driver/mysql"
|
||
|
```
|
||
|
|
||
|
## create or update db from simple description
|
||
|
|
||
|
```javascript
|
||
|
import db from 'apigo.cc/gojs/db'
|
||
|
|
||
|
db.make(`
|
||
|
// Account
|
||
|
|
||
|
User // User
|
||
|
id c12 PK // User ID
|
||
|
phone v20 U // Phone Number
|
||
|
password v80 n // Password
|
||
|
salt v50 // Salt
|
||
|
name v100 n // Name
|
||
|
serverKey v200 // Server Key
|
||
|
isValid b // Is Valid
|
||
|
|
||
|
Device // Device
|
||
|
id v30 PK // Device ID
|
||
|
userId c12 // Current User
|
||
|
salt v50 // Salt
|
||
|
secretTime dt // Secret Generation Time
|
||
|
|
||
|
// Log
|
||
|
|
||
|
LoginLog // Login Log
|
||
|
id ubi AI // Login ID
|
||
|
way v20 // Login Method (verifyCode/autoLogin/oneClickLogin)
|
||
|
userId c12 I // Current User
|
||
|
deviceId v30 I // Device ID
|
||
|
time dt I // Login Time
|
||
|
userAgent v200 // Device Information
|
||
|
requestId v20 // Request ID
|
||
|
sessionId v20 // Session ID
|
||
|
successful b // Was Successful
|
||
|
message v1024 // Failure Message
|
||
|
`)
|
||
|
```
|
||
|
|
||
|
### field types
|
||
|
|
||
|
```
|
||
|
c => char
|
||
|
v => varchar
|
||
|
dt => datetime
|
||
|
d => date
|
||
|
tm => time
|
||
|
i => int
|
||
|
ui => int unsigned
|
||
|
ti => tinyint
|
||
|
uti => tinyint unsigned
|
||
|
b => tinyint unsigned
|
||
|
bi => bigint
|
||
|
ubi => bigint unsigned
|
||
|
f => float
|
||
|
uf => float unsigned
|
||
|
ff => double
|
||
|
uff => double unsigned
|
||
|
si => smallint
|
||
|
usi => smallint unsigned
|
||
|
mi => middleint
|
||
|
umi => middleint unsigned
|
||
|
t => text
|
||
|
bb => blob
|
||
|
```
|
||
|
|
||
|
### index (mulit field use I1 for make mulit index named 1)
|
||
|
|
||
|
```
|
||
|
PK => PRIMARY KEY NOT NULL
|
||
|
AI => PRIMARY KEY AUTOINCREMENT NOT NULL
|
||
|
I => index
|
||
|
U => unique
|
||
|
TI => fulltext
|
||
|
```
|
||
|
|
||
|
### more defile doc see [ssgo/dao](https://github.com/ssgo/dao)
|
||
|
|
||
|
## example
|
||
|
|
||
|
```javascript
|
||
|
import db from 'apigo.cc/gojs/db'
|
||
|
import console from 'console'
|
||
|
|
||
|
let r1 = db.query('select * from user limit 10')
|
||
|
console.log(r1.result)
|
||
|
|
||
|
let conn2 = db.get('db2')
|
||
|
let r2 = conn2.query('select * from user limit 10')
|
||
|
console.log(r2.result)
|
||
|
|
||
|
let conn3 = db.get('sqlite3://test.db')
|
||
|
```
|
||
|
|
||
|
## module.exports
|
||
|
|
||
|
```ts
|
||
|
function get(dbName: string): DB {return null as any}
|
||
|
function setDefault(dbName: string): void {}
|
||
|
function make(descFileOrContent: string): Array<Object>{return null as any}
|
||
|
function query(sql: string, ...args:any): QueryResult{return null as any}
|
||
|
function query1(sql: string, ...args:any): QueryResult1{return null as any}
|
||
|
function query11(sql: string, ...args:any): QueryResult11{return null as any}
|
||
|
function exec(sql: string, ...args:any): ExecResult{return null as any}
|
||
|
function insert(table: string, data:Object): ExecResult{return null as any}
|
||
|
function replace(table: string, data:Object): ExecResult{return null as any}
|
||
|
function update(table: string, data:Object, where: string, ...args:any): ExecResult{return null as any}
|
||
|
function delete_(table: string, where: string, ...args:any): ExecResult{return null as any}
|
||
|
function destroy(): void{}
|
||
|
function begin(): Tx{return null as any}
|
||
|
```
|
||
|
|
||
|
## full api see [db.ts](https://apigo.cc/gojs/db/db.ts)
|