Skip to content

Commit ecdfca0

Browse files
committed
added build files and fixed cjs exports
1 parent 882871b commit ecdfca0

36 files changed

+1910
-23
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ npm-debug.log
33
docs/_build
44
docs/_static
55
docs/_templates
6-
.vscode/
7-
dist/
6+
.vscode/

dist/cjs/Database.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"use strict";
2+
var __assign = (this && this.__assign) || function () {
3+
__assign = Object.assign || function(t) {
4+
for (var s, i = 1, n = arguments.length; i < n; i++) {
5+
s = arguments[i];
6+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7+
t[p] = s[p];
8+
}
9+
return t;
10+
};
11+
return __assign.apply(this, arguments);
12+
};
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
var DatabaseSession_1 = require("./DatabaseSession");
15+
var DefaultModelFactory_1 = require("./DefaultModelFactory");
16+
var utils_1 = require("./utils");
17+
var defaultOptions = {
18+
cascadeAsDefault: false
19+
};
20+
var getMappedFunction = function (map, key, defaultFn) {
21+
if (!map)
22+
return defaultFn;
23+
if (typeof map === "function")
24+
return map;
25+
else if (map[key])
26+
return map[key];
27+
return defaultFn;
28+
};
29+
var Database = /** @class */ (function () {
30+
function Database(schema, options) {
31+
var _this = this;
32+
this.getNormalizer = function (schemaName) {
33+
return getMappedFunction(_this.options.onNormalize, schemaName, function (obj) { return obj; });
34+
};
35+
this.getPkGenerator = function (schemaName) {
36+
return getMappedFunction(_this.options.onGeneratePK, schemaName, function () { return undefined; });
37+
};
38+
this.getRecordComparer = function (schemaName) {
39+
return getMappedFunction(_this.options.onRecordCompare, schemaName, utils_1.isEqual);
40+
};
41+
utils_1.ensureParamObject("schema", schema);
42+
this.options = __assign({}, defaultOptions, options);
43+
this.factory = this.options.factory || new DefaultModelFactory_1.default();
44+
this.tables = Object.keys(schema).map(function (tableName) {
45+
return _this.factory.newTableSchema(_this, tableName, schema[tableName]);
46+
});
47+
this.tables.forEach(function (table) { return table.connect(_this.tables); });
48+
this._tableLookup = utils_1.toObject(this.tables, function (t) { return t.name; });
49+
}
50+
Database.prototype.combineReducers = function () {
51+
var _this = this;
52+
var reducers = [];
53+
for (var _i = 0; _i < arguments.length; _i++) {
54+
reducers[_i] = arguments[_i];
55+
}
56+
return function (state, action) {
57+
if (state === void 0) { state = {}; }
58+
return _this.reduce(state, action, reducers);
59+
};
60+
};
61+
Database.prototype.reduce = function (state, action, reducers, arg) {
62+
var session = this.createSession(state);
63+
utils_1.ensureArray(reducers).forEach(function (reducer) { return reducer(session.tables, action, arg); });
64+
return session.commit();
65+
};
66+
Database.prototype.createSession = function (state, options) {
67+
return new DatabaseSession_1.default(state, this, __assign({ readOnly: false }, options));
68+
};
69+
Database.prototype.selectTables = function (state) {
70+
var _this = this;
71+
var tableSchemas = Object.keys(state)
72+
.filter(function (tableName) { return _this._tableLookup[tableName]; })
73+
.map(function (tableName) { return _this._tableLookup[tableName]; });
74+
var session = this.createSession(state, {
75+
readOnly: true,
76+
tableSchemas: tableSchemas
77+
});
78+
return session.tables;
79+
};
80+
return Database;
81+
}());
82+
exports.default = Database;

dist/cjs/DatabaseSession.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"use strict";
2+
var __assign = (this && this.__assign) || function () {
3+
__assign = Object.assign || function(t) {
4+
for (var s, i = 1, n = arguments.length; i < n; i++) {
5+
s = arguments[i];
6+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7+
t[p] = s[p];
8+
}
9+
return t;
10+
};
11+
return __assign.apply(this, arguments);
12+
};
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
var errors_1 = require("./errors");
15+
var utils_1 = require("./utils");
16+
var DatabaseSession = /** @class */ (function () {
17+
function DatabaseSession(state, db, options) {
18+
if (state === void 0) { state = {}; }
19+
var _this = this;
20+
this.state = state;
21+
this.db = db;
22+
this.options = options;
23+
var tableSchemas = options.tableSchemas || db.tables;
24+
this.tables = utils_1.toObject(tableSchemas.map(function (tableSchema) {
25+
return _this.db.factory.newTableModel(_this, tableSchema, state[tableSchema.name]);
26+
}), function (t) { return t.schema.name; });
27+
}
28+
DatabaseSession.prototype.upsert = function (ctx) {
29+
var _this = this;
30+
if (this.options.readOnly)
31+
throw new Error(errors_1.default.sessionReadonly());
32+
Object.keys(ctx.output).forEach(function (name) {
33+
if (name !== ctx.schema.name)
34+
_this.tables[name].upsertNormalized(ctx.output[name]);
35+
});
36+
Object.keys(ctx.emits).forEach(function (name) {
37+
if (name !== ctx.schema.name)
38+
_this.tables[name].upsert(ctx.emits[name]);
39+
});
40+
};
41+
DatabaseSession.prototype.commit = function () {
42+
var _this = this;
43+
if (this.options.readOnly)
44+
throw new Error(errors_1.default.sessionReadonly());
45+
Object.keys(this.tables).forEach(function (table) {
46+
var _a;
47+
var oldState = _this.state[table];
48+
var newState = _this.tables[table].state;
49+
if (oldState !== newState)
50+
_this.state = __assign({}, _this.state, (_a = {}, _a[table] = newState, _a));
51+
});
52+
return this.state;
53+
};
54+
return DatabaseSession;
55+
}());
56+
exports.default = DatabaseSession;

