Skip to content

Commit

Permalink
feat: initiate orm
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 23, 2019
1 parent eb0648f commit 41c4d13
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
60 changes: 60 additions & 0 deletions adonis-typings/orm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// /*
// * @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>
// }
// }
72 changes: 72 additions & 0 deletions src/Orm/Adapter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* @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.
*/

/// <reference path="../../../adonis-typings/orm.ts" />
/// <reference path="../../../adonis-typings/database.ts" />

// import { DatabaseContract } from '@ioc:Adonis/Lucid/Database'
// import { AdapterContract, ModelContract } from '@ioc:Adonis/Lucid/Orm'

// /**
// * Adapter to execute queries for a given model. Please note that adapters
// * are stateless and only one instance of adapter is used across the
// * app, so make sure not to store any state.
// */
// export class Adapter implements AdapterContract {
// constructor (private _db: DatabaseContract) {}

// public async insert (model: ModelContract, attributes: any): Promise<void> {
// const modelConstructor = model.$getConstructor()

// /**
// * Pulling the query client for a given connection.
// */
// const client = this._db.connection(modelConstructor.$connection)

// *
// * Letting model give us the insert query. This enables the end user
// * to add some constraints to the query builder before returning
// * it back to us

// const query = modelConstructor.$getSaveQuery(client, 'insert')

// /**
// * Execute the query
// */
// const result = await query.insert(attributes)

// /**
// * Set id when increments is true
// */
// if (modelConstructor.$increments) {
// model.$consumeAdapterResult({ [modelConstructor.$primaryKey]: result[0] })
// }
// }

// public async update (model: ModelContract, dirty: any): Promise<void> {
// const modelConstructor = model.$getConstructor()

// /**
// * Pulling the query client for a given connection.
// */
// const client = this._db.connection(modelConstructor.$connection)

// /**
// * Letting model give us the insert query. This enables the end user
// * to add some constraints to the query builder before returning
// * it back to us
// */
// const query = modelConstructor.$getSaveQuery(client, 'update')

// /**
// * Execute the query
// */
// await query.update(dirty)
// }
// }

0 comments on commit 41c4d13

Please sign in to comment.