Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ezdata = require('./index.js');
- //our test table object
- var testdb;
- var SQLopts = {
- host: '',
- user: '',
- password: '',
- database: '',
- connectionLimit : 10
- }
- ezdata.ez_Init(SQLopts, function() {
- console.log("SQL Connected");
- console.log("Creating table...");
- testdb = ezdata.ez_CreateTable('test', {
- //columns definition
- val1: {default: 'Hello world', type:'varchar(64)'},
- val2: {default: 'Nice to see you', type:'varchar(64)'},
- valInt: {default: 2, type:'int(11)'},
- valObj: {
- default: {
- subitem1: 'object branch 1',
- subitem2: 'obj branch 2'
- },
- type: 'object(128)'
- }
- }, {}, () => {
- console.log('\nPost creation state:');
- console.log(testdb)
- // Async row add example, function returns after added to memory, callback called after SQL query completes
- console.log('\nStarting Async AddRow...')
- testdb.ez_AddRow(() => {
- console.log('Async AddRow complete!')
- if (testdb.MyKey.val1 === 'Hello world'){
- console.log('Updating specified key, val1');
- testdb.MyKey.val1 = "Im special";
- testdb.MyKey.ez_Save();
- }
- return
- }, "MyKey")//Custom, specific key lookup
- console.log('\nStarting load all...');
- testdb.ez_LoadAllRows(function(){
- //after load completes
- console.log('LoadAllRows complete...')
- console.log('\nAdding row...');
- var key = testdb.ez_AddRow(() => {
- console.log("Added row " + key + " to SQL...\n");
- console.log("testdb[key].val2 equals " + testdb[key].val2)
- testdb[key].val2 = 'A whole new value';
- console.log('Changed val2 to ' + testdb[key].val2);
- console.log("Saving...");
- testdb[key].ez_Save((err, result) => {
- console.log('Save completed!')
- console.log('\nUnloading key from memory...');
- testdb.ez_UnloadKey(key);
- console.log('\nCurrent memory key values:');
- console.log(testdb[key]);
- console.log('\nLoading key from SQL...');
- testdb.ez_LoadKey(key, () => {
- //key in memory guarenteed to match SQL values here.
- console.log('\nFinal dump:')
- console.log(testdb)
- process.exit(0);
- });
- return
- });
- return
- });
- console.log('Added row ' + key + ' to memory...')
- return;
- });
- console.log('LoadAll SQL statement awayyyy...');
- return;
- });
- });
- -----------------------------------------------------------------
- Console output after a few preliminary runs to fill the database:
- -----------------------------------------------------------------
- SQL Connected
- Creating table...
- Post creation state:
- { ez_Get: [Function: ez_Get],
- ez_Iterate: [Function: ez_Iterate],
- ez_AddRow: [Function: ez_AddRow],
- ez_UnloadKey: [Function: ez_UnloadKey],
- ez_LoadKey: [Function: ez_LoadKey],
- ez_LoadAllRows: [Function: ez_LoadAllRows],
- ez_TableName: 'test',
- ez_Columns:
- { val1: { default: 'Hello world', type: 'varchar(64)' },
- val2: { default: 'Nice to see you', type: 'varchar(64)' },
- valInt: { default: 2, type: 'int(11)' },
- valObj: { default: [Object], type: 'object(128)' } },
- MyKey: {},
- HyUFxDmHG: {},
- rkcN8vmrM: {},
- r1T8vDmBf: {} }
- Starting Async AddRow...
- WARNING --- tried to create row with existing key. Loading from SQL instead
- Starting load all...
- LoadAll SQL statement awayyyy...
- Async AddRow complete!
- LoadAllRows complete...
- Adding row...
- Added row r1PunD7SG to memory...
- Added row r1PunD7SG to SQL...
- testdb[key].val2 equals Nice to see you
- Changed val2 to A whole new value
- Saving...
- Save completed!
- Unloading key from memory...
- Current memory key values:
- { ez_TableName: 'test' }
- Loading key from SQL...
- Final dump:
- { ez_Get: [Function: ez_Get],
- ez_Iterate: [Function: ez_Iterate],
- ez_AddRow: [Function: ez_AddRow],
- ez_UnloadKey: [Function: ez_UnloadKey],
- ez_LoadKey: [Function: ez_LoadKey],
- ez_LoadAllRows: [Function: ez_LoadAllRows],
- ez_TableName: 'test',
- ez_Columns:
- { val1: { default: 'Hello world', type: 'varchar(64)' },
- val2: { default: 'Nice to see you', type: 'varchar(64)' },
- valInt: { default: 2, type: 'int(11)' },
- valObj: { default: [Object], type: 'object(128)' } },
- MyKey:
- { ez_Save: [Function: ez_Save],
- ez_RowID: 'MyKey',
- ez_TableName: 'test',
- val1: 'Im special',
- val2: 'Nice to see you',
- valInt: 2,
- valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } },
- HyUFxDmHG:
- { ez_RowID: 'HyUFxDmHG',
- val1: 'Hello world',
- val2: 'A whole new value',
- valInt: 2,
- valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } },
- rkcN8vmrM:
- { ez_RowID: 'rkcN8vmrM',
- val1: 'Hello world',
- val2: 'A whole new value',
- valInt: 2,
- valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } },
- r1T8vDmBf:
- { ez_RowID: 'r1T8vDmBf',
- val1: 'Hello world',
- val2: 'A whole new value',
- valInt: 2,
- valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } },
- r1PunD7SG:
- { ez_Save: [Function: ez_Save],
- ez_RowID: 'r1PunD7SG',
- ez_TableName: 'test',
- val1: 'Hello world',
- val2: 'A whole new value',
- valInt: 2,
- valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement