vuex-orm-decorators
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -6,3 +6,3 @@ import { Model } from '@vuex-orm/core'; | ||
export function PrimaryKey() { | ||
return function (target, propertyName) { | ||
return (target, propertyName) => { | ||
target.constructor.primaryKey = propertyName; | ||
@@ -16,5 +16,16 @@ }; | ||
export function Field(fieldType) { | ||
return function (target, propertyName) { | ||
target.constructor._fields = target.constructor._fields || {}; | ||
target.constructor._fields[propertyName] = fieldType; | ||
return (target, propertyName) => { | ||
const constructor = target.constructor; | ||
if (constructor.entity) { | ||
constructor._fields = constructor.fields() || {}; | ||
constructor.fields = () => { | ||
var _a, _b; | ||
const fields = constructor._fields || {}; | ||
return Object.assign(Object.assign({}, (_b = (_a = constructor.prototype) === null || _a === void 0 ? void 0 : _a._super) === null || _b === void 0 ? void 0 : _b.fields()), fields); | ||
}; | ||
} | ||
else { | ||
constructor._fields = constructor._fields || {}; | ||
} | ||
constructor._fields[propertyName] = fieldType; | ||
}; | ||
@@ -21,0 +32,0 @@ } |
import VuexORM from '@vuex-orm/core'; | ||
var ORMDatabase = /** @class */ (function () { | ||
function ORMDatabase() { | ||
export class ORMDatabase { | ||
static install(options) { | ||
this._models.forEach(model => this._ormDatabase.register(model)); | ||
return VuexORM.install(ORMDatabase._ormDatabase, options); | ||
} | ||
ORMDatabase.install = function (options) { | ||
return VuexORM.install(ORMDatabase._ormDatabase, options); | ||
}; | ||
ORMDatabase.registerEntity = function (model) { | ||
if (this._installed.indexOf(model) !== -1) { | ||
console.error("Unable to register entity " + model.name + ". Entity already registered."); | ||
static registerEntity(model) { | ||
if (this._models.includes(model)) { | ||
console.error(`Unable to register entity '${model.name}'. Entity '${model.name}' is already registered.`); | ||
return; | ||
} | ||
ORMDatabase._ormDatabase.register(model); | ||
}; | ||
ORMDatabase._ormDatabase = new VuexORM.Database(); | ||
ORMDatabase._installed = []; | ||
return ORMDatabase; | ||
}()); | ||
export { ORMDatabase }; | ||
this._models.push(model); | ||
} | ||
static models() { | ||
return this._models; | ||
} | ||
} | ||
ORMDatabase._ormDatabase = new VuexORM.Database(); | ||
ORMDatabase._models = []; | ||
//# sourceMappingURL=database.js.map |
import { ORMDatabase } from './database'; | ||
/** | ||
* Creates an vuex-orm Model | ||
* @param entityName The name of the entity to be used as the key for the state | ||
*/ | ||
export function OrmModel(entityName, parentEntity, types, typeKey) { | ||
export function OrmModel(entity, baseEntity, types) { | ||
return function (constructor) { | ||
var model = constructor; | ||
// Set the entity name on the model constructor | ||
constructor.entity = entityName; | ||
// Set the parent entity name on the model constructo (if present) | ||
if (parentEntity) { | ||
constructor.baseEntity = parentEntity; | ||
// The name that is going be used as module name in Vuex Store. | ||
constructor.entity = entity; | ||
// The reference to the base entity name if the class extends a base entity. | ||
if (baseEntity) { | ||
constructor.baseEntity = baseEntity; | ||
} | ||
// Setup the types and descriminator (if set) | ||
if (types) { | ||
constructor.types = function () { return types; }; | ||
// The definition of the fields of the model and its relations. | ||
constructor.fields = () => { | ||
var _a, _b; | ||
const fields = constructor._fields || {}; | ||
return Object.assign(Object.assign({}, (_b = (_a = constructor.prototype) === null || _a === void 0 ? void 0 : _a._super) === null || _b === void 0 ? void 0 : _b.fields()), fields); | ||
}; | ||
// The 'types mapping' used to dispatch entities based on their discriminator field. | ||
if (types && baseEntity) { | ||
ORMDatabase.models().forEach(model => { | ||
if (model.entity === baseEntity) { | ||
const _types = Object.assign(model.types(), types); | ||
model.types = () => _types; | ||
} | ||
}); | ||
} | ||
if (types && typeKey) { | ||
constructor.typeKey = typeKey; | ||
} | ||
// Add the fields generated by tha attribute decorators | ||
constructor.fields = function () { return constructor._fields || {}; }; | ||
// Register the entity in the database | ||
// Register a model to Database. | ||
ORMDatabase.registerEntity(constructor); | ||
@@ -26,0 +27,0 @@ return constructor; |
import { ORMDatabase } from './database'; | ||
/** | ||
* Creates an vuex-orm Model | ||
* @param entityName The name of the entity to be used as the key for the state | ||
*/ | ||
export function OrmModel(entityName, parentEntity, types, typeKey) { | ||
export function OrmModel(entity, baseEntity, types) { | ||
return function (constructor) { | ||
var model = constructor; | ||
// Set the entity name on the model constructor | ||
constructor.entity = entityName; | ||
// Set the parent entity name on the model constructo (if present) | ||
if (parentEntity) { | ||
constructor.baseEntity = parentEntity; | ||
// The name that is going be used as module name in Vuex Store. | ||
constructor.entity = entity; | ||
// The reference to the base entity name if the class extends a base entity. | ||
if (baseEntity) { | ||
constructor.baseEntity = baseEntity; | ||
} | ||
// Setup the types and descriminator (if set) | ||
if (types) { | ||
constructor.types = function () { return types; }; | ||
// The definition of the fields of the model and its relations. | ||
constructor.fields = () => { | ||
var _a, _b; | ||
const fields = constructor._fields || {}; | ||
return Object.assign(Object.assign({}, (_b = (_a = constructor.prototype) === null || _a === void 0 ? void 0 : _a._super) === null || _b === void 0 ? void 0 : _b.fields()), fields); | ||
}; | ||
// The 'types mapping' used to dispatch entities based on their discriminator field. | ||
if (types && baseEntity) { | ||
ORMDatabase.models().forEach(model => { | ||
if (model.entity === baseEntity) { | ||
const _types = Object.assign(model.types(), types); | ||
model.types = () => _types; | ||
} | ||
}); | ||
} | ||
if (types && typeKey) { | ||
constructor.typeKey = typeKey; | ||
} | ||
// Add the fields generated by tha attribute decorators | ||
constructor.fields = function () { return constructor._fields || {}; }; | ||
// Register the entity in the database | ||
// Register a model to Database. | ||
ORMDatabase.registerEntity(constructor); | ||
@@ -26,0 +27,0 @@ return constructor; |
@@ -6,5 +6,6 @@ import { Model } from '@vuex-orm/core'; | ||
private static _ormDatabase; | ||
private static _installed; | ||
private static _models; | ||
static install(options?: Options): Plugin<any>; | ||
static registerEntity(model: typeof Model): void; | ||
static models(): (typeof Model)[]; | ||
} |
import { Model } from '@vuex-orm/core'; | ||
/** | ||
* Creates an vuex-orm Model | ||
* @param entityName The name of the entity to be used as the key for the state | ||
*/ | ||
export declare function OrmModel(entityName: string, parentEntity?: string, types?: { | ||
export declare function OrmModel(entity: string, baseEntity?: string, types?: { | ||
[key: string]: typeof Model; | ||
}, typeKey?: string): <Model_1 extends Function>(constructor: Model_1) => void | Model_1; | ||
}): <Model_1 extends Function>(constructor: Model_1) => Model_1; |
import { Model } from '@vuex-orm/core'; | ||
/** | ||
* Creates an vuex-orm Model | ||
* @param entityName The name of the entity to be used as the key for the state | ||
*/ | ||
export declare function OrmModel(entityName: string, parentEntity?: string, types?: { | ||
export declare function OrmModel(entity: string, baseEntity?: string, types?: { | ||
[key: string]: typeof Model; | ||
}, typeKey?: string): <Model_1 extends Function>(constructor: Model_1) => void | Model_1; | ||
}): <Model_1 extends Function>(constructor: Model_1) => Model_1; |
{ | ||
"name": "vuex-orm-decorators", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "Simple Typescript decorators to improve the vuex-orm experience in Typescript by introducing typed property access.", | ||
@@ -26,3 +26,3 @@ "main": "dist/index.js", | ||
"@vuex-orm/core": "^0.36.3", | ||
"typescript": "^3.9.7", | ||
"typescript": "^4.0.2", | ||
"vue": "^2.6.12", | ||
@@ -29,0 +29,0 @@ "vuex": "^3.5.1" |
@@ -153,4 +153,80 @@ # vuex-orm-decorators | ||
If your model extends a base model, then STI inheritance needs to be used. The base entity name needs to be provided as the second argument to the ORMModel decorator. To use a discriminator field the third and fourth arguments provide the type mapping and property name overrides. | ||
If your model extends a base model, then STI inheritance needs to be used. The base entity name needs to be provided as the second argument to the ORMModel decorator and as third argument provide the discriminator fields: | ||
> Person : Base Entity | ||
```typescript | ||
@OrmModel('persons') | ||
class Person extends Model { | ||
@AttrField() | ||
public id!: string; | ||
@StringField() | ||
public name!: string; | ||
} | ||
``` | ||
> Teenager extends Person | ||
```typescript | ||
@OrmModel('teenagers', 'persons', { | ||
PERSON: Person, | ||
TEENAGER: Teenager | ||
}) | ||
class Teenager extends Person { | ||
@StringField() school!: string; | ||
} | ||
``` | ||
> Adult extends Person | ||
```typescript | ||
@OrmModel('adults', 'persons', { | ||
PERSON: Person, | ||
ADULT: Adult | ||
}) | ||
class Adult extends Person { | ||
@StringField() job!: string; | ||
} | ||
``` | ||
> Now, you can create mixed types of records at once. | ||
```typescript | ||
Person.insert({ | ||
data: [ | ||
{ type: 'PERSON', id: 1, name: 'John Doe' }, | ||
{ type: 'TEENAGER', id: 2, name: 'Jane Doe', school: '22nd Best School' }, | ||
{ type: 'ADULT', id: 3, name: 'Jane Roe', job: 'Software Engineer' } | ||
] | ||
}); | ||
``` | ||
##### Discriminator Field Override | ||
> You may define a `static typeKey` on the base entity of your hierarchy if you want to change the default discriminator field name. | ||
```typescript | ||
@OrmModel('persons') | ||
class Person extends Model { | ||
/** | ||
* The discriminator key to be used for the model when inheritance is used. | ||
*/ | ||
static typeKey = 'PERSON'; | ||
@AttrField() | ||
public id!: string; | ||
@StringField() | ||
public name!: string; | ||
} | ||
``` | ||
### Generic Types | ||
@@ -157,0 +233,0 @@ |
@@ -19,4 +19,20 @@ import { Attribute, Model } from '@vuex-orm/core'; | ||
return (target: Object, propertyName: string | symbol): void => { | ||
(target.constructor as any)._fields = (target.constructor as any)._fields || {}; | ||
(target.constructor as any)._fields[propertyName] = fieldType; | ||
const constructor = (target.constructor as any); | ||
if (constructor.entity) { | ||
constructor._fields = constructor.fields() || {}; | ||
constructor.fields = () => { | ||
const fields = constructor._fields || {}; | ||
return { | ||
...constructor.prototype?._super?.fields(), | ||
...fields | ||
}; | ||
}; | ||
} else { | ||
constructor._fields = constructor._fields || {}; | ||
} | ||
constructor._fields[propertyName] = fieldType; | ||
}; | ||
@@ -138,2 +154,1 @@ } | ||
} | ||
@@ -8,5 +8,8 @@ import VuexORM, { Model } from '@vuex-orm/core'; | ||
private static _ormDatabase = new VuexORM.Database(); | ||
private static _installed = <typeof Model[]>[]; | ||
private static _models = <typeof Model[]>[]; | ||
public static install(options?: Options): Plugin<any> { | ||
this._models.forEach(model => this._ormDatabase.register(model)); | ||
return VuexORM.install(ORMDatabase._ormDatabase, options); | ||
@@ -16,8 +19,15 @@ } | ||
public static registerEntity(model: typeof Model) { | ||
if (this._installed.indexOf(model) !== -1) { | ||
console.error(`Unable to register entity ${model.name}. Entity already registered.`); | ||
if (this._models.includes(model)) { | ||
console.error(`Unable to register entity '${model.name}'. Entity '${model.name}' is already registered.`); | ||
return; | ||
} | ||
ORMDatabase._ormDatabase.register(model); | ||
this._models.push(model); | ||
} | ||
public static models() { | ||
return this._models; | ||
} | ||
} |
import { Model } from '@vuex-orm/core'; | ||
import { ORMDatabase } from './database'; | ||
/** | ||
* Creates an vuex-orm Model | ||
* @param entityName The name of the entity to be used as the key for the state | ||
*/ | ||
export function OrmModel(entityName: string, parentEntity?: string, types?: { [key: string]: typeof Model }, typeKey?: string) { | ||
return function <Model extends Function>(constructor: Model): Model | void { | ||
const model: Function = constructor; | ||
export function OrmModel(entity: string, baseEntity?: string, types?: { [key: string]: typeof Model }) { | ||
return function <Model extends Function>(constructor: Model): Model { | ||
// The name that is going be used as module name in Vuex Store. | ||
(constructor as any).entity = entity; | ||
// Set the entity name on the model constructor | ||
(constructor as any).entity = entityName; | ||
// The reference to the base entity name if the class extends a base entity. | ||
if (baseEntity) { | ||
(constructor as any).baseEntity = baseEntity; | ||
} | ||
// Set the parent entity name on the model constructo (if present) | ||
if (parentEntity) { (constructor as any).baseEntity = parentEntity; } | ||
// The definition of the fields of the model and its relations. | ||
(constructor as any).fields = () => { | ||
const fields = (constructor as any)._fields || {}; | ||
// Setup the types and descriminator (if set) | ||
if (types) { (constructor as any).types = () => types; } | ||
if (types && typeKey) { (constructor as any).typeKey = typeKey; } | ||
return { | ||
...(constructor as any).prototype?._super?.fields(), | ||
...fields | ||
}; | ||
}; | ||
// Add the fields generated by tha attribute decorators | ||
(constructor as any).fields = () => (constructor as any)._fields || {}; | ||
// The 'types mapping' used to dispatch entities based on their discriminator field. | ||
if (types && baseEntity) { | ||
ORMDatabase.models().forEach(model => { | ||
if (model.entity === baseEntity) { | ||
const _types = Object.assign(model.types(), types); | ||
// Register the entity in the database | ||
model.types = () => _types; | ||
} | ||
}); | ||
} | ||
// Register a model to Database. | ||
ORMDatabase.registerEntity(constructor as unknown as typeof Model); | ||
@@ -30,2 +40,2 @@ | ||
}; | ||
} | ||
} |
{ | ||
"compilerOptions": { | ||
"module": "es2015", | ||
"target": "es5", | ||
"moduleResolution": "node", | ||
"experimentalDecorators": true, | ||
"lib": [ | ||
"dom", "es2015" | ||
], | ||
"importHelpers": true, | ||
"sourceMap": true, | ||
"declaration": true, | ||
"declarationDir": "dist/types", | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"sourceRoot": "src", | ||
"strict": true, | ||
"removeComments": false, | ||
"strictNullChecks": true | ||
"module": "ESNext", | ||
"target": "ES2015", | ||
"moduleResolution": "Node", | ||
"experimentalDecorators": true, | ||
"lib": [ | ||
"DOM", | ||
"ESNext" | ||
], | ||
"importHelpers": true, | ||
"sourceMap": true, | ||
"declaration": true, | ||
"declarationDir": "dist/types", | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"sourceRoot": "src", | ||
"strict": true, | ||
"removeComments": false, | ||
"strictNullChecks": true | ||
}, | ||
"include": [ | ||
"src" | ||
"src" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"test" | ||
"node_modules", | ||
"test" | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
43152
534
269