@@ -109,6 +109,9 @@ bool DatabaseSync::Open() {
109109 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
110110 int r = sqlite3_open_v2 (location_.c_str (), &connection_, flags, nullptr );
111111 CHECK_ERROR_OR_THROW (env ()->isolate (), connection_, r, SQLITE_OK, false );
112+ int r2 = sqlite3_db_config (
113+ connection_, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1 , NULL );
114+ CHECK_ERROR_OR_THROW (env ()->isolate (), connection_, r2, SQLITE_OK, false );
112115 return true ;
113116}
114117
@@ -211,6 +214,24 @@ void DatabaseSync::Exec(const FunctionCallbackInfo<Value>& args) {
211214 CHECK_ERROR_OR_THROW (env->isolate (), db->connection_ , r, SQLITE_OK, void ());
212215}
213216
217+ void DatabaseSync::LoadExtension (const FunctionCallbackInfo<Value>& args) {
218+ DatabaseSync* db;
219+ ASSIGN_OR_RETURN_UNWRAP (&db, args.This ());
220+ Environment* env = Environment::GetCurrent (args);
221+ THROW_AND_RETURN_ON_BAD_STATE (
222+ env, db->connection_ == nullptr , " database is not open" );
223+
224+ if (!args[0 ]->IsString ()) {
225+ node::THROW_ERR_INVALID_ARG_TYPE (env->isolate (),
226+ " The \" path\" argument must be a string." );
227+ return ;
228+ }
229+
230+ auto path = node::Utf8Value (env->isolate (), args[0 ].As <String>());
231+ int r = sqlite3_load_extension (db->connection_ , *path, nullptr , nullptr );
232+ CHECK_ERROR_OR_THROW (env->isolate (), db->connection_ , r, SQLITE_OK, void ());
233+ }
234+
214235StatementSync::StatementSync (Environment* env,
215236 Local<Object> object,
216237 sqlite3* db,
@@ -668,6 +689,8 @@ static void Initialize(Local<Object> target,
668689 SetProtoMethod (isolate, db_tmpl, " close" , DatabaseSync::Close);
669690 SetProtoMethod (isolate, db_tmpl, " prepare" , DatabaseSync::Prepare);
670691 SetProtoMethod (isolate, db_tmpl, " exec" , DatabaseSync::Exec);
692+ SetProtoMethod (
693+ isolate, db_tmpl, " loadExtension" , DatabaseSync::LoadExtension);
671694 SetConstructorFunction (context, target, " DatabaseSync" , db_tmpl);
672695 SetConstructorFunction (context,
673696 target,
0 commit comments