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

feat lc -a and store get set question by all question store #58

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/lc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { createQuestionById } from '#common/utils/cli-utils/createQuestion.js'
import { create } from '#common/utils/cli-utils/create.js'
import { commonMode } from '#common/utils/cli-utils/commonMode.js'
import { setAllQuestion } from '#common/utils/store/controller/allQuestion.js'
import { logger } from '#common/utils/logger/logger.js'

const version = process.env.VERSION ?? DefaultVer
program
Expand Down Expand Up @@ -62,7 +63,8 @@ export const callModeAction = {
},
all: async () => {
const allQuestionData = await getAllQuestionList()
for await (const question of allQuestionData) setAllQuestion(question)
await setAllQuestion(allQuestionData)
logger.info('拉取全部题目成功!')
process.exit(0)
}
}
Expand Down
80 changes: 74 additions & 6 deletions common/utils/store/controller/allQuestion.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,96 @@
import { exeOnce } from '#common/utils/store/store-realm.js'

const oSign = '$object$'
/**
* 转化对象
* @param obj
*/
function parseQuestion(obj) {
if (!obj) return null
Object.entries(obj).reduce((pre, [k, v]) => {
pre[k] =
typeof v == 'string' && v.startsWith(oSign)
? JSON.parse(v.replace(oSign, ''))
: v
return pre
}, {})
}
/**
* 转化字符串
*/
function stringifyQuestion(obj) {
if (!obj) return null
return (
Object.entries(obj)?.reduce((pre, [key, value]) => {
pre[key] =
value != null && typeof value === 'object'
? oSign + JSON.stringify(value)
: value
return pre
}, {}) ?? {}
)
}

/**
* 获取一个问题对象
* @param id
* @returns {Promise<void>}
*/
export function getOneQuestion(id) {
return exeOnce((realm) => {
const all = realm.objects('AllQuestion')
const question = all.filtered('questionId=$0', id)?.[0]
return question?.toJSON()
})
}

/**
* 存一个问题对象
* @param question
* @returns {*}
*/
export function setOneQuestion(question) {
return exeOnce((realm) => {
let newQuestion
realm.write(() => {
realm.delete(
realm
.objects('AllQuestion')
.filtered('questionId=$0', question.questionId)
)
newQuestion = realm.create('AllQuestion', question)
})
return newQuestion.toJSON()
})
}

/**
* 根据模式读取对象
* @returns {unknown}
*/
export function getAllQuestion() {
return exeOnce((realm) => {
const all = realm.objects('AllQuestion')
return all?.toJSON()
return all?.toJSON()?.map(parseQuestion)
})
}

