Skip to content

Commit

Permalink
1.0.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Lott authored and Scott Lott committed Dec 11, 2017
1 parent a49f153 commit ded5ec0
Show file tree
Hide file tree
Showing 17 changed files with 649 additions and 190 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Read: ~100 records/ms
- Test and improve performance of LevelDB on the server.
- Write Cordova plugin.
- Write SQLite plugin.
- Finish integration tests.
- Finish integration tests for history.

## [1.0.0] 12-10-2017
MANY BREAKING CHANGES, PLEASE READ THE [MIGRATION GUIDE](https://docs.nanosql.io/fine-print/migration)
Expand All @@ -33,6 +33,7 @@ This build is intended to stabilize the library, increase performance and make i
- `updateORM` query has been removed entirely.
- ORM updates now happen along side `upsert`, `delete` and `drop` queries.
- ORM updates are more consistent and way more performant.
- Added a large number of integration tests.

## [0.9.3] 10-15-2017
- Added changed rows property to database events.
Expand Down
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,6 @@ nSQL("users")

```

## Performance

nanoSQL is built on top of NoSQL systems so there are several performance optimizations you can take advantage of for super fast queries.

[Performance Docs](https://docs.nanosql.io//4.-Default-Store#performance-considerations)

[Documentation](https://docs.nanosql.io/)

## History

The Undo/Redo system is enabled by default and it's easy to use.
Expand Down
2 changes: 1 addition & 1 deletion dist/nano-sql.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/nano-sql.min.js

Large diffs are not rendered by default.

44 changes: 30 additions & 14 deletions lib/database/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,52 @@ var _NanoSQLStorageQuery = (function () {
};
_NanoSQLStorageQuery.prototype._updateORMRows = function (relation, fromPKs, add, primaryKey, complete) {
var _this = this;
this._store._nsql.table(relation._fromTable).query("select").where([this._store.tableInfo[relation._fromTable]._pk, "IN", fromPKs]).exec().then(function (rows) {
var fromPk = this._store.tableInfo[relation._fromTable]._pk;
this._store._read(relation._fromTable, fromPKs, function (rows) {
new utilities_1.ALL(rows.map(function (row) {
return function (rowDone) {
row = Object.isFrozen(row) ? utilities_1._assign(row) : row;
var newRow = Object.isFrozen(row) ? utilities_1._assign(row) : row;
if (relation._fromType === "array") {
row[relation._fromColumn] = row[relation._fromColumn] || [];
newRow[relation._fromColumn] = newRow[relation._fromColumn] || [];
var idxOf = newRow[relation._fromColumn].indexOf(primaryKey);
if (add) {
row[relation._fromColumn].push(primaryKey);
if (idxOf === -1) {
newRow[relation._fromColumn].push(primaryKey);
}
else {
rowDone();
}
}
else {
var idxOf = row[relation._fromColumn].indexOf(primaryKey);
if (idxOf !== -1) {
row[relation._fromColumn].splice(idxOf, 1);
newRow[relation._fromColumn].splice(idxOf, 1);
}
else {
rowDone();
}
}
row[relation._fromColumn].sort();
newRow[relation._fromColumn].sort();
}
else {
if (add) {
row[relation._fromColumn] = primaryKey;
newRow[relation._fromColumn] = primaryKey;
}
else {
row[relation._fromColumn] = undefined;
newRow[relation._fromColumn] = null;
}
}
_this._store._nsql.table(relation._fromTable).query("upsert", row).exec().then(rowDone);
_this._store._nsql.table(relation._fromTable).query("upsert", newRow).comment("ORM Update").exec().then(rowDone);
};
})).then(complete);
});
};
_NanoSQLStorageQuery.prototype._syncORM = function (type, oldRows, newRows, complete) {
var _this = this;
var useRelations = this._store._relToTable[this._query.table];
if (this._query.comments.indexOf("ORM Update") !== -1) {
complete();
return;
}
if (!useRelations || !useRelations.length) {
complete();
return;
Expand All @@ -128,6 +141,9 @@ var _NanoSQLStorageQuery = (function () {
return function (relationDone) {
var equals = function (val1, val2) {
if (Array.isArray(val1) && Array.isArray(val2)) {
if (val1.length !== val2.length) {
return false;
}
return val1.filter(function (v, i) { return v !== val2[i]; }).length > 0;
}
else {
Expand Down Expand Up @@ -158,14 +174,14 @@ var _NanoSQLStorageQuery = (function () {
}
else {
var addRelation = function () {
if (newRows[idx][relation._thisColumn] !== undefined) {
if (newRows[idx][relation._thisColumn] !== null && newRows[idx][relation._thisColumn] !== undefined) {
_this._updateORMRows(relation, [newRows[idx][relation._thisColumn]], true, primaryKey_1, relationDone);
}
else {
relationDone();
}
};
if (oldRows[idx][relation._thisColumn] !== undefined) {
if (oldRows[idx][relation._thisColumn] !== null && oldRows[idx][relation._thisColumn] !== undefined) {
_this._updateORMRows(relation, [oldRows[idx][relation._thisColumn]], false, primaryKey_1, addRelation);
}
else {
Expand Down Expand Up @@ -424,7 +440,7 @@ var _MutateSelection = (function () {
ormResult();
return;
}
var relateData = _this.s._relFromTable[_this.q.table][orm.key];
var relateData = _this.s._columnsAreTables[_this.q.table][orm.key];
if (relateData) {
_this.s._nsql.table(relateData._toTable).query("select").where([_this.s.tableInfo[relateData._toTable]._pk, relateData._thisType === "array" ? "IN" : "=", row[orm.key]]).exec().then(function (rows) {
var q = _this.s._nsql.table(rows).query("select", orm.select);
Expand All @@ -445,7 +461,7 @@ var _MutateSelection = (function () {
row[orm.key] = relateData._thisType === "array" ? [] : undefined;
}
else {
row[orm.key] = relateData._thisType === "array" ? rows.filter(function (r) { return r; }) : rows[0];
row[orm.key] = relateData._thisType === "array" ? rows : rows[0];
}
ormResult();
});
Expand Down
8 changes: 8 additions & 0 deletions lib/database/storage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export declare class _NanoSQLStorage {
};
};
};
_columnsAreTables: {
[tableName: string]: {
[thisColmn: string]: {
_toTable: string;
_thisType: "array" | "single";
};
};
};
_relToTable: {
[tableName: string]: {
_thisColumn: string;
Expand Down
34 changes: 21 additions & 13 deletions lib/database/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,35 @@ var _NanoSQLStorage = (function () {
this._relFromTable = {};
this._relToTable = {};
this._relationColumns = {};
this._columnsAreTables = {};
this._tableNames.forEach(function (table) {
var i = _this.models[table].length;
_this._relFromTable[table] = {};
_this._relationColumns[table] = [];
_this._relToTable[table] = [];
_this._columnsAreTables[table] = {};
var _loop_1 = function () {
var p = _this.models[table][i];
if (p.props && _this._tableNames.indexOf(p.type.replace("[]", "")) !== -1) {
if (_this._tableNames.indexOf(p.type.replace("[]", "")) !== -1) {
var mapTo_1 = "";
p.props.forEach(function (p) {
if (p.indexOf("ref=>") !== -1)
mapTo_1 = p.replace("ref=>", "");
});
if (mapTo_1) {
_this._relationColumns[table].push(p.key);
_this._relFromTable[table][p.key] = {
_toTable: p.type.replace("[]", ""),
_toColumn: mapTo_1.replace("[]", ""),
_toType: mapTo_1.indexOf("[]") === -1 ? "single" : "array",
_thisType: p.type.indexOf("[]") === -1 ? "single" : "array"
};
_this._columnsAreTables[table][p.key] = {
_toTable: p.type.replace("[]", ""),
_thisType: p.type.indexOf("[]") === -1 ? "single" : "array"
};
if (p.props) {
p.props.forEach(function (p) {
if (p.indexOf("ref=>") !== -1)
mapTo_1 = p.replace("ref=>", "");
});
if (mapTo_1) {
_this._relationColumns[table].push(p.key);
_this._relFromTable[table][p.key] = {
_toTable: p.type.replace("[]", ""),
_toColumn: mapTo_1.replace("[]", ""),
_toType: mapTo_1.indexOf("[]") === -1 ? "single" : "array",
_thisType: p.type.indexOf("[]") === -1 ? "single" : "array"
};
}
}
}
};
Expand Down
5 changes: 4 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ export declare class NanoSQLInstance {
[fnName: string]: NanoSQLFunction;
};
data: any;
_AVMod: IActionViewMod;
_hasEvents: StdObject<boolean>;
_tableNames: string[];
private _callbacks;
constructor();
table(table?: string): NanoSQLInstance;
connect(): Promise<Object | string>;
avFilter(filterFunc: IActionViewMod): this;
use(plugin: NanoSQLPlugin): this;
on(actions: string, callBack: (event: DatabaseEvent, database: NanoSQLInstance) => void): NanoSQLInstance;
off(actions: string, callBack: (event: DatabaseEvent, database: NanoSQLInstance) => void): NanoSQLInstance;
Expand All @@ -76,8 +78,9 @@ export declare class NanoSQLInstance {
getView(viewName: string, viewArgs?: any): Promise<Array<any> | NanoSQLInstance>;
actions(actionArray: Array<ActionOrView>): NanoSQLInstance;
doAction(actionName: string, actionArgs: any): Promise<Array<DBRow> | NanoSQLInstance>;
queryFilter(callBack: (args: IdbQuery, complete: (args: IdbQuery) => void) => void): NanoSQLInstance;
private _doAV(AVType, AVList, AVName, AVargs);
query(action: "select" | "upsert" | "delete" | "drop" | "show tables" | "describe", args?: any, bypassORMPurge?: boolean): _NanoSQLQuery;
query(action: "select" | "upsert" | "delete" | "drop" | "show tables" | "describe", args?: any): _NanoSQLQuery;
triggerEvent(eventData: DatabaseEvent): NanoSQLInstance;
default(replaceObj?: any): {
[key: string]: any;
Expand Down
31 changes: 27 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var NanoSQLInstance = (function () {
{ key: "key", type: "string", props: ["pk", "ai"] },
{ key: "value", type: "any" }
];
_this._plugins = [];
if (t._config && t._config.history) {
_this.use(new history_plugin_1._NanoSQLHistoryPlugin(t._config.historyMode));
}
Expand Down Expand Up @@ -122,6 +123,10 @@ var NanoSQLInstance = (function () {
});
});
};
NanoSQLInstance.prototype.avFilter = function (filterFunc) {
this._AVMod = filterFunc;
return this;
};
NanoSQLInstance.prototype.use = function (plugin) {
return this._plugins.push(plugin), this;
};
Expand Down Expand Up @@ -201,7 +206,12 @@ var NanoSQLInstance = (function () {
return new lie_ts_1.Promise(function (res, rej) { return rej(); });
return this._doAV("Action", this._actions[this.sTable], actionName, actionArgs);
};
NanoSQLInstance.prototype.queryFilter = function (callBack) {
this._queryMod = callBack;
return this;
};
NanoSQLInstance.prototype._doAV = function (AVType, AVList, AVName, AVargs) {
var _this = this;
var t = this;
var selAV = AVList.reduce(function (prev, cur) {
if (cur.name === AVName)
Expand All @@ -212,11 +222,24 @@ var NanoSQLInstance = (function () {
return new lie_ts_1.Promise(function (res, rej) { return rej("Action/View Not Found!"); });
}
t._activeAV = AVName;
return selAV.call(selAV.args ? utilities_1.cleanArgs(selAV.args, AVargs) : {}, t);
if (t._AVMod) {
return new lie_ts_1.Promise(function (res, rej) {
t._AVMod(_this.sTable, AVType, t._activeAV || "", utilities_1.cleanArgs, function (args) {
selAV ? selAV.call(args, t).then(function (result) {
res(result, t);
}) : false;
}, function (err) {
rej(err);
});
});
}
else {
return selAV.call(selAV.args ? utilities_1.cleanArgs(selAV.args, AVargs) : {}, t);
}
};
NanoSQLInstance.prototype.query = function (action, args, bypassORMPurge) {
NanoSQLInstance.prototype.query = function (action, args) {
var t = this;
var query = new std_query_1._NanoSQLQuery(t.sTable, t, action.toLowerCase(), args, t._activeAV, bypassORMPurge);
var query = new std_query_1._NanoSQLQuery(t.sTable, t, action.toLowerCase(), args, t._activeAV);
t._activeAV = undefined;
return query;
};
Expand Down Expand Up @@ -281,7 +304,7 @@ var NanoSQLInstance = (function () {
new utilities_1.CHAIN(queries.map(function (quer) {
return function (nextQuery) {
tables.push(quer.table);
t.table(quer.table).query(quer.action, quer.actionArgs, true).manualExec(__assign({}, quer, { transaction: true, queryID: transactionID, actionArgs: undefined })).then(nextQuery);
t.table(quer.table).query(quer.action, quer.actionArgs).manualExec(__assign({}, quer, { transaction: true, queryID: transactionID, actionArgs: undefined })).then(nextQuery);
};
})).then(function (results) {
new utilities_1.CHAIN(_this._plugins.map(function (p) {
Expand Down
4 changes: 2 additions & 2 deletions lib/query/std-query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export declare class _NanoSQLQuery {
_error: string;
_AV: string;
private _query;
constructor(table: string | any[], db: NanoSQLInstance, queryAction: string, queryArgs?: any, actionOrView?: string, bypassORM?: boolean);
constructor(table: string | any[], db: NanoSQLInstance, queryAction: string, queryArgs?: any, actionOrView?: string);
where(args: any[] | any): _NanoSQLQuery;
range(limit: number, offset: number): _NanoSQLQuery;
orm(ormArgs?: (string | ORMArgs)[]): _NanoSQLQuery;
Expand All @@ -52,6 +52,6 @@ export declare class _NanoSQLQuery {
extend(...args: any[]): _NanoSQLQuery;
offset(args: number): _NanoSQLQuery;
toCSV(headers?: boolean): Promise<string>;
manualExec(query: IdbQuery, complete?: (err: any, result: any[]) => void): Promise<any>;
manualExec(query: IdbQuery): Promise<any>;
exec(): Promise<(object | NanoSQLInstance)[]>;
}
Loading

0 comments on commit ded5ec0

Please sign in to comment.