duckdb 0.4.1-dev396.0 → 0.4.1-dev421.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "duckdb",
3
3
  "main": "./lib/duckdb.js",
4
- "version": "0.4.1-dev396.0",
4
+ "version": "0.4.1-dev421.0",
5
5
  "description": "DuckDB node.js API",
6
6
  "gypfile": true,
7
7
  "dependencies": {
package/src/database.cpp CHANGED
@@ -22,17 +22,39 @@ Napi::Object Database::Init(Napi::Env env, Napi::Object exports) {
22
22
  }
23
23
 
24
24
  struct OpenTask : public Task {
25
- OpenTask(Database &database_, std::string filename_, bool read_only_, Napi::Function callback_)
26
- : Task(database_, callback_), filename(filename_), read_only(read_only_) {
25
+ OpenTask(Database &database_, std::string filename_, duckdb::AccessMode access_mode_, Napi::Object config_,
26
+ Napi::Function callback_)
27
+ : Task(database_, callback_), filename(filename_) {
28
+
29
+ duckdb_config.access_mode = access_mode_;
30
+ Napi::Env env = database_.Env();
31
+ Napi::HandleScope scope(env);
32
+
33
+ if (!config_.IsUndefined()) {
34
+ const Napi::Array config_names = config_.GetPropertyNames();
35
+
36
+ for (duckdb::idx_t config_idx = 0; config_idx < config_names.Length(); config_idx++) {
37
+ std::string key = config_names.Get(config_idx).As<Napi::String>();
38
+ std::string val = config_.Get(key).As<Napi::String>();
39
+ auto config_property = duckdb::DBConfig::GetOptionByName(key);
40
+ if (!config_property) {
41
+ Napi::TypeError::New(env, "Unrecognized configuration property" + key).ThrowAsJavaScriptException();
42
+ return;
43
+ }
44
+ try {
45
+ duckdb_config.SetOption(*config_property, duckdb::Value(val));
46
+ } catch (std::exception &e) {
47
+ Napi::TypeError::New(env, "Failed to set configuration option " + key + ": " + e.what())
48
+ .ThrowAsJavaScriptException();
49
+ return;
50
+ }
51
+ }
52
+ }
27
53
  }
28
54
 
29
55
  void DoWork() override {
30
56
  try {
31
- duckdb::DBConfig config;
32
- if (read_only) {
33
- config.access_mode = duckdb::AccessMode::READ_ONLY;
34
- }
35
- Get<Database>().database = duckdb::make_unique<duckdb::DuckDB>(filename, &config);
57
+ Get<Database>().database = duckdb::make_unique<duckdb::DuckDB>(filename, &duckdb_config);
36
58
  duckdb::ParquetExtension extension;
37
59
  extension.Load(*Get<Database>().database);
38
60
  success = true;
@@ -59,22 +81,34 @@ struct OpenTask : public Task {
59
81
  }
60
82
 
61
83
  std::string filename;
62
- bool read_only = false;
84
+ duckdb::DBConfig duckdb_config;
63
85
  std::string error = "";
64
86
  bool success = false;
65
87
  };
66
88
 
67
89
  Database::Database(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Database>(info), task_inflight(false) {
68
90
  auto env = info.Env();
91
+
69
92
  if (info.Length() < 1 || !info[0].IsString()) {
70
93
  Napi::TypeError::New(env, "Database location expected").ThrowAsJavaScriptException();
71
94
  return;
72
95
  }
73
96
  std::string filename = info[0].As<Napi::String>();
74
97
  unsigned int pos = 1;
98
+
99
+ duckdb::AccessMode access_mode = duckdb::AccessMode::AUTOMATIC;
100
+
75
101
  int mode = 0;
76
102
  if (info.Length() >= pos && info[pos].IsNumber() && Utils::OtherIsInt(info[pos].As<Napi::Number>())) {
77
103
  mode = info[pos++].As<Napi::Number>().Int32Value();
104
+ if (mode == DUCKDB_NODEJS_READONLY) {
105
+ access_mode = duckdb::AccessMode::READ_ONLY;
106
+ }
107
+ }
108
+
109
+ Napi::Object config;
110
+ if (info.Length() >= pos && info[pos].IsObject() && !info[pos].IsFunction()) {
111
+ config = info[pos++].As<Napi::Object>();
78
112
  }
79
113
 
80
114
  Napi::Function callback;
@@ -82,7 +116,7 @@ Database::Database(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Database>(
82
116
  callback = info[pos++].As<Napi::Function>();
83
117
  }
84
118
 
85
- Schedule(env, duckdb::make_unique<OpenTask>(*this, filename, mode == DUCKDB_NODEJS_READONLY, callback));
119
+ Schedule(env, duckdb::make_unique<OpenTask>(*this, filename, access_mode, config, callback));
86
120
  }
87
121
 
88
122
  void Database::Schedule(Napi::Env env, std::unique_ptr<Task> task) {