Skip to content

Commit

Permalink
fix: attribute.uncast([]) (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake authored Oct 18, 2021
1 parent 52da6a0 commit 7181bfe
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test:mysql2": "./test/start.sh test/integration/mysql2.test.js",
"test:postgres": "./test/start.sh test/integration/postgres.test.js",
"test:sqlite": "./test/start.sh test/integration/sqlite.test.js",
"test:sqlcipher": "./test/start.sh test/integration/sqlcipher.test.js",
"test:dts": "./test/start.sh test/types/dts.test.js",
"test:coverage": "nyc ./test/start.sh && nyc report --reporter=lcov",
"lint": "eslint ./",
Expand Down
4 changes: 3 additions & 1 deletion src/drivers/abstract/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ class Attribute {
}

uncast(value) {
if (Array.isArray(value)) return value.map(entry => this.type.uncast(entry));
if (Array.isArray(value) && this.jsType !== JSON) {
return value.map(entry => this.type.uncast(entry));
}
return this.type.uncast(value);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ class Realm {
models = Object.values(this.models);
}

// models could be connected already if cached
models = models.filter(model => !model.synchronized);

if (models.length > 0) {
await loadModels(this.Bone, models.filter(model => !model.synchronized), this.options);
await loadModels(this.Bone, models, this.options);
}
this.connected = true;
return this.Bone;
Expand Down
19 changes: 11 additions & 8 deletions test/integration/sqlcipher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ before(async function() {
const database = new Database(plaintext, OPEN_READWRITE | OPEN_CREATE);
await fs.unlink(encrypted).catch(() => {/* ignored */});
// https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/868
await new Promise((resolve, reject) => {
database.serialize(function() {
database.run(`ATTACH DATABASE '${encrypted}' AS encrypted KEY 'Accio!'`);
database.run(`SELECT sqlcipher_export('encrypted')`);
database.run(`DETACH DATABASE encrypted`, function(err) {
if (err) reject(err);
resolve();
});

database.serialize(function() {
database.run(`ATTACH DATABASE '${encrypted}' AS encrypted KEY 'Accio!'`);
database.run(`SELECT sqlcipher_export('encrypted')`);
database.run(`DETACH DATABASE encrypted`);
});

await new Promise(function(resolve, reject) {
database.close(function(err) {
if (err) reject(err);
else resolve();
});
});

Expand Down
20 changes: 13 additions & 7 deletions test/integration/suite/data_types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('=> Data types', () => {
publishedAt: DATE(6),
}
};

before(async () => {
await Note.driver.dropTable('notes');
await Note.sync();
Expand Down Expand Up @@ -79,15 +80,15 @@ describe('=> Data types - JSON', () => {
}

beforeEach(async () => {
Object.defineProperties(Note, {
columns: { value: [], configurable: true },
synchronized: { value: undefined, configurable: true }
});
await Note.driver.dropTable('notes');
});

afterEach(async () => {
await Note.driver.dropTable('notes');
await Note.sync();
});

it('=> init', async () => {
await Note.sync();
await Note.create({ title: 'Leah', meta: { foo: 1, baz: 'baz' }, metab: { foo: 2, baz: 'baz1' } });
const foundNote = await Note.first;
const { meta, metab } = Note.attributes;
Expand All @@ -104,6 +105,11 @@ describe('=> Data types - JSON', () => {
assert.deepEqual(foundNote.meta, { foo: 1, baz: 'baz' });
assert.deepEqual(foundNote.metab, { foo: 2, baz: 'baz1' });
});

it('=> type casting', async function() {
await Note.create({ meta: {} });
await Note.create({ meta: [] });
});
});


Expand Down Expand Up @@ -132,11 +138,11 @@ describe('=> Data types - BINARY', () => {
});

beforeEach(async () => {
(Note && await Note.driver.dropTable('notes'));
if (Note) await Note.driver.dropTable('notes');
});

afterEach(async () => {
(Note && await Note.driver.dropTable('notes'));
if (Note) await Note.driver.dropTable('notes');
});

it('=> init', async () => {
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions test/unit/realm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,16 @@ describe('=> Realm', () => {
});
await realm.connect();

await assert.doesNotReject(async function() {
const realm2 = new Realm({
port: process.env.MYSQL_PORT,
user: 'root',
database: 'leoric',
models: [ User ],
});
await realm2.connect();
});

class Post extends Bone {
static table = 'articles'
}
Expand Down

0 comments on commit 7181bfe

Please sign in to comment.