我正在尝试从现有数据库中检索数据。我最近从 Expo 管理切换到裸工作流,尝试使用 TypeORM 测试 react-native-sqlite-storage。但是,TypeORM 原始查询一直返回一个空数组,我不知道为什么。
这是我的代码,它基于此示例https://dev.to/jgabriel1/expo-sqlite-typeorm-4mn8
DictionaryScreen.js
function DictionaryScreen(props) {
const { ejRepository } = useDatabaseConnection();
const handleinputWord = async () => {
console.log(props.inputWord); //=>this console.logs the text from the input below
try {
const results = await ejRepository.getWordandMean(props.inputWord);
console.log(results); //=> Here I expect an array of the query result but it only returns []
} catch (error) {
console.log(error)
}
}
return(
<View style={styles.searchContainer}>
<MaterialCommunityIcons name="text-search" color="white" size={30}/>
<TextInput
style={styles.input}
onChangeText={props.updateTextInput}
onSubmitEditing={handleinputWord}
value={props.inputWord}
placeholder='Look up the word (Type exactly)'
keyboardType="default"
/>
<StatusBar style='light' />
</View>
)
}
const mapStateToProps = state => {
return {
inputWord: state.inputWord
}
};
const mapDispatchToProps = {updateTextInput};
export default connect(mapStateToProps, mapDispatchToProps) (DictionaryScreen);
存储库.ts
export class EJRepository {
private ormRepository: Repository<EJModel>;
constructor(connection: Connection) {
this.ormRepository = connection.getRepository(EJModel);
}
public async getWordandMean(props: any): Promise<EJModel> {
console.log(props); //=> this returns the same text from DictionaryScreen
const results = await this.ormRepository.query(
`SELECT * FROM ejmodel WHERE word LIKE '%${props}%';`, [props]);
return results;
}
}
连接.tsx
interface DatabaseConnectionContextData {
ejRepository: EJRepository;
}
const DatabaseConnectionContext = createContext<DatabaseConnectionContextData>(
{} as DatabaseConnectionContextData,
);
export const DatabaseConnectionProvider: React.FC = ({ children }) => {
const [connection, setConnection] = useState<Connection | null>(null);
const connect = useCallback(async () => {
const createdConnection = await createConnection({
type: 'react-native',
name: "ejdict.v1.0",
database: '***.db',
entities: [EJModel],
location: 'default',
migrations: [ejdict1621603544180],
migrationsRun: true,
synchronize: false,
logging: true,
extra: {createFromLocation: '/Users/***/***/ios/***/www/***.db' }
});
setConnection(createdConnection);
}, []);
useEffect(() => {
if (!connection) {
connect();
}
}, [connect, connection]);
if (!connection) {
return <ActivityIndicator />;
}
return (
<DatabaseConnectionContext.Provider
value={{
ejRepository: new EJRepository(connection),
}}
>
{children}
</DatabaseConnectionContext.Provider>
);
};
export function useDatabaseConnection() {
const context = useContext(DatabaseConnectionContext);
return context;
}
EJModel.ts
import {Entity, PrimaryColumn, Column} from "typeorm";
@Entity({name: "ejmodel"})
export class EJModel {
@PrimaryColumn({type: 'integer'})
item_id: number;
@Column({type: 'text'})
word: string;
@Column({type: 'text'})
mean: string;
@Column({type: 'integer'})
level: number;
}
PS:在 React Native 项目中加载现有数据库是一种不寻常的尝试吗?工作示例几乎不存在。我曾经让它在 Expo 上工作,但我意识到我需要迁移功能,并试图用 TypeORM 做同样的事情,浪费大量时间......