Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
Neos
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
love_飞影
Neos
Commits
423c93c0
Commit
423c93c0
authored
Feb 05, 2023
by
Chunchi Che
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add FTS command
parent
57ab45b7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
45 deletions
+101
-45
src/api/cards.ts
src/api/cards.ts
+41
-33
src/middleware/sqlite.ts
src/middleware/sqlite.ts
+60
-12
No files found.
src/api/cards.ts
View file @
423c93c0
...
...
@@ -3,37 +3,42 @@ import sqliteMiddleWare, { sqliteCmd } from "../middleware/sqlite";
export
interface
CardMeta
{
id
:
number
;
data
:
{
ot
?:
number
;
setcode
?:
number
;
type
?:
number
;
atk
?:
number
;
def
?:
number
;
level
?:
number
;
race
?:
number
;
attribute
?:
number
;
};
text
:
{
name
?:
string
;
types
?:
string
;
desc
?:
string
;
str1
?:
string
;
str2
?:
string
;
str3
?:
string
;
str4
?:
string
;
str5
?:
string
;
str6
?:
string
;
str7
?:
string
;
str8
?:
string
;
str9
?:
string
;
str10
?:
string
;
str11
?:
string
;
str12
?:
string
;
str13
?:
string
;
str14
?:
string
;
str15
?:
string
;
str16
?:
string
;
};
data
:
CardData
;
text
:
CardText
;
}
export
interface
CardData
{
ot
?:
number
;
setcode
?:
number
;
type
?:
number
;
atk
?:
number
;
def
?:
number
;
level
?:
number
;
race
?:
number
;
attribute
?:
number
;
}
export
interface
CardText
{
id
?:
number
;
name
?:
string
;
types
?:
string
;
desc
?:
string
;
str1
?:
string
;
str2
?:
string
;
str3
?:
string
;
str4
?:
string
;
str5
?:
string
;
str6
?:
string
;
str7
?:
string
;
str8
?:
string
;
str9
?:
string
;
str10
?:
string
;
str11
?:
string
;
str12
?:
string
;
str13
?:
string
;
str14
?:
string
;
str15
?:
string
;
str16
?:
string
;
}
/*
...
...
@@ -48,8 +53,11 @@ export async function fetchCard(
local
?:
boolean
):
Promise
<
CardMeta
>
{
if
(
local
)
{
return
await
sqliteMiddleWare
({
cmd
:
sqliteCmd
.
SELECT
,
payload
:
id
}).
then
(
(
res
)
=>
(
res
?
res
:
{
id
,
data
:
{},
text
:
{}
})
return
await
sqliteMiddleWare
({
cmd
:
sqliteCmd
.
SELECT
,
payload
:
{
id
},
}).
then
((
res
)
=>
res
.
selectResult
?
res
.
selectResult
:
{
id
,
data
:
{},
text
:
{}
}
);
}
const
res
=
await
axios
.
get
<
CardMeta
>
(
"
http://localhost:3030/cards/
"
+
id
);
...
...
src/middleware/sqlite.ts
View file @
423c93c0
...
...
@@ -6,13 +6,15 @@
* */
import
initSqlJs
,
{
Database
}
from
"
sql.js
"
;
import
{
CardMeta
}
from
"
../api/cards
"
;
import
{
CardMeta
,
CardData
,
CardText
}
from
"
../api/cards
"
;
export
enum
sqliteCmd
{
// 初始化
INIT
,
// 读取操作
SELECT
,
// 全文搜索
FTS
,
}
export
interface
sqliteAction
{
...
...
@@ -22,7 +24,15 @@ export interface sqliteAction {
dbUrl
:
string
;
};
// 需要读取卡牌数据的ID
payload
?:
number
;
payload
?:
{
id
?:
number
;
query
?:
string
;
};
}
export
interface
sqliteResult
{
selectResult
?:
CardMeta
;
ftsResult
?:
CardMeta
[];
}
let
YGODB
:
Database
|
null
=
null
;
...
...
@@ -31,7 +41,7 @@ const sqlPromise = initSqlJs({
});
// FIXME: 应该有个返回值,告诉业务方本次请求的结果,比如初始化DB失败
export
default
async
function
(
action
:
sqliteAction
)
{
export
default
async
function
(
action
:
sqliteAction
)
:
Promise
<
sqliteResult
>
{
switch
(
action
.
cmd
)
{
case
sqliteCmd
.
INIT
:
{
const
info
=
action
.
initInfo
;
...
...
@@ -46,27 +56,65 @@ export default async function (action: sqliteAction) {
console
.
warn
(
"
init YGODB action without initInfo
"
);
}
break
;
return
{}
;
}
case
sqliteCmd
.
SELECT
:
{
if
(
YGODB
&&
action
.
payload
)
{
const
code
=
action
.
payload
;
const
dataStmt
=
YGODB
.
prepare
(
"
SELECT * from datas WHERE ID = $id
"
);
if
(
YGODB
&&
action
.
payload
&&
action
.
payload
.
id
)
{
const
code
=
action
.
payload
.
id
;
const
dataStmt
=
YGODB
.
prepare
(
"
SELECT * FROM datas WHERE ID = $id
"
);
const
dataResult
=
dataStmt
.
getAsObject
({
$id
:
code
});
const
textStmt
=
YGODB
.
prepare
(
"
SELECT *
from
texts WHERE ID = $id
"
);
const
textStmt
=
YGODB
.
prepare
(
"
SELECT *
FROM
texts WHERE ID = $id
"
);
const
textResult
=
textStmt
.
getAsObject
({
$id
:
code
});
return
constructCardMeta
(
code
,
dataResult
,
textResult
);
return
{
selectResult
:
constructCardMeta
(
code
,
dataResult
,
textResult
),
};
}
else
{
console
.
warn
(
"
ygo db not init or id not provied!
"
);
}
return
{};
}
case
sqliteCmd
.
FTS
:
{
if
(
YGODB
&&
action
.
payload
&&
action
.
payload
.
query
)
{
const
query
=
action
.
payload
.
query
;
const
ftsTexts
:
CardText
[]
=
[];
const
ftsMetas
:
CardMeta
[]
=
[];
const
textStmt
=
YGODB
.
prepare
(
"
SELECT * FROM texts WHERE name LIKE '%$query%'
"
);
textStmt
.
bind
({
$query
:
query
});
while
(
textStmt
.
step
())
{
const
row
=
textStmt
.
getAsObject
();
ftsTexts
.
push
(
row
);
}
for
(
const
text
of
ftsTexts
)
{
const
id
=
text
.
id
;
if
(
id
)
{
const
dataStmt
=
YGODB
.
prepare
(
"
SELECT * FROM datas WHERE ID = $id
"
);
const
data
:
CardData
=
dataStmt
.
getAsObject
({
$id
:
id
});
ftsMetas
.
push
({
id
,
data
,
text
});
}
}
return
{
ftsResult
:
ftsMetas
};
}
else
{
console
.
warn
(
"
ygo db not init!
"
);
console
.
warn
(
"
ygo db not init
or query not provied
!
"
);
}
break
;
return
{}
;
}
default
:
{
console
.
warn
(
`Unhandled sqlite command:
${
action
.
cmd
}
`
);
break
;
return
{}
;
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment