1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-08-17 06:00:33 +03:00

refactor(bridge/nodejs): remove lowdb and tiny custom ORM in favor of Prisma

This commit is contained in:
louistiti 2023-05-13 00:26:51 +08:00
parent 127a10f8ed
commit d6933a9e35
No known key found for this signature in database
GPG Key ID: 92CD6A2E497E1669
4 changed files with 1 additions and 227 deletions

View File

@ -14,8 +14,7 @@
},
"dependencies": {
"@prisma/client": "4.14.0",
"axios": "1.4.0",
"lowdb": "6.0.1"
"axios": "1.4.0"
},
"devDependencies": {
"prisma": "4.14.0"

View File

@ -1,145 +0,0 @@
import path from 'node:path'
import { SKILL_PATH } from '@bridge/constants'
const dynamicImport = new Function('specifier', 'return import(specifier)')
export class Memory {
private readonly memoryPath: string
private readonly name: string
public low: any
constructor(name: string) {
this.name = name
this.memoryPath = path.join(SKILL_PATH, 'memory', `${this.name}.json`)
}
public async load(): Promise<Memory> {
const { Low } = await dynamicImport('lowdb')
const { JSONFile } = await dynamicImport('lowdb/node')
const adapter = new JSONFile(this.memoryPath)
this.low = new Low(adapter, { [this.name]: [] })
await this.low.read()
return this
}
/**
* Create record
* @param record The record to create
* @example createOne({ id: 0, title: 'hello world' })
*/
public async createOne<T>(record: T): Promise<T> {
this.low.data[this.name].push(record)
await this.low.write()
return record
}
/**
* Create records
* @param records The records to create
* @example createMany([{ id: 0, title: 'hello world' }, { id: 1, title: 'hello world' }])
*/
public async createMany<T>(records: T[]): Promise<T[]> {
this.low.data[this.name].push(...records)
await this.low.write()
return records
}
/**
* Find a record
* @param filter The filter to apply
* @example findOne({ id: 0 })
*/
public async findOne<T>(filter: Partial<T>): Promise<T | undefined> {
return this.low.data[this.name].find((record: any) => {
return Object.entries(filter).every(([key, value]) => {
return record[key] === value
})
})
}
/**
* Find records
* @param filter The filter to apply
* @example findMany({ id: 0 })
*/
public async findMany<T>(filter: Partial<T>): Promise<T[]> {
return this.low.data[this.name].filter((record: T) => {
return this.isMatch(record, filter)
})
}
/**
* Check if a record match a filter
* @param record The record to check
* @param filter The filter to apply
* @example isMatch({ id: 0, title: 'hello world' }, { id: 0 })
*/
private isMatch<T>(record: any, filter: Partial<T>): boolean {
if (Array.isArray(record) && Array.isArray(filter)) {
return record.some((item) => {
return filter.some((filterItem) => {
return this.isMatch(item, filterItem)
})
})
} else if (typeof record === 'object' && typeof filter === 'object') {
return Object.entries(filter).every(([key, value]) => {
if (typeof value === 'object') {
return this.isMatch(record[key], value as Partial<T>)
} else {
return record[key] === value
}
})
} else {
return record === filter
}
}
/**
* Update a record
* @param filter The filter to apply
* @param update The update to apply
*/
public async updateOne<T, U>(
filter: Partial<T>,
update: Partial<U>
): Promise<T | undefined> {
const record = await this.findOne(filter)
if (!record) {
return
}
Object.assign(record, update)
await this.low.write()
return record
}
public async updateMany<T, U>(
filter: Partial<T>,
update: Partial<U>
): Promise<T[] | undefined> {
const records = await this.findMany(filter)
if (!records) {
return
}
records.forEach((record: any) => {
Object.assign(record, update)
})
await this.low.write()
return records
}
}

View File

@ -4,7 +4,6 @@ import stream from 'node:stream'
import readline from 'node:readline'
import axios from 'axios'
import { command } from 'execa'
import prettyBytes from 'pretty-bytes'
import prettyMilliseconds from 'pretty-ms'
import extractZip from 'extract-zip'
@ -12,7 +11,6 @@ import extractZip from 'extract-zip'
import {
BINARIES_FOLDER_NAME,
GITHUB_URL,
NODEJS_BRIDGE_ROOT_PATH,
NODEJS_BRIDGE_DIST_PATH,
PYTHON_BRIDGE_DIST_PATH,
TCP_SERVER_DIST_PATH,
@ -98,23 +96,6 @@ const setupBinaries = async (key) => {
fs.promises.rm(archivePath, { recursive: true, force: true })
])
if (key === 'nodejs-bridge') {
try {
LogHelper.info('Installing Node.js bridge npm packages...')
await command(
`npm install --package-lock=false --prefix ${NODEJS_BRIDGE_ROOT_PATH}`,
{
shell: true
}
)
LogHelper.success('Node.js bridge npm packages installed')
} catch (e) {
throw new Error(`Failed to download Node.js bridge npm packages: ${e}`)
}
}
try {
LogHelper.info(`Downloading ${name}...`)

View File

@ -4,7 +4,6 @@ import type { ActionFunction } from '@sdk/types'
import { leon } from '@sdk/leon'
import { PrismaClient } from '@sdk/prisma'
import { Network } from '@sdk/network'
import { Memory } from '@sdk/memory'
import { Button } from '@sdk/aurora/button'
export const run: ActionFunction = async function () {
@ -14,66 +13,6 @@ export const run: ActionFunction = async function () {
console.log('users', users)
const posts = await new Memory('posts').load()
// const sections = await new Memory('sections').load()
const specificPosts = await posts.updateMany(
{
id: 2,
author: {
name: 'Toto',
comments: {
id: 0
}
}
},
{
title: 'CHANGED!!!'
}
)
console.log('specificPosts', specificPosts)
/*await posts.createOne({
id: 0,
title: 'hello world',
content: 'hello world',
author: {
name: 'Louis'
},
createdAt: Date.now()
})
await posts.createMany([
{
id: 1,
title: 'hello world',
content: 'hello world',
author: {
name: 'Louis'
},
createdAt: Date.now()
},
{
id: 2,
title: 'hello world',
content: 'hello world',
author: {
name: 'Louis'
},
createdAt: Date.now()
}
])*/
/*await sections.push({
type: 'header',
width: '100%',
height: '100px'
})*/
// await blog.posts.getAll()
// await posts.find()
await leon.answer({ key: 'default' })
await leon.answer({ key: utility.md5('test') })