Skip to content

Commit

Permalink
docs: enhance query options, instance type, and toJSON() result type (#…
Browse files Browse the repository at this point in the history
…207)

* docs: enhance query options, instance type, and toJSON() result type

* docs: driver.query dts
  • Loading branch information
cyjake authored Oct 26, 2021
1 parent 8d30a9a commit fc9eb84
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 219 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage
docs/Gemfile.lock
package-lock.json
.DS_Store
test/types/*.js
2 changes: 2 additions & 0 deletions docs/contributing/guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ $ npm run test
$ npm run test:unit
# run integration tests
$ npm run test:integration
# run typescript definition tests
$ npm run test:dts
```

To be more specific, we can filter test files and cases:
Expand Down
2 changes: 2 additions & 0 deletions docs/zh/contributing/guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ $ npm run test
$ npm run test:unit
# 仅运行集成测试
$ npm run test:integration
# TypeScript 定义
$ npm run test:dts
```

还可以执行单个测试文件,或者使用 `--grep` 选项进一步限定执行范围:
Expand Down
1 change: 1 addition & 0 deletions src/drivers/sqlite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { escapeId, escape } = require('./sqlstring');
const schema = require('./schema');
const spellbook = require('./spellbook');
const Pool = require('./pool');

class SqliteDriver extends AbstractDriver {
constructor(opts = {}) {
super(opts);
Expand Down
141 changes: 0 additions & 141 deletions test/types/basics.test.js

This file was deleted.

32 changes: 28 additions & 4 deletions test/types/basics.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { strict as assert } from 'assert';
import { Bone, connect } from '../..';
import { Bone, DataTypes, connect } from '../..';

describe('=> Basics (TypeScript)', function() {
const { BIGINT, STRING, TEXT, DATE, BOOLEAN } = DataTypes;
class Post extends Bone {
static table = 'articles';

// TODO: generate definitions or deduce from model attributes
static attributes = {
id: BIGINT,
createdAt: { type: DATE, columnName: 'gmt_create' },
updatedAt: { type: DATE, columnName: 'gmt_modified' },
deletedAt: { type: DATE, columnName: 'gmt_deleted' },
title: STRING,
content: TEXT,
extra: TEXT,
thumb: STRING,
authorId: BIGINT,
isPrivate: BOOLEAN,
summary: TEXT,
settings: TEXT,
}

// TODO: should be generated or automated with decorator
id: number;
createdAt: Date;
updatedAt: Date;
Expand All @@ -14,7 +30,7 @@ describe('=> Basics (TypeScript)', function() {
content: string;
extra: string;
thumb: string;
authroId: number;
authorId: number;
isPrivate: boolean;
summary: string;
settings: string;
Expand Down Expand Up @@ -64,7 +80,10 @@ describe('=> Basics (TypeScript)', function() {
});

describe('=> Collection', function() {

it('collection.toJSON()', async function() {
const posts = await Post.all;
assert.deepEqual(posts.toJSON(), []);
});
});

describe('=> Create', function() {
Expand Down Expand Up @@ -117,6 +136,11 @@ describe('=> Basics (TypeScript)', function() {
const post = await Post.findOne({ title: 'Leah' });
assert.equal(post.title, 'Leah');
});

it('Post.where()', async function() {
const posts = await Post.where({ title: { $like: '%a%' } }).select('title');
assert.equal(posts.length, 3);
});
});

describe('=> Update', function() {
Expand Down
36 changes: 36 additions & 0 deletions test/types/querying.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { strict as assert } from 'assert';
import { Bone, connect } from '../..'

describe('=> Querying (TypeScript)', function() {
class Post extends Bone {
static table = 'articles'
}

before(async function() {
Bone.driver = null;
await connect({
dialect: 'sqlite',
database: '/tmp/leoric.sqlite3',
models: [ Post ],
});
});

beforeEach(async function() {
await Post.truncate();
});

describe('=> Driver', function() {
it('driver.query(SELECT)', async function() {
const { rows } = await Bone.driver.query('SELECT 1');
assert.equal(rows.length, 1);
});

it('driver.query(INSERT)', async function() {
const { insertId, affectedRows } = await Bone.driver.query(
'INSERT INTO articles (title) VALUES ("Leah")'
);
assert.equal(affectedRows, 1);
assert.ok(insertId);
});
});
});
5 changes: 5 additions & 0 deletions test/unit/drivers/mysql/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ describe('=> MySQL driver', () => {
assert.equal((await driver.query('SELECT count(*) AS count FROM notes')).rows[0].count, 0);
});

it('driver.query()', async function() {
const { affectedRows, insertId } = await driver.query('INSERT INTO articles (title) VALUES ("Leah")');
assert.ok(insertId);
assert.equal(affectedRows, 1);
});
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"target": "es2018",
"moduleResolution": "Node",
"module": "CommonJS"
"module": "CommonJS",
"experimentalDecorators": true
}
}
Loading

0 comments on commit fc9eb84

Please sign in to comment.