dist/cjs/DefaultModelFactory.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
}
9+
return function (d, b) {
10+
extendStatics(d, b);
11+
function __() { this.constructor = d; }
12+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13+
};
14+
})();
15+
Object.defineProperty(exports, "__esModule", { value: true });
16+
var constants_1 = require("./constants");
17+
var errors_1 = require("./errors");
18+
var RecordFieldModel_1 = require("./models/RecordFieldModel");
19+
var RecordModel_1 = require("./models/RecordModel");
20+
var RecordSetModel_1 = require("./models/RecordSetModel");
21+
var TableModel_1 = require("./models/TableModel");
22+
var TableSchemaModel_1 = require("./models/TableSchemaModel");
23+
var createRecordModelClass = function (Base) {
24+
return /** @class */ (function (_super) {
25+
__extends(ExtendedRecordModel, _super);
26+
function ExtendedRecordModel(id, table) {
27+
var _this = _super.call(this, id, table) || this;
28+
_this._fields = {};
29+
return _this;
30+
}
31+
return ExtendedRecordModel;
32+
}(Base));
33+
};
34+
var DefaultModelFactory = /** @class */ (function () {
35+
function DefaultModelFactory() {
36+
this._recordClass = {};
37+
this._defineProperty = function (model, name, field, factory, cache) {
38+
if (cache === void 0) { cache = true; }
39+
if (constants_1.RESERVED_PROPERTIES.indexOf(name) >= 0)
40+
throw new Error(errors_1.default.reservedProperty(field.table.name, name));
41+
Object.defineProperty(model.prototype, name, {
42+
get: function () {
43+
// TODO: Improve the instance cache mechanism. Invalidate when the field value changes..
44+
return cache
45+
? (this._fields[name] || (this._fields[name] = factory(field, this)))
46+
: factory(field, this);
47+
}
48+
});
49+
};
50+
}
51+
DefaultModelFactory.prototype.newTableSchema = function (db, name, schema) {
52+
return new TableSchemaModel_1.default(db, name, schema);
53+
};
54+
DefaultModelFactory.prototype.newTableModel = function (session, schema, state) {
55+
return new TableModel_1.default(session, schema, state);
56+
};
57+
DefaultModelFactory.prototype.newRecordModel = function (id, table) {
58+
var model = this.createRecordModel(table.schema);
59+
return new model(id, table);
60+
};
61+
DefaultModelFactory.prototype.newRecordSetModel = function (table, schema, owner) {
62+
return new RecordSetModel_1.default(table, schema, owner);
63+
};
64+
DefaultModelFactory.prototype.getRecordBaseClass = function (schema) {
65+
return RecordModel_1.default;
66+
};
67+
DefaultModelFactory.prototype.createRecordModel = function (schema) {
68+
var _this = this;
69+
if (this._recordClass[schema.name])
70+
return this._recordClass[schema.name];
71+
else {
72+
var model_1 = createRecordModelClass(this.getRecordBaseClass(schema));
73+
schema.fields.forEach(function (f) {
74+
return (f.isForeignKey || !f.isPrimaryKey)
75+
&& _this._defineProperty(model_1, f.propName, f, _this._newRecordField.bind(_this), false);
76+
});
77+
schema.relations.forEach(function (f) {
78+
return f.relationName && _this._defineProperty(model_1, f.relationName, f, f.unique
79+
? _this._newRecordRelation.bind(_this)
80+
: _this._newRecordSet.bind(_this), !f.unique);
81+
});
82+
return this._recordClass[schema.name] = model_1;
83+
}
84+
};
85+
DefaultModelFactory.prototype._newRecordField = function (schema, record) {
86+
if (!schema.isForeignKey)
87+
return new RecordFieldModel_1.default(schema, record);
88+
if (!schema.references)
89+
throw new Error(errors_1.default.fkInvalidReference(schema.name));
90+
var refTable = schema.references && record.table.session.tables[schema.references];
91+
if (!refTable)
92+
throw new Error(errors_1.default.fkReferenceNotInSession(schema.name, schema.references));
93+
var recordId = schema.getRecordValue(record);
94+
if (recordId === undefined)
95+
return null;
96+
return refTable.getOrDefault(recordId);
97+
};
98+
DefaultModelFactory.prototype._newRecordSet = function (schema, record) {
99+
var refTable = record.table.session.tables[schema.table.name];
100+
if (!refTable)
101+
throw new Error(errors_1.default.tableNotInSession(schema.table.name));
102+
return this.newRecordSetModel(refTable, schema, record);
103+
};
104+
DefaultModelFactory.prototype._newRecordRelation = function (schema, record) {
105+
var refTable = record.table.session.tables[schema.table.name];
106+
if (!refTable)
107+
throw new Error(errors_1.default.tableNotInSession(schema.table.name));
108+
var id = refTable.getIndex(schema.name, record.id)[0];
109+
if (id === undefined)
110+
return null;
111+
return this.newRecordModel(id, refTable);
112+
};
113+
return DefaultModelFactory;
114+
}());
115+
exports.default = DefaultModelFactory;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
// tslint:disable:object-literal-sort-keys
3+
Object.defineProperty(exports, "__esModule", { value: true });
4+
var constants_1 = require("../constants");
5+
var Database_1 = require("../Database");
6+
describe("constructor", function () {
7+
test("throws if no schema given", function () {
8+
var db = Database_1.default;
9+
expect(function () { return new db(); }).toThrow();
10+
});
11+
test("throws if invalid schema given", function () {
12+
var db = Database_1.default;
13+
expect(function () { return new db([]); }).toThrow();
14+
});
15+
test("creates table schemas", function () {
16+
return expect(new Database_1.default({
17+
table1: {},
18+
table2: {}
19+
}).tables).toHaveLength(2);
20+
});
21+
describe("with custom model factory", function () {
22+
var mockSchema = {
23+
connect: jest.fn()
24+
};
25+
var factory = {
26+
newTableSchema: jest.fn().mockReturnValue(mockSchema),
27+
newTableModel: jest.fn(),
28+
newRecordModel: jest.fn(),
29+
newRecordSetModel: jest.fn()
30+
};
31+
var db = new Database_1.default({ table1: {} }, { factory: factory });
32+
test("calls factory.newTableSchema", function () {
33+
return expect(factory.newTableSchema).toHaveBeenCalled();
34+
});
35+
test("calls connect on new table schema", function () {
36+
return expect(mockSchema.connect).toHaveBeenCalled();
37+
});
38+
});
39+
});
40+
describe("getNormalizer", function () {
41+
var _a, _b;
42+
var normalizer = jest.fn(function (val) { return val; });
43+
var tableName = "test";
44+
var db = new Database_1.default((_a = {},
45+
_a[tableName] = { id: { type: constants_1.TYPE_PK } },
46+
_a), { onNormalize: (_b = {}, _b[tableName] = normalizer, _b) });
47+
var state = db.reduce();
48+
var _c = tableName, table = db.createSession(state).tables[_c];
49+
table.insert({ id: 1 });
50+
test("custom normalizer called", function () {
51+
return expect(normalizer).toHaveBeenCalled();
52+
});
53+
});

