slite-change
Advanced tools
Comparing version 1.2.7-0 to 1.2.8-0
declare let sqlite: any; | ||
declare type ProcStack = { | ||
procName: string; | ||
procFunc: (args: string) => void; | ||
}; | ||
/** | ||
@@ -20,3 +24,4 @@ * While creating a moder Angular web service to run on Raspberry Pi, a DB change control was needed. | ||
private sourceFiles; | ||
constructor(Sql: typeof sqlite.Database, ChangeLog: string, Callback: any); | ||
private ProcCallStack; | ||
constructor(Sql: typeof sqlite.Database, ChangeLog: string, Callback?: any, ProcCallStack?: ProcStack[]); | ||
/** | ||
@@ -34,2 +39,3 @@ * Hold the first failure | ||
private ReadChangeFiles; | ||
private runProcStack; | ||
private CommitChange; | ||
@@ -36,0 +42,0 @@ /** |
@@ -15,3 +15,3 @@ "use strict"; | ||
class SliteChange { | ||
constructor(Sql, ChangeLog, Callback) { | ||
constructor(Sql, ChangeLog, Callback, ProcCallStack) { | ||
this.ChangeLog = ChangeLog; | ||
@@ -27,2 +27,6 @@ this.ChangeLogDir = ChangeLog; | ||
this.sourceFiles = 0; | ||
this.ProcCallStack = []; | ||
if (ProcCallStack) { | ||
this.ProcCallStack = ProcCallStack; | ||
} | ||
this.ChangeLogTable((errMsg) => { | ||
@@ -82,2 +86,3 @@ if (errMsg != '') { | ||
let ChangeNumber = ''; | ||
let Procedures = []; | ||
let SqlArray = data.toString().split("\n"); | ||
@@ -91,2 +96,9 @@ SqlArray.forEach((line) => { | ||
} | ||
if (line.startsWith('--')) { | ||
return; // comment line | ||
} | ||
if (line.startsWith('@Procedure ')) { | ||
Procedures.push(line.substring('@Procedure '.length)); | ||
return; | ||
} | ||
Buffer += line; | ||
@@ -105,3 +117,4 @@ Buffer += '\n'; | ||
sql: Buffer, | ||
file: file | ||
file: file, | ||
procedures: Procedures | ||
}); | ||
@@ -119,2 +132,22 @@ if (this.callStack.length === files.length) { | ||
} | ||
runProcStack(procStack, CallBack) { | ||
if (procStack.length == 0) { | ||
CallBack(); | ||
} | ||
else { | ||
procStack.forEach((pC, i) => { | ||
let CallName = pC.substring(0, pC.indexOf(' ')); | ||
let CallArgs = pC.substring(pC.indexOf(' ') + 1); | ||
this.ProcCallStack.filter(caller => { | ||
if (caller.procName === CallName) { | ||
caller.procFunc(CallArgs); | ||
} | ||
}); | ||
// Should really do this after the last funciton call. | ||
if (i == procStack.length - 1) { | ||
CallBack(); | ||
} | ||
}); | ||
} | ||
} | ||
CommitChange(callStack, Callback) { | ||
@@ -141,10 +174,12 @@ this.ChangesIssued++; | ||
} | ||
let Next = this.callStack.find((cB) => cB.previous == callStack.id); | ||
if (Next) { | ||
this.CommitChange(Next, Callback); | ||
} | ||
else { | ||
// This the all done callback | ||
Callback(this.ErrorMessage); | ||
} | ||
this.runProcStack(callStack.procedures, () => { | ||
let Next = this.callStack.find((cB) => cB.previous == callStack.id); | ||
if (Next) { | ||
this.CommitChange(Next, Callback); | ||
} | ||
else { | ||
// This the all done callback | ||
Callback(this.ErrorMessage); | ||
} | ||
}); | ||
} | ||
@@ -151,0 +186,0 @@ }); |
{ | ||
"name": "slite-change", | ||
"version": "1.2.7-0", | ||
"version": "1.2.8-0", | ||
"description": "A simple change management system for SQLite3 written in pure JavaScript/TypeScript.", | ||
@@ -5,0 +5,0 @@ "main": "dist/slite-change.js", |
@@ -41,2 +41,3 @@ # slite-change | ||
-- Coment lines are supported | ||
INSERT INTO 'users' (id, firstname, lastname, username, passwd, emailAddress) VALUES (0, 'admin', 'admin', 'admin', 'password', 'roo@localhost'); | ||
@@ -80,2 +81,40 @@ | ||
# Using Stored Procedures | ||
Sqlite does not support stored procedures but **slite-change** does. Stored procedures are written to the SQL files and can be called using the tag **@Procedure** followed by the function name | ||
and arguments to be passed to the function. | ||
```sql | ||
@Procedure AddUser guest guest guest guest guest | ||
``` | ||
```javascript | ||
import SliteChange from 'slite-change'; | ||
... | ||
this.Sql = new sqlite.Database('my_database.db', (err) => { | ||
if (err) { | ||
console.log('Could not connect to database', err) | ||
} else { | ||
console.log('Connected to database'); | ||
new SliteChange(this.Sql, './src/resources/db-changes/', (ReturnMsg) => { | ||
if(ReturnMsg != '') { | ||
console.log('Error: %s', ReturnMsg); | ||
} | ||
}); | ||
} | ||
}, [ | ||
{ procName: 'AddUser', procFunc: (args: string) => { this.AddUser(args); } } | ||
]); | ||
... | ||
private AddUser(args: string) { | ||
let argsArray = args.split(' '); | ||
this.Sql.all('INSERT INTO users (firstname, lastname, username, password, emailAddress) \ | ||
VALUES (?, ?, ?, ?, ?)', argsArray, (error) => { | ||
if(error) { | ||
console.error(error.message); | ||
} | ||
}); | ||
} | ||
``` | ||
--- | ||
@@ -82,0 +121,0 @@ |
@@ -15,4 +15,10 @@ "use strict"; | ||
file: string; | ||
procedures: string[]; | ||
}; | ||
type ProcStack = { | ||
procName: string; | ||
procFunc: (args: string) => void; | ||
} | ||
/** | ||
@@ -36,4 +42,5 @@ * While creating a moder Angular web service to run on Raspberry Pi, a DB change control was needed. | ||
private sourceFiles: number; | ||
private ProcCallStack: ProcStack[]; | ||
constructor(Sql: typeof sqlite.Database, ChangeLog: string, Callback: any) { | ||
constructor(Sql: typeof sqlite.Database, ChangeLog: string, Callback?: any, ProcCallStack?: ProcStack[]) { | ||
this.ChangeLog = ChangeLog; | ||
@@ -49,2 +56,6 @@ this.ChangeLogDir = ChangeLog; | ||
this.sourceFiles = 0; | ||
this.ProcCallStack = []; | ||
if(ProcCallStack) { | ||
this.ProcCallStack = ProcCallStack; | ||
} | ||
@@ -106,2 +117,3 @@ this.ChangeLogTable((errMsg: string) => { | ||
let ChangeNumber = ''; | ||
let Procedures: string[] = []; | ||
let SqlArray = data.toString().split("\n"); | ||
@@ -115,2 +127,9 @@ SqlArray.forEach((line: string) => { | ||
} | ||
if(line.startsWith('--')) { | ||
return // comment line | ||
} | ||
if(line.startsWith('@Procedure ')) { | ||
Procedures.push(line.substring('@Procedure '.length)); | ||
return | ||
} | ||
Buffer += line; | ||
@@ -130,3 +149,4 @@ Buffer += '\n'; | ||
sql: Buffer, | ||
file: file | ||
file: file, | ||
procedures: Procedures | ||
}); | ||
@@ -146,2 +166,20 @@ | ||
private runProcStack(procStack: string[], CallBack: any) { | ||
if(procStack.length == 0) { | ||
CallBack(); | ||
} else { | ||
procStack.forEach( (pC: string, i: number) => { | ||
let CallName = pC.substring(0, pC.indexOf(' ')); | ||
let CallArgs = pC.substring(pC.indexOf(' ')+1); | ||
this.ProcCallStack.filter( caller => { if(caller.procName === CallName) { | ||
caller.procFunc(CallArgs); | ||
}}); | ||
// Should really do this after the last funciton call. | ||
if(i == procStack.length - 1) { | ||
CallBack(); | ||
} | ||
}); | ||
} | ||
} | ||
private CommitChange(callStack: CallStack, Callback: any) { | ||
@@ -173,9 +211,11 @@ this.ChangesIssued++; | ||
} | ||
let Next = this.callStack.find( (cB: CallStack) => cB.previous == callStack.id); | ||
if(Next) { | ||
this.CommitChange(Next, Callback); | ||
} else { | ||
// This the all done callback | ||
Callback(this.ErrorMessage); | ||
} | ||
this.runProcStack(callStack.procedures, () => { | ||
let Next = this.callStack.find( (cB: CallStack) => cB.previous == callStack.id); | ||
if(Next) { | ||
this.CommitChange(Next, Callback); | ||
} else { | ||
// This the all done callback | ||
Callback(this.ErrorMessage); | ||
} | ||
}); | ||
} | ||
@@ -182,0 +222,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51516
616
224