Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
asdfghjkkl11 committed Nov 25, 2024
1 parent 76c315f commit df4626d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ npm i @asdfghjkkl11/sqlite-sync

# Usage
```js
var sqlite = require('sqlite-sync'); //requiring
var sqlite = require('@asdfghjkkl11/sqlite-sync'); //requiring

//Initiating
await sqlite.init();

//Connecting - if the file does not exist it will be created
await sqlite.connect('test/test.db');
sqlite.connect('test/test.db');

//Creating table - you can run any command
sqlite.run("CREATE TABLE COMPANYS(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL);",function(res){
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"async",
"database"
],
"version": "1.12.1",
"version": "0.0.1",
"author": {
"name": "asdfghjkkl11",
"email": "[email protected]"
Expand All @@ -30,7 +30,8 @@
"sql.js": "^1.12.0"
},
"scripts": {
"test": "node test/test.js"
"test": "node test/test.js",
"publish": "npm publish --access=public"
},
"license": "MIT",
"engines": {
Expand Down
16 changes: 11 additions & 5 deletions sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const initSqlJs = require('sql.js');
const memoryBuffers = {};

//Variables
sqlite.prototype.SQL = null;
sqlite.prototype.db = null;
sqlite.prototype.buffer = null;
sqlite.prototype.writer = null;
Expand All @@ -49,13 +50,20 @@ function sqlite() {

}

/**
* Database initiate
*/
sqlite.prototype.init = async function () {
this.SQL = await initSqlJs();
}

/**
* Database connection
*
* @param {String|Object} db - File directory+filename | buffer
* @return {Object}
*/
sqlite.prototype.connect = async function (db) {
sqlite.prototype.connect = function (db) {
if (typeof (db) == 'string') {
const fileMemory = db.indexOf('file::memory:') === 0;
this.file = db;
Expand All @@ -75,17 +83,15 @@ sqlite.prototype.connect = async function (db) {
this.buffer = db;
}

const SQL = await initSqlJs();

if (this.buffer) {
try {
this.db = new SQL.Database(this.buffer);
this.db = new this.SQL.Database(this.buffer);
} catch (x) {
throw x;
}
} else {
try {
this.db = new SQL.Database();
this.db = new this.SQL.Database();
this.write();
} catch (x) {
throw x;
Expand Down
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const sqlite = require('../sqlite');

async function test_func() {
await sqlite.connect('test/test.db');
await sqlite.init();
sqlite.connect('test/test.db');
// sqlite.debug = true;

let res = sqlite.run("CREATE TABLE COMPANYS(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL);");
Expand Down

0 comments on commit df4626d

Please sign in to comment.