dist/cjs/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.TYPE_PK = "PK";
4+
exports.TYPE_ATTR = "ATTR";
5+
exports.TYPE_MODIFIED = "MODIFIED";
6+
exports.RESERVED_PROPERTIES = ["id", "table", "value", "_fields"];

dist/cjs/errors.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
// tslint:disable:max-line-length
3+
Object.defineProperty(exports, "__esModule", { value: true });
4+
exports.default = {
5+
fkInvalidReference: function (key) { return "The foreign key: \"" + key + "\" does not define a valid referenced table."; },
6+
fkReferenceNotInSession: function (key, references) { return "The foreign key: \"" + key + "\" references an unregistered table: \"" + references + "\" in the current session."; },
7+
fkUndefined: function (table, key) { return "No foreign key named: " + key + " in the schema: \"" + table + "\"."; },
8+
fkViolation: function (table, key) { return "The insert/update operation violates the unique foreign key \"" + table + "." + key + "\"."; },
9+
recordNotFound: function (table, id) { return "No \"" + table + "\" record with id: " + id + " exists."; },
10+
recordUpdateNotFound: function (table, id) { return "Failed to apply update. No \"" + table + "\" record with id: " + id + " exists."; },
11+
reservedProperty: function (name, prop) { return "The property \"" + name + "." + prop + "\" is a reserved name. Please specify another name using the \"propName\" definition."; },
12+
sessionReadonly: function () { return "Invalid attempt to alter a readonly session."; },
13+
stateTableUndefined: function () { return "Failed to select table. Could not identify table schema."; },
14+
tableInvalidState: function (table) { return "The table \"" + table + "\" has an invalid state."; },
15+
tableNotInSession: function (table) { return "The table: \"" + table + "\" does not exist in the current session."; }
16+
};

dist/cjs/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
66
var Database_1 = require("./Database");
77
exports.createDatabase = function (schema, options) { return new Database_1.default(schema, options); };
88
__export(require("./constants"));
9-
__export(require("./models"));
10-
__export(require("./DefaultModelFactory"));
9+
__export(require("./models").default);
10+
exports.DefaultModelFactory = require("./DefaultModelFactory").default;

0 commit comments

Comments
 (0)