-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement basic functionality for the base model
The model is capable of issuing direct requests to the database using the adapter.
- Loading branch information
1 parent
cd4206f
commit aca8ad5
Showing
10 changed files
with
556 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,113 @@ | ||
// /* | ||
// * @adonisjs/lucid | ||
// * | ||
// * (c) Harminder Virk <virk@adonisjs.com> | ||
// * | ||
// * For the full copyright and license information, please view the LICENSE | ||
// * file that was distributed with this source code. | ||
// */ | ||
|
||
// declare module '@ioc:Adonis/Lucid/Orm' { | ||
// import { | ||
// InsertQueryBuilderContract, | ||
// DatabaseQueryBuilderContract, | ||
// } from '@ioc:Adonis/Lucid/DatabaseQueryBuilder' | ||
|
||
// import { | ||
// ModelConstructorContract as BaseModelConstructorContract, | ||
// ModelContract as BaseModelContract, | ||
// AdapterContract as BaseAdapterContract, | ||
// } from '@poppinss/data-models' | ||
|
||
// export interface OrmQueryBuilder< | ||
// Model extends ModelConstructorContract, | ||
// Result extends any, | ||
// > extends DatabaseQueryBuilderContract<Model['refs']>, ExcutableQueryBuilderContract<Result> { | ||
// } | ||
|
||
// export interface ModelConstructorContract extends BaseModelConstructorContract { | ||
// $connection?: string, | ||
// $increments: boolean, | ||
// $table: string, | ||
|
||
// refs: any, | ||
|
||
// $getSaveQuery ( | ||
// client: QueryClientContract, | ||
// action: 'insert', | ||
// ): InsertQueryBuilderContract, | ||
|
||
// $getSaveQuery ( | ||
// client: QueryClientContract, | ||
// action: 'update', | ||
// ): OrmQueryBuilder<any, any>, | ||
|
||
// $getSaveQuery ( | ||
// client: QueryClientContract, | ||
// action: 'insert' | 'update', | ||
// ): InsertQueryBuilderContract | OrmQueryBuilder<any, any>, | ||
|
||
// query (): OrmQueryBuilder<this, new () => this>, | ||
// } | ||
|
||
// export interface ModelContract extends BaseModelContract { | ||
// $getConstructor (): ModelConstructorContract, | ||
// } | ||
|
||
// export interface AdapterContract extends BaseAdapterContract { | ||
// insert (instance: ModelContract, attributes: any): Promise<void> | ||
// } | ||
// } | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare module '@ioc:Adonis/Lucid/Orm' { | ||
import knex from 'knex' | ||
import { QueryClientContract, ExcutableQueryBuilderContract } from '@ioc:Adonis/Lucid/Database' | ||
|
||
import { | ||
InsertQueryBuilderContract, | ||
DatabaseQueryBuilderContract, | ||
} from '@ioc:Adonis/Lucid/DatabaseQueryBuilder' | ||
|
||
import { | ||
BaseModel as DataModelBaseModel, | ||
AdapterContract as DataModelAdapterContract, | ||
ModelConstructorContract as DataModelConstructorContract, | ||
ModelContract as DataModelContract, | ||
column as baseColumn, | ||
} from '@poppinss/data-models' | ||
|
||
/** | ||
* Orm query builder will have extras methods on top of Database query builder | ||
*/ | ||
export interface OrmQueryBuilder< | ||
Record extends any, | ||
Result extends any, | ||
> extends DatabaseQueryBuilderContract<Record, Result>, ExcutableQueryBuilderContract<Result> { | ||
} | ||
|
||
/** | ||
* The shape of query adapter | ||
*/ | ||
export interface AdapterContract extends DataModelAdapterContract { | ||
insert (instance: ModelContract, attributes: any): Promise<void> | ||
update (instance: ModelContract, dirty: any): Promise<void> | ||
delete (instance: ModelContract): Promise<void> | ||
find (model: ModelConstructorContract, key: string, value: any): Promise<ModelContract | null> | ||
findAll (model: ModelConstructorContract): Promise<ModelContract[]> | ||
} | ||
|
||
/** | ||
* Shape of base model | ||
*/ | ||
export interface ModelContract extends DataModelContract { | ||
/** | ||
* Gives an option to the end user to define constraints for update, insert | ||
* and delete queries. Since the query builder for these queries aren't | ||
* exposed to the end user, this method opens up the API to build | ||
* custom queries. | ||
*/ | ||
$getQueryFor ( | ||
action: 'insert', | ||
client: QueryClientContract, | ||
): ReturnType<QueryClientContract['insertQuery']> | ||
$getQueryFor ( | ||
action: 'update' | 'delete', | ||
client: QueryClientContract, | ||
): ReturnType<QueryClientContract['query']> | ||
$getQueryFor ( | ||
action: 'insert' | 'delete' | 'update', | ||
client: QueryClientContract, | ||
): ReturnType<QueryClientContract['query']> | ReturnType<QueryClientContract['insertQuery']> | ||
} | ||
|
||
/** | ||
* Shape of base model static properties | ||
*/ | ||
export interface ModelConstructorContract extends DataModelConstructorContract { | ||
/** | ||
* The database connection to use | ||
*/ | ||
$connection?: string | ||
|
||
/** | ||
* Whether primary key is auto incrementing or not. If not, then | ||
* end user must provide the value for the primary key | ||
*/ | ||
$increments: boolean | ||
|
||
/** | ||
* Database table to use | ||
*/ | ||
$table: string | ||
|
||
/** | ||
* Refs are named value pair of model | ||
*/ | ||
refs: any | ||
|
||
/** | ||
* Returns the query for fetching a model instance | ||
*/ | ||
query< | ||
Model extends ModelConstructorContract, | ||
Instance extends ModelContract, | ||
> (this: new () => Instance): OrmQueryBuilder<Model, Instance> | ||
|
||
$createFromAdapterResult (result?: any, sideloadAttributes?: string[]): null | ModelContract | ||
$createMultipleFromAdapterResult (results: any[], sideloadAttributes?: string[]): ModelContract[] | ||
} | ||
|
||
export const BaseModel: ModelConstructorContract & { | ||
new (): ModelContract, | ||
} | ||
|
||
export const column: typeof baseColumn | ||
} |
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,10 @@ | ||
import { BaseModel } from '@ioc:Adonis/Lucid/Orm' | ||
|
||
class User extends BaseModel { | ||
public username: string | ||
} | ||
|
||
const user = User.query().then((a) => { | ||
a.username.toLocaleLowerCase() | ||
}) | ||
console.log(user) |
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
Oops, something went wrong.