-
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
779 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Usage with JavaScript | ||
|
||
To use `mikro-orm` with Vanilla JavaScript, define your entities like this: | ||
|
||
```javascript | ||
const { Collection } = require('mikro-orm'); | ||
const { Book } = require('./Book'); | ||
const { BaseEntity } = require('./BaseEntity'); | ||
|
||
/** | ||
* @property {number} id | ||
* @property {Date} createdAt | ||
* @property {Date} updatedAt | ||
* @property {string} name | ||
* @property {string} email | ||
* @property {number} age | ||
* @property {boolean} termsAccepted | ||
* @property {string[]} identities | ||
* @property {Date} born | ||
* @property {Collection<Book>} books | ||
* @property {Book} favouriteBook | ||
* @property {number} version | ||
* @property {string} versionAsString | ||
*/ | ||
class Author extends BaseEntity { | ||
|
||
/** | ||
* @param {string} name | ||
* @param {string} email | ||
*/ | ||
constructor(name, email) { | ||
super(); | ||
this.name = name; | ||
this.email = email; | ||
this.createdAt = new Date(); | ||
this.updatedAt = new Date(); | ||
this.termsAccepted = false; | ||
} | ||
|
||
} | ||
|
||
const schema = { | ||
name: 'Author', | ||
extends: 'BaseEntity', | ||
properties: { | ||
createdAt: 'Date', | ||
updatedAt: { | ||
type: 'Date', | ||
onUpdate: () => new Date(), | ||
}, | ||
name: 'string', | ||
email: 'string', | ||
age: 'number', | ||
termsAccepted: 'boolean', | ||
identities: 'string[]', | ||
born: 'Date', | ||
books: { | ||
reference: '1:m', | ||
fk: 'author', | ||
type: 'Book', | ||
}, | ||
favouriteBook: { | ||
reference: 'm:1', | ||
type: 'Book', | ||
fk: 'id', | ||
}, | ||
}, | ||
}; | ||
|
||
module.exports = { Author, schema }; | ||
``` | ||
|
||
Reference parameter can be one of (where `SCALAR` is the default one): | ||
|
||
```typescript | ||
export enum ReferenceType { | ||
SCALAR = 'scalar', | ||
MANY_TO_ONE = 'm:1', | ||
ONE_TO_MANY = '1:m', | ||
MANY_TO_MANY = 'm:n', | ||
} | ||
``` | ||
|
||
When initializing ORM, provide `JavaScriptMetadataProvider` as metadata provider: | ||
|
||
```javascript | ||
const orm = await MikroORM.init({ | ||
entitiesDirs: ['entities'], | ||
dbName: '...', | ||
metadataProvider: JavaScriptMetadataProvider, | ||
}); | ||
``` | ||
|
||
For more examples of plain JavaScript entity definitions, | ||
[take a look here](https://github.com/B4nan/mikro-orm/blob/master/tests/entities-js). | ||
|
||
[← Back to table of contents](index.md#table-of-contents) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { MetadataProvider } from './MetadataProvider'; | ||
import { Cascade, EntityMetadata, EntityProperty, ReferenceType } from '../decorators/Entity'; | ||
import { Utils } from '../utils/Utils'; | ||
|
||
export class JavaScriptMetadataProvider extends MetadataProvider { | ||
|
||
discoverEntity(meta: EntityMetadata, name: string): void { | ||
const { schema } = require(meta.path); | ||
Object.entries(schema.properties).forEach(([name, prop]) => { | ||
if (typeof prop === 'string') { | ||
schema.properties[name] = { type: prop }; | ||
} | ||
}); | ||
|
||
Utils.merge(meta, schema); | ||
Object.entries(meta.properties).forEach(([name, prop]) => { | ||
this.initProperty(prop, name); | ||
}); | ||
} | ||
|
||
/** | ||
* Re-hydrates missing attributes like `onUpdate` (functions are lost when caching to JSON) | ||
*/ | ||
loadFromCache(meta: EntityMetadata, cache: EntityMetadata): void { | ||
Utils.merge(meta, cache); | ||
const { schema } = require(meta.path); | ||
|
||
Object.entries(schema.properties).forEach(([name, prop]) => { | ||
if (Utils.isObject(prop)) { | ||
Object.entries(prop).forEach(([attribute, value]) => { | ||
if (!(attribute in meta.properties[name])) { | ||
meta.properties[name][attribute as keyof EntityProperty] = value; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
private initProperty(prop: EntityProperty, propName: string): void { | ||
prop.name = propName; | ||
|
||
if (typeof prop.reference === 'undefined') { | ||
prop.reference = ReferenceType.SCALAR; | ||
} | ||
|
||
if (prop.reference !== ReferenceType.SCALAR && typeof prop.cascade === 'undefined') { | ||
prop.cascade = [Cascade.PERSIST]; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { EntityMetadata } from '../decorators/Entity'; | ||
import { MikroORMOptions } from '../MikroORM'; | ||
import { Utils } from '../utils/Utils'; | ||
|
||
export abstract class MetadataProvider { | ||
|
||
constructor(protected readonly options: MikroORMOptions) { } | ||
|
||
abstract discoverEntity(meta: EntityMetadata, name: string): void; | ||
|
||
loadFromCache(meta: EntityMetadata, cache: EntityMetadata): void { | ||
Utils.merge(meta, cache); | ||
} | ||
|
||
} |
Oops, something went wrong.