/**
* 存对象
* @param question
* @param questions
* @returns {*}
*/
export function setAllQuestion(question) {
export function setAllQuestion(questions) {
return exeOnce((realm) => {
let newQuestion
const newQuestions = []
realm.write(() => {
newQuestion = realm.create('AllQuestion', question)
for (const question of questions) {
const data = stringifyQuestion(question)
if (!data?.questionId) continue
newQuestions.push(realm.create('AllQuestion', data))
}
})
return newQuestion
return newQuestions
})
}

Expand Down
91 changes: 15 additions & 76 deletions common/utils/store/schemas/allQuestion.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,25 @@
import Realm from 'realm'

export class TopCompanyTagsItem extends Realm.Object {
static schema = {
name: 'TopCompanyTagsItem',
properties: {
id: 'string',
name: 'string',
slug: 'string',
imgUrl: 'string?',
nameTranslated: 'string?'
}
}
}

export class ExtraObject extends Realm.Object {
static schema = {
name: 'ExtraObject',
properties: {
companyTagNum: 'int',
topCompanyTags: 'TopCompanyTagsItem[]',
twoYearsCompanyTagNum: 'int'
}
}
}

export class CodeSnippetsItem extends Realm.Object {
static schema = {
name: 'CodeSnippetsItem',
properties: {
langSlug: 'string',
lang: 'string'
}
}
}

export class TagTypeObject extends Realm.Object {
static schema = {
name: 'TagTypeObject',
properties: {
name: 'string',
slug: 'string'
}
}
}

export class TopicTagsItem extends Realm.Object {
static schema = {
name: 'TopicTagsItem',
properties: {
id: 'string',
name: 'string',
slug: 'string',
tagType: 'TagTypeObject',
createdAt: 'string',
isEnabled: 'bool',
keywords: 'string',
imgUrl: 'string?',
translatedName: 'string'
}
}
}

export class AllQuestion extends Realm.Object {
static schema = {
name: 'AllQuestion',
properties: {
questionId: 'string',
questionFrontendId: 'string',
questionType: 'string',
categoryTitle: 'string',
title: 'string',
titleSlug: 'string',
difficulty: 'string',
isPaidOnly: 'string',
codeSnippets: 'CodeSnippetsItem[]',
topicTags: 'string',
relatedTags: 'TopicTagsItem[]',
translatedTitle: 'string',
stats: 'string',
extra: 'ExtraObject',
isNewQuestion: 'bool',
frequency: 'string'
questionFrontendId: 'string?',
questionType: 'string?',
categoryTitle: 'string?',
title: 'string?',
titleSlug: 'string?',
difficulty: 'string?',
isPaidOnly: 'bool?',
codeSnippets: 'string?',
topicTags: 'string?',
relatedTags: 'string?',
translatedTitle: 'string?',
stats: 'string?',
extra: 'string?',
isNewQuestion: 'bool?',
frequency: 'string?'
},
primaryKey: 'questionId'
}
Expand Down
31 changes: 3 additions & 28 deletions common/utils/store/store-realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import path from 'node:path'
import { readdirSync, rmSync } from 'node:fs'
import Realm from 'realm'
import { Question } from '#common/utils/store/schemas/question.js'
import {
AllQuestion,
CodeSnippetsItem,
ExtraObject,
TagTypeObject,
TopCompanyTagsItem,
TopicTagsItem
} from '#common/utils/store/schemas/allQuestion.js'
import { AllQuestion } from '#common/utils/store/schemas/allQuestion.js'
import { rootPath } from '#common/utils/file/getRootPath.js'
import { Store } from '#common/utils/store/schemas/store.js'

Expand All @@ -22,32 +15,14 @@ export async function open() {
let realm
try {
realm = await Realm.open({
schema: [
Question,
TopCompanyTagsItem,
ExtraObject,
CodeSnippetsItem,
TagTypeObject,
TopicTagsItem,
AllQuestion,
Store
],
schema: [Question, AllQuestion, Store],
path: localPath
})
} catch (e) {
if (e?.message?.includes('Migration')) await cleanStore()

realm = await Realm.open({
schema: [
Question,
TopCompanyTagsItem,
ExtraObject,
CodeSnippetsItem,
TagTypeObject,
TopicTagsItem,
AllQuestion,
Store
],
schema: [Question, AllQuestion, Store],
path: localPath
})
}
Expand Down
8 changes: 4 additions & 4 deletions common/utils/update/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function getNpmVersion() {
const res = await fetch_(npmUrl, { method: 'GET' })
return res['dist-tags']?.latest
} catch (e) {
throw new Error(e)
return false
}
}
/**
Expand All @@ -52,7 +52,7 @@ export async function getGithubVersion() {
const ver = github?.version ?? gitee?.version
return ver
} catch (e) {
throw new Error(e)
return false
}
}
export function getLocalVersion() {
Expand All @@ -77,7 +77,7 @@ export async function checkUpdate() {
localVersion: local,
npmVersion: remote,
githubVersion: github,
isCliUpdate: remote !== local,
isGithubUpdate: github !== local
isCliUpdate: remote && remote !== local,
isGithubUpdate: github && github !== local
}
}
Loading