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

Simplified api #336

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ gradle-app.setting
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

instructions/
instructions/

*.test.js
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@babel/preset-flow']
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "SQLite3 bindings for React Native (Android & iOS)",
"main": "sqlite.js",
"scripts": {
"test": "echo \"Error: no test specified yet\" && exit 1"
"test": "jest"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -39,5 +39,9 @@
"ios": {
"project": "src/ios/SQLite.xcodeproj"
}
},
"devDependencies": {
"@babel/preset-flow": "^7.0.0",
"jest": "^24.4.0"
}
}
131 changes: 131 additions & 0 deletions simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* @flow strict-local
*/

'use strict';

const { NativeModules } = require('react-native');
const { SQLite } = NativeModules;

export type DataType = number | string;

export type QueryFailResult = {
qid: number,
result: {
message: string
},
type: 'error'
};

export type QuerySuccessResult<T> = {
qid: number,
result: {
insertId?: number,
rows?: T[],
rowsAffected?: number
},
type: 'success'
};

export type QueryResult<T> = QueryFailResult | QuerySuccessResult<T>;

class Database {
lastQueryID: number;
name: string;

constructor(name: string) {
this.lastQueryID = 0;
this.name = name;
}

/**
* Get all results from the query.
*/
async all<T>(sql: string, params?: DataType[]): Promise<T[]> {
const results = await this.executeBatch<T>([{ sql, params }]);
const result = results[0];
if (result.type === 'success') {
if (result.result.rows == null) {
// Statement was not a SELECT.
return [];
} else {
return result.result.rows;
}
} else {
throw new Error(result.result.message);
}
}

/**
* Close the database.
*/
async close(): Promise<void> {
return new Promise((resolve, reject) => {
SQLite.close({ path: this.name }, () => resolve(), reject);
});
}

/**
* Execute a statement.
*/
async exec(sql: string, params?: DataType[]): Promise<void> {
await this.all(sql, params);
}

/**
* Execute a batch of queries, returning the status and output of each.
*/
async executeBatch<T>(
queries: { sql: string, params?: DataType[] }[]
): Promise<QueryResult<T>[]> {
if (queries.length === 0) {
return [];
}

const executes: { qid: number, sql: string, params: ?(DataType[]) }[] = [];
for (const query of queries) {
this.lastQueryID += 1;
executes.push({
qid: this.lastQueryID,
sql: query.sql,
params: query.params == null ? [] : query.params
});
}

return new Promise((resolve, reject) => {
SQLite.executeSqlBatch(
{
dbargs: { dbname: this.name },
executes
},
resolve,
reject
);
});
}

/**
* Get the first result from the query.
*/
async get<T>(sql: string, params?: DataType[]): Promise<?T> {
const rows = await this.all<T>(sql, params);
return rows.length > 0 ? rows[0] : null;
}
}

export type { Database };

/**
* Open the database.
*/
async function open(filename: string): Promise<Database> {
return new Promise((resolve, reject) => {
SQLite.open(
{ name: filename, dblocation: 'nosync' },
() => resolve(new Database(filename)),
reject
);
});
}

module.exports = { open };
Loading