Skip to content

Commit 0e13ee2

Browse files
committed
add initial connection creation
1 parent 48ed9be commit 0e13ee2

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

.dockerignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ An experimental SQLite library for Node using Neon
1212
* Parallel queries
1313
* Serialization
1414
* Traces and profiling
15+
* Interrupting queries
16+
* Exporting a database
1517
* Modern infrastructure
1618
* Target latest node and electron versions
1719
* Promise based API

native/src/lib.rs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,82 @@
22
extern crate neon;
33
extern crate rusqlite;
44

5-
use rusqlite::version::{version as sqlite_version};
5+
use rusqlite::Connection;
66
use neon::prelude::*;
77

88
fn version(mut cx: FunctionContext) -> JsResult<JsString> {
9-
Ok(cx.string(sqlite_version()))
9+
Ok(cx.string(rusqlite::version::version()))
10+
}
11+
12+
pub struct Sqlite {
13+
pub conn: Option<Connection>,
14+
pub database: Option<String>,
15+
pub verbose: Option<bool>
16+
}
17+
18+
declare_types! {
19+
pub class JsSqlite for Sqlite {
20+
init(_cx) {
21+
Ok(Sqlite {
22+
conn: None,
23+
database: None,
24+
verbose: None
25+
})
26+
}
27+
28+
method create(mut cx) {
29+
let database_value: String;
30+
let verbose_value: bool;
31+
32+
match cx.argument_opt(0) {
33+
Some(arg) => {
34+
let obj = arg.downcast::<JsObject>().or_throw(&mut cx)?;
35+
// Handle verbose property defaults
36+
if obj.get(&mut cx, "verbose")?.is_a::<JsUndefined>() {
37+
verbose_value = true;
38+
} else {
39+
verbose_value = obj.get(&mut cx, "verbose")?.downcast::<JsBoolean>().or_throw(&mut cx)?.value();
40+
}
41+
// Handle database property defaults
42+
if obj.get(&mut cx, "database")?.is_a::<JsUndefined>() {
43+
database_value = ":memory:".to_string();
44+
} else {
45+
database_value = obj.get(&mut cx, "database")?.downcast::<JsString>().or_throw(&mut cx)?.value();
46+
}
47+
},
48+
None => {
49+
database_value = ":memory:".to_string();
50+
verbose_value = true;
51+
}
52+
}
53+
54+
let conn = if database_value == ":memory:".to_string() {
55+
Connection::open_in_memory().unwrap();
56+
} else {
57+
Connection::open(&database_value).unwrap();
58+
};
59+
60+
let this = cx.this();
61+
let js_database_value = cx.string(database_value);
62+
let js_verbose_value = cx.boolean(verbose_value);
63+
this.set(&mut cx, "database", js_database_value)?;
64+
this.set(&mut cx, "verbose", js_verbose_value)?;
65+
66+
Ok(this.upcast())
67+
}
68+
69+
method execute() {
70+
71+
}
72+
73+
method statement(mut cx) {
74+
Ok(cx.undefined().upcast())
75+
}
76+
}
1077
}
1178

1279
register_module!(mut m, {
13-
m.export_function("version", version)
80+
m.export_class::<JsSqlite>("Sqlite")?;
81+
m.export_function("version", version)?;
82+
Ok(())
1483
});

test/basic.spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
const Sqlite = require('..');
1+
const { version, Sqlite } = require('..');
22

33
describe('basic', () => {
44
it('should get sqlite version', () => {
5-
expect(Sqlite.version()).toMatchSnapshot();
5+
expect(version()).toMatchSnapshot();
6+
})
7+
8+
it('should get ', async () => {
9+
const conn = new Sqlite();
10+
await conn.create({
11+
verbose: true
12+
});
613
})
714
})

0 commit comments

Comments
 (0)